Examples of TIntStack


Examples of gnu.trove.stack.TIntStack

    //  return immediately if given an invalid "count" parameter
    if (count <= 0) {
      return;
    }

    TIntStack parents = new TIntArrayStack();
    parents.push(rootNodeId);

    TIntStack parentsEntry = new TIntArrayStack();
    parentsEntry.push(-1);

    TIntArrayList savedValues = new TIntArrayList();
    float savedPriority = 0;

    // TODO: possible shortcut here - could test for intersection with the
    //       MBR of the root node. If no intersection, return immediately.

    float furthestDistanceSq = furthestDistance * furthestDistance;

    while (parents.size() > 0) {
      Node n = getNode(parents.peek());
      int startIndex = parentsEntry.peek() + 1;

      if (!n.isLeaf()) {
        // go through every entry in the index node to check
        // if it could contain an entry closer than the farthest entry
        // currently stored.
        boolean near = false;
        for (int i = startIndex; i < n.entryCount; i++) {
          if (Rectangle.distanceSq(n.entriesMinX[i], n.entriesMinY[i],
                                 n.entriesMaxX[i], n.entriesMaxY[i],
                                 p.x, p.y) <= furthestDistanceSq) {
            parents.push(n.ids[i]);
            parentsEntry.pop();
            parentsEntry.push(i); // this becomes the start index when the child has been searched
            parentsEntry.push(-1);
            near = true;
            break; // ie go to next iteration of while()
          }
        }
        if (near) {
          continue;
        }
      } else {
        // go through every entry in the leaf to check if
        // it is currently one of the nearest N entries.
        for (int i = 0; i < n.entryCount; i++) {
          float entryDistanceSq = Rectangle.distanceSq(n.entriesMinX[i], n.entriesMinY[i],
                                                   n.entriesMaxX[i], n.entriesMaxY[i],
                                                   p.x, p.y);
          int entryId = n.ids[i];

          if (entryDistanceSq <= furthestDistanceSq) {
            distanceQueue.insert(entryId, entryDistanceSq);

            while (distanceQueue.size() > count) {
              // normal case - we can simply remove the lowest priority (highest distance) entry
              int value = distanceQueue.getValue();
              float distanceSq = distanceQueue.getPriority();
              distanceQueue.pop();

              // rare case - multiple items of the same priority (distance)
              if (distanceSq == distanceQueue.getPriority()) {
                savedValues.add(value);
                savedPriority = distanceSq;
              } else {
                savedValues.reset();
              }
            }

            // if the saved values have the same distance as the
            // next one in the tree, add them back in.
            if (savedValues.size() > 0 && savedPriority == distanceQueue.getPriority()) {
              for (int svi = 0; svi < savedValues.size(); svi++) {
                distanceQueue.insert(savedValues.get(svi), savedPriority);
              }
              savedValues.reset();
            }

            // narrow the search, if we have already found N items
            if (distanceQueue.getPriority() < furthestDistanceSq && distanceQueue.size() >= count) {
              furthestDistanceSq = distanceQueue.getPriority();
            }
          }
        }
      }
      parents.pop();
      parentsEntry.pop();
    }
  }
View Full Code Here

