/** Removes and returns the least element of the PriorityQueue in log(size) time. */ public final T pop() { if (size > 0) { T result = heap[1]; // save first value heap[1] = heap[size]; // move last to first heap[size] = null; // permit GC of objects size--; downHeap(1); // adjust heap return result; } else { return null; } }
/** * Should be called when the Object at top changes values. Still log(n) worst * case, but it's at least twice as fast to * * <pre class="prettyprint"> * pq.top().change(); * pq.updateTop(); * </pre> * * instead of * * <pre class="prettyprint"> * o = pq.pop(); * o.change(); * pq.push(o); * </pre> * * @return the new 'top' element. */ public final T updateTop() { downHeap(1); return heap[1]; }
/** * Removes an existing element currently stored in the PriorityQueue. Cost is * linear with the size of the queue. (A specialization of PriorityQueue which * tracks element positions would provide a constant remove time but the * trade-off would be extra cost to all additions/insertions) */ public final boolean remove(T element) { for (int i = 1; i <= size; i++) { if (heap[i] == element) { heap[i] = heap[size]; heap[size] = null; // permit GC of objects size--; if (i <= size) { if (!upHeap(i)) { downHeap(i); } } return true; } } return false; }
/** Removes and returns the least element of the PriorityQueue in log(size) time. */ public final Object pop() { if (size > 0) { Object result = heap[1]; // save first value heap[1] = heap[size]; // move last to first heap[size] = null; // permit GC of objects size--; downHeap(); // adjust heap return result; } else return null; }
/** Should be called when the Object at top changes values. Still log(n) * worst case, but it's at least twice as fast to <pre> * { pq.top().change(); pq.adjustTop(); } * </pre> instead of <pre> * { o = pq.pop(); o.change(); pq.push(o); } * </pre> */ public final void adjustTop() { downHeap(); }
/** Should be called when the Object at top changes values. Still log(n) * worst case, but it's at least twice as fast to <pre> * { pq.top().change(); pq.adjustTop(); } * </pre> instead of <pre> * { o = pq.pop(); o.change(); pq.push(o); } * </pre> */ public final void adjustTop() { downHeap(); }
/** Removes and returns the least element of the PriorityQueue in log(size) time. */ public final T pop() { if (size > 0) { T result = heap[1]; // save first value heap[1] = heap[size]; // move last to first heap[size] = null; // permit GC of objects size--; downHeap(1); // adjust heap return result; } else { return null; } }
/** Should be called when the Object at top changes values. Still log(n) * worst case, but it's at least twice as fast to <pre> * { pq.top().change(); pq.adjustTop(); } * </pre> instead of <pre> * { o = pq.pop(); o.change(); pq.push(o); } * </pre> */ public final void adjustTop() { downHeap(); }
/** Removes and returns the least element of the PriorityQueue in log(size) time. */ public final Object pop() { if (size > 0) { Object result = heap[1]; // save first value heap[1] = heap[size]; // move last to first heap[size] = null; // permit GC of objects size--; downHeap(); // adjust heap return result; } else return null; }
/** Removes and returns the least element of the PriorityQueue in log(size) time. */ public final T pop() { if (size > 0) { T result = heap[1]; // save first value heap[1] = heap[size]; // move last to first heap[size] = null; // permit GC of objects size--; downHeap(); // adjust heap return result; } else return null; }
/** Removes and returns the least element of the PriorityQueue in log(size) time. */ public final Object pop() { if (size > 0) { Object result = heap[1]; // save first value heap[1] = heap[size]; // move last to first heap[size] = null; // permit GC of objects size--; downHeap(); // adjust heap return result; } else return null; }
/** Removes and returns the least element of the PriorityQueue in log(size) time. */ public final T pop() { if (size > 0) { T result = heap[1]; // save first value heap[1] = heap[size]; // move last to first heap[size] = null; // permit GC of objects size--; downHeap(1); // adjust heap return result; } else { return null; } }
/** Removes and returns the least element of the PriorityQueue in log(size) time. */ public final T pop() { if (size > 0) { T result = heap[1]; // save first value heap[1] = heap[size]; // move last to first heap[size] = null; // permit GC of objects size--; downHeap(1); // adjust heap return result; } else { return null; } }
/** * Should be called when the Object at top changes values. Still log(n) worst * case, but it's at least twice as fast to * * <pre class="prettyprint"> * pq.top().change(); * pq.updateTop(); * </pre> * * instead of * * <pre class="prettyprint"> * o = pq.pop(); * o.change(); * pq.push(o); * </pre> * * @return the new 'top' element. */ public final T updateTop() { downHeap(1); return heap[1]; }
/** * Should be called when the Object at top changes values. Still log(n) worst * case, but it's at least twice as fast to * * <pre class="prettyprint"> * pq.top().change(); * pq.updateTop(); * </pre> * * instead of * * <pre class="prettyprint"> * o = pq.pop(); * o.change(); * pq.push(o); * </pre> * * @return the new 'top' element. */ public final T updateTop() { downHeap(); return heap[1]; }
/** * Should be called when the Object at top changes values. Still log(n) worst * case, but it's at least twice as fast to * * <pre class="prettyprint"> * pq.top().change(); * pq.updateTop(); * </pre> * * instead of * * <pre class="prettyprint"> * o = pq.pop(); * o.change(); * pq.push(o); * </pre> * * @return the new 'top' element. */ public final T updateTop() { downHeap(1); return heap[1]; }
/** * Should be called when the Object at top changes values. Still log(n) worst * case, but it's at least twice as fast to * * <pre class="prettyprint"> * pq.top().change(); * pq.updateTop(); * </pre> * * instead of * * <pre class="prettyprint"> * o = pq.pop(); * o.change(); * pq.push(o); * </pre> * * @return the new 'top' element. */ public final T updateTop() { downHeap(1); return heap[1]; }
/** * Removes an existing element currently stored in the PriorityQueue. Cost is * linear with the size of the queue. (A specialization of PriorityQueue which * tracks element positions would provide a constant remove time but the * trade-off would be extra cost to all additions/insertions) */ public final boolean remove(T element) { for (int i = 1; i <= size; i++) { if (heap[i] == element) { heap[i] = heap[size]; heap[size] = null; // permit GC of objects size--; if (i <= size) { if (!upHeap(i)) { downHeap(i); } } return true; } } return false; }
/** * Removes an existing element currently stored in the PriorityQueue. Cost is * linear with the size of the queue. (A specialization of PriorityQueue which * tracks element positions would provide a constant remove time but the * trade-off would be extra cost to all additions/insertions) */ public final boolean remove(T element) { for (int i = 1; i <= size; i++) { if (heap[i] == element) { heap[i] = heap[size]; heap[size] = null; // permit GC of objects size--; if (i <= size) { if (!upHeap(i)) { downHeap(i); } } return true; } } return false; }
/** * Removes an existing element currently stored in the PriorityQueue. Cost is * linear with the size of the queue. (A specialization of PriorityQueue which * tracks element positions would provide a constant remove time but the * trade-off would be extra cost to all additions/insertions) */ public final boolean remove(T element) { for (int i = 1; i <= size; i++) { if (heap[i] == element) { heap[i] = heap[size]; heap[size] = null; // permit GC of objects size--; if (i <= size) { if (!upHeap(i)) { downHeap(i); } } return true; } } return false; }