PriorityQueue<ScheduledFutureTask<?>> scheduledTaskQueue() { if (scheduledTaskQueue == null) { scheduledTaskQueue = new DefaultPriorityQueue<ScheduledFutureTask<?>>( SCHEDULED_FUTURE_TASK_COMPARATOR, // Use same initial capacity as java.util.PriorityQueue 11); } return scheduledTaskQueue; }
@Override public void priorityChanged(T node) { int i = node.priorityQueueIndex(this); if (!contains(node, i)) { return; } // Preserve the min-heap property by comparing the new priority with parents/children in the heap. if (i == 0) { bubbleDown(i, node); } else { // Get the parent to see if min-heap properties are violated. int iParent = (i - 1) >>> 1; T parent = queue[iParent]; if (comparator.compare(node, parent) < 0) { bubbleUp(i, node); } else { bubbleDown(i, node); } } }
@SuppressWarnings("unchecked") @Override public boolean remove(Object o) { final T node; try { node = (T) o; } catch (ClassCastException e) { return false; } return removeTyped(node); }
@Override public boolean contains(Object o) { if (!(o instanceof PriorityQueueNode)) { return false; } PriorityQueueNode node = (PriorityQueueNode) o; return contains(node, node.priorityQueueIndex(this)); }
@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) { // Make sure we don't add the last element back. bubbleDown(0, last); } return result; }
@Override public boolean offer(T e) { if (e.priorityQueueIndex(this) != INDEX_NOT_IN_QUEUE) { throw new IllegalArgumentException("e.priorityQueueIndex(): " + e.priorityQueueIndex(this) + " (expected: " + INDEX_NOT_IN_QUEUE + ") + e: " + e); } // Check that the array capacity is enough to hold values by doubling capacity. if (size >= queue.length) { // Use a policy which allows for a 0 initial capacity. Same policy as JDK's priority queue, double when // "small", then grow by 50% when "large". queue = Arrays.copyOf(queue, queue.length + ((queue.length < 64) ? (queue.length + 2) : (queue.length >>> 1))); } bubbleUp(size++, e); return true; }
@Override public boolean containsTyped(T node) { return contains(node, node.priorityQueueIndex(this)); }
@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) { // Make sure we don't add the last element back. bubbleDown(0, last); } return result; }
@Override public boolean offer(T e) { if (e.priorityQueueIndex(this) != INDEX_NOT_IN_QUEUE) { throw new IllegalArgumentException("e.priorityQueueIndex(): " + e.priorityQueueIndex(this) + " (expected: " + INDEX_NOT_IN_QUEUE + ") + e: " + e); } // Check that the array capacity is enough to hold values by doubling capacity. if (size >= queue.length) { // Use a policy which allows for a 0 initial capacity. Same policy as JDK's priority queue, double when // "small", then grow by 50% when "large". queue = Arrays.copyOf(queue, queue.length + ((queue.length < 64) ? (queue.length + 2) : (queue.length >>> 1))); } bubbleUp(size++, e); return true; }
@Override public void priorityChanged(T node) { int i = node.priorityQueueIndex(this); if (!contains(node, i)) { return; } // Preserve the min-heap property by comparing the new priority with parents/children in the heap. if (i == 0) { bubbleDown(i, node); } else { // Get the parent to see if min-heap properties are violated. int iParent = (i - 1) >>> 1; T parent = queue[iParent]; if (comparator.compare(node, parent) < 0) { bubbleUp(i, node); } else { bubbleDown(i, node); } } }
PriorityQueue<ScheduledFutureTask<?>> scheduledTaskQueue() { if (scheduledTaskQueue == null) { scheduledTaskQueue = new DefaultPriorityQueue<ScheduledFutureTask<?>>( SCHEDULED_FUTURE_TASK_COMPARATOR, // Use same initial capacity as java.util.PriorityQueue 11); } return scheduledTaskQueue; }
@Override public boolean contains(Object o) { if (!(o instanceof PriorityQueueNode)) { return false; } PriorityQueueNode node = (PriorityQueueNode) o; return contains(node, node.priorityQueueIndex(this)); }
@SuppressWarnings("unchecked") @Override public boolean remove(Object o) { final T node; try { node = (T) o; } catch (ClassCastException e) { return false; } return removeTyped(node); }
@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) { // Make sure we don't add the last element back. bubbleDown(0, last); } return result; }
@Override public boolean offer(T e) { if (e.priorityQueueIndex(this) != INDEX_NOT_IN_QUEUE) { throw new IllegalArgumentException("e.priorityQueueIndex(): " + e.priorityQueueIndex(this) + " (expected: " + INDEX_NOT_IN_QUEUE + ") + e: " + e); } // Check that the array capacity is enough to hold values by doubling capacity. if (size >= queue.length) { // Use a policy which allows for a 0 initial capacity. Same policy as JDK's priority queue, double when // "small", then grow by 50% when "large". queue = Arrays.copyOf(queue, queue.length + ((queue.length < 64) ? (queue.length + 2) : (queue.length >>> 1))); } bubbleUp(size++, e); return true; }
@Override public boolean removeTyped(T node) { int i = node.priorityQueueIndex(this); if (!contains(node, i)) { return false; } node.priorityQueueIndex(this, INDEX_NOT_IN_QUEUE); if (--size == 0 || size == i) { // If there are no node left, or this is the last node in the array just remove and return. queue[i] = null; return true; } // Move the last element where node currently lives in the array. T moved = queue[i] = queue[size]; queue[size] = null; // priorityQueueIndex will be updated below in bubbleUp or bubbleDown // Make sure the moved node still preserves the min-heap properties. if (comparator.compare(node, moved) < 0) { bubbleDown(i, moved); } else { bubbleUp(i, moved); } return true; }
State(int streamId, Http2Stream stream, int initialSize) { this.stream = stream; this.streamId = streamId; pseudoTimeQueue = new DefaultPriorityQueue<State>(StatePseudoTimeComparator.INSTANCE, initialSize); }
@Override public boolean containsTyped(T node) { return contains(node, node.priorityQueueIndex(this)); }
@SuppressWarnings("unchecked") @Override public boolean remove(Object o) { final T node; try { node = (T) o; } catch (ClassCastException e) { return false; } return removeTyped(node); }
@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) { // Make sure we don't add the last element back. bubbleDown(0, last); } return result; }