| OLD | NEW |
| (Empty) |
| 1 dart_library.library('collection/src/priority_queue', null, /* Imports */[ | |
| 2 'dart/_runtime', | |
| 3 'dart/core', | |
| 4 'collection/src/utils', | |
| 5 'dart/collection' | |
| 6 ], /* Lazy imports */[ | |
| 7 ], function(exports, dart, core, utils, collection) { | |
| 8 'use strict'; | |
| 9 let dartx = dart.dartx; | |
| 10 const PriorityQueue$ = dart.generic(function(E) { | |
| 11 class PriorityQueue extends core.Object { | |
| 12 static new(comparison) { | |
| 13 return new (HeapPriorityQueue$(E))(comparison); | |
| 14 } | |
| 15 } | |
| 16 dart.setSignature(PriorityQueue, { | |
| 17 constructors: () => ({new: [PriorityQueue$(E), [], [dart.functionType(core
.int, [E, E])]]}) | |
| 18 }); | |
| 19 return PriorityQueue; | |
| 20 }); | |
| 21 let PriorityQueue = PriorityQueue$(); | |
| 22 const _queue = Symbol('_queue'); | |
| 23 const _length = Symbol('_length'); | |
| 24 const _add = Symbol('_add'); | |
| 25 const _locate = Symbol('_locate'); | |
| 26 const _removeLast = Symbol('_removeLast'); | |
| 27 const _bubbleUp = Symbol('_bubbleUp'); | |
| 28 const _bubbleDown = Symbol('_bubbleDown'); | |
| 29 const _grow = Symbol('_grow'); | |
| 30 const HeapPriorityQueue$ = dart.generic(function(E) { | |
| 31 class HeapPriorityQueue extends core.Object { | |
| 32 HeapPriorityQueue(comparison) { | |
| 33 if (comparison === void 0) comparison = null; | |
| 34 this[_queue] = core.List$(E).new(HeapPriorityQueue$()._INITIAL_CAPACITY)
; | |
| 35 this.comparison = (() => { | |
| 36 let l = comparison; | |
| 37 return l != null ? l : utils.defaultCompare(); | |
| 38 })(); | |
| 39 this[_length] = 0; | |
| 40 } | |
| 41 add(element) { | |
| 42 dart.as(element, E); | |
| 43 this[_add](element); | |
| 44 } | |
| 45 addAll(elements) { | |
| 46 dart.as(elements, core.Iterable$(E)); | |
| 47 for (let element of elements) { | |
| 48 this[_add](element); | |
| 49 } | |
| 50 } | |
| 51 clear() { | |
| 52 this[_queue] = dart.const(dart.list([], E)); | |
| 53 this[_length] = 0; | |
| 54 } | |
| 55 contains(object) { | |
| 56 dart.as(object, E); | |
| 57 return dart.notNull(this[_locate](object)) >= 0; | |
| 58 } | |
| 59 get first() { | |
| 60 if (this[_length] == 0) dart.throw(new core.StateError("No such element"
)); | |
| 61 return this[_queue][dartx.get](0); | |
| 62 } | |
| 63 get isEmpty() { | |
| 64 return this[_length] == 0; | |
| 65 } | |
| 66 get isNotEmpty() { | |
| 67 return this[_length] != 0; | |
| 68 } | |
| 69 get length() { | |
| 70 return this[_length]; | |
| 71 } | |
| 72 remove(element) { | |
| 73 dart.as(element, E); | |
| 74 let index = this[_locate](element); | |
| 75 if (dart.notNull(index) < 0) return false; | |
| 76 let last = this[_removeLast](); | |
| 77 if (dart.notNull(index) < dart.notNull(this[_length])) { | |
| 78 let comp = this.comparison(last, element); | |
| 79 if (dart.notNull(comp) <= 0) { | |
| 80 this[_bubbleUp](last, index); | |
| 81 } else { | |
| 82 this[_bubbleDown](last, index); | |
| 83 } | |
| 84 } | |
| 85 return true; | |
| 86 } | |
| 87 removeAll() { | |
| 88 let result = this[_queue]; | |
| 89 let length = this[_length]; | |
| 90 this[_queue] = dart.const(dart.list([], E)); | |
| 91 this[_length] = 0; | |
| 92 return result[dartx.take](length); | |
| 93 } | |
| 94 removeFirst() { | |
| 95 if (this[_length] == 0) dart.throw(new core.StateError("No such element"
)); | |
| 96 let result = this[_queue][dartx.get](0); | |
| 97 let last = this[_removeLast](); | |
| 98 if (dart.notNull(this[_length]) > 0) { | |
| 99 this[_bubbleDown](last, 0); | |
| 100 } | |
| 101 return result; | |
| 102 } | |
| 103 toList() { | |
| 104 let list = core.List$(E).new(); | |
| 105 list[dartx.length] = this[_length]; | |
| 106 list[dartx.setRange](0, this[_length], this[_queue]); | |
| 107 list[dartx.sort](this.comparison); | |
| 108 return list; | |
| 109 } | |
| 110 toSet() { | |
| 111 let set = new (collection.SplayTreeSet$(E))(this.comparison); | |
| 112 for (let i = 0; i < dart.notNull(this[_length]); i++) { | |
| 113 set.add(this[_queue][dartx.get](i)); | |
| 114 } | |
| 115 return set; | |
| 116 } | |
| 117 toString() { | |
| 118 return dart.toString(this[_queue][dartx.take](this[_length])); | |
| 119 } | |
| 120 [_add](element) { | |
| 121 dart.as(element, E); | |
| 122 if (this[_length] == this[_queue][dartx.length]) this[_grow](); | |
| 123 this[_bubbleUp](element, (() => { | |
| 124 let x = this[_length]; | |
| 125 this[_length] = dart.notNull(x) + 1; | |
| 126 return x; | |
| 127 })()); | |
| 128 } | |
| 129 [_locate](object) { | |
| 130 dart.as(object, E); | |
| 131 if (this[_length] == 0) return -1; | |
| 132 let position = 1; | |
| 133 do { | |
| 134 let index = position - 1; | |
| 135 let element = this[_queue][dartx.get](index); | |
| 136 let comp = this.comparison(element, object); | |
| 137 if (comp == 0) return index; | |
| 138 if (dart.notNull(comp) < 0) { | |
| 139 let leftChildPosition = position * 2; | |
| 140 if (leftChildPosition <= dart.notNull(this[_length])) { | |
| 141 position = leftChildPosition; | |
| 142 continue; | |
| 143 } | |
| 144 } | |
| 145 do { | |
| 146 while (dart.notNull(position[dartx.isOdd])) { | |
| 147 position = position >> 1; | |
| 148 } | |
| 149 position = position + 1; | |
| 150 } while (position > dart.notNull(this[_length])); | |
| 151 } while (position != 1); | |
| 152 return -1; | |
| 153 } | |
| 154 [_removeLast]() { | |
| 155 let newLength = dart.notNull(this[_length]) - 1; | |
| 156 let last = this[_queue][dartx.get](newLength); | |
| 157 this[_queue][dartx.set](newLength, null); | |
| 158 this[_length] = newLength; | |
| 159 return last; | |
| 160 } | |
| 161 [_bubbleUp](element, index) { | |
| 162 dart.as(element, E); | |
| 163 while (dart.notNull(index) > 0) { | |
| 164 let parentIndex = ((dart.notNull(index) - 1) / 2)[dartx.truncate](); | |
| 165 let parent = this[_queue][dartx.get](parentIndex); | |
| 166 if (dart.notNull(this.comparison(element, parent)) > 0) break; | |
| 167 this[_queue][dartx.set](index, parent); | |
| 168 index = parentIndex; | |
| 169 } | |
| 170 this[_queue][dartx.set](index, element); | |
| 171 } | |
| 172 [_bubbleDown](element, index) { | |
| 173 dart.as(element, E); | |
| 174 let rightChildIndex = dart.notNull(index) * 2 + 2; | |
| 175 while (rightChildIndex < dart.notNull(this[_length])) { | |
| 176 let leftChildIndex = rightChildIndex - 1; | |
| 177 let leftChild = this[_queue][dartx.get](leftChildIndex); | |
| 178 let rightChild = this[_queue][dartx.get](rightChildIndex); | |
| 179 let comp = this.comparison(leftChild, rightChild); | |
| 180 let minChildIndex = null; | |
| 181 let minChild = null; | |
| 182 if (dart.notNull(comp) < 0) { | |
| 183 minChild = leftChild; | |
| 184 minChildIndex = leftChildIndex; | |
| 185 } else { | |
| 186 minChild = rightChild; | |
| 187 minChildIndex = rightChildIndex; | |
| 188 } | |
| 189 comp = this.comparison(element, minChild); | |
| 190 if (dart.notNull(comp) <= 0) { | |
| 191 this[_queue][dartx.set](index, element); | |
| 192 return; | |
| 193 } | |
| 194 this[_queue][dartx.set](index, minChild); | |
| 195 index = minChildIndex; | |
| 196 rightChildIndex = dart.notNull(index) * 2 + 2; | |
| 197 } | |
| 198 let leftChildIndex = rightChildIndex - 1; | |
| 199 if (leftChildIndex < dart.notNull(this[_length])) { | |
| 200 let child = this[_queue][dartx.get](leftChildIndex); | |
| 201 let comp = this.comparison(element, child); | |
| 202 if (dart.notNull(comp) > 0) { | |
| 203 this[_queue][dartx.set](index, child); | |
| 204 index = leftChildIndex; | |
| 205 } | |
| 206 } | |
| 207 this[_queue][dartx.set](index, element); | |
| 208 } | |
| 209 [_grow]() { | |
| 210 let newCapacity = dart.notNull(this[_queue][dartx.length]) * 2 + 1; | |
| 211 if (dart.notNull(newCapacity) < dart.notNull(HeapPriorityQueue$()._INITI
AL_CAPACITY)) newCapacity = HeapPriorityQueue$()._INITIAL_CAPACITY; | |
| 212 let newQueue = core.List$(E).new(newCapacity); | |
| 213 newQueue[dartx.setRange](0, this[_length], this[_queue]); | |
| 214 this[_queue] = newQueue; | |
| 215 } | |
| 216 } | |
| 217 HeapPriorityQueue[dart.implements] = () => [PriorityQueue$(E)]; | |
| 218 dart.setSignature(HeapPriorityQueue, { | |
| 219 constructors: () => ({HeapPriorityQueue: [HeapPriorityQueue$(E), [], [dart
.functionType(core.int, [E, E])]]}), | |
| 220 methods: () => ({ | |
| 221 add: [dart.void, [E]], | |
| 222 addAll: [dart.void, [core.Iterable$(E)]], | |
| 223 clear: [dart.void, []], | |
| 224 contains: [core.bool, [E]], | |
| 225 remove: [core.bool, [E]], | |
| 226 removeAll: [core.Iterable$(E), []], | |
| 227 removeFirst: [E, []], | |
| 228 toList: [core.List$(E), []], | |
| 229 toSet: [core.Set$(E), []], | |
| 230 [_add]: [dart.void, [E]], | |
| 231 [_locate]: [core.int, [E]], | |
| 232 [_removeLast]: [E, []], | |
| 233 [_bubbleUp]: [dart.void, [E, core.int]], | |
| 234 [_bubbleDown]: [dart.void, [E, core.int]], | |
| 235 [_grow]: [dart.void, []] | |
| 236 }) | |
| 237 }); | |
| 238 HeapPriorityQueue._INITIAL_CAPACITY = 7; | |
| 239 return HeapPriorityQueue; | |
| 240 }); | |
| 241 let HeapPriorityQueue = HeapPriorityQueue$(); | |
| 242 // Exports: | |
| 243 exports.PriorityQueue$ = PriorityQueue$; | |
| 244 exports.PriorityQueue = PriorityQueue; | |
| 245 exports.HeapPriorityQueue$ = HeapPriorityQueue$; | |
| 246 exports.HeapPriorityQueue = HeapPriorityQueue; | |
| 247 }); | |
| OLD | NEW |