Example #1
0
        public void Test_InnerNode_Merge_Test()
        {
            var leftInnerNode = new InnerNode<int, int>(3);
            var leaf = new LeafNode<int, int>(3);
            leaf.Keys.Add(1);
            leaf.Values.Add(new List<int>());
            leaf.Values[0].Add(1);
            leftInnerNode.Children.Add(leaf);

            leaf = new LeafNode<int, int>(3);
            leaf.Keys.Add(2);
            leaf.Values.Add(new List<int>());
            leaf.Values[0].Add(2);
            leftInnerNode.Children.Add(leaf);

            leftInnerNode.Keys.Add(1);

            var rightInnerNode = new InnerNode<int, int>(3);
            leaf = new LeafNode<int, int>(3);
            leaf.Keys.Add(3);
            leaf.Values.Add(new List<int>());
            leaf.Values[0].Add(3);
            rightInnerNode.Children.Add(leaf);

            rightInnerNode.Keys.Add(4);

            leftInnerNode.Merge(rightInnerNode);

            Assert.AreEqual(0, rightInnerNode.Keys.Count);
            Assert.AreEqual(0, rightInnerNode.Children.Count);
            Assert.AreEqual(2, leftInnerNode.Keys.Count);
            Assert.AreEqual(3, leftInnerNode.Children.Count);
        }
Example #2
0
        public MathTree(InnerNode root, MathNode child)
        {
            this.nodeStack = new Stack();
            this.root = root;

            ((InnerNode) this.root).AddNode(child);
        }
Example #3
0
        public MathTree(InnerNode root, MathNode left, MathNode right)
        {
            this.nodeStack = new Stack();
            this.root = root;

            ((InnerNode)this.root).AddNode(left);
            ((InnerNode)this.root).AddNode(right);
        }
Example #4
0
        public void Test_InnerNode_Redistribute()
        {
            var child = new InnerNode<int, int>(4);
            var sibling = new InnerNode<int, int>(4);

            child.Keys.Add(1);
            var childLeaf = new LeafNode<int, int>(4);
            childLeaf.Keys.Add(1);
            childLeaf.Values.Add(new List<int>());
            childLeaf.Values[0].Add(1);
            child.Children.Add(childLeaf);

            sibling.Keys.Add(2);
            var siblingChild1 = new LeafNode<int, int>(4);
            siblingChild1.Keys.Add(2);
            siblingChild1.Values.Add(new List<int>());
            siblingChild1.Values[0].Add(2);

            siblingChild1.Keys.Add(3);
            siblingChild1.Values.Add(new List<int>());
            siblingChild1.Values[1].Add(3);

            sibling.Children.Add(siblingChild1);

            sibling.Keys.Add(4);
            var siblingChild2 = new LeafNode<int, int>(4);
            siblingChild2.Keys.Add(4);
            siblingChild2.Values.Add(new List<int>());
            siblingChild2.Values[0].Add(4);
            siblingChild2.Keys.Add(5);
            siblingChild2.Values.Add(new List<int>());
            siblingChild2.Values[1].Add(5);
            sibling.Children.Add(siblingChild2);

            sibling.Keys.Add(6);
            var siblingChild3 = new LeafNode<int, int>(4);
            siblingChild3.Keys.Add(6);
            siblingChild3.Values.Add(new List<int>());
            siblingChild3.Values[0].Add(6);
            siblingChild3.Keys.Add(7);
            siblingChild3.Values.Add(new List<int>());
            siblingChild3.Values[1].Add(7);
            sibling.Children.Add(siblingChild3);

            child.Redistribute(sibling, -1);

            Assert.AreEqual(2, sibling.Children.Count);
            Assert.AreEqual(2, sibling.Keys.Count);
            Assert.AreEqual(2, child.Keys.Count);
            Assert.AreEqual(2, child.Children.Count);
        }
