/** * 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; }
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());
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; }
/** * 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; }
/** * 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; }
/** * 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]; } }
/** * 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); }
/** * 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); }
@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; }
/** * 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; } }
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));
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)));
/** * Return a new, empty {@link Object} to used for testing. */ public Collection makeCollection() { return new PriorityBuffer(); }
/** * 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; }
/** * 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; }
/** * 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]; } }
/** * 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); }
/** * 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); }
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; }
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; }