@@ -22,9 +22,9 @@ import 'utils.dart';
22
22
/// always give equal objects the same priority,
23
23
/// otherwise [contains] or [remove] might not work correctly.
24
24
abstract class PriorityQueue <E > {
25
- /// Creates an empty [PriorityQueue] .
25
+ /// Creates an empty priority queue .
26
26
///
27
- /// The created [ PriorityQueue] is a plain [HeapPriorityQueue] .
27
+ /// The created ` PriorityQueue` is a plain [HeapPriorityQueue] .
28
28
///
29
29
/// The [comparison] is a [Comparator] used to compare the priority of
30
30
/// elements. An element that compares as less than another element has
@@ -36,15 +36,16 @@ abstract class PriorityQueue<E> {
36
36
factory PriorityQueue ([int Function (E , E )? comparison]) =
37
37
HeapPriorityQueue <E >;
38
38
39
- /// Create a new priority queue .
39
+ /// Creates a new [HeapPriorityQueue] containing [elements] .
40
40
///
41
41
/// The [comparison] is a [Comparator] used to compare the priority of
42
42
/// elements. An element that compares as less than another element has
43
43
/// a higher priority.
44
44
///
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.
48
49
factory PriorityQueue .of (
49
50
Iterable <E > elements, int Function (E , E ) comparison) =
50
51
HeapPriorityQueue <E >.of;
@@ -164,23 +165,30 @@ abstract class PriorityQueue<E> {
164
165
///
165
166
/// The elements are kept in a heap structure,
166
167
/// 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.
169
170
///
170
171
/// * 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.
174
176
/// * The [first] getter takes constant time, O(1).
175
177
/// * The [clear] and [removeAll] methods also take constant time, O(1).
176
178
/// * 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* ))
179
181
/// time.
180
182
/// * The [toUnorderedList] operation copies, but does not sort, the elements,
181
183
/// 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.
184
192
class HeapPriorityQueue <E > implements PriorityQueue <E > {
185
193
/// The comparison being used to compare the priority of elements.
186
194
final int Function (E , E ) comparison;
@@ -316,16 +324,14 @@ class HeapPriorityQueue<E> implements PriorityQueue<E> {
316
324
317
325
/// Removes all the elements from this queue and returns them.
318
326
///
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.
323
329
@override
324
- Iterable <E > removeAll () {
330
+ List <E > removeAll () {
325
331
_modificationCount++ ;
326
332
var result = _queue;
327
333
_queue = < E > [];
328
- return result. skip ( 0 ); // Hide list nature.
334
+ return result;
329
335
}
330
336
331
337
@override
@@ -401,7 +407,7 @@ class HeapPriorityQueue<E> implements PriorityQueue<E> {
401
407
} while (
402
408
position > _queue.length); // Happens if last element is a left child.
403
409
} while (position != 1 ); // At root again. Happens for right-most element.
404
- return - 1 ;
410
+ return - 1 ;
405
411
}
406
412
407
413
/// Place [element] in heap at [index] or above.
0 commit comments