Example #5
0
        public void Test_InnerNode_Split_Test()
        {
            var innerNode = new InnerNode<int, int>(3);

            var leafNode = new LeafNode<int, int>(3);
            leafNode.Keys.Add(1);
            leafNode.Values.Add(new List<int>());
            leafNode.Values[0].Add(1);
            innerNode.Children.Add(leafNode);

            innerNode.Keys.Add(2);

            leafNode = new LeafNode<int, int>(3);
            leafNode.Keys.Add(2);
            leafNode.Values.Add(new List<int>());
            leafNode.Values[0].Add(2);
            innerNode.Children.Add(leafNode);

            innerNode.Keys.Add(3);

            leafNode = new LeafNode<int, int>(3);
            leafNode.Keys.Add(3);
            leafNode.Values.Add(new List<int>());
            leafNode.Values[0].Add(3);
            innerNode.Children.Add(leafNode);

            innerNode.Keys.Add(4);

            leafNode = new LeafNode<int, int>(3);
            leafNode.Keys.Add(4);
            leafNode.Values.Add(new List<int>());
            leafNode.Values[0].Add(4);
            leafNode.Keys.Add(5);
            leafNode.Values.Add(new List<int>());
            leafNode.Values[1].Add(5);
            innerNode.Children.Add(leafNode);

            var split = innerNode.Split() as InnerNode<int, int>;
            Assert.IsNotNull(split);
            Assert.AreEqual(2, innerNode.Children.Count);
            Assert.AreEqual(2, innerNode.Keys.Count);
            Assert.AreEqual(2, split.Children.Count);

            // this key will get promoted
            Assert.AreEqual(1, split.Keys.Count);
        }
Example #6
0
 private MathTree joinTrees(InnerNode op, LeafNode leaf1)
 {
     MathTree math = new MathTree(op, leaf1);
     return math;
 }
Example #7
0
 private MathTree joinTrees(InnerNode op, MathTree subtree1)
 {
     MathNode root1 = subtree1.root;
     MathTree math = new MathTree(op, root1);
     return math;
 }
Example #8
0
        private MathTree joinTrees(InnerNode op, MathTree subtree1, MathTree subtree2)
        {
            MathNode root1 = subtree1.root;
            MathNode root2 = subtree2.root;

            // create new tree with op as the root, and root1, root2
            // as the two child nodes
            MathTree math = new MathTree(op, root2, root1);

            return math;
        }
Example #9
0
        // Takes an operator, pops appropriate number of operands from 
        // operand stack, builds them into a new subtree and pushes the 
        // subtree back to the operand stack.
        private void buildNewSubtree(InnerNode op)
        {
           
            // take op2, build new subtree and push to operand stack
            Object topOperand = operandStack.Pop();
			
            // can be LeafNode or MathTree (subtree)
            if (topOperand is LeafNode)
            {
                LeafNode leafNode1 = (LeafNode)topOperand;

                if (op.numArgs() == 2) // binary operator
                {
                    Object nextOperand = operandStack.Pop();
                    if (nextOperand is LeafNode)
                    {
                        LeafNode leafNode2 = (LeafNode)nextOperand;
                        MathTree newSubtree = this.joinTrees(op, leafNode1, leafNode2);
                        operandStack.Push(newSubtree);
                    }
                    else
                    {
                        MathTree subtree2 = (MathTree)nextOperand;
                        MathTree newSubtree = this.joinTrees(op, leafNode1, subtree2);
                        operandStack.Push(newSubtree);
                    }
                }
                else // unary operator
                {
                    MathTree newSubtree = this.joinTrees(op, leafNode1);
                    operandStack.Push(newSubtree);
                }
            }
            else
            {
                MathTree subtree1 = (MathTree)topOperand;

                if (op.numArgs() == 2)  // binary operator
                {
                    Object nextOperand = operandStack.Pop();
                    if (nextOperand is LeafNode)
                    {
                        LeafNode leafNode2 = (LeafNode)nextOperand;
                        MathTree newSubtree = this.joinTrees(op, subtree1, leafNode2);
                        operandStack.Push(newSubtree);
                    }
                    else
                    {
                        MathTree subtree2 = (MathTree)nextOperand;
                        MathTree newSubtree = this.joinTrees(op, subtree1, subtree2);
                        operandStack.Push(newSubtree);
                    }

                }
                else // unary operator
                {
                    MathTree newSubtree = this.joinTrees(op, subtree1);
                    operandStack.Push(newSubtree);
                }
            }

            
        }
 protected override int CalculateChildHeight(ViewInfo view)
 {
     return(InnerNode.CalculateHeight(view));
 }
