@Test public void testPoll() { PriorityQueue<TestElement> queue = new DefaultPriorityQueue<TestElement>(TestElementComparator.INSTANCE, 0); assertEmptyQueue(queue); TestElement a = new TestElement(5); TestElement b = new TestElement(10); TestElement c = new TestElement(2); TestElement d = new TestElement(7); TestElement e = new TestElement(6); assertOffer(queue, a); assertOffer(queue, b); assertOffer(queue, c); assertOffer(queue, d); assertSame(c, queue.peek()); assertSame(c, queue.poll()); assertEquals(3, queue.size()); assertOffer(queue, e); assertEquals(4, queue.size()); assertSame(a, queue.peek()); assertSame(a, queue.poll()); assertEquals(3, queue.size()); assertSame(e, queue.peek()); assertSame(e, queue.poll()); assertEquals(2, queue.size()); assertSame(d, queue.peek()); assertSame(d, queue.poll()); assertEquals(1, queue.size()); assertSame(b, queue.peek()); assertSame(b, queue.poll()); assertEmptyQueue(queue); }
@Override public T poll() { if (size == 0) { return null; } T result = queue[0]; result.priorityQueueIndex(this, INDEX_NOT_IN_QUEUE); T last = queue[--size]; queue[size] = null; if (size != 0) { bubbleDown(0, last); } return result; }
DefaultPriorityQueue extends AbstractQueue<T> implements PriorityQueue<T> { @Override public T poll() { if (size == 0) { return null; } T result = queue[0]; result.priorityQueueIndex(this, INDEX_NOT_IN_QUEUE); T last = queue[--size]; queue[size] = null; if (size != 0) { bubbleDown(0, last); } return result; } }
DefaultPriorityQueue extends AbstractQueue<T> implements PriorityQueue<T> { @Override public T poll() { if (size == 0) { return null; } T result = queue[0]; result.priorityQueueIndex(this, INDEX_NOT_IN_QUEUE); T last = queue[--size]; queue[size] = null; if (size != 0) { bubbleDown(0, last); } return result; } @SuppressWarnings("unchecked")  DefaultPriorityQueue(Comparator<T> comparator, int initialSize); }
DefaultPriorityQueue extends AbstractQueue<T> implements PriorityQueue<T> { @Override public T poll() { if (size == 0) { return null; } T result = queue[0]; result.priorityQueueIndex(this, INDEX_NOT_IN_QUEUE); T last = queue[--size]; queue[size] = null; if (size != 0) { bubbleDown(0, last); } return result; } @SuppressWarnings("unchecked")  DefaultPriorityQueue(Comparator<T> comparator, int initialSize); @Override int size(); @Override boolean isEmpty(); @Override boolean contains(Object o); @Override boolean containsTyped(T node); @Override void clear(); @Override void clearIgnoringIndexes(); @Override boolean offer(T e); @Override T poll(); @Override T peek(); @SuppressWarnings("unchecked") @Override boolean remove(Object o); @Override boolean removeTyped(T node); @Override void priorityChanged(T node); @Override Object[] toArray(); @SuppressWarnings("unchecked") @Override X[] toArray(X[] a); @Override Iterator<T> iterator(); }
DefaultPriorityQueue extends AbstractQueue<T> implements PriorityQueue<T> { @Override public T poll() { if (size == 0) { return null; } T result = queue[0]; result.priorityQueueIndex(this, INDEX_NOT_IN_QUEUE); T last = queue[--size]; queue[size] = null; if (size != 0) { bubbleDown(0, last); } return result; } @SuppressWarnings("unchecked")  DefaultPriorityQueue(Comparator<T> comparator, int initialSize); @Override int size(); @Override boolean isEmpty(); @Override boolean contains(Object o); @Override boolean containsTyped(T node); @Override void clear(); @Override void clearIgnoringIndexes(); @Override boolean offer(T e); @Override T poll(); @Override T peek(); @SuppressWarnings("unchecked") @Override boolean remove(Object o); @Override boolean removeTyped(T node); @Override void priorityChanged(T node); @Override Object[] toArray(); @SuppressWarnings("unchecked") @Override X[] toArray(X[] a); @Override Iterator<T> iterator();  }