congrats Icon
New! Announcing Tabnine Chat Beta
Learn More
Tabnine Logo
PriorityBuffer
Code IndexAdd Tabnine to your IDE (free)

How to use
PriorityBuffer
in
org.apache.commons.collections.buffer

Best Java code snippets using org.apache.commons.collections.buffer.PriorityBuffer (Showing top 20 results out of 315)

origin: commons-collections/commons-collections

/**
 * Gets and removes the next element (pop).
 *
 * @return the next element
 * @throws BufferUnderflowException if the buffer is empty
 */
public Object remove() {
  final Object result = get();
  elements[1] = elements[size--];
  // set the unused element to 'null' so that the garbage collector
  // can free the object if not used anywhere else.(remove reference)
  elements[size + 1] = null;
  if (size != 0) {
    // percolate top element to it's place in tree
    if (ascendingOrder) {
      percolateDownMinHeap(1);
    } else {
      percolateDownMaxHeap(1);
    }
  }
  return result;
}
origin: commons-collections/commons-collections

public void testBasicOps() {
  PriorityBuffer heap = new PriorityBuffer();
  heap.add("a");
  heap.add("c");
  heap.add("e");
  heap.add("b");
  heap.add("d");
  heap.add("n");
  heap.add("m");
  heap.add("l");
  heap.add("k");
  heap.add("j");
  heap.add("i");
  heap.add("h");
  heap.add("g");
  heap.add("f");
  assertTrue("heap should not be empty after adds", !heap.isEmpty());
      "get using default constructor should return minimum value in the binary heap",
      String.valueOf((char) ('a' + i)),
      heap.get());
      heap.remove());
      assertTrue("heap should not be empty before all elements are removed", !heap.isEmpty());
    } else {
      assertTrue("heap should be empty after all elements are removed", heap.isEmpty());
origin: commons-collections/commons-collections

public void remove() {
  if (lastReturnedIndex == -1) {
    throw new IllegalStateException();
  }
  elements[ lastReturnedIndex ] = elements[ size ];
  elements[ size ] = null;
  size--;  
  if( size != 0 && lastReturnedIndex <= size) {
    int compareToParent = 0;
    if (lastReturnedIndex > 1) {
      compareToParent = compare(elements[lastReturnedIndex], 
        elements[lastReturnedIndex / 2]);  
    }
    if (ascendingOrder) {
      if (lastReturnedIndex > 1 && compareToParent < 0) {
        percolateUpMinHeap(lastReturnedIndex); 
      } else {
        percolateDownMinHeap(lastReturnedIndex);
      }
    } else {  // max heap
      if (lastReturnedIndex > 1 && compareToParent > 0) {
        percolateUpMaxHeap(lastReturnedIndex); 
      } else {
        percolateDownMaxHeap(lastReturnedIndex);
      }
    }          
  }
  index--;
  lastReturnedIndex = -1; 
}
origin: commons-collections/commons-collections

/**
 * Adds an element to the buffer.
 * <p>
 * The element added will be sorted according to the comparator in use.
 *
 * @param element  the element to be added
 * @return true always
 */
public boolean add(Object element) {
  if (isAtCapacity()) {
    grow();
  }
  // percolate element to it's place in tree
  if (ascendingOrder) {
    percolateUpMinHeap(element);
  } else {
    percolateUpMaxHeap(element);
  }
  return true;
}
origin: commons-collections/commons-collections

/**
 * Percolates element up heap from the position given by the index.
 * <p>
 * Assumes it is a minimum heap.
 *
 * @param index the index of the element to be percolated up
 */
protected void percolateUpMinHeap(final int index) {
  int hole = index;
  Object element = elements[hole];
  while (hole > 1 && compare(element, elements[hole / 2]) < 0) {
    // save element that is being pushed down
    // as the element "bubble" is percolated up
    final int next = hole / 2;
    elements[hole] = elements[next];
    hole = next;
  }
  elements[hole] = element;
}
origin: commons-collections/commons-collections

/**
 * Gets the next element to be removed without actually removing it (peek).
 *
 * @return the next element
 * @throws BufferUnderflowException if the buffer is empty
 */
public Object get() {
  if (isEmpty()) {
    throw new BufferUnderflowException();
  } else {
    return elements[1];
  }
}
origin: commons-collections/commons-collections

/**
 * Percolates a new element up heap from the bottom.
 * <p>
 * Assumes it is a minimum heap.
 *
 * @param element the element
 */
protected void percolateUpMinHeap(final Object element) {
  elements[++size] = element;
  percolateUpMinHeap(size);
}
origin: commons-collections/commons-collections

/**
 * Percolates a new element up heap from the bottom.
 * <p>
 * Assume it is a maximum heap.
 *
 * @param element the element
 */
protected void percolateUpMaxHeap(final Object element) {
  elements[++size] = element;
  percolateUpMaxHeap(size);
}
origin: apache/accumulo

@VisibleForTesting
synchronized boolean seek(WritableComparable<?> key) throws IOException {
 PriorityBuffer reheap = new PriorityBuffer(heap.size());
 boolean result = false;
 for (Object obj : heap) {
  Index index = (Index) obj;
  try {
   WritableComparable<?> found = index.reader.getClosest(key, index.value, true);
   if (found != null && found.equals(key)) {
    result = true;
   }
  } catch (EOFException ex) {
   // thrown if key is beyond all data in the map
  }
  index.cached = false;
  reheap.add(index);
 }
 heap = reheap;
 return result;
}
origin: commons-collections/commons-collections

/**
 * Pops all elements from the heap and verifies that the elements come off
 * in the correct order.  NOTE: this method empties the heap.
 */
protected void checkOrder(PriorityBuffer h) {
  Integer lastNum = null;
  Integer num = null;
  while (!h.isEmpty()) {
    num = (Integer) h.remove();
    if (h.ascendingOrder) {
      assertTrue(lastNum == null || num.intValue() >= lastNum.intValue());
    } else { // max heap
      assertTrue(lastNum == null || num.intValue() <= lastNum.intValue());
    }
    lastNum = num;
    num = null;
  }
}

origin: commons-collections/commons-collections

for (int i = 0; i < iterations; i++) {
  if (i < iterations / 2) {
    h = new PriorityBuffer(true);
  } else {
    h = new PriorityBuffer(false);
    h.add(new Integer(randGenerator.nextInt(heapSize)));
  assertTrue(h.size() == heapSize);
  PriorityBuffer h1 = serializeAndRestore(h);
  assertTrue(h1.size() == heapSize);
  Iterator hit = h.iterator();
  while (hit.hasNext()) {
    Integer n = (Integer) hit.next();
    assertTrue(h1.contains(n));
origin: commons-collections/commons-collections

for(int i=0; i < iterations; i++) {
  if (i < iterations / 2) {          
    h = new PriorityBuffer(true);
  } else {
    h = new PriorityBuffer(false);
    h.add( new Integer( randGenerator.nextInt(heapSize)) );
    h.remove(new Integer(r));
    h.add(new Integer(randGenerator.nextInt(heapSize)));
origin: commons-collections/commons-collections

/**
 * Return a new, empty {@link Object} to used for testing.
 */
public Collection makeCollection() {
  return new PriorityBuffer();
}
origin: wildfly/wildfly

/**
 * Adds an element to the buffer.
 * <p>
 * The element added will be sorted according to the comparator in use.
 *
 * @param element  the element to be added
 * @return true always
 */
public boolean add(Object element) {
  if (isAtCapacity()) {
    grow();
  }
  // percolate element to it's place in tree
  if (ascendingOrder) {
    percolateUpMinHeap(element);
  } else {
    percolateUpMaxHeap(element);
  }
  return true;
}
origin: commons-collections/commons-collections

/**
 * Percolates element up heap from from the position given by the index.
 * <p>
 * Assume it is a maximum heap.
 *
 * @param index the index of the element to be percolated up
 */
protected void percolateUpMaxHeap(final int index) {
  int hole = index;
  Object element = elements[hole];
  while (hole > 1 && compare(element, elements[hole / 2]) > 0) {
    // save element that is being pushed down
    // as the element "bubble" is percolated up
    final int next = hole / 2;
    elements[hole] = elements[next];
    hole = next;
  }
  elements[hole] = element;
}
origin: wildfly/wildfly

/**
 * Gets the next element to be removed without actually removing it (peek).
 *
 * @return the next element
 * @throws BufferUnderflowException if the buffer is empty
 */
public Object get() {
  if (isEmpty()) {
    throw new BufferUnderflowException();
  } else {
    return elements[1];
  }
}
origin: wildfly/wildfly

/**
 * Percolates a new element up heap from the bottom.
 * <p>
 * Assumes it is a minimum heap.
 *
 * @param element the element
 */
protected void percolateUpMinHeap(final Object element) {
  elements[++size] = element;
  percolateUpMinHeap(size);
}
origin: wildfly/wildfly

/**
 * Percolates a new element up heap from the bottom.
 * <p>
 * Assume it is a maximum heap.
 *
 * @param element the element
 */
protected void percolateUpMaxHeap(final Object element) {
  elements[++size] = element;
  percolateUpMaxHeap(size);
}
origin: org.apache.accumulo/accumulo-server

public synchronized boolean seek(WritableComparable key) throws IOException {
 PriorityBuffer reheap = new PriorityBuffer(heap.size());
 boolean result = false;
 for (Object obj : heap) {
  Index index = (Index) obj;
  try {
   WritableComparable found = index.reader.getClosest(key, index.value, true);
   if (found != null && found.equals(key)) {
    result = true;
   }
  } catch (EOFException ex) {
   // thrown if key is beyond all data in the map
  }
  index.cached = false;
  reheap.add(index);
 }
 heap = reheap;
 return result;
}
origin: wildfly/wildfly

public void remove() {
  if (lastReturnedIndex == -1) {
    throw new IllegalStateException();
  }
  elements[ lastReturnedIndex ] = elements[ size ];
  elements[ size ] = null;
  size--;  
  if( size != 0 && lastReturnedIndex <= size) {
    int compareToParent = 0;
    if (lastReturnedIndex > 1) {
      compareToParent = compare(elements[lastReturnedIndex], 
        elements[lastReturnedIndex / 2]);  
    }
    if (ascendingOrder) {
      if (lastReturnedIndex > 1 && compareToParent < 0) {
        percolateUpMinHeap(lastReturnedIndex); 
      } else {
        percolateDownMinHeap(lastReturnedIndex);
      }
    } else {  // max heap
      if (lastReturnedIndex > 1 && compareToParent > 0) {
        percolateUpMaxHeap(lastReturnedIndex); 
      } else {
        percolateDownMaxHeap(lastReturnedIndex);
      }
    }          
  }
  index--;
  lastReturnedIndex = -1; 
}
org.apache.commons.collections.bufferPriorityBuffer

Javadoc

Binary heap implementation of Buffer that provides for removal based on Comparator ordering.

The removal order of a binary heap is based on either the natural sort order of its elements or a specified Comparator. The #remove() method always returns the first element as determined by the sort order. (The ascendingOrder flag in the constructors can be used to reverse the sort order, in which case #remove()will always remove the last element.) The removal order is not the same as the order of iteration; elements are returned by the iterator in no particular order.

The #add(Object) and #remove() operations perform in logarithmic time. The #get() operation performs in constant time. All other operations perform in linear time or worse.

Note that this implementation is not synchronized. Use org.apache.commons.collections.BufferUtils#synchronizedBuffer(Buffer) or org.apache.commons.collections.buffer.SynchronizedBuffer#decorate(Buffer)to provide synchronized access to a PriorityBuffer:

 
Buffer heap = SynchronizedBuffer.decorate(new PriorityBuffer()); 

This class is Serializable from Commons Collections 3.2.

Most used methods

  • get
    Gets the next element to be removed without actually removing it (peek).
  • isEmpty
  • compare
    Compares two objects using the comparator if specified, or the natural order otherwise.
  • grow
    Increases the size of the heap to support additional elements
  • isAtCapacity
    Tests if the buffer is at capacity.
  • percolateDownMaxHeap
    Percolates element down heap from the position given by the index. Assumes it is a maximum heap.
  • percolateDownMinHeap
    Percolates element down heap from the position given by the index. Assumes it is a minimum heap.
  • percolateUpMaxHeap
    Percolates a new element up heap from the bottom. Assume it is a maximum heap.
  • percolateUpMinHeap
    Percolates a new element up heap from the bottom. Assumes it is a minimum heap.
  • <init>
    Constructs a new empty buffer specifying the sort order and comparator.
  • add
    Adds an element to the buffer. The element added will be sorted according to the comparator in use.
  • remove
  • add,
  • remove,
  • size,
  • contains,
  • iterator

Popular in Java

  • Running tasks concurrently on multiple threads
  • getResourceAsStream (ClassLoader)
  • setScale (BigDecimal)
  • putExtra (Intent)
  • GridLayout (java.awt)
    The GridLayout class is a layout manager that lays out a container's components in a rectangular gri
  • FileInputStream (java.io)
    An input stream that reads bytes from a file. File file = ...finally if (in != null) in.clos
  • HttpURLConnection (java.net)
    An URLConnection for HTTP (RFC 2616 [http://tools.ietf.org/html/rfc2616]) used to send and receive d
  • URLConnection (java.net)
    A connection to a URL for reading or writing. For HTTP connections, see HttpURLConnection for docume
  • TimeZone (java.util)
    TimeZone represents a time zone offset, and also figures out daylight savings. Typically, you get a
  • Logger (org.slf4j)
    The org.slf4j.Logger interface is the main user entry point of SLF4J API. It is expected that loggin
  • From CI to AI: The AI layer in your organization
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now