Example #11
0
        public override Size Draw(DrawContext context, int x, int y)
        {
            if (IsHidden && !IsWrapped)
            {
                return(DrawHidden(context, x, y));
            }

            var origX = x;
            var origY = y;

            AddSelection(context, x, y, context.Font.Height);

            x = AddOpenCloseIcon(context, x, y);
            x = AddIcon(context, x, y, context.IconProvider.Pointer, -1, HotSpotType.None);

            var tx = x;

            x = AddAddressOffset(context, x, y);

            x = AddText(context, x, y, context.Settings.TypeColor, HotSpot.NoneId, "WeakPtr") + context.Font.Width;
            x = AddText(context, x, y, context.Settings.NameColor, HotSpot.NameId, Name) + context.Font.Width;
            x = AddText(context, x, y, context.Settings.ValueColor, HotSpot.NoneId, $"<{InnerNode.Name}>");
            x = AddIcon(context, x, y, context.IconProvider.Change, 4, HotSpotType.ChangeClassType);

            x += context.Font.Width;

            AddComment(context, x, y);

            DrawInvalidMemoryIndicatorIcon(context, y);
            AddContextDropDownIcon(context, y);
            AddDeleteIcon(context, y);

            y += context.Font.Height;

            var size = new Size(x - origX, y - origY);

            if (LevelsOpen[context.Level])
            {
                var ptr = context.Memory.ReadObject <IntPtr>(Offset);
                if (!ptr.IsNull())
                {
                    ptr = context.Process.ReadRemoteObject <IntPtr>(ptr);
                    if (!ptr.IsNull())
                    {
                        ptr -= IntPtr.Size;
                    }
                }

                memory.Size = InnerNode.MemorySize;
                memory.UpdateFrom(context.Process, ptr);

                var v = context.Clone();
                v.Address = ptr;
                v.Memory  = memory;

                var innerSize = InnerNode.Draw(v, tx, y);

                size.Width   = Math.Max(size.Width, innerSize.Width + tx - origX);
                size.Height += innerSize.Height;
            }

            return(size);
        }
Example #12
0
        public override Size Draw(ViewInfo view, int x, int y)
        {
            if (IsHidden && !IsWrapped)
            {
                return(DrawHidden(view, x, y));
            }

            var origX = x;
            var origY = y;

            AddSelection(view, x, y, view.Font.Height);

            if (InnerNode != null)
            {
                x = AddOpenCloseIcon(view, x, y);
            }
            else
            {
                x += TextPadding;
            }
            x = AddIcon(view, x, y, Icons.Pointer, HotSpot.NoneId, HotSpotType.None);

            var tx = x;

            x = AddAddressOffset(view, x, y);

            x = AddText(view, x, y, view.Settings.TypeColor, HotSpot.NoneId, "Ptr") + view.Font.Width;
            if (!IsWrapped)
            {
                x = AddText(view, x, y, view.Settings.NameColor, HotSpot.NameId, Name) + view.Font.Width;
            }
            if (InnerNode == null)
            {
                x = AddText(view, x, y, view.Settings.ValueColor, HotSpot.NoneId, "<void>") + view.Font.Width;
            }
            x = AddIcon(view, x, y, Icons.Change, 4, HotSpotType.ChangeWrappedType) + view.Font.Width;

            var ptr = view.Memory.ReadIntPtr(Offset);

            x = AddText(view, x, y, view.Settings.OffsetColor, HotSpot.NoneId, "->") + view.Font.Width;
            x = AddText(view, x, y, view.Settings.ValueColor, 0, "0x" + ptr.ToString(Constants.AddressHexFormat)) + view.Font.Width;

            x = AddComment(view, x, y);

            DrawInvalidMemoryIndicatorIcon(view, y);
            AddContextDropDownIcon(view, y);
            AddDeleteIcon(view, y);

            y += view.Font.Height;

            var size = new Size(x - origX, y - origY);

            if (LevelsOpen[view.Level] && InnerNode != null)
            {
                memory.Size    = InnerNode.MemorySize;
                memory.Process = view.Memory.Process;
                memory.Update(ptr);

                var v = view.Clone();
                v.Address = ptr;
                v.Memory  = memory;

                var innerSize = InnerNode.Draw(v, tx, y);

                size.Width   = Math.Max(size.Width, innerSize.Width + tx - origX);
                size.Height += innerSize.Height;
            }

            return(size);
        }
