private void contains(Rectangle r, intproc v) { // find all rectangles in the tree that are contained by the passed rectangle // written to be non-recursive (should model other searches on this?) parents.Clear(); parents.Push(rootNodeId); parentsEntry.Clear(); parentsEntry.Push(-1); // TODO: possible shortcut here - could test for intersection with the // MBR of the root node. If no intersection, return immediately. while (parents.Count > 0) { Node <T> n = getNode(parents.Peek()); int startIndex = parentsEntry.Peek() + 1; if (!n.isLeaf()) { // go through every entry in the index Node<T> to check // if it intersects the passed rectangle. If so, it // could contain entries that are contained. bool intersects = false; for (int i = startIndex; i < n.entryCount; i++) { if (r.intersects(n.entries[i])) { parents.Push(n.ids[i]); parentsEntry.Pop(); parentsEntry.Push(i); // this becomes the start index when the child has been searched parentsEntry.Push(-1); intersects = true; break; // ie go to next iteration of while() } } if (intersects) { continue; } } else { // go through every entry in the leaf to check if // it is contained by the passed rectangle for (int i = 0; i < n.entryCount; i++) { if (r.contains(n.entries[i])) { v(n.ids[i]); } } } parents.Pop(); parentsEntry.Pop(); } }
/// <summary> /// Recursively searches the tree for all intersecting entries. /// Immediately calls execute() on the passed IntProcedure when /// a matching entry is found. /// [x] TODO rewrite this to be non-recursive? Make sure it /// doesn't slow it down. /// </summary> /// <param name="r"></param> /// <param name="v"></param> /// <param name="n"></param> private void intersects(Rectangle r, intproc v, Node <T> n) { for (int i = 0; i < n.entryCount; i++) { if (r.intersects(n.entries[i])) { if (n.isLeaf()) { v(n.ids[i]); } else { Node <T> childNode = getNode(n.ids[i]); intersects(r, v, childNode); } } } }