OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import "dart:collection"; | 5 import "dart:collection"; |
6 | 6 |
7 /// A priority queue is a priority based work-list of elements. | 7 /// A priority queue is a priority based work-list of elements. |
8 /// | 8 /// |
9 /// The queue allows adding elements, and removing them again in priority order. | 9 /// The queue allows adding elements, and removing them again in priority order. |
10 abstract class PriorityQueue<E> { | 10 abstract class PriorityQueue<E> { |
11 /// Creates an empty [PriorityQueue]. | 11 /// Creates an empty [PriorityQueue]. |
12 /// | 12 /// |
13 /// The created [PriorityQueue] is a plain [HeapPriorityQueue]. | 13 /// The created [PriorityQueue] is a plain [HeapPriorityQueue]. |
14 /// | 14 /// |
15 /// The [comparison] is a [Comparator] used to compare the priority of | 15 /// The [comparison] is a [Comparator] used to compare the priority of |
16 /// elements. An element that compares as less than another element has | 16 /// elements. An element that compares as less than another element has |
17 /// a higher priority. | 17 /// a higher priority. |
18 /// | 18 /// |
19 /// If [comparison] is omitted, it defaults to [Comparable.compare]. | 19 /// If [comparison] is omitted, it defaults to [Comparable.compare]. If this |
| 20 /// is the case, `E` must implement [Comparable], and this is checked at |
| 21 /// runtime for every comparison. |
20 factory PriorityQueue([int comparison(E e1, E e2)]) = HeapPriorityQueue<E>; | 22 factory PriorityQueue([int comparison(E e1, E e2)]) = HeapPriorityQueue<E>; |
21 | 23 |
22 /// Number of elements in the queue. | 24 /// Number of elements in the queue. |
23 int get length; | 25 int get length; |
24 | 26 |
25 /// Whether the queue is empty. | 27 /// Whether the queue is empty. |
26 bool get isEmpty; | 28 bool get isEmpty; |
27 | 29 |
28 /// Whether the queue has any elements. | 30 /// Whether the queue has any elements. |
29 bool get isNotEmpty; | 31 bool get isNotEmpty; |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 /// an expected O(n*log(n)) time. | 116 /// an expected O(n*log(n)) time. |
115 class HeapPriorityQueue<E> implements PriorityQueue<E> { | 117 class HeapPriorityQueue<E> implements PriorityQueue<E> { |
116 /// Initial capacity of a queue when created, or when added to after a | 118 /// Initial capacity of a queue when created, or when added to after a |
117 /// [clear]. | 119 /// [clear]. |
118 /// | 120 /// |
119 /// Number can be any positive value. Picking a size that gives a whole | 121 /// Number can be any positive value. Picking a size that gives a whole |
120 /// number of "tree levels" in the heap is only done for aesthetic reasons. | 122 /// number of "tree levels" in the heap is only done for aesthetic reasons. |
121 static const int _INITIAL_CAPACITY = 7; | 123 static const int _INITIAL_CAPACITY = 7; |
122 | 124 |
123 /// The comparison being used to compare the priority of elements. | 125 /// The comparison being used to compare the priority of elements. |
124 final Comparator comparison; | 126 final Comparator<E> comparison; |
125 | 127 |
126 /// List implementation of a heap. | 128 /// List implementation of a heap. |
127 List<E> _queue = new List<E>(_INITIAL_CAPACITY); | 129 List<E> _queue = new List<E>(_INITIAL_CAPACITY); |
128 | 130 |
129 /// Number of elements in queue. | 131 /// Number of elements in queue. |
130 /// | 132 /// |
131 /// The heap is implemented in the first [_length] entries of [_queue]. | 133 /// The heap is implemented in the first [_length] entries of [_queue]. |
132 int _length = 0; | 134 int _length = 0; |
133 | 135 |
134 /// Create a new priority queue. | 136 /// Create a new priority queue. |
135 /// | 137 /// |
136 /// The [comparison] is a [Comparator] used to compare the priority of | 138 /// The [comparison] is a [Comparator] used to compare the priority of |
137 /// elements. An element that compares as less than another element has | 139 /// elements. An element that compares as less than another element has |
138 /// a higher priority. | 140 /// a higher priority. |
139 /// | 141 /// |
140 /// If [comparison] is omitted, it defaults to [Comparable.compare]. | 142 /// If [comparison] is omitted, it defaults to [Comparable.compare]. If this |
| 143 /// is the case, `E` must implement [Comparable], and this is checked at |
| 144 /// runtime for every comparison. |
141 HeapPriorityQueue([int comparison(E e1, E e2)]) | 145 HeapPriorityQueue([int comparison(E e1, E e2)]) |
142 : comparison = (comparison != null) ? comparison : Comparable.compare; | 146 : comparison = comparison ?? |
| 147 ((e1, e2) => (e1 as Comparable).compareTo(e2)); |
143 | 148 |
144 void add(E element) { | 149 void add(E element) { |
145 _add(element); | 150 _add(element); |
146 } | 151 } |
147 | 152 |
148 void addAll(Iterable<E> elements) { | 153 void addAll(Iterable<E> elements) { |
149 for (E element in elements) { | 154 for (E element in elements) { |
150 _add(element); | 155 _add(element); |
151 } | 156 } |
152 } | 157 } |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 /// | 350 /// |
346 /// Called when the list is full. | 351 /// Called when the list is full. |
347 void _grow() { | 352 void _grow() { |
348 int newCapacity = _queue.length * 2 + 1; | 353 int newCapacity = _queue.length * 2 + 1; |
349 if (newCapacity < _INITIAL_CAPACITY) newCapacity = _INITIAL_CAPACITY; | 354 if (newCapacity < _INITIAL_CAPACITY) newCapacity = _INITIAL_CAPACITY; |
350 List<E> newQueue = new List<E>(newCapacity); | 355 List<E> newQueue = new List<E>(newCapacity); |
351 newQueue.setRange(0, _length, _queue); | 356 newQueue.setRange(0, _length, _queue); |
352 _queue = newQueue; | 357 _queue = newQueue; |
353 } | 358 } |
354 } | 359 } |
OLD | NEW |