Examples of gnu.trove.stack.TIntStack

   * @see net.sf.jsi.SpatialIndex#contains(Rectangle, TIntProcedure)
   */
  public void contains(Rectangle r, TIntProcedure 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?)
    TIntStack parents = new TIntArrayStack();
    parents.push(rootNodeId);

    TIntStack parentsEntry = new TIntArrayStack();
    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.size() > 0) {
      Node n = getNode(parents.peek());
      int startIndex = parentsEntry.peek() + 1;

      if (!n.isLeaf()) {
        // go through every entry in the index node to check
        // if it intersects the passed rectangle. If so, it
        // could contain entries that are contained.
        boolean intersects = false;
        for (int i = startIndex; i < n.entryCount; i++) {
          if (Rectangle.intersects(r.minX, r.minY, r.maxX, r.maxY,
                                   n.entriesMinX[i], n.entriesMinY[i], n.entriesMaxX[i], n.entriesMaxY[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 (Rectangle.contains(r.minX, r.minY, r.maxX, r.maxY,
                                 n.entriesMinX[i], n.entriesMinY[i], n.entriesMaxX[i], n.entriesMaxY[i])) {
            if (!v.execute(n.ids[i])) {
              return;
            }
          }
        }
      }
      parents.pop();
      parentsEntry.pop();
    }
  }
View Full Code Here

Examples of gnu.trove.stack.TIntStack

    // nodes to be empty.
    Node n = l;
    Node parent = null;
    int parentEntry = 0;

    TIntStack eliminatedNodeIds = new TIntArrayStack();

    // CT2 [Find parent entry] If N is the root, go to CT6. Otherwise
    // let P be the parent of N, and let En be N's entry in P
    while (n.level != treeHeight) {
      parent = getNode(parents.pop());
      parentEntry = parentsEntry.pop();

      // CT3 [Eliminiate under-full node] If N has too few entries,
      // delete En from P and add N to the list of eliminated nodes
      if (n.entryCount < minNodeEntries) {
        parent.deleteEntry(parentEntry);
        eliminatedNodeIds.push(n.nodeId);
      } else {
        // CT4 [Adjust covering rectangle] If N has not been eliminated,
        // adjust EnI to tightly contain all entries in N
        if (n.mbrMinX != parent.entriesMinX[parentEntry] ||
            n.mbrMinY != parent.entriesMinY[parentEntry] ||
            n.mbrMaxX != parent.entriesMaxX[parentEntry] ||
            n.mbrMaxY != parent.entriesMaxY[parentEntry]) {
          float deletedMinX = parent.entriesMinX[parentEntry];
          float deletedMinY = parent.entriesMinY[parentEntry];
          float deletedMaxX = parent.entriesMaxX[parentEntry];
          float deletedMaxY = parent.entriesMaxY[parentEntry];
          parent.entriesMinX[parentEntry] = n.mbrMinX;
          parent.entriesMinY[parentEntry] = n.mbrMinY;
          parent.entriesMaxX[parentEntry] = n.mbrMaxX;
          parent.entriesMaxY[parentEntry] = n.mbrMaxY;
          parent.recalculateMBRIfInfluencedBy(deletedMinX, deletedMinY, deletedMaxX, deletedMaxY);
        }
      }
      // CT5 [Move up one level in tree] Set N=P and repeat from CT2
      n = parent;
    }

    // CT6 [Reinsert orphaned entries] Reinsert all entries of nodes in set Q.
    // Entries from eliminated leaf nodes are reinserted in tree leaves as in
    // Insert(), but entries from higher level nodes must be placed higher in
    // the tree, so that leaves of their dependent subtrees will be on the same
    // level as leaves of the main tree
    while (eliminatedNodeIds.size() > 0) {
      Node e = getNode(eliminatedNodeIds.pop());
      for (int j = 0; j < e.entryCount; j++) {
        add(e.entriesMinX[j], e.entriesMinY[j], e.entriesMaxX[j], e.entriesMaxY[j], e.ids[j], e.level);
        e.ids[j] = -1;
      }
      e.entryCount = 0;
View Full Code Here

Examples of gnu.trove.stack.TIntStack

      stack.equals( copy ) );
    }


    public void testBasic() {
        TIntStack stack = new TIntArrayStack();

        assertEquals( 0, stack.size() );

        stack.push( 10 );

        assertEquals( 1, stack.size() );

        assertEquals( 10, stack.peek() );
        assertEquals( 1, stack.size() );
        assertEquals( 10, stack.peek() );
        assertEquals( 1, stack.size() );

        assertEquals( 10, stack.pop() );
        assertEquals( 0, stack.size() );

        stack.push( 10 );
        stack.push( 20 );
        stack.push( 30 );

        assertEquals( 3, stack.size() );
        assertEquals( 30, stack.pop() );
        assertEquals( 20, stack.pop() );
        assertEquals( 10, stack.pop() );
    }
View Full Code Here

Examples of gnu.trove.stack.TIntStack

    }


    public void testArrays() {
        int no_entry_value = Integer.MIN_VALUE;
        TIntStack stack = new TIntArrayStack(10, no_entry_value);
        assertEquals( no_entry_value, stack.getNoEntryValue() );

        int[] array;

        array = stack.toArray();
        assertNotNull( array );
        assertEquals( 0, array.length );

        stack.push( 10 );
        stack.push( 20 );
        stack.push( 30 );
        stack.push( 40 );

        array = stack.toArray();
        assertNotNull( array );
        assertEquals( 4, array.length );
        // NOTE: Top element in stack should be first element in array
        assertEquals( 40, array[0] );
        assertEquals( 30, array[1] );
        assertEquals( 20, array[2] );
        assertEquals( 10, array[3] );
        assertEquals( 4, stack.size() );

        int[] array_correct_size = new int[4];
        stack.toArray( array_correct_size );
        assertEquals( 40, array_correct_size[0] );
        assertEquals( 30, array_correct_size[1] );
        assertEquals( 20, array_correct_size[2] );
        assertEquals( 10, array_correct_size[3] );

        int[] array_too_long = new int[6];
        stack.toArray( array_too_long );
        assertEquals( 40, array_too_long[0] );
        assertEquals( 30, array_too_long[1] );
        assertEquals( 20, array_too_long[2] );
        assertEquals( 10, array_too_long[3] );
        assertEquals( stack.getNoEntryValue(), array_too_long[4] );

        int[] array_too_short = new int[2];
        stack.toArray( array_too_short );
        assertEquals( 40, array_too_short[0] );
        assertEquals( 30, array_too_short[1] );
    }
