@@ -133,27 +133,6 @@ module {
133133 priorityQueue : PriorityQueue < T > ,
134134 compare : (T , T ) -> Order . Order ,
135135 element : T
136- ) {
137- let heap = priorityQueue. heap;
138- List . add(heap, element);
139- var index = List . size(heap) - 1 ;
140- while (index > 0 ) {
141- switch (compare(List . at(heap, index), List . at(heap, (index - 1 ) / 2 ))) {
142- case (#greater) {
143- swapHeapElements(heap, index, (index - 1 ) / 2 );
144- index := (index - 1 ) / 2
145- };
146- case _ return
147- }
148- }
149- };
150-
151- // Optimized version.
152- // Main optimization is using the hole technique to avoid copies.
153- public func pushBetter< T > (
154- priorityQueue : PriorityQueue < T > ,
155- compare : (T , T ) -> Order . Order ,
156- element : T
157136 ) {
158137 let heap = priorityQueue. heap;
159138 List . add(heap, element);
@@ -206,39 +185,6 @@ module {
206185 public func pop< T > (
207186 priorityQueue : PriorityQueue < T > ,
208187 compare : (T , T ) -> Order . Order
209- ) : ?T {
210- let heap = priorityQueue. heap;
211- if (List . isEmpty(heap)) {
212- return null
213- };
214- let lastIndex = List . size(heap) - 1 ;
215- swapHeapElements(heap, 0 , lastIndex);
216- var index = 0 ;
217- loop {
218- var best = index;
219- let left = 2 * index + 1 ;
220- if (left < lastIndex and compare(List . at(heap, left), List . at(heap, best)) == #greater) {
221- best := left
222- };
223- let right = left + 1 ;
224- if (right < lastIndex and compare(List . at(heap, right), List . at(heap, best)) == #greater) {
225- best := right
226- };
227- if (best == index) {
228- return List . removeLast(heap)
229- };
230- swapHeapElements(heap, index, best);
231- index := best
232- }
233- };
234-
235- // Optimized version.
236- // Main optimization is using the hole technique to avoid copies.
237- // N.B.: the control flow could be slightly optimized further, but the difference in speed
238- // is minimal while the impact on readability would be significant.
239- public func popBetter< T > (
240- priorityQueue : PriorityQueue < T > ,
241- compare : (T , T ) -> Order . Order
242188 ) : ?T {
243189 let heap = priorityQueue. heap;
244190 if (List . isEmpty(heap)) {
@@ -276,13 +222,5 @@ module {
276222 List . put(heap, index, bestElem);
277223 index := best
278224 }
279- };
280-
281- // / Swaps two elements in the heap at the given indices.
282- // / Internal helper function.
283- func swapHeapElements< T > (heap : Types . List < T > , id1 : Nat , id2 : Nat ) {
284- let aux = List . at(heap, id1);
285- List . put(heap, id1, List . at(heap, id2));
286- List . put(heap, id2, aux)
287225 }
288226}
0 commit comments