Rename BinaryHeap to BinaryBuffer
Remove PriorityQueue interface git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131491 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
23fcdc1e0a
commit
acd90426b4
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/buffer/Attic/BinaryHeap.java,v 1.3 2003/12/28 17:58:54 scolebourne Exp $
|
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/buffer/Attic/BinaryBuffer.java,v 1.1 2004/01/01 19:01:34 scolebourne Exp $
|
||||||
* ====================================================================
|
* ====================================================================
|
||||||
*
|
*
|
||||||
* The Apache Software License, Version 1.1
|
* The Apache Software License, Version 1.1
|
||||||
|
@ -64,16 +64,15 @@ import java.util.NoSuchElementException;
|
||||||
|
|
||||||
import org.apache.commons.collections.Buffer;
|
import org.apache.commons.collections.Buffer;
|
||||||
import org.apache.commons.collections.BufferUnderflowException;
|
import org.apache.commons.collections.BufferUnderflowException;
|
||||||
import org.apache.commons.collections.PriorityQueue;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Binary heap implementation of <code>PriorityQueue</code> that provides for
|
* Binary heap implementation of <code>Buffer</code> that provides for
|
||||||
* removal based on <code>Comparator</code> ordering.
|
* removal based on <code>Comparator</code> ordering.
|
||||||
* <p>
|
* <p>
|
||||||
* The removal order of a binary heap is based on either the natural sort
|
* The removal order of a binary heap is based on either the natural sort
|
||||||
* order of its elements or a specified {@link Comparator}. The
|
* order of its elements or a specified {@link Comparator}. The
|
||||||
* {@link #remove()} method always returns the first element as determined
|
* {@link #remove()} method always returns the first element as determined
|
||||||
* by the sort order. (The <code>isMinHeap</code> flag in the constructors
|
* by the sort order. (The <code>ascendingOrder</code> flag in the constructors
|
||||||
* can be used to reverse the sort order, in which case {@link #remove()}
|
* can be used to reverse the sort order, in which case {@link #remove()}
|
||||||
* will always remove the last element.) The removal order is
|
* will always remove the last element.) The removal order is
|
||||||
* <i>not</i> the same as the order of iteration; elements are
|
* <i>not</i> the same as the order of iteration; elements are
|
||||||
|
@ -84,15 +83,16 @@ import org.apache.commons.collections.PriorityQueue;
|
||||||
* time. All other operations perform in linear time or worse.
|
* time. All other operations perform in linear time or worse.
|
||||||
* <p>
|
* <p>
|
||||||
* Note that this implementation is not synchronized. Use
|
* Note that this implementation is not synchronized. Use
|
||||||
* {@link org.apache.commons.collections.PriorityQueueUtils#synchronizedPriorityQueue(PriorityQueue)}
|
* {@link org.apache.commons.collections.BufferUtils#synchronizedBuffer(Buffer)} or
|
||||||
* to provide synchronized access to a <code>BinaryHeap</code>:
|
* {@link org.apache.commons.collections.buffer.SynchronizedBuffer#decorate(Buffer)}
|
||||||
|
* to provide synchronized access to a <code>BinaryBuffer</code>:
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* Buffer heap = BufferUtils.synchronizedBuffer(new BinaryHeap());
|
* Buffer heap = SynchronizedBuffer.decorate(new BinaryBuffer());
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @since Commons Collections 3.0 (previously in main package v1.0)
|
* @since Commons Collections 3.0 (previously BinaryHeap v1.0)
|
||||||
* @version $Revision: 1.3 $ $Date: 2003/12/28 17:58:54 $
|
* @version $Revision: 1.1 $ $Date: 2004/01/01 19:01:34 $
|
||||||
*
|
*
|
||||||
* @author Peter Donald
|
* @author Peter Donald
|
||||||
* @author Ram Chidambaram
|
* @author Ram Chidambaram
|
||||||
|
@ -100,215 +100,223 @@ import org.apache.commons.collections.PriorityQueue;
|
||||||
* @author Paul Jack
|
* @author Paul Jack
|
||||||
* @author Stephen Colebourne
|
* @author Stephen Colebourne
|
||||||
*/
|
*/
|
||||||
public final class BinaryHeap extends AbstractCollection
|
public class BinaryBuffer extends AbstractCollection implements Buffer {
|
||||||
implements PriorityQueue, Buffer {
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The default capacity for a binary heap.
|
* The default capacity for a binary heap.
|
||||||
*/
|
*/
|
||||||
private final static int DEFAULT_CAPACITY = 13;
|
private static final int DEFAULT_CAPACITY = 13;
|
||||||
/**
|
|
||||||
* The number of elements currently in this heap.
|
|
||||||
*/
|
|
||||||
int m_size; // package scoped for testing
|
|
||||||
/**
|
/**
|
||||||
* The elements in this heap.
|
* The elements in this heap.
|
||||||
*/
|
*/
|
||||||
Object[] m_elements; // package scoped for testing
|
protected Object[] elements;
|
||||||
|
/**
|
||||||
|
* The number of elements currently in this heap.
|
||||||
|
*/
|
||||||
|
protected int size;
|
||||||
/**
|
/**
|
||||||
* If true, the first element as determined by the sort order will
|
* If true, the first element as determined by the sort order will
|
||||||
* be returned. If false, the last element as determined by the
|
* be returned. If false, the last element as determined by the
|
||||||
* sort order will be returned.
|
* sort order will be returned.
|
||||||
*/
|
*/
|
||||||
boolean m_isMinHeap; // package scoped for testing
|
protected boolean ascendingOrder;
|
||||||
/**
|
/**
|
||||||
* The comparator used to order the elements
|
* The comparator used to order the elements
|
||||||
*/
|
*/
|
||||||
Comparator m_comparator; // package scoped for testing
|
protected Comparator comparator;
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
/**
|
/**
|
||||||
* Constructs a new minimum binary heap.
|
* Constructs a new empty buffer that sorts in ascending order by the
|
||||||
|
* natural order of the objects added.
|
||||||
*/
|
*/
|
||||||
public BinaryHeap() {
|
public BinaryBuffer() {
|
||||||
this(DEFAULT_CAPACITY, true);
|
this(DEFAULT_CAPACITY, true, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new <code>BinaryHeap</code> that will use the given
|
* Constructs a new empty buffer that sorts in ascending order using the
|
||||||
* comparator to order its elements.
|
* specified comparator.
|
||||||
*
|
*
|
||||||
* @param comparator the comparator used to order the elements, null
|
* @param comparator the comparator used to order the elements,
|
||||||
* means use natural order
|
* null means use natural order
|
||||||
*/
|
*/
|
||||||
public BinaryHeap(Comparator comparator) {
|
public BinaryBuffer(Comparator comparator) {
|
||||||
this();
|
this(DEFAULT_CAPACITY, true, comparator);
|
||||||
m_comparator = comparator;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new minimum binary heap with the specified initial capacity.
|
* Constructs a new empty buffer specifying the sort order and using the
|
||||||
|
* natural order of the objects added.
|
||||||
*
|
*
|
||||||
* @param capacity The initial capacity for the heap. This value must
|
* @param ascendingOrder if <code>true</code> the heap is created as a
|
||||||
* be greater than zero.
|
|
||||||
* @throws IllegalArgumentException
|
|
||||||
* if <code>capacity</code> is <= <code>0</code>
|
|
||||||
*/
|
|
||||||
public BinaryHeap(int capacity) {
|
|
||||||
this(capacity, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructs a new <code>BinaryHeap</code>.
|
|
||||||
*
|
|
||||||
* @param capacity the initial capacity for the heap
|
|
||||||
* @param comparator the comparator used to order the elements, null
|
|
||||||
* means use natural order
|
|
||||||
* @throws IllegalArgumentException
|
|
||||||
* if <code>capacity</code> is <= <code>0</code>
|
|
||||||
*/
|
|
||||||
public BinaryHeap(int capacity, Comparator comparator) {
|
|
||||||
this(capacity);
|
|
||||||
m_comparator = comparator;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructs a new minimum or maximum binary heap
|
|
||||||
*
|
|
||||||
* @param isMinHeap if <code>true</code> the heap is created as a
|
|
||||||
* minimum heap; otherwise, the heap is created as a maximum heap
|
* minimum heap; otherwise, the heap is created as a maximum heap
|
||||||
*/
|
*/
|
||||||
public BinaryHeap(boolean isMinHeap) {
|
public BinaryBuffer(boolean ascendingOrder) {
|
||||||
this(DEFAULT_CAPACITY, isMinHeap);
|
this(DEFAULT_CAPACITY, ascendingOrder, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new <code>BinaryHeap</code>.
|
* Constructs a new empty buffer specifying the sort order and comparator.
|
||||||
*
|
*
|
||||||
* @param isMinHeap true to use the order imposed by the given
|
* @param ascendingOrder true to use the order imposed by the given
|
||||||
* comparator; false to reverse that order
|
* comparator; false to reverse that order
|
||||||
* @param comparator the comparator used to order the elements, null
|
* @param comparator the comparator used to order the elements,
|
||||||
* means use natural order
|
* null means use natural order
|
||||||
*/
|
*/
|
||||||
public BinaryHeap(boolean isMinHeap, Comparator comparator) {
|
public BinaryBuffer(boolean ascendingOrder, Comparator comparator) {
|
||||||
this(isMinHeap);
|
this(DEFAULT_CAPACITY, ascendingOrder, comparator);
|
||||||
m_comparator = comparator;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new minimum or maximum binary heap with the specified
|
* Constructs a new empty buffer that sorts in ascending order by the
|
||||||
* initial capacity.
|
* natural order of the objects added, specifying an initial capacity.
|
||||||
*
|
*
|
||||||
* @param capacity the initial capacity for the heap. This value must
|
* @param capacity the initial capacity for the buffer, greater than zero
|
||||||
* be greater than zero.
|
* @throws IllegalArgumentException if <code>capacity</code> is <= <code>0</code>
|
||||||
* @param isMinHeap if <code>true</code> the heap is created as a
|
|
||||||
* minimum heap; otherwise, the heap is created as a maximum heap.
|
|
||||||
* @throws IllegalArgumentException
|
|
||||||
* if <code>capacity</code> is <code><= 0</code>
|
|
||||||
*/
|
*/
|
||||||
public BinaryHeap(int capacity, boolean isMinHeap) {
|
public BinaryBuffer(int capacity) {
|
||||||
|
this(capacity, true, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new empty buffer that sorts in ascending order using the
|
||||||
|
* specified comparator and initial capacity.
|
||||||
|
*
|
||||||
|
* @param capacity the initial capacity for the buffer, greater than zero
|
||||||
|
* @param comparator the comparator used to order the elements,
|
||||||
|
* null means use natural order
|
||||||
|
* @throws IllegalArgumentException if <code>capacity</code> is <= <code>0</code>
|
||||||
|
*/
|
||||||
|
public BinaryBuffer(int capacity, Comparator comparator) {
|
||||||
|
this(capacity, true, comparator);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new empty buffer that specifying initial capacity and
|
||||||
|
* sort order, using the natural order of the objects added.
|
||||||
|
*
|
||||||
|
* @param capacity the initial capacity for the buffer, greater than zero
|
||||||
|
* @param ascendingOrder if <code>true</code> the heap is created as a
|
||||||
|
* minimum heap; otherwise, the heap is created as a maximum heap.
|
||||||
|
* @throws IllegalArgumentException if <code>capacity</code> is <code><= 0</code>
|
||||||
|
*/
|
||||||
|
public BinaryBuffer(int capacity, boolean ascendingOrder) {
|
||||||
|
this(capacity, ascendingOrder, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new empty buffer that specifying initial capacity,
|
||||||
|
* sort order and comparator.
|
||||||
|
*
|
||||||
|
* @param capacity the initial capacity for the buffer, greater than zero
|
||||||
|
* @param ascendingOrder true to use the order imposed by the given
|
||||||
|
* comparator; false to reverse that order
|
||||||
|
* @param comparator the comparator used to order the elements,
|
||||||
|
* null means use natural order
|
||||||
|
* @throws IllegalArgumentException if <code>capacity</code> is <code><= 0</code>
|
||||||
|
*/
|
||||||
|
public BinaryBuffer(int capacity, boolean ascendingOrder, Comparator comparator) {
|
||||||
|
super();
|
||||||
if (capacity <= 0) {
|
if (capacity <= 0) {
|
||||||
throw new IllegalArgumentException("invalid capacity");
|
throw new IllegalArgumentException("invalid capacity");
|
||||||
}
|
}
|
||||||
m_isMinHeap = isMinHeap;
|
this.ascendingOrder = ascendingOrder;
|
||||||
|
|
||||||
//+1 as 0 is noop
|
//+1 as 0 is noop
|
||||||
m_elements = new Object[capacity + 1];
|
this.elements = new Object[capacity + 1];
|
||||||
|
this.comparator = comparator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
/**
|
/**
|
||||||
* Constructs a new <code>BinaryHeap</code>.
|
* Checks whether the heap is ascending or descending order.
|
||||||
*
|
*
|
||||||
* @param capacity the initial capacity for the heap
|
* @return true if ascending order (a min heap)
|
||||||
* @param isMinHeap true to use the order imposed by the given
|
|
||||||
* comparator; false to reverse that order
|
|
||||||
* @param comparator the comparator used to order the elements, null
|
|
||||||
* means use natural order
|
|
||||||
* @throws IllegalArgumentException
|
|
||||||
* if <code>capacity</code> is <code><= 0</code>
|
|
||||||
*/
|
*/
|
||||||
public BinaryHeap(int capacity, boolean isMinHeap, Comparator comparator) {
|
public boolean isAscendingOrder() {
|
||||||
this(capacity, isMinHeap);
|
return ascendingOrder;
|
||||||
m_comparator = comparator;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the comparator being used for this buffer, null is natural order.
|
||||||
|
*
|
||||||
|
* @return the comparator in use, null is natural order
|
||||||
|
*/
|
||||||
|
public Comparator comparator() {
|
||||||
|
return comparator;
|
||||||
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
|
/**
|
||||||
|
* Returns the number of elements in this buffer.
|
||||||
|
*
|
||||||
|
* @return the number of elements in this buffer
|
||||||
|
*/
|
||||||
|
public int size() {
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clears all elements from queue.
|
* Clears all elements from the buffer.
|
||||||
*/
|
*/
|
||||||
public void clear() {
|
public void clear() {
|
||||||
m_elements = new Object[m_elements.length]; // for gc
|
elements = new Object[elements.length]; // for gc
|
||||||
m_size = 0;
|
size = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests if queue is empty.
|
* Adds an element to the buffer.
|
||||||
|
* <p>
|
||||||
|
* The element added will be sorted according to the comparator in use.
|
||||||
*
|
*
|
||||||
* @return <code>true</code> if queue is empty; <code>false</code>
|
* @param element the element to be added
|
||||||
* otherwise.
|
|
||||||
*/
|
*/
|
||||||
public boolean isEmpty() {
|
public boolean add(Object element) {
|
||||||
return m_size == 0;
|
if (isAtCapacity()) {
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Tests if queue is full.
|
|
||||||
*
|
|
||||||
* @return <code>true</code> if queue is full; <code>false</code>
|
|
||||||
* otherwise.
|
|
||||||
*/
|
|
||||||
public boolean isFull() {
|
|
||||||
//+1 as element 0 is noop
|
|
||||||
return m_elements.length == m_size + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Inserts an element into queue.
|
|
||||||
*
|
|
||||||
* @param element the element to be inserted
|
|
||||||
*/
|
|
||||||
public void insert(Object element) {
|
|
||||||
if (isFull()) {
|
|
||||||
grow();
|
grow();
|
||||||
}
|
}
|
||||||
//percolate element to it's place in tree
|
// percolate element to it's place in tree
|
||||||
if (m_isMinHeap) {
|
if (ascendingOrder) {
|
||||||
percolateUpMinHeap(element);
|
percolateUpMinHeap(element);
|
||||||
} else {
|
} else {
|
||||||
percolateUpMaxHeap(element);
|
percolateUpMaxHeap(element);
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the element on top of heap but don't remove it.
|
* Gets the next element to be removed without actually removing it (peek).
|
||||||
*
|
*
|
||||||
* @return the element at top of heap
|
* @return the next element
|
||||||
* @throws NoSuchElementException if <code>isEmpty() == true</code>
|
* @throws BufferUnderflowException if the buffer is empty
|
||||||
*/
|
*/
|
||||||
public Object peek() throws NoSuchElementException {
|
public Object get() {
|
||||||
if (isEmpty()) {
|
if (isEmpty()) {
|
||||||
throw new NoSuchElementException();
|
throw new BufferUnderflowException();
|
||||||
} else {
|
} else {
|
||||||
return m_elements[1];
|
return elements[1];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the element on top of heap and remove it.
|
* Gets and removes the next element (pop).
|
||||||
*
|
*
|
||||||
* @return the element at top of heap
|
* @return the next element
|
||||||
* @throws NoSuchElementException if <code>isEmpty() == true</code>
|
* @throws BufferUnderflowException if the buffer is empty
|
||||||
*/
|
*/
|
||||||
public Object pop() throws NoSuchElementException {
|
public Object remove() {
|
||||||
final Object result = peek();
|
final Object result = get();
|
||||||
m_elements[1] = m_elements[m_size--];
|
elements[1] = elements[size--];
|
||||||
|
|
||||||
// set the unused element to 'null' so that the garbage collector
|
// set the unused element to 'null' so that the garbage collector
|
||||||
// can free the object if not used anywhere else.(remove reference)
|
// can free the object if not used anywhere else.(remove reference)
|
||||||
m_elements[m_size + 1] = null;
|
elements[size + 1] = null;
|
||||||
|
|
||||||
if (m_size != 0) {
|
if (size != 0) {
|
||||||
// percolate top element to it's place in tree
|
// percolate top element to it's place in tree
|
||||||
if (m_isMinHeap) {
|
if (ascendingOrder) {
|
||||||
percolateDownMinHeap(1);
|
percolateDownMinHeap(1);
|
||||||
} else {
|
} else {
|
||||||
percolateDownMaxHeap(1);
|
percolateDownMaxHeap(1);
|
||||||
|
@ -318,35 +326,46 @@ public final class BinaryHeap extends AbstractCollection
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
|
/**
|
||||||
|
* Tests if the buffer is at capacity.
|
||||||
|
*
|
||||||
|
* @return <code>true</code> if buffer is full; <code>false</code> otherwise.
|
||||||
|
*/
|
||||||
|
protected boolean isAtCapacity() {
|
||||||
|
//+1 as element 0 is noop
|
||||||
|
return elements.length == size + 1;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Percolates element down heap from top.
|
* Percolates element down heap from top.
|
||||||
* Assume it is a maximum heap.
|
* Assume it is a minimum heap.
|
||||||
*
|
*
|
||||||
* @param index the index for the element
|
* @param index the index for the element
|
||||||
*/
|
*/
|
||||||
protected void percolateDownMinHeap(final int index) {
|
protected void percolateDownMinHeap(final int index) {
|
||||||
final Object element = m_elements[index];
|
final Object element = elements[index];
|
||||||
int hole = index;
|
int hole = index;
|
||||||
|
|
||||||
while ((hole * 2) <= m_size) {
|
while ((hole * 2) <= size) {
|
||||||
int child = hole * 2;
|
int child = hole * 2;
|
||||||
|
|
||||||
// if we have a right child and that child can not be percolated
|
// if we have a right child and that child can not be percolated
|
||||||
// up then move onto other child
|
// up then move onto other child
|
||||||
if (child != m_size && compare(m_elements[child + 1], m_elements[child]) < 0) {
|
if (child != size && compare(elements[child + 1], elements[child]) < 0) {
|
||||||
child++;
|
child++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if we found resting place of bubble then terminate search
|
// if we found resting place of bubble then terminate search
|
||||||
if (compare(m_elements[child], element) >= 0) {
|
if (compare(elements[child], element) >= 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_elements[hole] = m_elements[child];
|
elements[hole] = elements[child];
|
||||||
hole = child;
|
hole = child;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_elements[hole] = element;
|
elements[hole] = element;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -356,50 +375,50 @@ public final class BinaryHeap extends AbstractCollection
|
||||||
* @param index the index of the element
|
* @param index the index of the element
|
||||||
*/
|
*/
|
||||||
protected void percolateDownMaxHeap(final int index) {
|
protected void percolateDownMaxHeap(final int index) {
|
||||||
final Object element = m_elements[index];
|
final Object element = elements[index];
|
||||||
int hole = index;
|
int hole = index;
|
||||||
|
|
||||||
while ((hole * 2) <= m_size) {
|
while ((hole * 2) <= size) {
|
||||||
int child = hole * 2;
|
int child = hole * 2;
|
||||||
|
|
||||||
// if we have a right child and that child can not be percolated
|
// if we have a right child and that child can not be percolated
|
||||||
// up then move onto other child
|
// up then move onto other child
|
||||||
if (child != m_size && compare(m_elements[child + 1], m_elements[child]) > 0) {
|
if (child != size && compare(elements[child + 1], elements[child]) > 0) {
|
||||||
child++;
|
child++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if we found resting place of bubble then terminate search
|
// if we found resting place of bubble then terminate search
|
||||||
if (compare(m_elements[child], element) <= 0) {
|
if (compare(elements[child], element) <= 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_elements[hole] = m_elements[child];
|
elements[hole] = elements[child];
|
||||||
hole = child;
|
hole = child;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_elements[hole] = element;
|
elements[hole] = element;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Percolates element up heap from bottom.
|
* Percolates element up heap from bottom.
|
||||||
* Assume it is a maximum heap.
|
* Assume it is a minimum heap.
|
||||||
*
|
*
|
||||||
* @param element the element
|
* @param element the element
|
||||||
*/
|
*/
|
||||||
protected void percolateUpMinHeap(final Object element) {
|
protected void percolateUpMinHeap(final Object element) {
|
||||||
int hole = ++m_size;
|
int hole = ++size;
|
||||||
|
|
||||||
m_elements[hole] = element;
|
elements[hole] = element;
|
||||||
|
|
||||||
while (hole > 1 && compare(element, m_elements[hole / 2]) < 0) {
|
while (hole > 1 && compare(element, elements[hole / 2]) < 0) {
|
||||||
// save element that is being pushed down
|
// save element that is being pushed down
|
||||||
// as the element "bubble" is percolated up
|
// as the element "bubble" is percolated up
|
||||||
final int next = hole / 2;
|
final int next = hole / 2;
|
||||||
m_elements[hole] = m_elements[next];
|
elements[hole] = elements[next];
|
||||||
hole = next;
|
hole = next;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_elements[hole] = element;
|
elements[hole] = element;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -409,17 +428,17 @@ public final class BinaryHeap extends AbstractCollection
|
||||||
* @param element the element
|
* @param element the element
|
||||||
*/
|
*/
|
||||||
protected void percolateUpMaxHeap(final Object element) {
|
protected void percolateUpMaxHeap(final Object element) {
|
||||||
int hole = ++m_size;
|
int hole = ++size;
|
||||||
|
|
||||||
while (hole > 1 && compare(element, m_elements[hole / 2]) > 0) {
|
while (hole > 1 && compare(element, elements[hole / 2]) > 0) {
|
||||||
// save element that is being pushed down
|
// save element that is being pushed down
|
||||||
// as the element "bubble" is percolated up
|
// as the element "bubble" is percolated up
|
||||||
final int next = hole / 2;
|
final int next = hole / 2;
|
||||||
m_elements[hole] = m_elements[next];
|
elements[hole] = elements[next];
|
||||||
hole = next;
|
hole = next;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_elements[hole] = element;
|
elements[hole] = element;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -430,9 +449,9 @@ public final class BinaryHeap extends AbstractCollection
|
||||||
* @param b the second object
|
* @param b the second object
|
||||||
* @return -ve if a less than b, 0 if they are equal, +ve if a greater than b
|
* @return -ve if a less than b, 0 if they are equal, +ve if a greater than b
|
||||||
*/
|
*/
|
||||||
private int compare(Object a, Object b) {
|
protected int compare(Object a, Object b) {
|
||||||
if (m_comparator != null) {
|
if (comparator != null) {
|
||||||
return m_comparator.compare(a, b);
|
return comparator.compare(a, b);
|
||||||
} else {
|
} else {
|
||||||
return ((Comparable) a).compareTo(b);
|
return ((Comparable) a).compareTo(b);
|
||||||
}
|
}
|
||||||
|
@ -442,9 +461,56 @@ public final class BinaryHeap extends AbstractCollection
|
||||||
* Increases the size of the heap to support additional elements
|
* Increases the size of the heap to support additional elements
|
||||||
*/
|
*/
|
||||||
protected void grow() {
|
protected void grow() {
|
||||||
final Object[] elements = new Object[m_elements.length * 2];
|
final Object[] array = new Object[elements.length * 2];
|
||||||
System.arraycopy(m_elements, 0, elements, 0, m_elements.length);
|
System.arraycopy(elements, 0, array, 0, elements.length);
|
||||||
m_elements = elements;
|
elements = array;
|
||||||
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
|
/**
|
||||||
|
* Returns an iterator over this heap's elements.
|
||||||
|
*
|
||||||
|
* @return an iterator over this heap's elements
|
||||||
|
*/
|
||||||
|
public Iterator iterator() {
|
||||||
|
return new Iterator() {
|
||||||
|
|
||||||
|
private int index = 1;
|
||||||
|
private int lastReturnedIndex = -1;
|
||||||
|
|
||||||
|
public boolean hasNext() {
|
||||||
|
return index <= size;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object next() {
|
||||||
|
if (!hasNext()) {
|
||||||
|
throw new NoSuchElementException();
|
||||||
|
}
|
||||||
|
lastReturnedIndex = index;
|
||||||
|
index++;
|
||||||
|
return elements[lastReturnedIndex];
|
||||||
|
}
|
||||||
|
|
||||||
|
public void remove() {
|
||||||
|
if (lastReturnedIndex == -1) {
|
||||||
|
throw new IllegalStateException();
|
||||||
|
}
|
||||||
|
elements[lastReturnedIndex] = elements[size];
|
||||||
|
elements[size] = null;
|
||||||
|
size--;
|
||||||
|
if (size != 0) {
|
||||||
|
//percolate top element to it's place in tree
|
||||||
|
if (ascendingOrder) {
|
||||||
|
percolateDownMinHeap(lastReturnedIndex);
|
||||||
|
} else {
|
||||||
|
percolateDownMaxHeap(lastReturnedIndex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
index--;
|
||||||
|
lastReturnedIndex = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -458,11 +524,11 @@ public final class BinaryHeap extends AbstractCollection
|
||||||
|
|
||||||
sb.append("[ ");
|
sb.append("[ ");
|
||||||
|
|
||||||
for (int i = 1; i < m_size + 1; i++) {
|
for (int i = 1; i < size + 1; i++) {
|
||||||
if (i != 1) {
|
if (i != 1) {
|
||||||
sb.append(", ");
|
sb.append(", ");
|
||||||
}
|
}
|
||||||
sb.append(m_elements[i]);
|
sb.append(elements[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
sb.append(" ]");
|
sb.append(" ]");
|
||||||
|
@ -470,94 +536,4 @@ public final class BinaryHeap extends AbstractCollection
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns an iterator over this heap's elements.
|
|
||||||
*
|
|
||||||
* @return an iterator over this heap's elements
|
|
||||||
*/
|
|
||||||
public Iterator iterator() {
|
|
||||||
return new Iterator() {
|
|
||||||
|
|
||||||
private int index = 1;
|
|
||||||
private int lastReturnedIndex = -1;
|
|
||||||
|
|
||||||
public boolean hasNext() {
|
|
||||||
return index <= m_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object next() {
|
|
||||||
if (!hasNext()) throw new NoSuchElementException();
|
|
||||||
lastReturnedIndex = index;
|
|
||||||
index++;
|
|
||||||
return m_elements[lastReturnedIndex];
|
|
||||||
}
|
|
||||||
|
|
||||||
public void remove() {
|
|
||||||
if (lastReturnedIndex == -1) throw new IllegalStateException();
|
|
||||||
m_elements[ lastReturnedIndex ] = m_elements[ m_size ];
|
|
||||||
m_elements[ m_size ] = null;
|
|
||||||
m_size--;
|
|
||||||
if( m_size != 0 )
|
|
||||||
{
|
|
||||||
//percolate top element to it's place in tree
|
|
||||||
if( m_isMinHeap ) percolateDownMinHeap( lastReturnedIndex );
|
|
||||||
else percolateDownMaxHeap( lastReturnedIndex );
|
|
||||||
}
|
|
||||||
index--;
|
|
||||||
lastReturnedIndex = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Adds an object to this heap. Same as {@link #insert(Object)}.
|
|
||||||
*
|
|
||||||
* @param object the object to add
|
|
||||||
* @return true, always
|
|
||||||
*/
|
|
||||||
public boolean add(Object object) {
|
|
||||||
insert(object);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the priority element. Same as {@link #peek()}.
|
|
||||||
*
|
|
||||||
* @return the priority element
|
|
||||||
* @throws BufferUnderflowException if this heap is empty
|
|
||||||
*/
|
|
||||||
public Object get() {
|
|
||||||
try {
|
|
||||||
return peek();
|
|
||||||
} catch (NoSuchElementException e) {
|
|
||||||
throw new BufferUnderflowException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Removes the priority element. Same as {@link #pop()}.
|
|
||||||
*
|
|
||||||
* @return the removed priority element
|
|
||||||
* @throws BufferUnderflowException if this heap is empty
|
|
||||||
*/
|
|
||||||
public Object remove() {
|
|
||||||
try {
|
|
||||||
return pop();
|
|
||||||
} catch (NoSuchElementException e) {
|
|
||||||
throw new BufferUnderflowException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the number of elements in this heap.
|
|
||||||
*
|
|
||||||
* @return the number of elements in this heap
|
|
||||||
*/
|
|
||||||
public int size() {
|
|
||||||
return m_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,15 +1,14 @@
|
||||||
<BODY>
|
<BODY>
|
||||||
<p>
|
<p>
|
||||||
This package contains implementations of the
|
This package contains implementations of the
|
||||||
{@link org.apache.commons.collections.Buffer Buffer} and
|
{@link org.apache.commons.collections.Buffer Buffer} interface.
|
||||||
{@link org.apache.commons.collections.PriorityQueue PriorityQueue} interfaces.
|
|
||||||
<p>
|
<p>
|
||||||
The following implementations are provided in the package:
|
The following implementations are provided in the package:
|
||||||
<ul>
|
<ul>
|
||||||
<li>BinaryHeap - implements both Buffer and PriorityQueue
|
<li>BinaryBuffer - provides for removal based on a comparator ordering (priority queue)
|
||||||
<li>BoundedBuffer - implements a buffer with a fixed size that throws exceptions when full
|
<li>BoundedFifoBuffer - implements a buffer with a fixed size that throws exceptions when full
|
||||||
<li>CircularBuffer - implements a buffer with a fixed size that discards oldest when full
|
<li>CircularFifoBuffer - implements a buffer with a fixed size that discards oldest when full
|
||||||
<li>UnboundedBuffer - implements a buffer that grows in size if necessary
|
<li>UnboundedFifoBuffer - implements a buffer that grows in size if necessary
|
||||||
</ul>
|
</ul>
|
||||||
<p>
|
<p>
|
||||||
The following decorators are provided in the package:
|
The following decorators are provided in the package:
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/buffer/TestAll.java,v 1.2 2003/11/29 18:04:56 scolebourne Exp $
|
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/buffer/TestAll.java,v 1.3 2004/01/01 19:01:34 scolebourne Exp $
|
||||||
* ====================================================================
|
* ====================================================================
|
||||||
*
|
*
|
||||||
* The Apache Software License, Version 1.1
|
* The Apache Software License, Version 1.1
|
||||||
|
@ -65,7 +65,7 @@ import junit.framework.TestSuite;
|
||||||
* Entry point for tests.
|
* Entry point for tests.
|
||||||
*
|
*
|
||||||
* @since Commons Collections 3.0
|
* @since Commons Collections 3.0
|
||||||
* @version $Revision: 1.2 $ $Date: 2003/11/29 18:04:56 $
|
* @version $Revision: 1.3 $ $Date: 2004/01/01 19:01:34 $
|
||||||
*
|
*
|
||||||
* @author Stephen Colebourne
|
* @author Stephen Colebourne
|
||||||
*/
|
*/
|
||||||
|
@ -83,7 +83,7 @@ public class TestAll extends TestCase {
|
||||||
public static Test suite() {
|
public static Test suite() {
|
||||||
TestSuite suite = new TestSuite();
|
TestSuite suite = new TestSuite();
|
||||||
|
|
||||||
suite.addTest(TestBinaryHeap.suite());
|
suite.addTest(TestBinaryBuffer.suite());
|
||||||
suite.addTest(TestBlockingBuffer.suite());
|
suite.addTest(TestBlockingBuffer.suite());
|
||||||
suite.addTest(TestBoundedFifoBuffer.suite());
|
suite.addTest(TestBoundedFifoBuffer.suite());
|
||||||
suite.addTest(TestBoundedFifoBuffer2.suite());
|
suite.addTest(TestBoundedFifoBuffer2.suite());
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/buffer/Attic/TestBinaryHeap.java,v 1.1 2003/11/29 18:04:56 scolebourne Exp $
|
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/buffer/Attic/TestBinaryBuffer.java,v 1.1 2004/01/01 19:01:34 scolebourne Exp $
|
||||||
* ====================================================================
|
* ====================================================================
|
||||||
*
|
*
|
||||||
* The Apache Software License, Version 1.1
|
* The Apache Software License, Version 1.1
|
||||||
|
@ -61,11 +61,12 @@ import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.NoSuchElementException;
|
|
||||||
|
|
||||||
import junit.framework.Test;
|
import junit.framework.Test;
|
||||||
import junit.framework.TestSuite;
|
import junit.framework.TestSuite;
|
||||||
|
|
||||||
|
import org.apache.commons.collections.Buffer;
|
||||||
|
import org.apache.commons.collections.BufferUnderflowException;
|
||||||
import org.apache.commons.collections.ComparatorUtils;
|
import org.apache.commons.collections.ComparatorUtils;
|
||||||
import org.apache.commons.collections.collection.AbstractTestCollection;
|
import org.apache.commons.collections.collection.AbstractTestCollection;
|
||||||
import org.apache.commons.collections.comparators.ComparableComparator;
|
import org.apache.commons.collections.comparators.ComparableComparator;
|
||||||
|
@ -74,38 +75,44 @@ import org.apache.commons.collections.comparators.ReverseComparator;
|
||||||
/**
|
/**
|
||||||
* Tests the BinaryHeap.
|
* Tests the BinaryHeap.
|
||||||
*
|
*
|
||||||
* @version $Revision: 1.1 $ $Date: 2003/11/29 18:04:56 $
|
* @version $Revision: 1.1 $ $Date: 2004/01/01 19:01:34 $
|
||||||
*
|
*
|
||||||
* @author Michael A. Smith
|
* @author Michael A. Smith
|
||||||
*/
|
*/
|
||||||
public class TestBinaryHeap extends AbstractTestCollection {
|
public class TestBinaryBuffer extends AbstractTestCollection {
|
||||||
|
|
||||||
public static Test suite() {
|
public static void main(String[] args) {
|
||||||
return new TestSuite(TestBinaryHeap.class);
|
junit.textui.TestRunner.run(suite());
|
||||||
}
|
}
|
||||||
|
|
||||||
public TestBinaryHeap(String testName) {
|
public static Test suite() {
|
||||||
|
return new TestSuite(TestBinaryBuffer.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public TestBinaryBuffer(String testName) {
|
||||||
super(testName);
|
super(testName);
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
public void verify() {
|
public void verify() {
|
||||||
super.verify();
|
super.verify();
|
||||||
BinaryHeap heap = (BinaryHeap) collection;
|
BinaryBuffer heap = (BinaryBuffer) collection;
|
||||||
|
|
||||||
Comparator c = heap.m_comparator;
|
Comparator c = heap.comparator;
|
||||||
if (c == null)
|
if (c == null) {
|
||||||
c = ComparatorUtils.naturalComparator();
|
c = ComparatorUtils.naturalComparator();
|
||||||
if (!heap.m_isMinHeap)
|
}
|
||||||
|
if (!heap.ascendingOrder) {
|
||||||
c = ComparatorUtils.reversedComparator(c);
|
c = ComparatorUtils.reversedComparator(c);
|
||||||
|
}
|
||||||
|
|
||||||
Object[] tree = heap.m_elements;
|
Object[] tree = heap.elements;
|
||||||
for (int i = 1; i <= heap.m_size; i++) {
|
for (int i = 1; i <= heap.size; i++) {
|
||||||
Object parent = tree[i];
|
Object parent = tree[i];
|
||||||
if (i * 2 <= heap.m_size) {
|
if (i * 2 <= heap.size) {
|
||||||
assertTrue("Parent is less than or equal to its left child", c.compare(parent, tree[i * 2]) <= 0);
|
assertTrue("Parent is less than or equal to its left child", c.compare(parent, tree[i * 2]) <= 0);
|
||||||
}
|
}
|
||||||
if (i * 2 + 1 < heap.m_size) {
|
if (i * 2 + 1 < heap.size) {
|
||||||
assertTrue("Parent is less than or equal to its right child", c.compare(parent, tree[i * 2 + 1]) <= 0);
|
assertTrue("Parent is less than or equal to its right child", c.compare(parent, tree[i * 2 + 1]) <= 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -113,7 +120,7 @@ public class TestBinaryHeap extends AbstractTestCollection {
|
||||||
|
|
||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
/**
|
/**
|
||||||
* Overridden because UnboundedFifoBuffer isn't fail fast.
|
* Overridden because BinaryBuffer isn't fail fast.
|
||||||
* @return false
|
* @return false
|
||||||
*/
|
*/
|
||||||
public boolean isFailFastSupported() {
|
public boolean isFailFastSupported() {
|
||||||
|
@ -135,7 +142,7 @@ public class TestBinaryHeap extends AbstractTestCollection {
|
||||||
* Return a new, empty {@link Object} to used for testing.
|
* Return a new, empty {@link Object} to used for testing.
|
||||||
*/
|
*/
|
||||||
public Collection makeCollection() {
|
public Collection makeCollection() {
|
||||||
return new BinaryHeap();
|
return new BinaryBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
|
@ -148,110 +155,103 @@ public class TestBinaryHeap extends AbstractTestCollection {
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
|
public void testBufferEmpty() {
|
||||||
|
resetEmpty();
|
||||||
|
Buffer buffer = (Buffer) collection;
|
||||||
|
|
||||||
|
assertEquals(0, buffer.size());
|
||||||
|
assertEquals(true, buffer.isEmpty());
|
||||||
|
try {
|
||||||
|
buffer.get();
|
||||||
|
fail();
|
||||||
|
} catch (BufferUnderflowException ex) {}
|
||||||
|
|
||||||
|
try {
|
||||||
|
buffer.remove();
|
||||||
|
fail();
|
||||||
|
} catch (BufferUnderflowException ex) {}
|
||||||
|
}
|
||||||
|
|
||||||
public void testBasicOps() {
|
public void testBasicOps() {
|
||||||
BinaryHeap heap = new BinaryHeap();
|
BinaryBuffer heap = new BinaryBuffer();
|
||||||
|
|
||||||
assertTrue("heap should be empty after create", heap.isEmpty());
|
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");
|
||||||
|
|
||||||
try {
|
assertTrue("heap should not be empty after adds", !heap.isEmpty());
|
||||||
heap.peek();
|
|
||||||
fail("NoSuchElementException should be thrown if peek is called before any elements are inserted");
|
|
||||||
} catch (NoSuchElementException e) {
|
|
||||||
// expected
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
heap.pop();
|
|
||||||
fail("NoSuchElementException should be thrown if pop is called before any elements are inserted");
|
|
||||||
} catch (NoSuchElementException e) {
|
|
||||||
// expected
|
|
||||||
}
|
|
||||||
|
|
||||||
heap.insert("a");
|
|
||||||
heap.insert("c");
|
|
||||||
heap.insert("e");
|
|
||||||
heap.insert("b");
|
|
||||||
heap.insert("d");
|
|
||||||
heap.insert("n");
|
|
||||||
heap.insert("m");
|
|
||||||
heap.insert("l");
|
|
||||||
heap.insert("k");
|
|
||||||
heap.insert("j");
|
|
||||||
heap.insert("i");
|
|
||||||
heap.insert("h");
|
|
||||||
heap.insert("g");
|
|
||||||
heap.insert("f");
|
|
||||||
|
|
||||||
assertTrue("heap should not be empty after inserts", !heap.isEmpty());
|
|
||||||
|
|
||||||
for (int i = 0; i < 14; i++) {
|
for (int i = 0; i < 14; i++) {
|
||||||
assertEquals(
|
assertEquals(
|
||||||
"peek using default constructor should return minimum value in the binary heap",
|
"get using default constructor should return minimum value in the binary heap",
|
||||||
String.valueOf((char) ('a' + i)),
|
String.valueOf((char) ('a' + i)),
|
||||||
heap.peek());
|
heap.get());
|
||||||
|
|
||||||
assertEquals(
|
assertEquals(
|
||||||
"pop using default constructor should return minimum value in the binary heap",
|
"remove using default constructor should return minimum value in the binary heap",
|
||||||
String.valueOf((char) ('a' + i)),
|
String.valueOf((char) ('a' + i)),
|
||||||
heap.pop());
|
heap.remove());
|
||||||
|
|
||||||
if (i + 1 < 14) {
|
if (i + 1 < 14) {
|
||||||
assertTrue("heap should not be empty before all elements are popped", !heap.isEmpty());
|
assertTrue("heap should not be empty before all elements are removed", !heap.isEmpty());
|
||||||
} else {
|
} else {
|
||||||
assertTrue("heap should be empty after all elements are popped", heap.isEmpty());
|
assertTrue("heap should be empty after all elements are removed", heap.isEmpty());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
heap.peek();
|
heap.get();
|
||||||
fail("NoSuchElementException should be thrown if peek is called after all elements are popped");
|
fail("NoSuchElementException should be thrown if get is called after all elements are removed");
|
||||||
} catch (NoSuchElementException e) {
|
} catch (BufferUnderflowException ex) {}
|
||||||
// expected
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
heap.pop();
|
heap.remove();
|
||||||
fail("NoSuchElementException should be thrown if pop is called after all elements are popped");
|
fail("NoSuchElementException should be thrown if remove is called after all elements are removed");
|
||||||
} catch (NoSuchElementException e) {
|
} catch (BufferUnderflowException ex) {}
|
||||||
// expected
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testBasicComparatorOps() {
|
public void testBasicComparatorOps() {
|
||||||
BinaryHeap heap = new BinaryHeap(new ReverseComparator(new ComparableComparator()));
|
BinaryBuffer heap = new BinaryBuffer(new ReverseComparator(new ComparableComparator()));
|
||||||
|
|
||||||
assertTrue("heap should be empty after create", heap.isEmpty());
|
assertTrue("heap should be empty after create", heap.isEmpty());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
heap.peek();
|
heap.get();
|
||||||
fail("NoSuchElementException should be thrown if peek is called before any elements are inserted");
|
fail("NoSuchElementException should be thrown if get is called before any elements are added");
|
||||||
} catch (NoSuchElementException e) {
|
} catch (BufferUnderflowException ex) {}
|
||||||
// expected
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
heap.pop();
|
heap.remove();
|
||||||
fail("NoSuchElementException should be thrown if pop is called before any elements are inserted");
|
fail("NoSuchElementException should be thrown if remove is called before any elements are added");
|
||||||
} catch (NoSuchElementException e) {
|
} catch (BufferUnderflowException ex) {}
|
||||||
// expected
|
|
||||||
}
|
|
||||||
|
|
||||||
heap.insert("a");
|
heap.add("a");
|
||||||
heap.insert("c");
|
heap.add("c");
|
||||||
heap.insert("e");
|
heap.add("e");
|
||||||
heap.insert("b");
|
heap.add("b");
|
||||||
heap.insert("d");
|
heap.add("d");
|
||||||
heap.insert("n");
|
heap.add("n");
|
||||||
heap.insert("m");
|
heap.add("m");
|
||||||
heap.insert("l");
|
heap.add("l");
|
||||||
heap.insert("k");
|
heap.add("k");
|
||||||
heap.insert("j");
|
heap.add("j");
|
||||||
heap.insert("i");
|
heap.add("i");
|
||||||
heap.insert("h");
|
heap.add("h");
|
||||||
heap.insert("g");
|
heap.add("g");
|
||||||
heap.insert("f");
|
heap.add("f");
|
||||||
|
|
||||||
assertTrue("heap should not be empty after inserts", !heap.isEmpty());
|
assertTrue("heap should not be empty after adds", !heap.isEmpty());
|
||||||
|
|
||||||
for (int i = 0; i < 14; i++) {
|
for (int i = 0; i < 14; i++) {
|
||||||
|
|
||||||
|
@ -259,35 +259,51 @@ public class TestBinaryHeap extends AbstractTestCollection {
|
||||||
// "minimum" item is "n", and the "maximum" item is "a".
|
// "minimum" item is "n", and the "maximum" item is "a".
|
||||||
|
|
||||||
assertEquals(
|
assertEquals(
|
||||||
"peek using default constructor should return minimum value in the binary heap",
|
"get using default constructor should return minimum value in the binary heap",
|
||||||
String.valueOf((char) ('n' - i)),
|
String.valueOf((char) ('n' - i)),
|
||||||
heap.peek());
|
heap.get());
|
||||||
|
|
||||||
assertEquals(
|
assertEquals(
|
||||||
"pop using default constructor should return minimum value in the binary heap",
|
"remove using default constructor should return minimum value in the binary heap",
|
||||||
String.valueOf((char) ('n' - i)),
|
String.valueOf((char) ('n' - i)),
|
||||||
heap.pop());
|
heap.remove());
|
||||||
|
|
||||||
if (i + 1 < 14) {
|
if (i + 1 < 14) {
|
||||||
assertTrue("heap should not be empty before all elements are popped", !heap.isEmpty());
|
assertTrue("heap should not be empty before all elements are removed", !heap.isEmpty());
|
||||||
} else {
|
} else {
|
||||||
assertTrue("heap should be empty after all elements are popped", heap.isEmpty());
|
assertTrue("heap should be empty after all elements are removed", heap.isEmpty());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
heap.peek();
|
heap.get();
|
||||||
fail("NoSuchElementException should be thrown if peek is called after all elements are popped");
|
fail("NoSuchElementException should be thrown if get is called after all elements are removed");
|
||||||
} catch (NoSuchElementException e) {
|
} catch (BufferUnderflowException ex) {}
|
||||||
// expected
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
heap.pop();
|
heap.remove();
|
||||||
fail("NoSuchElementException should be thrown if pop is called after all elements are popped");
|
fail("NoSuchElementException should be thrown if remove is called after all elements are removed");
|
||||||
} catch (NoSuchElementException e) {
|
} catch (BufferUnderflowException ex) {}
|
||||||
// expected
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// public void testAddRemove() {
|
||||||
|
// resetEmpty();
|
||||||
|
// BinaryBuffer heap = (BinaryBuffer) collection;
|
||||||
|
// heap.add(new Integer(0));
|
||||||
|
// heap.add(new Integer(2));
|
||||||
|
// heap.add(new Integer(4));
|
||||||
|
// heap.add(new Integer(3));
|
||||||
|
// heap.add(new Integer(8));
|
||||||
|
// heap.add(new Integer(10));
|
||||||
|
// heap.add(new Integer(12));
|
||||||
|
// heap.add(new Integer(3));
|
||||||
|
// confirmed.addAll(heap);
|
||||||
|
// System.out.println(heap);
|
||||||
|
// Object obj = new Integer(10);
|
||||||
|
// heap.remove(obj);
|
||||||
|
// confirmed.remove(obj);
|
||||||
|
// System.out.println(heap);
|
||||||
|
// verify();
|
||||||
|
// }
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue