/// <summary> /// /// </summary> /// <param name="searchBounds"></param> /// <param name="node"></param> /// <param name="item"></param> /// <returns></returns> private bool Remove(object searchBounds, AbstractNode node, object item) { // first try removing item from this node bool found = RemoveItem(node, item); if (found) return true; AbstractNode childToPrune = null; // next try removing item from lower nodes for (IEnumerator i = node.ChildBoundables.GetEnumerator(); i.MoveNext(); ) { IBoundable childBoundable = (IBoundable) i.Current; if (!IntersectsOp.Intersects(childBoundable.Bounds, searchBounds)) continue; if (childBoundable is AbstractNode) { found = Remove(searchBounds, (AbstractNode) childBoundable, item); // if found, record child for pruning and exit if (found) { childToPrune = (AbstractNode) childBoundable; break; } } } // prune child if possible if (childToPrune != null) if (childToPrune.ChildBoundables.Count == 0) node.ChildBoundables.Remove(childToPrune); return found; }
/// <summary> /// /// </summary> /// <param name="node"></param> /// <param name="item"></param> /// <returns></returns> private bool RemoveItem(AbstractNode node, object item) { IBoundable childToRemove = null; for (IEnumerator i = node.ChildBoundables.GetEnumerator(); i.MoveNext(); ) { IBoundable childBoundable = (IBoundable) i.Current; if (childBoundable is ItemBoundable) if (((ItemBoundable) childBoundable).Item == item) childToRemove = childBoundable; } if (childToRemove != null) { node.ChildBoundables.Remove(childToRemove); return true; } return false; }
/// <summary> /// /// </summary> /// <param name="searchBounds"></param> /// <param name="node"></param> /// <param name="visitor"></param> private void Query(object searchBounds, AbstractNode node, IItemVisitor visitor) { foreach(object obj in node.ChildBoundables) { IBoundable childBoundable = (IBoundable) obj; if (!IntersectsOp.Intersects(childBoundable.Bounds, searchBounds)) continue; if (childBoundable is AbstractNode) Query(searchBounds, (AbstractNode) childBoundable, visitor); else if (childBoundable is ItemBoundable) visitor.VisitItem(((ItemBoundable) childBoundable).Item); else Assert.ShouldNeverReachHere(); } }
/// <summary> /// /// </summary> /// <param name="level">-1 to get items.</param> /// <param name="top"></param> /// <param name="boundables"></param> private void BoundablesAtLevel(int level, AbstractNode top, ref IList boundables) { Assert.IsTrue(level > -2); if (top.Level == level) { boundables.Add(top); return; } for (IEnumerator i = top.ChildBoundables.GetEnumerator(); i.MoveNext(); ) { IBoundable boundable = (IBoundable) i.Current; if (boundable is AbstractNode) BoundablesAtLevel(level, (AbstractNode) boundable, ref boundables); else { Assert.IsTrue(boundable is ItemBoundable); if (level == -1) boundables.Add(boundable); } } return; }
/// <summary> /// /// </summary> /// <param name="node"></param> /// <returns></returns> protected int GetSize(AbstractNode node) { int size = 0; for (IEnumerator i = node.ChildBoundables.GetEnumerator(); i.MoveNext(); ) { IBoundable childBoundable = (IBoundable) i.Current; if (childBoundable is AbstractNode) size += GetSize((AbstractNode) childBoundable); else if (childBoundable is ItemBoundable) size += 1; } return size; }
/// <summary> /// /// </summary> /// <param name="node"></param> /// <returns></returns> protected int GetDepth(AbstractNode node) { int maxChildDepth = 0; for (IEnumerator i = node.ChildBoundables.GetEnumerator(); i.MoveNext(); ) { IBoundable childBoundable = (IBoundable) i.Current; if (childBoundable is AbstractNode) { int childDepth = GetDepth((AbstractNode) childBoundable); if (childDepth > maxChildDepth) maxChildDepth = childDepth; } } return maxChildDepth + 1; }
/// <summary> /// Creates parent nodes, grandparent nodes, and so forth up to the root /// node, for the data that has been inserted into the tree. Can only be /// called once, and thus can be called only after all of the data has been /// inserted into the tree. /// </summary> public void Build() { Assert.IsTrue(!built); root = (itemBoundables.Count == 0) ? CreateNode(0) : CreateHigherLevels(itemBoundables, -1); built = true; }