@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 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 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); } } }
@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; }
@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); } } }
@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; }
@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; }
@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); } } }
@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); } } }
@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; }
@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 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 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); } } }
@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); } } }
@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); } } }
@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; }
@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; }