@@ -22,9 +22,9 @@ import 'utils.dart';
2222/// always give equal objects the same priority,
2323/// otherwise [contains] or [remove] might not work correctly.
2424abstract class PriorityQueue <E > {
25- /// Creates an empty [PriorityQueue] .
25+ /// Creates an empty priority queue .
2626 ///
27- /// The created [ PriorityQueue] is a plain [HeapPriorityQueue] .
27+ /// The created ` PriorityQueue` is a plain [HeapPriorityQueue] .
2828 ///
2929 /// The [comparison] is a [Comparator] used to compare the priority of
3030 /// elements. An element that compares as less than another element has
@@ -36,15 +36,16 @@ abstract class PriorityQueue<E> {
3636 factory PriorityQueue ([int Function (E , E )? comparison]) =
3737 HeapPriorityQueue <E >;
3838
39- /// Create a new priority queue .
39+ /// Creates a new [HeapPriorityQueue] containing [elements] .
4040 ///
4141 /// The [comparison] is a [Comparator] used to compare the priority of
4242 /// elements. An element that compares as less than another element has
4343 /// a higher priority.
4444 ///
45- /// If [comparison] is omitted, it defaults to [Comparable.compare] . If this
46- /// is the case, `E` must implement [Comparable] , and this is checked at
47- /// runtime for every comparison.
45+ /// Unlike [PriorityQueue.new] , the [comparison] cannot be omitted.
46+ /// If the elements are comparable to each other, use [Comparable.compare]
47+ /// as the comparison function, or use a more specialized function
48+ /// if one is available.
4849 factory PriorityQueue .of (
4950 Iterable <E > elements, int Function (E , E ) comparison) =
5051 HeapPriorityQueue <E >.of;
@@ -164,23 +165,30 @@ abstract class PriorityQueue<E> {
164165///
165166/// The elements are kept in a heap structure,
166167/// where the element with the highest priority is immediately accessible,
167- /// and modifying a single element takes
168- /// logarithmic time in the number of elements on average .
168+ /// and modifying a single element takes, on average,
169+ /// logarithmic time in the number of elements.
169170///
170171/// * The [add] and [removeFirst] operations take amortized logarithmic time,
171- /// O(log(n)), but may occasionally take linear time when growing the capacity
172- /// of the heap.
173- /// * The [addAll] operation works as doing repeated [add] operations.
172+ /// O(log(*N*)) where *N* is the number of elements, but may occasionally
173+ /// take linear time when growing the capacity of the heap.
174+ /// * The [addAll] operation works by doing repeated [add] operations.
175+ /// May be more efficient in some cases.
174176/// * The [first] getter takes constant time, O(1).
175177/// * The [clear] and [removeAll] methods also take constant time, O(1).
176178/// * The [contains] and [remove] operations may need to search the entire
177- /// queue for the elements, taking O(n ) time.
178- /// * The [toList] operation effectively sorts the elements, taking O(n* log(n ))
179+ /// queue for the elements, taking O(*N* ) time.
180+ /// * The [toList] operation effectively sorts the elements, taking O(n * log(*N* ))
179181/// time.
180182/// * The [toUnorderedList] operation copies, but does not sort, the elements,
181183/// and is linear, O(n).
182- /// * The [toSet] operation effectively adds each element to the new set, taking
183- /// an expected O(n*log(n)) time.
184+ /// * The [toSet] operation effectively adds each element to the new
185+ /// [SplayTreeSet] , taking an expected O(n * log(*N*)) time.
186+ ///
187+ /// The [comparison] function is used to order elements, with earlier elements
188+ /// having higher priority. That is, elements are extracted from the queue
189+ /// in ascending [comparison] order.
190+ /// If two elements have the same priority, their ordering is unspecified
191+ /// and may be arbitary.
184192class HeapPriorityQueue <E > implements PriorityQueue <E > {
185193 /// The comparison being used to compare the priority of elements.
186194 final int Function (E , E ) comparison;
@@ -316,16 +324,14 @@ class HeapPriorityQueue<E> implements PriorityQueue<E> {
316324
317325 /// Removes all the elements from this queue and returns them.
318326 ///
319- /// The returned iterable has no specified order.
320- /// The operation does not copy the elements,
321- /// but instead keeps them in the existing heap structure,
322- /// and iterates over that directly.
327+ /// The [HeapPriorityQueue] returns a [List] of its elements,
328+ /// with no guaranteed order.
323329 @override
324- Iterable <E > removeAll () {
330+ List <E > removeAll () {
325331 _modificationCount++ ;
326332 var result = _queue;
327333 _queue = < E > [];
328- return result. skip ( 0 ); // Hide list nature.
334+ return result;
329335 }
330336
331337 @override
@@ -401,7 +407,7 @@ class HeapPriorityQueue<E> implements PriorityQueue<E> {
401407 } while (
402408 position > _queue.length); // Happens if last element is a left child.
403409 } while (position != 1 ); // At root again. Happens for right-most element.
404- return - 1 ;
410+ return - 1 ;
405411 }
406412
407413 /// Place [element] in heap at [index] or above.
0 commit comments