public void testReverse() { PriorityQueue q = new MinHeap(N); Item items[] = new Item[N]; for (int i = 0; i < N; i++) { Item e = new Item(N - i - 1); q.insert(e); items[i] = e; } for (int i = 0; i < N; i++) { q.changePriority(items[i], i); } int j = 0; double pr_last = Double.NEGATIVE_INFINITY; assertTrue("ascending size", q.size() == N); while (q.size() > 0) { assertTrue("ascending extract", j < N); QueueElement e = q.extractMin(); assertTrue("ascending order", e.getPriority() > pr_last); pr_last = e.getPriority(); assertEquals("ascending priority", items[j].getPriority(), e.getPriority()); assertEquals("ascending identity", items[j], e); j++; } }
/** * Insert the specified {@link QueueElement} element into the queue. * * @param queueElement the {@link QueueElement} element to add. * @param ignoreSize if the queue is bound to a maximum size and the maximum size is reached, this * parameter (if set to <tt>true</tt>) allows to ignore the maximum size and add the element * to the queue. * @return <tt>true</tt> if the element has been inserted, <tt>false</tt> if the element was not * inserted (the queue has reached its maximum size). * @throws NullPointerException if the specified element is null */ boolean offer(QueueElement<E> queueElement, boolean ignoreSize) { if (queueElement == null) { throw new NullPointerException("queueElement is NULL"); } if (queueElement.getPriority() < 0 || queueElement.getPriority() >= priorities) { throw new IllegalArgumentException("priority out of range: " + queueElement); } if (queueElement.inQueue) { throw new IllegalStateException("queueElement already in a queue: " + queueElement); } if (!ignoreSize && currentSize != null && currentSize.get() >= maxSize) { return false; } boolean accepted; lock.lock(); try { accepted = queues[queueElement.getPriority()].offer(queueElement); debug( "offer([{0}]), to P[{1}] delay[{2}ms] accepted[{3}]", queueElement.getElement().toString(), queueElement.getPriority(), queueElement.getDelay(TimeUnit.MILLISECONDS), accepted); if (accepted) { if (currentSize != null) { currentSize.incrementAndGet(); } queueElement.inQueue = true; } } finally { lock.unlock(); } return accepted; }
public void testEqualKeys() { PriorityQueue q = new MinHeap(N); Item[] items = new Item[20]; int j = 0; for (int i = 0; i < 5; i++) { items[j] = new Item(5); q.insert(items[j]); j++; } for (int i = 0; i < 5; i++) { items[j] = new Item(3); q.insert(items[j]); j++; } for (int i = 0; i < 5; i++) { items[j] = new Item(4); q.insert(items[j]); j++; } for (int i = 0; i < 5; i++) { items[j] = new Item(7); q.insert(items[j]); j++; } assertEquals(20, q.size()); for (int i = 0; i < items.length; i++) { assertTrue(q.contains(items[i])); } for (int i = 0; i < 5; i++) { QueueElement e = q.extractMin(); assertTrue(q.contains(q.min())); assertEquals(3.0, e.getPriority()); } for (int i = 0; i < 5; i++) { QueueElement e = q.extractMin(); assertTrue(q.contains(q.min())); assertEquals(4.0, e.getPriority()); } for (int i = 0; i < 5; i++) { QueueElement e = q.extractMin(); assertTrue(q.contains(q.min())); assertEquals(5.0, e.getPriority()); } for (int i = 0; i < 5; i++) { QueueElement e = q.extractMin(); if (q.size() > 0) assertTrue(q.contains(q.min())); assertEquals(7.0, e.getPriority()); } }
public void testDescending() { PriorityQueue q = new MinHeap(N); double p[] = new double[N]; for (int i = 0; i < N; i++) { p[i] = i; Item e = new Item(N - i - 1); q.insert(e); } int j = 0; double pr = Double.NEGATIVE_INFINITY; assertTrue("descending size", q.size() == N); while (q.size() > 0) { assertTrue("descending extract", j < N); QueueElement e = q.extractMin(); assertTrue("descending order", e.getPriority() > pr); assertEquals("descending priority", e.getPriority(), p[j++], 1e-5); pr = e.getPriority(); } }
/** * Retrieve, but does not remove, the head of this queue, or returns <tt>null</tt> if this queue * is empty. Unlike <tt>poll</tt>, if no expired elements are available in the queue, this method * returns the element that will expire next, if one exists. * * @return the head of this queue, or <tt>null</tt> if this queue is empty. */ @Override public QueueElement<E> peek() { lock.lock(); try { antiStarvation(); QueueElement<E> e = null; QueueElement<E>[] seeks = new QueueElement[priorities]; boolean foundElement = false; for (int i = priorities - 1; i > -1; i--) { e = queues[i].peek(); debug("peek(): considering [{0}] from P[{1}]", e, i); seeks[priorities - i - 1] = e; foundElement |= e != null; } if (foundElement) { e = null; for (int i = 0; e == null && i < priorities; i++) { if (seeks[i] != null && seeks[i].getDelay(TimeUnit.MILLISECONDS) > 0) { debug("peek, ignoring [{0}]", seeks[i]); } else { e = seeks[i]; } } if (e != null) { debug("peek(): choosing [{0}]", e); } if (e == null) { int first; for (first = 0; e == null && first < priorities; first++) { e = seeks[first]; } if (e != null) { debug("peek(): initial choosing [{0}]", e); } for (int i = first; i < priorities; i++) { QueueElement<E> ee = seeks[i]; if (ee != null && ee.getDelay(TimeUnit.MILLISECONDS) < e.getDelay(TimeUnit.MILLISECONDS)) { debug("peek(): choosing [{0}] over [{1}]", ee, e); e = ee; } } } } if (e != null) { debug("peek(): [{0}], from P[{1}]", e.getElement().toString(), e.getPriority()); } else { debug("peek(): NULL"); } return e; } finally { lock.unlock(); } }
public void testChangePriority() { PriorityQueue q = new MinHeap(N); Item items[] = new Item[N]; for (int i = 0; i < N; i++) { Item e = new Item(N - i - 1); q.insert(e); items[i] = e; } q.changePriority(items[N - 1], -2); q.changePriority(items[N / 2], -1); q.changePriority(items[N / 2 + 1], N * 2); int j = 0; double pr_last = Double.NEGATIVE_INFINITY; assertTrue("descending size", q.size() == N); while (q.size() > 0) { assertTrue("descending extract", j < N); QueueElement e = q.extractMin(); assertTrue("descending order", e.getPriority() > pr_last); pr_last = e.getPriority(); if (j == 0) assertTrue("lowest elt", e.getPriority() == -2); if (j == 1) assertTrue("second-lowest elt", e.getPriority() == -1); if (q.size() == 1) assertTrue("penultimate elt", e.getPriority() == N - 1); if (q.size() == 0) assertTrue("final elt", e.getPriority() == N * 2); j++; } }