private Decision GenCond(ArraySlice <DecisionTest> S)
        {
            double probability;
            int    splitIndex = Split(S, out probability);

            int splitElement = S[splitIndex].Interval.First;
            RelationalBranchDecision result;
            RelationalOperator       op;

            if (splitIndex == 1 && S[0].Interval.LongSize == 1)
            {
                op     = RelationalOperator.Equal;
                result = new RelationalBranchDecision(op, S[0].Interval.First);
            }
            else if (splitIndex == (S.Count - 1) && S[splitIndex].Interval.LongSize == 1)
            {
                op     = RelationalOperator.NotEqual;
                result = new RelationalBranchDecision(op, splitElement);
            }
            else
            {
                op     = RelationalOperator.Less;
                result = new RelationalBranchDecision(op, splitElement);
            }

            result.Left  = GenTree(S.SubSlice(0, splitIndex));
            result.Right = GenTree(S.SubSlice(splitIndex));

            return(result);
        }
        void IDecisionVisitor.Visit(RelationalBranchDecision decision)
        {
            emit
                .Label(GetNodeLabel(decision).Def)
                .Do(ldvalue)
                .Ldc_I4(decision.Operand)
                ;

            var label = GetNodeLabel(decision.Right);
            switch (decision.Operator.Negate())
            {
                case RelationalOperator.Equal:          emit.Beq(label);    break;
                case RelationalOperator.NotEqual:       emit.Bne_Un(label); break;
                case RelationalOperator.Less:           emit.Blt(label);    break;
                case RelationalOperator.Greater:        emit.Bgt(label);    break;
                case RelationalOperator.LessOrEqual:    emit.Ble(label);    break;
                case RelationalOperator.GreaterOrEqual: emit.Bge(label);    break;
                default:
                    throw new InvalidOperationException("Not supported operator");
            }

            GenerateCodeOrJump(decision.Left);

            strategy.IntermediateGenerateCode();
        }
        public void Visit(RelationalBranchDecision decision)
        {
            PutLabel(decision);

            output
            .AppendFormat(
                "if (x {0} {1}) goto {2}",
                decision.Operator.Negate().GetOpeatorText(),
                decision.Operand,
                GetLabelText(decision.Right))
            .AppendLine();

            GenerateCodeOrJump(decision.Left);
            strategy.IntermediateGenerateCode();
        }
        public void Visit(RelationalBranchDecision decision)
        {
            PutLabel(decision);

            output
                .AppendFormat(
                    "if (x {0} {1}) goto {2}",
                    decision.Operator.Negate().GetOpeatorText(),
                    decision.Operand,
                    GetLabelText(decision.Right))
                .AppendLine();

            GenerateCodeOrJump(decision.Left);
            strategy.IntermediateGenerateCode();
        }
Beispiel #5
0
        private static Decision TrySingleKey(IntArrow <int>[] intArrows, int defaultValue)
        {
            if (intArrows.Length == 1)
            {
                var interval = intArrows[0].Key;
                if (interval.First == interval.Last)
                {
                    var branch = new RelationalBranchDecision(RelationalOperator.Equal, interval.First);
                    branch.Left  = new ActionDecision(intArrows[0].Value);
                    branch.Right = new ActionDecision(defaultValue);
                    return(branch);
                }
            }

            return(null);
        }
        private static Decision TrySingleKey(IntArrow<int>[] intArrows, int defaultValue)
        {
            if (intArrows.Length == 1)
            {
                var interval = intArrows[0].Key;
                if (interval.First == interval.Last)
                {
                    var branch = new RelationalBranchDecision(RelationalOperator.Equal, interval.First);
                    branch.Left = new ActionDecision(intArrows[0].Value);
                    branch.Right = new ActionDecision(defaultValue);
                    return branch;
                }
            }

            return null;
        }
        private bool TryBuildElementaryTree(IIntMap <int> map, out Decision result)
        {
            result = null;

            int count = 0;

            foreach (var arrow in map.Enumerate())
            {
                if (arrow.Value == map.DefaultValue)
                {
                    continue;
                }

                ++count;
                if (count > platform.MaxLinearCount || arrow.Key.LongSize != 1)
                {
                    return(false);
                }
            }

            RelationalBranchDecision lastParent = null;

            foreach (var arrow in map.Enumerate())
            {
                if (arrow.Value == map.DefaultValue)
                {
                    continue;
                }

                var node = new RelationalBranchDecision(RelationalOperator.Equal, arrow.Key.First);
                node.Left = GetActionDecision(arrow.Value);
                if (lastParent == null)
                {
                    result = node;
                }
                else
                {
                    lastParent.Right = node;
                }

                lastParent = node;
            }

            lastParent.Right = DefaultActionDecision;

            return(true);
        }
