Skip to content

Commit 664d092

Browse files
author
Andrei Constantinescu
committed
Remove inefficient push and pop operations (and benchmarks for them).
1 parent 95f5223 commit 664d092

File tree

3 files changed

+0
-90
lines changed

3 files changed

+0
-90
lines changed

bench/PriorityQueues.bench.mo

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -192,32 +192,6 @@ module {
192192
}
193193
}
194194
),
195-
(
196-
"A2) PriorityQueue better push",
197-
func(ops : [PriorityQueueUpdateOperation<Nat>]) {
198-
let priorityQueue = PriorityQueue.empty<Nat>();
199-
for (op in ops.values()) {
200-
switch (op) {
201-
case (#Push element) PriorityQueue.pushBetter(priorityQueue, Nat.compare, element);
202-
case (#Pop) ignore PriorityQueue.pop(priorityQueue, Nat.compare);
203-
case (#Clear) PriorityQueue.clear(priorityQueue)
204-
}
205-
}
206-
}
207-
),
208-
(
209-
"A3) PriorityQueue better push, better pop",
210-
func(ops : [PriorityQueueUpdateOperation<Nat>]) {
211-
let priorityQueue = PriorityQueue.empty<Nat>();
212-
for (op in ops.values()) {
213-
switch (op) {
214-
case (#Push element) PriorityQueue.pushBetter(priorityQueue, Nat.compare, element);
215-
case (#Pop) ignore PriorityQueue.popBetter(priorityQueue, Nat.compare);
216-
case (#Clear) PriorityQueue.clear(priorityQueue)
217-
}
218-
}
219-
}
220-
),
221195
(
222196
"B) PriorityQueueSet",
223197
func(ops : [PriorityQueueUpdateOperation<Nat>]) {

src/PriorityQueue.mo

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

validation/api/api.lock.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -913,10 +913,8 @@
913913
"public func isEmpty<T>(priorityQueue : PriorityQueue<T>) : Bool",
914914
"public func peek<T>(priorityQueue : PriorityQueue<T>) : ?T",
915915
"public func pop<T>( priorityQueue : PriorityQueue<T>, compare : (T, T) -> Order.Order ) : ?T",
916-
"public func popBetter<T>( priorityQueue : PriorityQueue<T>, compare : (T, T) -> Order.Order ) : ?T",
917916
"public type PriorityQueue<T>",
918917
"public func push<T>( priorityQueue : PriorityQueue<T>, compare : (T, T) -> Order.Order, element : T )",
919-
"public func pushBetter<T>( priorityQueue : PriorityQueue<T>, compare : (T, T) -> Order.Order, element : T )",
920918
"public func singleton<T>(element : T) : PriorityQueue<T>",
921919
"public func size<T>(priorityQueue : PriorityQueue<T>) : Nat"
922920
]

0 commit comments

Comments
 (0)