Example #13
0
 public override void Intialize()
 {
     InnerNode = ClassNode.Create();
     InnerNode.Intialize();
 }
Example #14
0
        private static Node BuildNode(List <Entry> elements, Bucket[] buckets)
        {
            Box boundingBox = new Box(new Range(float.MaxValue, float.MinValue));

            foreach (Entry e in elements)
            {
                boundingBox.Include(e.BoundingBox);
            }
            if (elements.Count <= 4)
            {
                LeafNode leaf = new LeafNode(boundingBox);
                leaf.Elements = elements;
                return(leaf);
            }

            InnerNode innerNode = new InnerNode(boundingBox);

            Box centerVolume = new Box(new Range(float.MaxValue, float.MinValue));

            foreach (Entry e in elements)
            {
                centerVolume.Include(e.BoundingBox.Center);
            }


            float minVolume     = float.MaxValue;
            int   useAxis       = -1;
            int   splitAtBucket = 0;

            for (int axis = 0; axis < 3; axis++)
            {
                Range axisRange = centerVolume[axis];
                float extend    = axisRange.Extend;
                if (extend == 0)
                {
                    continue;
                }
                //extend = 1.0f;
                foreach (Entry e in elements)
                {
                    Bucket bucket = buckets[Math.Min(buckets.Length - 1, (int)((e.BoundingBox[axis].Center - axisRange.Min) / extend * buckets.Length))];
                    bucket.Elements.Add(e);
                    bucket.BoundingBox.Include(e.BoundingBox);
                }
                Box   bounds = buckets[0].BoundingBox;
                float volume = bounds.Volume;
                buckets[0].VolumeUntilThis = volume;
                for (int i = 1; i < buckets.Length; i++)
                {
                    if (buckets[i].Elements.Count > 0)
                    {
                        bounds.Include(buckets[i].BoundingBox);
                        volume = bounds.Volume;
                    }
                    buckets[i].VolumeUntilThis = volume;
                    // Debug.WriteLine("volumeUntil(axis " + axis + ", bucket " + i + ") = " + volume);
                }
                bounds = buckets[buckets.Length - 1].BoundingBox;
                volume = bounds.Volume;
                buckets[buckets.Length - 1].VolumeFromThis = volume;
                for (int i = buckets.Length - 2; i > -1; i--)
                {
                    if (buckets[i].Elements.Count > 0)
                    {
                        bounds.Include(buckets[i].BoundingBox);
                        volume = bounds.Volume;
                    }
                    buckets[i].VolumeFromThis = volume;
                    //Debug.WriteLine("volumeFrom(axis " + axis + ", bucket " + i + ") = " + volume);
                }

                for (int b = 1; b < buckets.Length; b++)
                {
                    volume = buckets[b - 1].VolumeUntilThis + buckets[b].VolumeFromThis;
                    if (volume < minVolume)
                    {
                        minVolume     = volume;
                        useAxis       = axis;
                        splitAtBucket = b;
                    }
                    //Debug.WriteLine("volume(axis " + axis + ", split " + b + ") = " + volume);
                }
                foreach (Bucket b in buckets)
                {
                    b.Reset();
                }
            }
            if (useAxis == -1)
            {
                LeafNode leaf = new LeafNode(boundingBox);
                leaf.Elements = elements;
                return(leaf);
            }

            Range axisRange2 = centerVolume[useAxis];
            float extend2    = axisRange2.Extend;

            Debug.Assert(extend2 != 0);

            List <Entry> lower = new List <Entry>(),
                         upper = new List <Entry>();

            foreach (Entry e in elements)
            {
                int bucketIndex = Math.Min(buckets.Length - 1, (int)((e.BoundingBox[useAxis].Center - axisRange2.Min) / extend2 * buckets.Length));
                if (bucketIndex >= splitAtBucket)
                {
                    upper.Add(e);
                }
                else
                {
                    lower.Add(e);
                }
            }

            innerNode.Axis   = useAxis;
            innerNode.Child0 = BuildNode(lower, buckets);
            innerNode.Child1 = BuildNode(upper, buckets);
            return(innerNode);
        }
