Exemplo n.º 1
0
        public virtual void SanityCheckParentAndChildIndexes(ITree parent, int i)
        {
            if (parent != this.Parent)
            {
                throw new InvalidOperationException("parents don't match; expected " + parent + " found " + this.Parent);
            }
            if (i != this.ChildIndex)
            {
                throw new InvalidOperationException("child indexes don't match; expected " + i + " found " + this.ChildIndex);
            }
            int n = this.ChildCount;

            for (int c = 0; c < n; c++)
            {
                BaseTree child = (BaseTree)this.GetChild(c);
                child.SanityCheckParentAndChildIndexes(this, c);
            }
        }
Exemplo n.º 2
0
        /** <summary>
         *  Delete children from start to stop and replace with t even if t is
         *  a list (nil-root tree).  num of children can increase or decrease.
         *  For huge child lists, inserting children can force walking rest of
         *  children to set their childindex; could be slow.
         *  </summary>
         */
        public virtual void ReplaceChildren(int startChildIndex, int stopChildIndex, object t)
        {
            if (startChildIndex < 0)
            {
                throw new ArgumentOutOfRangeException();
            }
            if (stopChildIndex < 0)
            {
                throw new ArgumentOutOfRangeException();
            }
            if (t == null)
            {
                throw new ArgumentNullException("t");
            }
            if (stopChildIndex < startChildIndex)
            {
                throw new ArgumentException();
            }

            /*
             * System.out.println("replaceChildren "+startChildIndex+", "+stopChildIndex+
             *                 " with "+((BaseTree)t).toStringTree());
             * System.out.println("in="+toStringTree());
             */
            if (Children == null)
            {
                throw new ArgumentException("indexes invalid; no children in list");
            }
            int           replacingHowMany = stopChildIndex - startChildIndex + 1;
            int           replacingWithHowMany;
            ITree         newTree     = (ITree)t;
            IList <ITree> newChildren = null;

            // normalize to a list of children to add: newChildren
            if (newTree.IsNil)
            {
                BaseTree baseTree = newTree as BaseTree;
                if (baseTree != null && baseTree.Children != null)
                {
                    newChildren = baseTree.Children;
                }
                else
                {
                    newChildren = CreateChildrenList();
                    int n = newTree.ChildCount;
                    for (int i = 0; i < n; i++)
                    {
                        newChildren.Add(newTree.GetChild(i));
                    }
                }
            }
            else
            {
                newChildren = new List <ITree>(1);
                newChildren.Add(newTree);
            }
            replacingWithHowMany = newChildren.Count;
            int numNewChildren = newChildren.Count;
            int delta          = replacingHowMany - replacingWithHowMany;

            // if same number of nodes, do direct replace
            if (delta == 0)
            {
                int j = 0; // index into new children
                for (int i = startChildIndex; i <= stopChildIndex; i++)
                {
                    ITree child = newChildren[j];
                    Children[i]      = child;
                    child.Parent     = this;
                    child.ChildIndex = i;
                    j++;
                }
            }
            else if (delta > 0)
            {
                // fewer new nodes than there were
                // set children and then delete extra
                for (int j = 0; j < numNewChildren; j++)
                {
                    Children[startChildIndex + j] = newChildren[j];
                }
                int indexToDelete = startChildIndex + numNewChildren;
                for (int c = indexToDelete; c <= stopChildIndex; c++)
                {
                    // delete same index, shifting everybody down each time
                    Children.RemoveAt(indexToDelete);
                }
                FreshenParentAndChildIndexes(startChildIndex);
            }
            else
            {
                // more new nodes than were there before
                // fill in as many children as we can (replacingHowMany) w/o moving data
                for (int j = 0; j < replacingHowMany; j++)
                {
                    Children[startChildIndex + j] = newChildren[j];
                }
                int numToInsert = replacingWithHowMany - replacingHowMany;
                for (int j = replacingHowMany; j < replacingWithHowMany; j++)
                {
                    Children.Insert(startChildIndex + j, newChildren[j]);
                }
                FreshenParentAndChildIndexes(startChildIndex);
            }
            //System.out.println("out="+toStringTree());
        }
 public AntlrRuntime_BaseTreeDebugView(BaseTree tree)
 {
     _tree = tree;
 }