diff --git a/src/java/org/apache/commons/collections/buffer/BinaryHeap.java b/src/java/org/apache/commons/collections/buffer/BinaryBuffer.java similarity index 54% rename from src/java/org/apache/commons/collections/buffer/BinaryHeap.java rename to src/java/org/apache/commons/collections/buffer/BinaryBuffer.java index 87fcfd0a1..84eb2b16f 100644 --- a/src/java/org/apache/commons/collections/buffer/BinaryHeap.java +++ b/src/java/org/apache/commons/collections/buffer/BinaryBuffer.java @@ -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 @@ -64,16 +64,15 @@ import java.util.NoSuchElementException; import org.apache.commons.collections.Buffer; import org.apache.commons.collections.BufferUnderflowException; -import org.apache.commons.collections.PriorityQueue; /** - * Binary heap implementation of PriorityQueue that provides for + * 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 {@link Comparator}. The * {@link #remove()} method always returns the first element as determined - * by the sort order. (The isMinHeap flag in the constructors + * by the sort order. (The ascendingOrder flag in the constructors * can be used to reverse the sort order, in which case {@link #remove()} * will always remove the last element.) The removal order is * not 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. *

* Note that this implementation is not synchronized. Use - * {@link org.apache.commons.collections.PriorityQueueUtils#synchronizedPriorityQueue(PriorityQueue)} - * to provide synchronized access to a BinaryHeap: + * {@link org.apache.commons.collections.BufferUtils#synchronizedBuffer(Buffer)} or + * {@link org.apache.commons.collections.buffer.SynchronizedBuffer#decorate(Buffer)} + * to provide synchronized access to a BinaryBuffer: * *

- * Buffer heap = BufferUtils.synchronizedBuffer(new BinaryHeap());
+ * Buffer heap = SynchronizedBuffer.decorate(new BinaryBuffer());
  * 
* - * @since Commons Collections 3.0 (previously in main package v1.0) - * @version $Revision: 1.3 $ $Date: 2003/12/28 17:58:54 $ + * @since Commons Collections 3.0 (previously BinaryHeap v1.0) + * @version $Revision: 1.1 $ $Date: 2004/01/01 19:01:34 $ * * @author Peter Donald * @author Ram Chidambaram @@ -100,215 +100,223 @@ import org.apache.commons.collections.PriorityQueue; * @author Paul Jack * @author Stephen Colebourne */ -public final class BinaryHeap extends AbstractCollection - implements PriorityQueue, Buffer { +public class BinaryBuffer extends AbstractCollection implements Buffer { /** * The default capacity for a binary heap. */ - private final static int DEFAULT_CAPACITY = 13; - /** - * The number of elements currently in this heap. - */ - int m_size; // package scoped for testing + private static final int DEFAULT_CAPACITY = 13; + /** * 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 * be returned. If false, the last element as determined by the * sort order will be returned. */ - boolean m_isMinHeap; // package scoped for testing + protected boolean ascendingOrder; /** * 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() { - this(DEFAULT_CAPACITY, true); + public BinaryBuffer() { + this(DEFAULT_CAPACITY, true, null); } /** - * Constructs a new BinaryHeap that will use the given - * comparator to order its elements. + * Constructs a new empty buffer that sorts in ascending order using the + * specified comparator. * - * @param comparator the comparator used to order the elements, null - * means use natural order + * @param comparator the comparator used to order the elements, + * null means use natural order */ - public BinaryHeap(Comparator comparator) { - this(); - m_comparator = comparator; - } - - /** - * Constructs a new minimum binary heap with the specified initial capacity. - * - * @param capacity The initial capacity for the heap. This value must - * be greater than zero. - * @throws IllegalArgumentException - * if capacity is <= 0 - */ - public BinaryHeap(int capacity) { - this(capacity, true); + public BinaryBuffer(Comparator comparator) { + this(DEFAULT_CAPACITY, true, comparator); } /** - * Constructs a new BinaryHeap. + * 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 - * @param comparator the comparator used to order the elements, null - * means use natural order - * @throws IllegalArgumentException - * if capacity is <= 0 - */ - public BinaryHeap(int capacity, Comparator comparator) { - this(capacity); - m_comparator = comparator; - } - - /** - * Constructs a new minimum or maximum binary heap - * - * @param isMinHeap if true the heap is created as a + * @param ascendingOrder if true the heap is created as a * minimum heap; otherwise, the heap is created as a maximum heap */ - public BinaryHeap(boolean isMinHeap) { - this(DEFAULT_CAPACITY, isMinHeap); + public BinaryBuffer(boolean ascendingOrder) { + this(DEFAULT_CAPACITY, ascendingOrder, null); } /** - * Constructs a new BinaryHeap. + * 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 - * @param comparator the comparator used to order the elements, null - * means use natural order + * @param comparator the comparator used to order the elements, + * null means use natural order */ - public BinaryHeap(boolean isMinHeap, Comparator comparator) { - this(isMinHeap); - m_comparator = comparator; + public BinaryBuffer(boolean ascendingOrder, Comparator comparator) { + this(DEFAULT_CAPACITY, ascendingOrder, comparator); } /** - * Constructs a new minimum or maximum binary heap with the specified - * initial capacity. - * - * @param capacity the initial capacity for the heap. This value must - * be greater than zero. - * @param isMinHeap if true the heap is created as a - * minimum heap; otherwise, the heap is created as a maximum heap. - * @throws IllegalArgumentException - * if capacity is <= 0 + * Constructs a new empty buffer that sorts in ascending order by the + * natural order of the objects added, specifying an initial capacity. + * + * @param capacity the initial capacity for the buffer, greater than zero + * @throws IllegalArgumentException if capacity is <= 0 */ - 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 capacity is <= 0 + */ + 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 true the heap is created as a + * minimum heap; otherwise, the heap is created as a maximum heap. + * @throws IllegalArgumentException if capacity is <= 0 + */ + 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 capacity is <= 0 + */ + public BinaryBuffer(int capacity, boolean ascendingOrder, Comparator comparator) { + super(); if (capacity <= 0) { throw new IllegalArgumentException("invalid capacity"); } - m_isMinHeap = isMinHeap; + this.ascendingOrder = ascendingOrder; //+1 as 0 is noop - m_elements = new Object[capacity + 1]; + this.elements = new Object[capacity + 1]; + this.comparator = comparator; } + //----------------------------------------------------------------------- /** - * Constructs a new BinaryHeap. - * - * @param capacity the initial capacity for the 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 capacity is <= 0 + * Checks whether the heap is ascending or descending order. + * + * @return true if ascending order (a min heap) */ - public BinaryHeap(int capacity, boolean isMinHeap, Comparator comparator) { - this(capacity, isMinHeap); - m_comparator = comparator; + public boolean isAscendingOrder() { + return ascendingOrder; } - /** - * Clears all elements from queue. + * 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 the buffer. */ public void clear() { - m_elements = new Object[m_elements.length]; // for gc - m_size = 0; + elements = new Object[elements.length]; // for gc + size = 0; } /** - * Tests if queue is empty. + * Adds an element to the buffer. + *

+ * The element added will be sorted according to the comparator in use. * - * @return true if queue is empty; false - * otherwise. + * @param element the element to be added */ - public boolean isEmpty() { - return m_size == 0; - } - - /** - * Tests if queue is full. - * - * @return true if queue is full; false - * 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()) { + public boolean add(Object element) { + if (isAtCapacity()) { grow(); } - //percolate element to it's place in tree - if (m_isMinHeap) { + // percolate element to it's place in tree + if (ascendingOrder) { percolateUpMinHeap(element); } else { 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 - * @throws NoSuchElementException if isEmpty() == true + * @return the next element + * @throws BufferUnderflowException if the buffer is empty */ - public Object peek() throws NoSuchElementException { + public Object get() { if (isEmpty()) { - throw new NoSuchElementException(); + throw new BufferUnderflowException(); } 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 - * @throws NoSuchElementException if isEmpty() == true + * @return the next element + * @throws BufferUnderflowException if the buffer is empty */ - public Object pop() throws NoSuchElementException { - final Object result = peek(); - m_elements[1] = m_elements[m_size--]; + 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) - 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 - if (m_isMinHeap) { + if (ascendingOrder) { percolateDownMinHeap(1); } else { percolateDownMaxHeap(1); @@ -318,35 +326,46 @@ public final class BinaryHeap extends AbstractCollection return result; } + //----------------------------------------------------------------------- + /** + * Tests if the buffer is at capacity. + * + * @return true if buffer is full; false otherwise. + */ + protected boolean isAtCapacity() { + //+1 as element 0 is noop + return elements.length == size + 1; + } + /** * 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 */ protected void percolateDownMinHeap(final int index) { - final Object element = m_elements[index]; + final Object element = elements[index]; int hole = index; - while ((hole * 2) <= m_size) { + while ((hole * 2) <= size) { int child = hole * 2; // if we have a right child and that child can not be percolated // 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++; } // if we found resting place of bubble then terminate search - if (compare(m_elements[child], element) >= 0) { + if (compare(elements[child], element) >= 0) { break; } - m_elements[hole] = m_elements[child]; + elements[hole] = elements[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 */ protected void percolateDownMaxHeap(final int index) { - final Object element = m_elements[index]; + final Object element = elements[index]; int hole = index; - while ((hole * 2) <= m_size) { + while ((hole * 2) <= size) { int child = hole * 2; // if we have a right child and that child can not be percolated // 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++; } // if we found resting place of bubble then terminate search - if (compare(m_elements[child], element) <= 0) { + if (compare(elements[child], element) <= 0) { break; } - m_elements[hole] = m_elements[child]; + elements[hole] = elements[child]; hole = child; } - m_elements[hole] = element; + elements[hole] = element; } /** * Percolates element up heap from bottom. - * Assume it is a maximum heap. + * Assume it is a minimum heap. * * @param element the 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 // as the element "bubble" is percolated up final int next = hole / 2; - m_elements[hole] = m_elements[next]; + elements[hole] = elements[next]; hole = next; } - m_elements[hole] = element; + elements[hole] = element; } /** @@ -409,19 +428,19 @@ public final class BinaryHeap extends AbstractCollection * @param element the 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 // as the element "bubble" is percolated up final int next = hole / 2; - m_elements[hole] = m_elements[next]; + elements[hole] = elements[next]; hole = next; } - m_elements[hole] = element; + elements[hole] = element; } - + /** * Compares two objects using the comparator if specified, or the * natural order otherwise. @@ -430,9 +449,9 @@ public final class BinaryHeap extends AbstractCollection * @param b the second object * @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) { - if (m_comparator != null) { - return m_comparator.compare(a, b); + protected int compare(Object a, Object b) { + if (comparator != null) { + return comparator.compare(a, b); } else { 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 */ protected void grow() { - final Object[] elements = new Object[m_elements.length * 2]; - System.arraycopy(m_elements, 0, elements, 0, m_elements.length); - m_elements = elements; + final Object[] array = new Object[elements.length * 2]; + System.arraycopy(elements, 0, array, 0, elements.length); + 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("[ "); - for (int i = 1; i < m_size + 1; i++) { + for (int i = 1; i < size + 1; i++) { if (i != 1) { sb.append(", "); } - sb.append(m_elements[i]); + sb.append(elements[i]); } sb.append(" ]"); @@ -470,94 +536,4 @@ public final class BinaryHeap extends AbstractCollection 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; - } - } diff --git a/src/java/org/apache/commons/collections/buffer/package.html b/src/java/org/apache/commons/collections/buffer/package.html index 6cc4611c2..d5784e8ca 100644 --- a/src/java/org/apache/commons/collections/buffer/package.html +++ b/src/java/org/apache/commons/collections/buffer/package.html @@ -1,15 +1,14 @@

This package contains implementations of the -{@link org.apache.commons.collections.Buffer Buffer} and -{@link org.apache.commons.collections.PriorityQueue PriorityQueue} interfaces. +{@link org.apache.commons.collections.Buffer Buffer} interface.

The following implementations are provided in the package:

The following decorators are provided in the package: diff --git a/src/test/org/apache/commons/collections/buffer/TestAll.java b/src/test/org/apache/commons/collections/buffer/TestAll.java index 17537bfbb..da9fa4284 100644 --- a/src/test/org/apache/commons/collections/buffer/TestAll.java +++ b/src/test/org/apache/commons/collections/buffer/TestAll.java @@ -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 @@ -65,7 +65,7 @@ import junit.framework.TestSuite; * Entry point for tests. * * @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 */ @@ -83,7 +83,7 @@ public class TestAll extends TestCase { public static Test suite() { TestSuite suite = new TestSuite(); - suite.addTest(TestBinaryHeap.suite()); + suite.addTest(TestBinaryBuffer.suite()); suite.addTest(TestBlockingBuffer.suite()); suite.addTest(TestBoundedFifoBuffer.suite()); suite.addTest(TestBoundedFifoBuffer2.suite()); diff --git a/src/test/org/apache/commons/collections/buffer/TestBinaryHeap.java b/src/test/org/apache/commons/collections/buffer/TestBinaryBuffer.java similarity index 57% rename from src/test/org/apache/commons/collections/buffer/TestBinaryHeap.java rename to src/test/org/apache/commons/collections/buffer/TestBinaryBuffer.java index 238c53c8c..987b73b5a 100644 --- a/src/test/org/apache/commons/collections/buffer/TestBinaryHeap.java +++ b/src/test/org/apache/commons/collections/buffer/TestBinaryBuffer.java @@ -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 @@ -61,11 +61,12 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Comparator; -import java.util.NoSuchElementException; import junit.framework.Test; 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.collection.AbstractTestCollection; import org.apache.commons.collections.comparators.ComparableComparator; @@ -74,38 +75,44 @@ import org.apache.commons.collections.comparators.ReverseComparator; /** * 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 */ -public class TestBinaryHeap extends AbstractTestCollection { +public class TestBinaryBuffer extends AbstractTestCollection { - public static Test suite() { - return new TestSuite(TestBinaryHeap.class); + public static void main(String[] args) { + junit.textui.TestRunner.run(suite()); } - public TestBinaryHeap(String testName) { + public static Test suite() { + return new TestSuite(TestBinaryBuffer.class); + } + + public TestBinaryBuffer(String testName) { super(testName); } //----------------------------------------------------------------------- public void verify() { super.verify(); - BinaryHeap heap = (BinaryHeap) collection; + BinaryBuffer heap = (BinaryBuffer) collection; - Comparator c = heap.m_comparator; - if (c == null) + Comparator c = heap.comparator; + if (c == null) { c = ComparatorUtils.naturalComparator(); - if (!heap.m_isMinHeap) + } + if (!heap.ascendingOrder) { c = ComparatorUtils.reversedComparator(c); + } - Object[] tree = heap.m_elements; - for (int i = 1; i <= heap.m_size; i++) { + Object[] tree = heap.elements; + for (int i = 1; i <= heap.size; 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); } - 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); } } @@ -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 */ public boolean isFailFastSupported() { @@ -135,7 +142,7 @@ public class TestBinaryHeap extends AbstractTestCollection { * Return a new, empty {@link Object} to used for testing. */ 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() { - 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 { - 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()); + assertTrue("heap should not be empty after adds", !heap.isEmpty()); for (int i = 0; i < 14; i++) { 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)), - heap.peek()); + heap.get()); 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)), - heap.pop()); + heap.remove()); 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 { - 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 { - heap.peek(); - fail("NoSuchElementException should be thrown if peek is called after all elements are popped"); - } catch (NoSuchElementException e) { - // expected - } + heap.get(); + fail("NoSuchElementException should be thrown if get is called after all elements are removed"); + } catch (BufferUnderflowException ex) {} try { - heap.pop(); - fail("NoSuchElementException should be thrown if pop is called after all elements are popped"); - } catch (NoSuchElementException e) { - // expected - } + heap.remove(); + fail("NoSuchElementException should be thrown if remove is called after all elements are removed"); + } catch (BufferUnderflowException ex) {} } 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()); try { - heap.peek(); - fail("NoSuchElementException should be thrown if peek is called before any elements are inserted"); - } catch (NoSuchElementException e) { - // expected - } + heap.get(); + fail("NoSuchElementException should be thrown if get is called before any elements are added"); + } catch (BufferUnderflowException ex) {} try { - heap.pop(); - fail("NoSuchElementException should be thrown if pop is called before any elements are inserted"); - } catch (NoSuchElementException e) { - // expected - } + heap.remove(); + fail("NoSuchElementException should be thrown if remove is called before any elements are added"); + } catch (BufferUnderflowException ex) {} - 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"); + 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 inserts", !heap.isEmpty()); + assertTrue("heap should not be empty after adds", !heap.isEmpty()); 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". 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)), - heap.peek()); + heap.get()); 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)), - heap.pop()); + heap.remove()); 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 { - 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 { - heap.peek(); - fail("NoSuchElementException should be thrown if peek is called after all elements are popped"); - } catch (NoSuchElementException e) { - // expected - } + heap.get(); + fail("NoSuchElementException should be thrown if get is called after all elements are removed"); + } catch (BufferUnderflowException ex) {} try { - heap.pop(); - fail("NoSuchElementException should be thrown if pop is called after all elements are popped"); - } catch (NoSuchElementException e) { - // expected - } + heap.remove(); + fail("NoSuchElementException should be thrown if remove is called after all elements are removed"); + } catch (BufferUnderflowException ex) {} } +// 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(); +// } + }