This commit is contained in:
Armin 2024-10-20 17:38:24 +02:00
parent d60183bd17
commit aacd539f8f
1 changed files with 8 additions and 8 deletions

View File

@ -117,13 +117,13 @@ public abstract class PriorityQueue<T> implements Iterable<T> {
* ArrayIndexOutOfBoundsException} is thrown. * ArrayIndexOutOfBoundsException} is thrown.
*/ */
public void addAll(Collection<T> elements) { public void addAll(Collection<T> elements) {
int s = size; int size = this.size;
if (s + elements.size() > this.maxSize) { if (size + elements.size() > this.maxSize) {
throw new ArrayIndexOutOfBoundsException( throw new ArrayIndexOutOfBoundsException(
"Cannot add " "Cannot add "
+ elements.size() + elements.size()
+ " elements to a queue with remaining capacity: " + " elements to a queue with remaining capacity: "
+ (maxSize - s)); + (maxSize - size));
} }
// Heap with size S always takes first S elements of the array, // Heap with size S always takes first S elements of the array,
@ -131,15 +131,15 @@ public abstract class PriorityQueue<T> implements Iterable<T> {
Iterator<T> iterator = elements.iterator(); Iterator<T> iterator = elements.iterator();
var heap = this.heap; var heap = this.heap;
while (iterator.hasNext()) { while (iterator.hasNext()) {
heap[s + 1] = iterator.next(); heap[size + 1] = iterator.next();
s++; size++;
} }
// The loop goes down to 1 as heap is 1-based not 0-based. // The loop goes down to 1 as heap is 1-based not 0-based.
for (int i = (s >>> 1); i >= 1; i--) { for (int i = (size >>> 1); i >= 1; i--) {
downHeap(i, heap, s); downHeap(i, heap, size);
} }
this.size = s; this.size = size;
} }
/** /**