public virtual void AddChild(ITree t) { if (t != null) { if (t.IsNil) { BaseTree baseTree = t as BaseTree; if (baseTree != null && this.children != null && this.children == baseTree.children) { throw new Exception("attempt to add child list to itself"); } if (t.ChildCount > 0) { if (this.children != null || baseTree == null) { if (this.children == null) { this.children = this.CreateChildrenList(); } int childCount = t.ChildCount; for (int i = 0; i < childCount; i++) { ITree child = t.GetChild(i); this.children.Add(child); child.Parent = this; child.ChildIndex = this.children.Count - 1; } } else { this.children = baseTree.children; this.FreshenParentAndChildIndexes(); } } } else { if (this.children == null) { this.children = this.CreateChildrenList(); } this.children.Add(t); t.Parent = this; t.ChildIndex = this.children.Count - 1; } } }
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 childCount = this.ChildCount; for (int j = 0; j < childCount; j++) { BaseTree baseTree = (BaseTree)this.GetChild(j); baseTree.SanityCheckParentAndChildIndexes(this, j); } }
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(); } if (this.children == null) { throw new ArgumentException("indexes invalid; no children in list"); } int num = stopChildIndex - startChildIndex + 1; ITree tree = (ITree)t; List <ITree> list = null; if (tree.IsNil) { BaseTree baseTree = tree as BaseTree; if (baseTree != null && baseTree.children != null) { list = baseTree.children; } else { list = this.CreateChildrenList(); int childCount = tree.ChildCount; for (int i = 0; i < childCount; i++) { list.Add(tree.GetChild(i)); } } } else { list = new List <ITree>(1); list.Add(tree); } int count = list.Count; int count2 = list.Count; int num2 = num - count; if (num2 == 0) { int num3 = 0; for (int j = startChildIndex; j <= stopChildIndex; j++) { ITree tree2 = list[num3]; this.children[j] = tree2; tree2.Parent = this; tree2.ChildIndex = j; num3++; } } else if (num2 > 0) { for (int k = 0; k < count2; k++) { this.children[startChildIndex + k] = list[k]; } int num4 = startChildIndex + count2; for (int l = num4; l <= stopChildIndex; l++) { this.children.RemoveAt(num4); } this.FreshenParentAndChildIndexes(startChildIndex); } else { for (int m = 0; m < num; m++) { this.children[startChildIndex + m] = list[m]; } for (int n = num; n < count; n++) { this.children.Insert(startChildIndex + n, list[n]); } this.FreshenParentAndChildIndexes(startChildIndex); } }