View Full Code Here

Examples of gnu.trove.stack.TIntStack

        assertEquals( 30, array_too_short[1] );
    }


    public void testClear() {
        TIntStack stack = new TIntArrayStack();

        assertEquals( 0, stack.size() );

        stack.push( 10 );

        assertEquals( 1, stack.size() );
        assertEquals( 10, stack.pop() );
        assertEquals( 0, stack.size() );

        stack.push( 10 );
        stack.push( 20 );
        stack.push( 30 );

        assertEquals( 3, stack.size() );
        stack.clear();

        assertEquals( 0, stack.size() );
    }
View Full Code Here

Examples of gnu.trove.stack.TIntStack

        assertEquals( 0, stack.size() );
    }


    public void testEquals() {
        TIntStack stack = new TIntArrayStack();
        assertEquals( 0, stack.size() );

        stack.push( 10 );
        stack.push( 20 );
        assertEquals( 2, stack.size() );

        TIntStack other = new TIntArrayStack( 20 );
        other.push( 10 );
        other.push( 20 );
        assertEquals( 2, other.size() );

        assertTrue( "stacks should equal itself: " + stack,
      stack.equals( stack ) );

        assertTrue( "stacks should be equal: " + stack + ", " + other,
View Full Code Here

Examples of gnu.trove.stack.TIntStack

      stack.equals( list ) );
    }


    public void testHashCode() {
        TIntStack stack = new TIntArrayStack();
        assertEquals( 0, stack.size() );

        stack.push( 10 );
        stack.push( 20 );
        assertEquals( 2, stack.size() );

        TIntStack other = new TIntArrayStack( 20 );
        other.push( 10 );
        other.push( 20 );
        assertEquals( 2, other.size() );

        assertTrue( "stack hashcode should equal itself: " + stack,
      stack.hashCode() == stack.hashCode() );

        assertTrue( "stacks should be equal: " + stack + ", " + other,
      stack.hashCode() == other.hashCode() );

        other.push( 30 );
        assertFalse( "stack should not equal list: " + stack + ", " + other,
      stack.hashCode() == other.hashCode() );
    }
View Full Code Here

Examples of gnu.trove.stack.TIntStack

      stack.hashCode() == other.hashCode() );
    }


    public void testSerialize() throws Exception {
        TIntStack stack = new TIntArrayStack();
        stack.push( 10 );
        stack.push( 20 );
        stack.push( 30 );
        stack.push( 40 );
        stack.push( 50 );

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream( baos );
        oos.writeObject( stack );

        ByteArrayInputStream bais = new ByteArrayInputStream( baos.toByteArray() );
        ObjectInputStream ois = new ObjectInputStream( bais );

        TIntStack serialized = (TIntStack) ois.readObject();

        assertEquals( stack, serialized );
    }
View Full Code Here

Examples of gnu.trove.stack.TIntStack

        super( string );
    }


    public void testConstructors() {
        TIntStack stack = new TIntArrayStack();
        assertEquals( 0, stack.size() );

        stack.push( 10 );
        stack.push( 20 );
        assertEquals( 2, stack.size() );

        TIntStack other = new TIntArrayStack( 20 );
        other.push( 10 );
        other.push( 20 );
        assertEquals( 2, other.size() );

        assertTrue( "stacks should be equal: " + stack + ", " + other,
      stack.equals( other ) );

        TIntStack copy = new TIntArrayStack( stack );
        assertTrue( "stacks should be equal: " + stack + ", " + copy,
      stack.equals( copy ) );
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.