Example #15
0
 public abstract void Visit(InnerNode node);
Example #16
0
        private MathTree makeFormulaTree(String formula, bool aggregateVariables)
        {


            // Approximate regexp of tokens
            // Fix - 06/03/2008 - Dorchard
            // First clause from: [a-zA-Z_]+
            // to: [a-zA-Z][a-zA-Z0-9_]*
            // Allows variables with numbers but which must start with at least one letter
            Regex formulaRegexp;
            if (aggregateVariables)
			{
				formulaRegexp = new Regex("([a-zA-Z_][a-zA-Z0-9_.]*)|([0-9]+([.][0-9]+)?)|([0-9]+)|[+-/*^| \t]|[()]");
			}
			else
			{
            	formulaRegexp = new Regex("([a-zA-Z_][a-zA-Z0-9_]*)|([0-9]+([.][0-9]+)?)|([0-9]+)|[+-/*^| \t]|[()]");
            }

            foreach(Match match in formulaRegexp.Matches(formula))
            {           
            		// Get the matched token
            		String token = match.ToString();
            		//System.Console.WriteLine(token);

            		// Look at idtable
            		//foreach (Object o in model.IdTable.Keys)
            		//{
            		//	System.Console.WriteLine(o.ToString()+" - "+model.IdTable[o].ToString());
            		//}
                if (token == " ")
                {
                    // just whitespace, ignore
                    continue;
                }
                // If we are parsing a formula that takes aggregate variables
                if (aggregateVariables && !token.Contains("."))
				{
					// Create aggreaget node first
                		AggregateReferenceNode operand = new AggregateReferenceNode(this.experiment, this.simulation);
                			
                		if (!TryParseAggregateSpecies(operand, token))
                		{
            				if (!TryParseAggregateGroup(operand, token))
            				{
            					if(TryParseAggregateCellDefinition(operand, token))
            					{
            						// Just a cell def
            						operandStack.Push(operand);
            						continue;
            					}
            				}
            				else
            				{
            					// Just a group
            					operandStack.Push(operand);
            					continue;
            				}
            			}
            			else
            			{
            				// Just species
            				operandStack.Push(operand);
            				continue;
            			}
            		}
                
                // if token is a number
                // or a name that will evaluate to a number (in ID table)
                // push to operand stack
                double d = 0.0d;
                if (double.TryParse(token, out d)) {
                    NumberLeafNode operand = new NumberLeafNode();
                    operand.AddData(token);
                    operandStack.Push(operand);
                }
                // some kind of split reference
                	else if (aggregateVariables && token.Contains(".")) 
                	{
                		// Create aggreaget node first
					AggregateReferenceNode operand = new AggregateReferenceNode(this.experiment, this.simulation);
                	
                		string[] tokens = token.Split('.');
                		if(tokens.Length==1)
                		{
                			System.Console.WriteLine("Parse error in formula - "+formula+" at token - "+token); 
                		}
                		else if (tokens.Length==2)
                		{
                			// Try a group
                			if (!TryParseAggregateGroup(operand, tokens[0]))
                			{
                				// Try a celldef
                				if (!TryParseAggregateCellDefinition(operand, tokens[0]))
                				{
                					// If this failed then error
                					System.Console.WriteLine("Parse error in formula - CellDefinition/Group not found: "+tokens[0]);
                				}
                				else
                				{
                					// Starts with a celldef, could be a group of celldef
                					if (!TryParseAggregateGroup(operand, tokens[1]))
                					{
                						// must be a species
									if (TryParseAggregateSpecies(operand, tokens[1]))
			                			{
			                				// CellDef.Species
			                				operandStack.Push(operand);
			                			}
			                			else
			                			{
			                				// Fail
			                				System.Console.WriteLine("Parse error in formula - Species not found: "+tokens[1]);
			                			}
                					}
                					else
                					{
                						// Was a group, ok
                						// CellDef.Group
                						operandStack.Push(operand);
                					}
                				}
                			}
                			else
                			{
                				// Starts with a group
                				// Must therefore be a species
                				if (TryParseAggregateSpecies(operand, tokens[1]))
                				{
                					// Group.Species
                					operandStack.Push(operand);
                				}
                				else
                				{
                					// Fail
                					System.Console.WriteLine("Parse error in formula - Species not found: "+tokens[1]);
                				}
                			}
                		}
                		else if (tokens.Length==3)
                		{
                			// should be CellDef.Group.Species
                			if (TryParseAggregateCellDefinition(operand, tokens[0]))
                			{
                				if (TryParseAggregateGroup(operand, tokens[1]))
                				{
                					if (TryParseAggregateSpecies(operand, tokens[2]))
                					{
                						operandStack.Push(operand);		
                					}
                					else
                					{
                						System.Console.WriteLine("Parse error in formula - Species not found: "+tokens[2]);
                					}
                				}
                				else
                				{
                					System.Console.WriteLine("Parse error in formula - Group not found: "+tokens[1]);
                				}
                			}
                			else
                			{
                				System.Console.WriteLine("Parse error in formula - CellDef not found: "+tokens[0]);
                			}
                		}
                		else
                		{
                			// Too many periods
                			System.Console.WriteLine("Parse error in formula - "+formula+" at token - "+token);
                		}
                	}
                else if ((model != null) && (model.idExists(token)))
                {
                    		ReferenceLeafNode operand = new ReferenceLeafNode();                    
                    		operand.AddData(token, model);
                    		operandStack.Push(operand);

                            if (operand.data is Parameter)
                            {
                                this.formulaParameters.Add((Parameter)operand.data);
                            }
                            if (operand.data is Species)
                            {
                                this.formulaSpecies.Add((Species)operand.data);
                            }
                }
                else if (reader.unaryMathOperatorsLookup.ContainsKey(token))
                {
                    // function call, push to stack
                    InnerNode functionNode = new InnerNode(reader.unaryMathOperatorsLookup[token]);
                    operatorStack.Push(functionNode);

                    // Cannot be user-defined FunctionDefinition, because
                    // string formulae and FunctionDefinitions are in 
                    // mutually exclusive SBML Levels.
                }
                 else if (reader.binaryMathOperatorsLookup.ContainsKey(token) && !opPrecedence.ContainsKey(token))
                {
                    // function call, push to stack
                    InnerNode functionNode = new InnerNode(reader.binaryMathOperatorsLookup[token]);
                    operatorStack.Push(functionNode);
                }
                else if (opPrecedence.ContainsKey(token))
                {
                    // While there is an operator on the top of the stack
                    while (operatorStack.Count>0 &&	operatorStack.Peek() is InnerNode)
                    {
                        InnerNode op2 = (InnerNode)operatorStack.Peek();
                        int op2Precedence = (int)opPrecedence[op2.data];
                        int tokenPrecedence = (int)opPrecedence[token];

                        if (op2Precedence > tokenPrecedence)
                        {
                            // pop, build new subtree, push to operand stack
                            operatorStack.Pop();
                            buildNewSubtree(op2);
                        }
                        else
                        {
                            break;
                        }
                    }
                    // create token as new OperatorNode
                    if (reader.binaryMathOperatorsLookup.ContainsKey(token))
                    {
                    		BinaryMathOperators op = reader.binaryMathOperatorsLookup[token];
                    		InnerNode opToken = new InnerNode(op);
                    		// push token to operator stack
                    		operatorStack.Push(opToken);
                    	}
                    	else if (reader.unaryMathOperatorsLookup.ContainsKey(token))
                    	{
                    	 	UnaryMathOperators op = reader.unaryMathOperatorsLookup[token];
                    		InnerNode opToken = new InnerNode(op);
                    		// push token to operator stack
                    		operatorStack.Push(opToken);
                    	}
                    	else
                    	{
                    		// <todo> raise parse exception </todo>
                    	}
                }
                else if (token == "(")
                {
                    operatorStack.Push(token);
                }
                else if (token == ")")
                {
                    while (operatorStack.Count>0 && !((operatorStack.Peek() is string) && ((string)operatorStack.Peek())=="("))
                    {
                        // pop operator, build new subtree, push to operand stack
                        InnerNode op = (InnerNode)operatorStack.Pop();
                        buildNewSubtree(op);
                    }
                    // remove the "(" from the stack
                    Object o = operatorStack.Pop();
                    
                    // check if top of stack is a function call
                    // if so, pop, build new subtree, push to operand stack
                    if (operatorStack.Count>0 && (reader.unaryMathOperatorsLookup.ContainsValue((UnaryMathOperators)(((InnerNode)(operatorStack.Peek())).data)) ||
                    		reader.binaryMathOperatorsLookup.ContainsValue((BinaryMathOperators)(((InnerNode)(operatorStack.Peek())).data))))
                    {
                        InnerNode op = (InnerNode)operatorStack.Pop();
                        buildNewSubtree(op);
                    }
                }
                else
                {
                    Regex checkAlpha = new Regex("[a-zA-Z_][a-zA-Z0-9_.]*");
                        Match matchAlpha = checkAlpha.Match(token);
                        // Approximate test of unknown token
                        if (!matchAlpha.Success)
                        {
                            // Likely some unknown or badly formatted symbols
                            // No recognition - parse error
                            // <todo>Raise exception?</todo>
                            System.Console.WriteLine("Parse error in formula - " + formula + " at token - " + token);
                        }
                        else
                        {

                            // Potentially an unknown ID try and add as an unknown identifier and try to continue parsing
                            ReferenceLeafNode operand = new ReferenceLeafNode();
                            operand.data = new UnknownEntity();
                            operand.data.ID = token;
                            operandStack.Push(operand);

                            this.formulaUnknownEntities.Add((UnknownEntity)operand.data);
                            System.Console.WriteLine("Unknown entity in formula - " + token);
                        }
                		
                }
            }

            // We have now reached the end of the tokens.
            // Pop the rest of the operators.
            while (operatorStack.Count>0)
            {
                InnerNode op = (InnerNode)operatorStack.Pop();
                buildNewSubtree(op);
            }
            // No more operators.

			MathTree math;
			// if we just have a leaf node on the stack convert it to a mathtree
			if (operandStack.Peek() is LeafNode)
			{
				math = new MathTree();
				math.root = (LeafNode)operandStack.Pop();
			}
			else
			{
            		math = (MathTree)operandStack.Pop();
            	}
            	
            //	debug();
            	
            return math;
        }
