Skip to content

Commit 52ab8c9

Browse files
committed
Adds [Heap]PriorityQueue.of constructor.
Introduces efficient "heapify" algorithm for converting an unsorted list to a heap-sorted list, using it for the `of` constructor, and after a large `addAll` operation, when it's presumed faster than just bubbling down all the new elements. Also rewrites `HeapPriorityQueue` to use a growable list as backing array, instead of implementing the same thing using the double-when-full algorithm, and still having to deal with nullable cells. The platform growable list implementation is assumed to efficiently avoid some of those `null` checks.
1 parent 44bb5c6 commit 52ab8c9

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

pkgs/collection/lib/src/priority_queue.dart

+4-2
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ abstract class PriorityQueue<E> {
181181
/// time.
182182
/// * The [toUnorderedList] operation copies, but does not sort, the elements,
183183
/// and is linear, O(n).
184-
/// * The [toSet] operation effectively adds each element to the new
184+
/// * The [toSet] operation effectively adds each element to the new
185185
/// [SplayTreeSet], taking an expected O(n * log(*N*)) time.
186186
///
187187
/// The [comparison] function is used to order elements, with earlier elements
@@ -326,6 +326,8 @@ class HeapPriorityQueue<E> implements PriorityQueue<E> {
326326
///
327327
/// The [HeapPriorityQueue] returns a [List] of its elements,
328328
/// with no guaranteed order.
329+
///
330+
/// If the elements are not needed, use [clear] instead.
329331
@override
330332
List<E> removeAll() {
331333
_modificationCount++;
@@ -407,7 +409,7 @@ class HeapPriorityQueue<E> implements PriorityQueue<E> {
407409
} while (
408410
position > _queue.length); // Happens if last element is a left child.
409411
} while (position != 1); // At root again. Happens for right-most element.
410-
return -1;
412+
return -1;
411413
}
412414

413415
/// Place [element] in heap at [index] or above.

0 commit comments

Comments
 (0)