Data update
This commit is contained in:
parent
29a5eea0d4
commit
5c1bb7bfa9
2011 changed files with 35081 additions and 3229 deletions
|
|
@ -24,21 +24,20 @@ const Task = struct {
|
|||
///
|
||||
/// fn(T, T) bool
|
||||
const Comparator = struct {
|
||||
fn maxCompare(a: Task, b: Task) bool {
|
||||
return a.priority > b.priority;
|
||||
fn maxCompare(_: void, a: Task, b: Task) std.math.Order {
|
||||
return std.math.order(a.priority, b.priority);
|
||||
}
|
||||
|
||||
fn minCompare(a: Task, b: Task) bool {
|
||||
return a.priority < b.priority;
|
||||
fn minCompare(_: void, a: Task, b: Task) std.math.Order {
|
||||
return std.math.order(a.priority, b.priority);
|
||||
}
|
||||
};
|
||||
|
||||
test "priority queue (max heap)" {
|
||||
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
||||
defer arena.deinit();
|
||||
var allocator = &arena.allocator;
|
||||
|
||||
var pq = PriorityQueue(Task).init(allocator, Comparator.maxCompare);
|
||||
const allocator = arena.allocator();
|
||||
var pq = PriorityQueue(Task, void, Comparator.maxCompare).init(allocator, {});
|
||||
defer pq.deinit();
|
||||
|
||||
try pq.add(Task.init(3, "Clear drains"));
|
||||
|
|
@ -46,14 +45,14 @@ test "priority queue (max heap)" {
|
|||
try pq.add(Task.init(5, "Make tea"));
|
||||
try pq.add(Task.init(1, "Solve RC tasks"));
|
||||
try pq.add(Task.init(2, "Tax returns"));
|
||||
testing.expectEqual(pq.count(), 5);
|
||||
try testing.expectEqual(pq.count(), 5);
|
||||
|
||||
std.debug.print("\n", .{});
|
||||
|
||||
// execute the tasks in decreasing order of priority
|
||||
while (pq.count() != 0) {
|
||||
const task = pq.remove();
|
||||
std.debug.print("Executing: {} (priority {})\n", .{ task.name, task.priority });
|
||||
std.debug.print("Executing: {s} (priority {d})\n", .{ task.name, task.priority });
|
||||
}
|
||||
std.debug.print("\n", .{});
|
||||
}
|
||||
|
|
@ -61,9 +60,8 @@ test "priority queue (max heap)" {
|
|||
test "priority queue (min heap)" {
|
||||
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
||||
defer arena.deinit();
|
||||
var allocator = &arena.allocator;
|
||||
|
||||
var pq = PriorityQueue(Task).init(allocator, Comparator.minCompare);
|
||||
const allocator = arena.allocator();
|
||||
var pq = PriorityQueue(Task, void, Comparator.minCompare).init(allocator, {});
|
||||
defer pq.deinit();
|
||||
|
||||
try pq.add(Task.init(3, "Clear drains"));
|
||||
|
|
@ -71,14 +69,14 @@ test "priority queue (min heap)" {
|
|||
try pq.add(Task.init(5, "Make tea"));
|
||||
try pq.add(Task.init(1, "Solve RC tasks"));
|
||||
try pq.add(Task.init(2, "Tax returns"));
|
||||
testing.expectEqual(pq.count(), 5);
|
||||
try testing.expectEqual(pq.count(), 5);
|
||||
|
||||
std.debug.print("\n", .{});
|
||||
|
||||
// execute the tasks in increasing order of priority
|
||||
while (pq.count() != 0) {
|
||||
const task = pq.remove();
|
||||
std.debug.print("Executing: {} (priority {})\n", .{ task.name, task.priority });
|
||||
std.debug.print("Executing: {s} (priority {d})\n", .{ task.name, task.priority });
|
||||
}
|
||||
std.debug.print("\n", .{});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue