Esempio n. 1
0
 /// <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;
 }
Esempio n. 2
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="level">-1 to get items.</param>
 /// <param name="top"></param>
 /// <param name="boundables"></param>
 private static 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;
 }
Esempio n. 3
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="node"></param>
 /// <param name="item"></param>
 /// <returns></returns>
 private static 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;
 }
Esempio n. 4
0
 /// <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 throw new ShouldNeverReachHereException();
     }
 }
Esempio n. 5
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="node"></param>
 /// <returns></returns>
 protected virtual 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;
 }
Esempio n. 6
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="node"></param>
 /// <returns></returns>
 protected virtual 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;
 }
Esempio n. 7
0
 /// <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 virtual void Build()
 {
     Assert.IsTrue(!_built);
     _root = (_itemBoundables.Count == 0) ? CreateNode(0) : CreateHigherLevels(_itemBoundables, -1);
     _built = true;
 }