Example #17
0
        public void Test_Tree_Delete()
        {
            var root = new InnerNode<int, int>(3);
            root.Keys.Add(15);

                var level1Child1 = new InnerNode<int, int>(3);
                level1Child1.Keys.Add(9);
                level1Child1.Keys.Add(12);

                    var level2Child1 = new LeafNode<int, int>(3);
                    level2Child1.Add(1, 1);
                    level2Child1.Add(4, 4);

                level1Child1.Children.Add(level2Child1);

                    var level2Child2 = new LeafNode<int, int>(3);
                    level2Child2.Add(9, 9);
                    level2Child2.Add(10, 10);

                level1Child1.Children.Add(level2Child2);

                    var level2Child3 = new LeafNode<int, int>(3);
                    level2Child3.Add(12, 12);
                    level2Child3.Add(13, 13);

                level1Child1.Children.Add(level2Child3);

            root.Children.Add(level1Child1);

                var level1Child2 = new InnerNode<int, int>(3);
                level1Child2.Keys.Add(20);

                    var level2Child4 = new LeafNode<int, int>(3);
                    level2Child4.Add(15, 15);
                    level2Child4.Add(16, 16);

                level1Child2.Children.Add(level2Child4);

                    var level2Child5 = new LeafNode<int, int>(3);
                    level2Child5.Add(20,20);
                    level2Child5.Add(25,25);

                level1Child2.Children.Add(level2Child5);

            root.Children.Add(level1Child2);

            var tree = new Tree<int, int>(root);

            tree.Delete(13);
            Assert.AreEqual(1, root.Keys.Count);
            Assert.AreEqual(15, root.Keys[0]);

            var delete13Parent = (root.Children[0] as InnerNode<int, int>);
            Assert.AreEqual(2, delete13Parent.Keys.Count);
            Assert.AreEqual(09, delete13Parent.Keys[0]);
            Assert.AreEqual(12, delete13Parent.Keys[1]);
            Assert.AreEqual(3, delete13Parent.Children.Count);
            var delete13Child3 = delete13Parent.Children[2];
            Assert.AreEqual(1, delete13Child3.Keys.Count);
            Assert.AreEqual(12, delete13Child3.Keys[0]);

            tree.Delete(15);

            var delete15Parent = (root.Children[1] as InnerNode<int, int>);
            Assert.AreEqual(1, delete15Parent.Keys.Count);
            Assert.AreEqual(20, delete15Parent.Keys[0]);
            Assert.AreEqual(2, delete15Parent.Children.Count);
            var delete15Child1 = delete15Parent.Children[0];
            Assert.AreEqual(1, delete15Child1.Keys.Count);
            Assert.AreEqual(16, delete15Child1.Keys[0]);

            tree.Delete(12);
            var delete12Parent = (root.Children[0] as InnerNode<int, int>);
            Assert.AreEqual(2, delete12Parent.Keys.Count);
            Assert.AreEqual(9, delete12Parent.Keys[0]);
            Assert.AreEqual(10, delete12Parent.Keys[1]);
            Assert.AreEqual(3, delete12Parent.Children.Count);
            var delete12Child2 = delete12Parent.Children[1];
            Assert.AreEqual(1, delete12Child2.Keys.Count);
            Assert.AreEqual(9, delete12Child2.Keys[0]);
            var delete12Child3 = delete12Parent.Children[2];
            Assert.AreEqual(1, delete12Child3.Keys.Count);
            Assert.AreEqual(10, delete12Child3.Keys[0]);

            tree.Delete(16);
            var delete16Parent = (root.Children[1] as InnerNode<int, int>);
            Assert.AreEqual(2, delete16Parent.Children.Count);
            Assert.AreEqual(1, delete16Parent.Keys.Count);
            Assert.AreEqual(25, delete16Parent.Keys[0]);
            var delete16Child1 = delete16Parent.Children[0];
            Assert.AreEqual(1, delete16Child1.Keys.Count);
            Assert.AreEqual(20, delete16Child1.Keys[0]);
            var delete16Child2 = delete16Parent.Children[1];
            Assert.AreEqual(1, delete16Child2.Keys.Count);
            Assert.AreEqual(25, delete16Child2.Keys[0]);

            tree.Delete(25);

            tree.Delete(10);
        }
        /// <summary>Draws this node.</summary>
        /// <param name="view">The view information.</param>
        /// <param name="x">The x coordinate.</param>
        /// <param name="y">The y coordinate.</param>
        /// <returns>The pixel size the node occupies.</returns>
        public override Size Draw(ViewInfo view, int x, int y)
        {
            if (IsHidden)
            {
                return(DrawHidden(view, x, y));
            }

            var origX = x;
            var origY = y;

            AddSelection(view, x, y, view.Font.Height);
            AddDelete(view, y);
            AddTypeDrop(view, y);

            x = AddOpenClose(view, x, y);
            x = AddIcon(view, x, y, Icons.Pointer, -1, HotSpotType.None);

            var tx = x;

            x = AddAddressOffset(view, x, y);

            x = AddText(view, x, y, view.Settings.TypeColor, HotSpot.NoneId, "WeakPtr") + view.Font.Width;
            x = AddText(view, x, y, view.Settings.NameColor, HotSpot.NameId, Name) + view.Font.Width;
            x = AddText(view, x, y, view.Settings.ValueColor, HotSpot.NoneId, $"<{InnerNode.Name}>");
            x = AddIcon(view, x, y, Icons.Change, 4, HotSpotType.ChangeType);

            x += view.Font.Width;
            AddComment(view, x, y);

            y += view.Font.Height;

            var size = new Size(x - origX, y - origY);

            if (levelsOpen[view.Level])
            {
                var ptr = view.Memory.ReadObject <IntPtr>(Offset);
                if (!ptr.IsNull())
                {
                    ptr = view.Memory.Process.ReadRemoteObject <IntPtr>(ptr);
                    if (!ptr.IsNull())
                    {
                        ptr = ptr - IntPtr.Size;
                    }
                }

                memory.Size    = InnerNode.MemorySize;
                memory.Process = view.Memory.Process;
                memory.Update(ptr);

                var v = view.Clone();
                v.Address = ptr;
                v.Memory  = memory;

                var innerSize = InnerNode.Draw(v, tx, y);

                size.Width   = Math.Max(size.Width, innerSize.Width + tx - origX);
                size.Height += innerSize.Height;
            }

            return(size);
        }