Beispiel #8
0
        private Decision Build(
            int first,
            int last,
            KeyValuePair <int, int>[] upperBounds)
        {
            int size = last - first;

            if (size > platform.MaxLinearCount)
            {
                // binary search nodes
                int middle = first + (size - 1) / 2;

                var branch = new RelationalBranchDecision(
                    RelationalOperator.LessOrEqual,
                    upperBounds[middle].Key);

                // positive branch
                branch.Left = Build(first, middle + 1, upperBounds);

                // negative branch
                branch.Right = Build(middle + 1, last, upperBounds);

                return(branch);
            }
            else
            {
                Decision result = null;
                RelationalBranchDecision lastBranch = null;

                // linear search nodes
                for (; first != last; ++first)
                {
                    int key = upperBounds[first].Key;

                    RelationalBranchDecision branch;

                    if (first > 0 && (key - upperBounds[first - 1].Key) == 1)
                    {
                        branch = new RelationalBranchDecision(RelationalOperator.Equal, key);
                    }
                    else
                    {
                        branch = new RelationalBranchDecision(RelationalOperator.LessOrEqual, key);
                    }

                    branch.Left  = new ActionDecision(upperBounds[first].Value);
                    branch.Right = null;

                    if (lastBranch != null)
                    {
                        lastBranch.Right = branch;
                    }
                    else
                    {
                        result = branch;
                    }

                    lastBranch = branch;
                }

                if (lastBranch != null)
                {
                    lastBranch.Right = new ActionDecision(defaultAction);
                }
                else
                {
                    result = new ActionDecision(defaultAction);
                }

                return(result);
            }
        }
        private Decision GenCond(ArraySlice<DecisionTest> S)
        {
            double probability;
            int splitIndex = Split(S, out probability);

            int splitElement = S[splitIndex].Interval.First;
            RelationalBranchDecision result;
            RelationalOperator op;
            if (splitIndex == 1 && S[0].Interval.LongSize == 1)
            {
                op = RelationalOperator.Equal;
                result = new RelationalBranchDecision(op, S[0].Interval.First);
            }
            else if (splitIndex == (S.Count - 1) && S[splitIndex].Interval.LongSize == 1)
            {
                op = RelationalOperator.NotEqual;
                result = new RelationalBranchDecision(op, splitElement);
            }
            else
            {
                op = RelationalOperator.Less;
                result = new RelationalBranchDecision(op, splitElement);
            }

            result.Left  = GenTree(S.SubSlice(0, splitIndex));
            result.Right = GenTree(S.SubSlice(splitIndex));

            return result;
        }
        private bool TryBuildElementaryTree(IIntMap<int> map, out Decision result)
        {
            result = null;

            int count = 0;
            foreach (var arrow in map.Enumerate())
            {
                if (arrow.Value == map.DefaultValue)
                {
                    continue;
                }

                ++count;
                if (count > platform.MaxLinearCount || arrow.Key.LongSize != 1)
                {
                    return false;
                }
            }

            RelationalBranchDecision lastParent = null;
            foreach (var arrow in map.Enumerate())
            {
                if (arrow.Value == map.DefaultValue)
                {
                    continue;
                }

                var node = new RelationalBranchDecision(RelationalOperator.Equal, arrow.Key.First);
                node.Left = GetActionDecision(arrow.Value);
                if (lastParent == null)
                {
                    result = node;
                }
                else
                {
                    lastParent.Right = node;
                }

                lastParent = node;
            }

            lastParent.Right = DefaultActionDecision;

            return true;
        }
        private Decision Build(
            int first,
            int last,
            KeyValuePair<int,int>[] upperBounds)
        {
            int size = last - first;
            if (size > platform.MaxLinearCount)
            {
                // binary search nodes
                int middle = first + (size - 1) / 2;

                var branch = new RelationalBranchDecision(
                                    RelationalOperator.LessOrEqual,
                                    upperBounds[middle].Key);

                // positive branch
                branch.Left = Build(first, middle + 1, upperBounds);

                // negative branch
                branch.Right = Build(middle + 1, last, upperBounds);

                return branch;
            }
            else
            {
                Decision result = null;
                RelationalBranchDecision lastBranch = null;

                // linear search nodes
                for (; first != last; ++first)
                {
                    int key = upperBounds[first].Key;

                    RelationalBranchDecision branch;

                    if (first > 0 && (key - upperBounds[first - 1].Key) == 1)
                    {
                        branch = new RelationalBranchDecision(RelationalOperator.Equal, key);
                    }
                    else
                    {
                        branch = new RelationalBranchDecision(RelationalOperator.LessOrEqual, key);
                    }

                    branch.Left = new ActionDecision(upperBounds[first].Value);
                    branch.Right = null;

                    if (lastBranch != null)
                    {
                        lastBranch.Right = branch;
                    }
                    else
                    {
                        result = branch;
                    }

                    lastBranch = branch;
                }

                if (lastBranch != null)
                {
                    lastBranch.Right = new ActionDecision(defaultAction);
                }
                else
                {
                    result = new ActionDecision(defaultAction);
                }

                return result;
            }
        }