| ... | @@ -8,27 +8,29 @@ const expectEqual = testing.expectEqual; | ... | @@ -8,27 +8,29 @@ const expectEqual = testing.expectEqual; |
| 8 | const expectError = testing.expectError; | 8 | const expectError = testing.expectError; |
| 9 | | 9 | |
| 10 | /// Priority Dequeue for storing generic data. Initialize with `init`. | 10 | /// Priority Dequeue for storing generic data. Initialize with `init`. |
| 11 | /// Provide `compareFn` that returns `Order.lt` when its first | 11 | /// Provide `compareFn` that returns `Order.lt` when its second |
| 12 | /// argument should get min-popped before its second argument, | 12 | /// argument should get min-popped before its third argument, |
| 13 | /// `Order.eq` if the arguments are of equal priority, or `Order.gt` | 13 | /// `Order.eq` if the arguments are of equal priority, or `Order.gt` |
| 14 | /// if the second argument should be min-popped first. | 14 | /// if the third argument should be min-popped second. |
| 15 | /// Popping the max element works in reverse. For example, | 15 | /// Popping the max element works in reverse. For example, |
| 16 | /// to make `popMin` return the smallest number, provide | 16 | /// to make `popMin` return the smallest number, provide |
| 17 | /// `fn lessThan(a: T, b: T) Order { return std.math.order(a, b); }` | 17 | /// `fn lessThan(context: void, a: T, b: T) Order { _ = context; return std.math.order(a, b); }` |
| 18 | pub fn PriorityDequeue(comptime T: type, comptime compareFn: fn (T, T) Order) type { | 18 | pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compareFn: fn (context: Context, a: T, b: T) Order) type { |
| 19 | return struct { | 19 | return struct { |
| 20 | const Self = @This(); | 20 | const Self = @This(); |
| 21 | | 21 | |
| 22 | items: []T, | 22 | items: []T, |
| 23 | len: usize, | 23 | len: usize, |
| 24 | allocator: Allocator, | 24 | allocator: Allocator, |
| | 25 | context: Context, |
| 25 | | 26 | |
| 26 | /// Initialize and return a new priority dequeue. | 27 | /// Initialize and return a new priority dequeue. |
| 27 | pub fn init(allocator: Allocator) Self { | 28 | pub fn init(allocator: Allocator, context: Context) Self { |
| 28 | return Self{ | 29 | return Self{ |
| 29 | .items = &[_]T{}, | 30 | .items = &[_]T{}, |
| 30 | .len = 0, | 31 | .len = 0, |
| 31 | .allocator = allocator, | 32 | .allocator = allocator, |
| | 33 | .context = context, |
| 32 | }; | 34 | }; |
| 33 | } | 35 | } |
| 34 | | 36 | |
| ... | @@ -87,7 +89,7 @@ pub fn PriorityDequeue(comptime T: type, comptime compareFn: fn (T, T) Order) ty | ... | @@ -87,7 +89,7 @@ pub fn PriorityDequeue(comptime T: type, comptime compareFn: fn (T, T) Order) ty |
| 87 | const parent = self.items[parent_index]; | 89 | const parent = self.items[parent_index]; |
| 88 | | 90 | |
| 89 | const min_layer = self.nextIsMinLayer(); | 91 | const min_layer = self.nextIsMinLayer(); |
| 90 | const order = compareFn(child, parent); | 92 | const order = compareFn(self.context, child, parent); |
| 91 | if ((min_layer and order == .gt) or (!min_layer and order == .lt)) { | 93 | if ((min_layer and order == .gt) or (!min_layer and order == .lt)) { |
| 92 | // We must swap the item with it's parent if it is on the "wrong" layer | 94 | // We must swap the item with it's parent if it is on the "wrong" layer |
| 93 | self.items[parent_index] = child; | 95 | self.items[parent_index] = child; |
| ... | @@ -120,7 +122,7 @@ pub fn PriorityDequeue(comptime T: type, comptime compareFn: fn (T, T) Order) ty | ... | @@ -120,7 +122,7 @@ pub fn PriorityDequeue(comptime T: type, comptime compareFn: fn (T, T) Order) ty |
| 120 | const grandparent = self.items[grandparent_index]; | 122 | const grandparent = self.items[grandparent_index]; |
| 121 | | 123 | |
| 122 | // If the grandparent is already better or equal, we have gone as far as we need to | 124 | // If the grandparent is already better or equal, we have gone as far as we need to |
| 123 | if (compareFn(child, grandparent) != target_order) break; | 125 | if (compareFn(self.context, child, grandparent) != target_order) break; |
| 124 | | 126 | |
| 125 | // Otherwise swap the item with it's grandparent | 127 | // Otherwise swap the item with it's grandparent |
| 126 | self.items[grandparent_index] = child; | 128 | self.items[grandparent_index] = child; |
| ... | @@ -215,10 +217,10 @@ pub fn PriorityDequeue(comptime T: type, comptime compareFn: fn (T, T) Order) ty | ... | @@ -215,10 +217,10 @@ pub fn PriorityDequeue(comptime T: type, comptime compareFn: fn (T, T) Order) ty |
| 215 | // Find the best grandchild | 217 | // Find the best grandchild |
| 216 | const best_left = self.bestItemAtIndices(first_grandchild_index, index2, target_order); | 218 | const best_left = self.bestItemAtIndices(first_grandchild_index, index2, target_order); |
| 217 | const best_right = self.bestItemAtIndices(index3, last_grandchild_index, target_order); | 219 | const best_right = self.bestItemAtIndices(index3, last_grandchild_index, target_order); |
| 218 | const best_grandchild = Self.bestItem(best_left, best_right, target_order); | 220 | const best_grandchild = self.bestItem(best_left, best_right, target_order); |
| 219 | | 221 | |
| 220 | // If the item is better than or equal to its best grandchild, we are done | 222 | // If the item is better than or equal to its best grandchild, we are done |
| 221 | if (compareFn(best_grandchild.item, elem) != target_order) return; | 223 | if (compareFn(self.context, best_grandchild.item, elem) != target_order) return; |
| 222 | | 224 | |
| 223 | // Otherwise, swap them | 225 | // Otherwise, swap them |
| 224 | self.items[best_grandchild.index] = elem; | 226 | self.items[best_grandchild.index] = elem; |
| ... | @@ -235,7 +237,7 @@ pub fn PriorityDequeue(comptime T: type, comptime compareFn: fn (T, T) Order) ty | ... | @@ -235,7 +237,7 @@ pub fn PriorityDequeue(comptime T: type, comptime compareFn: fn (T, T) Order) ty |
| 235 | const best_descendent = self.bestDescendent(first_child_index, first_grandchild_index, target_order); | 237 | const best_descendent = self.bestDescendent(first_child_index, first_grandchild_index, target_order); |
| 236 | | 238 | |
| 237 | // If the item is better than or equal to its best descendant, we are done | 239 | // If the item is better than or equal to its best descendant, we are done |
| 238 | if (compareFn(best_descendent.item, elem) != target_order) return; | 240 | if (compareFn(self.context, best_descendent.item, elem) != target_order) return; |
| 239 | | 241 | |
| 240 | // Otherwise swap them | 242 | // Otherwise swap them |
| 241 | self.items[best_descendent.index] = elem; | 243 | self.items[best_descendent.index] = elem; |
| ... | @@ -259,7 +261,7 @@ pub fn PriorityDequeue(comptime T: type, comptime compareFn: fn (T, T) Order) ty | ... | @@ -259,7 +261,7 @@ pub fn PriorityDequeue(comptime T: type, comptime compareFn: fn (T, T) Order) ty |
| 259 | const parent_index = parentIndex(child_index); | 261 | const parent_index = parentIndex(child_index); |
| 260 | const parent = self.items[parent_index]; | 262 | const parent = self.items[parent_index]; |
| 261 | | 263 | |
| 262 | if (compareFn(parent, child) == target_order) { | 264 | if (compareFn(self.context, parent, child) == target_order) { |
| 263 | self.items[parent_index] = child; | 265 | self.items[parent_index] = child; |
| 264 | self.items[child_index] = parent; | 266 | self.items[child_index] = parent; |
| 265 | } | 267 | } |
| ... | @@ -277,8 +279,8 @@ pub fn PriorityDequeue(comptime T: type, comptime compareFn: fn (T, T) Order) ty | ... | @@ -277,8 +279,8 @@ pub fn PriorityDequeue(comptime T: type, comptime compareFn: fn (T, T) Order) ty |
| 277 | }; | 279 | }; |
| 278 | } | 280 | } |
| 279 | | 281 | |
| 280 | fn bestItem(item1: ItemAndIndex, item2: ItemAndIndex, target_order: Order) ItemAndIndex { | 282 | fn bestItem(self: Self, item1: ItemAndIndex, item2: ItemAndIndex, target_order: Order) ItemAndIndex { |
| 281 | if (compareFn(item1.item, item2.item) == target_order) { | 283 | if (compareFn(self.context, item1.item, item2.item) == target_order) { |
| 282 | return item1; | 284 | return item1; |
| 283 | } else { | 285 | } else { |
| 284 | return item2; | 286 | return item2; |
| ... | @@ -288,7 +290,7 @@ pub fn PriorityDequeue(comptime T: type, comptime compareFn: fn (T, T) Order) ty | ... | @@ -288,7 +290,7 @@ pub fn PriorityDequeue(comptime T: type, comptime compareFn: fn (T, T) Order) ty |
| 288 | fn bestItemAtIndices(self: Self, index1: usize, index2: usize, target_order: Order) ItemAndIndex { | 290 | fn bestItemAtIndices(self: Self, index1: usize, index2: usize, target_order: Order) ItemAndIndex { |
| 289 | var item1 = self.getItem(index1); | 291 | var item1 = self.getItem(index1); |
| 290 | var item2 = self.getItem(index2); | 292 | var item2 = self.getItem(index2); |
| 291 | return Self.bestItem(item1, item2, target_order); | 293 | return self.bestItem(item1, item2, target_order); |
| 292 | } | 294 | } |
| 293 | | 295 | |
| 294 | fn bestDescendent(self: Self, first_child_index: usize, first_grandchild_index: usize, target_order: Order) ItemAndIndex { | 296 | fn bestDescendent(self: Self, first_child_index: usize, first_grandchild_index: usize, target_order: Order) ItemAndIndex { |
| ... | @@ -336,11 +338,12 @@ pub fn PriorityDequeue(comptime T: type, comptime compareFn: fn (T, T) Order) ty | ... | @@ -336,11 +338,12 @@ pub fn PriorityDequeue(comptime T: type, comptime compareFn: fn (T, T) Order) ty |
| 336 | /// Dequeue takes ownership of the passed in slice. The slice must have been | 338 | /// Dequeue takes ownership of the passed in slice. The slice must have been |
| 337 | /// allocated with `allocator`. | 339 | /// allocated with `allocator`. |
| 338 | /// De-initialize with `deinit`. | 340 | /// De-initialize with `deinit`. |
| 339 | pub fn fromOwnedSlice(allocator: Allocator, items: []T) Self { | 341 | pub fn fromOwnedSlice(allocator: Allocator, items: []T, context: Context) Self { |
| 340 | var queue = Self{ | 342 | var queue = Self{ |
| 341 | .items = items, | 343 | .items = items, |
| 342 | .len = items.len, | 344 | .len = items.len, |
| 343 | .allocator = allocator, | 345 | .allocator = allocator, |
| | 346 | .context = context, |
| 344 | }; | 347 | }; |
| 345 | | 348 | |
| 346 | if (queue.len <= 1) return queue; | 349 | if (queue.len <= 1) return queue; |
| ... | @@ -394,7 +397,7 @@ pub fn PriorityDequeue(comptime T: type, comptime compareFn: fn (T, T) Order) ty | ... | @@ -394,7 +397,7 @@ pub fn PriorityDequeue(comptime T: type, comptime compareFn: fn (T, T) Order) ty |
| 394 | } | 397 | } |
| 395 | | 398 | |
| 396 | pub const Iterator = struct { | 399 | pub const Iterator = struct { |
| 397 | queue: *PriorityDequeue(T, compareFn), | 400 | queue: *PriorityDequeue(T, Context, compareFn), |
| 398 | count: usize, | 401 | count: usize, |
| 399 | | 402 | |
| 400 | pub fn next(it: *Iterator) ?T { | 403 | pub fn next(it: *Iterator) ?T { |
| ... | @@ -453,14 +456,15 @@ pub fn PriorityDequeue(comptime T: type, comptime compareFn: fn (T, T) Order) ty | ... | @@ -453,14 +456,15 @@ pub fn PriorityDequeue(comptime T: type, comptime compareFn: fn (T, T) Order) ty |
| 453 | }; | 456 | }; |
| 454 | } | 457 | } |
| 455 | | 458 | |
| 456 | fn lessThanComparison(a: u32, b: u32) Order { | 459 | fn lessThanComparison(context: void, a: u32, b: u32) Order { |
| | 460 | _ = context; |
| 457 | return std.math.order(a, b); | 461 | return std.math.order(a, b); |
| 458 | } | 462 | } |
| 459 | | 463 | |
| 460 | const PDQ = PriorityDequeue(u32, lessThanComparison); | 464 | const PDQ = PriorityDequeue(u32, void, lessThanComparison); |
| 461 | | 465 | |
| 462 | test "std.PriorityDequeue: add and remove min" { | 466 | test "std.PriorityDequeue: add and remove min" { |
| 463 | var queue = PDQ.init(testing.allocator); | 467 | var queue = PDQ.init(testing.allocator, {}); |
| 464 | defer queue.deinit(); | 468 | defer queue.deinit(); |
| 465 | | 469 | |
| 466 | try queue.add(54); | 470 | try queue.add(54); |
| ... | @@ -482,11 +486,12 @@ test "std.PriorityDequeue: add and remove min structs" { | ... | @@ -482,11 +486,12 @@ test "std.PriorityDequeue: add and remove min structs" { |
| 482 | const S = struct { | 486 | const S = struct { |
| 483 | size: u32, | 487 | size: u32, |
| 484 | }; | 488 | }; |
| 485 | var queue = PriorityDequeue(S, struct { | 489 | var queue = PriorityDequeue(S, void, struct { |
| 486 | fn order(a: S, b: S) Order { | 490 | fn order(context: void, a: S, b: S) Order { |
| | 491 | _ = context; |
| 487 | return std.math.order(a.size, b.size); | 492 | return std.math.order(a.size, b.size); |
| 488 | } | 493 | } |
| 489 | }.order).init(testing.allocator); | 494 | }.order).init(testing.allocator, {}); |
| 490 | defer queue.deinit(); | 495 | defer queue.deinit(); |
| 491 | | 496 | |
| 492 | try queue.add(.{ .size = 54 }); | 497 | try queue.add(.{ .size = 54 }); |
| ... | @@ -505,7 +510,7 @@ test "std.PriorityDequeue: add and remove min structs" { | ... | @@ -505,7 +510,7 @@ test "std.PriorityDequeue: add and remove min structs" { |
| 505 | } | 510 | } |
| 506 | | 511 | |
| 507 | test "std.PriorityDequeue: add and remove max" { | 512 | test "std.PriorityDequeue: add and remove max" { |
| 508 | var queue = PDQ.init(testing.allocator); | 513 | var queue = PDQ.init(testing.allocator, {}); |
| 509 | defer queue.deinit(); | 514 | defer queue.deinit(); |
| 510 | | 515 | |
| 511 | try queue.add(54); | 516 | try queue.add(54); |
| ... | @@ -524,7 +529,7 @@ test "std.PriorityDequeue: add and remove max" { | ... | @@ -524,7 +529,7 @@ test "std.PriorityDequeue: add and remove max" { |
| 524 | } | 529 | } |
| 525 | | 530 | |
| 526 | test "std.PriorityDequeue: add and remove same min" { | 531 | test "std.PriorityDequeue: add and remove same min" { |
| 527 | var queue = PDQ.init(testing.allocator); | 532 | var queue = PDQ.init(testing.allocator, {}); |
| 528 | defer queue.deinit(); | 533 | defer queue.deinit(); |
| 529 | | 534 | |
| 530 | try queue.add(1); | 535 | try queue.add(1); |
| ... | @@ -543,7 +548,7 @@ test "std.PriorityDequeue: add and remove same min" { | ... | @@ -543,7 +548,7 @@ test "std.PriorityDequeue: add and remove same min" { |
| 543 | } | 548 | } |
| 544 | | 549 | |
| 545 | test "std.PriorityDequeue: add and remove same max" { | 550 | test "std.PriorityDequeue: add and remove same max" { |
| 546 | var queue = PDQ.init(testing.allocator); | 551 | var queue = PDQ.init(testing.allocator, {}); |
| 547 | defer queue.deinit(); | 552 | defer queue.deinit(); |
| 548 | | 553 | |
| 549 | try queue.add(1); | 554 | try queue.add(1); |
| ... | @@ -562,7 +567,7 @@ test "std.PriorityDequeue: add and remove same max" { | ... | @@ -562,7 +567,7 @@ test "std.PriorityDequeue: add and remove same max" { |
| 562 | } | 567 | } |
| 563 | | 568 | |
| 564 | test "std.PriorityDequeue: removeOrNull empty" { | 569 | test "std.PriorityDequeue: removeOrNull empty" { |
| 565 | var queue = PDQ.init(testing.allocator); | 570 | var queue = PDQ.init(testing.allocator, {}); |
| 566 | defer queue.deinit(); | 571 | defer queue.deinit(); |
| 567 | | 572 | |
| 568 | try expect(queue.removeMinOrNull() == null); | 573 | try expect(queue.removeMinOrNull() == null); |
| ... | @@ -570,7 +575,7 @@ test "std.PriorityDequeue: removeOrNull empty" { | ... | @@ -570,7 +575,7 @@ test "std.PriorityDequeue: removeOrNull empty" { |
| 570 | } | 575 | } |
| 571 | | 576 | |
| 572 | test "std.PriorityDequeue: edge case 3 elements" { | 577 | test "std.PriorityDequeue: edge case 3 elements" { |
| 573 | var queue = PDQ.init(testing.allocator); | 578 | var queue = PDQ.init(testing.allocator, {}); |
| 574 | defer queue.deinit(); | 579 | defer queue.deinit(); |
| 575 | | 580 | |
| 576 | try queue.add(9); | 581 | try queue.add(9); |
| ... | @@ -583,7 +588,7 @@ test "std.PriorityDequeue: edge case 3 elements" { | ... | @@ -583,7 +588,7 @@ test "std.PriorityDequeue: edge case 3 elements" { |
| 583 | } | 588 | } |
| 584 | | 589 | |
| 585 | test "std.PriorityDequeue: edge case 3 elements max" { | 590 | test "std.PriorityDequeue: edge case 3 elements max" { |
| 586 | var queue = PDQ.init(testing.allocator); | 591 | var queue = PDQ.init(testing.allocator, {}); |
| 587 | defer queue.deinit(); | 592 | defer queue.deinit(); |
| 588 | | 593 | |
| 589 | try queue.add(9); | 594 | try queue.add(9); |
| ... | @@ -596,7 +601,7 @@ test "std.PriorityDequeue: edge case 3 elements max" { | ... | @@ -596,7 +601,7 @@ test "std.PriorityDequeue: edge case 3 elements max" { |
| 596 | } | 601 | } |
| 597 | | 602 | |
| 598 | test "std.PriorityDequeue: peekMin" { | 603 | test "std.PriorityDequeue: peekMin" { |
| 599 | var queue = PDQ.init(testing.allocator); | 604 | var queue = PDQ.init(testing.allocator, {}); |
| 600 | defer queue.deinit(); | 605 | defer queue.deinit(); |
| 601 | | 606 | |
| 602 | try expect(queue.peekMin() == null); | 607 | try expect(queue.peekMin() == null); |
| ... | @@ -610,7 +615,7 @@ test "std.PriorityDequeue: peekMin" { | ... | @@ -610,7 +615,7 @@ test "std.PriorityDequeue: peekMin" { |
| 610 | } | 615 | } |
| 611 | | 616 | |
| 612 | test "std.PriorityDequeue: peekMax" { | 617 | test "std.PriorityDequeue: peekMax" { |
| 613 | var queue = PDQ.init(testing.allocator); | 618 | var queue = PDQ.init(testing.allocator, {}); |
| 614 | defer queue.deinit(); | 619 | defer queue.deinit(); |
| 615 | | 620 | |
| 616 | try expect(queue.peekMin() == null); | 621 | try expect(queue.peekMin() == null); |
| ... | @@ -624,7 +629,7 @@ test "std.PriorityDequeue: peekMax" { | ... | @@ -624,7 +629,7 @@ test "std.PriorityDequeue: peekMax" { |
| 624 | } | 629 | } |
| 625 | | 630 | |
| 626 | test "std.PriorityDequeue: sift up with odd indices" { | 631 | test "std.PriorityDequeue: sift up with odd indices" { |
| 627 | var queue = PDQ.init(testing.allocator); | 632 | var queue = PDQ.init(testing.allocator, {}); |
| 628 | defer queue.deinit(); | 633 | defer queue.deinit(); |
| 629 | const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 }; | 634 | const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 }; |
| 630 | for (items) |e| { | 635 | for (items) |e| { |
| ... | @@ -638,7 +643,7 @@ test "std.PriorityDequeue: sift up with odd indices" { | ... | @@ -638,7 +643,7 @@ test "std.PriorityDequeue: sift up with odd indices" { |
| 638 | } | 643 | } |
| 639 | | 644 | |
| 640 | test "std.PriorityDequeue: sift up with odd indices" { | 645 | test "std.PriorityDequeue: sift up with odd indices" { |
| 641 | var queue = PDQ.init(testing.allocator); | 646 | var queue = PDQ.init(testing.allocator, {}); |
| 642 | defer queue.deinit(); | 647 | defer queue.deinit(); |
| 643 | const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 }; | 648 | const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 }; |
| 644 | for (items) |e| { | 649 | for (items) |e| { |
| ... | @@ -652,7 +657,7 @@ test "std.PriorityDequeue: sift up with odd indices" { | ... | @@ -652,7 +657,7 @@ test "std.PriorityDequeue: sift up with odd indices" { |
| 652 | } | 657 | } |
| 653 | | 658 | |
| 654 | test "std.PriorityDequeue: addSlice min" { | 659 | test "std.PriorityDequeue: addSlice min" { |
| 655 | var queue = PDQ.init(testing.allocator); | 660 | var queue = PDQ.init(testing.allocator, {}); |
| 656 | defer queue.deinit(); | 661 | defer queue.deinit(); |
| 657 | const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 }; | 662 | const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 }; |
| 658 | try queue.addSlice(items[0..]); | 663 | try queue.addSlice(items[0..]); |
| ... | @@ -664,7 +669,7 @@ test "std.PriorityDequeue: addSlice min" { | ... | @@ -664,7 +669,7 @@ test "std.PriorityDequeue: addSlice min" { |
| 664 | } | 669 | } |
| 665 | | 670 | |
| 666 | test "std.PriorityDequeue: addSlice max" { | 671 | test "std.PriorityDequeue: addSlice max" { |
| 667 | var queue = PDQ.init(testing.allocator); | 672 | var queue = PDQ.init(testing.allocator, {}); |
| 668 | defer queue.deinit(); | 673 | defer queue.deinit(); |
| 669 | const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 }; | 674 | const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 }; |
| 670 | try queue.addSlice(items[0..]); | 675 | try queue.addSlice(items[0..]); |
| ... | @@ -678,7 +683,7 @@ test "std.PriorityDequeue: addSlice max" { | ... | @@ -678,7 +683,7 @@ test "std.PriorityDequeue: addSlice max" { |
| 678 | test "std.PriorityDequeue: fromOwnedSlice trivial case 0" { | 683 | test "std.PriorityDequeue: fromOwnedSlice trivial case 0" { |
| 679 | const items = [0]u32{}; | 684 | const items = [0]u32{}; |
| 680 | const queue_items = try testing.allocator.dupe(u32, &items); | 685 | const queue_items = try testing.allocator.dupe(u32, &items); |
| 681 | var queue = PDQ.fromOwnedSlice(testing.allocator, queue_items[0..]); | 686 | var queue = PDQ.fromOwnedSlice(testing.allocator, queue_items[0..], {}); |
| 682 | defer queue.deinit(); | 687 | defer queue.deinit(); |
| 683 | try expectEqual(@as(usize, 0), queue.len); | 688 | try expectEqual(@as(usize, 0), queue.len); |
| 684 | try expect(queue.removeMinOrNull() == null); | 689 | try expect(queue.removeMinOrNull() == null); |
| ... | @@ -687,7 +692,7 @@ test "std.PriorityDequeue: fromOwnedSlice trivial case 0" { | ... | @@ -687,7 +692,7 @@ test "std.PriorityDequeue: fromOwnedSlice trivial case 0" { |
| 687 | test "std.PriorityDequeue: fromOwnedSlice trivial case 1" { | 692 | test "std.PriorityDequeue: fromOwnedSlice trivial case 1" { |
| 688 | const items = [1]u32{1}; | 693 | const items = [1]u32{1}; |
| 689 | const queue_items = try testing.allocator.dupe(u32, &items); | 694 | const queue_items = try testing.allocator.dupe(u32, &items); |
| 690 | var queue = PDQ.fromOwnedSlice(testing.allocator, queue_items[0..]); | 695 | var queue = PDQ.fromOwnedSlice(testing.allocator, queue_items[0..], {}); |
| 691 | defer queue.deinit(); | 696 | defer queue.deinit(); |
| 692 | | 697 | |
| 693 | try expectEqual(@as(usize, 1), queue.len); | 698 | try expectEqual(@as(usize, 1), queue.len); |
| ... | @@ -698,7 +703,7 @@ test "std.PriorityDequeue: fromOwnedSlice trivial case 1" { | ... | @@ -698,7 +703,7 @@ test "std.PriorityDequeue: fromOwnedSlice trivial case 1" { |
| 698 | test "std.PriorityDequeue: fromOwnedSlice" { | 703 | test "std.PriorityDequeue: fromOwnedSlice" { |
| 699 | const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 }; | 704 | const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 }; |
| 700 | const queue_items = try testing.allocator.dupe(u32, items[0..]); | 705 | const queue_items = try testing.allocator.dupe(u32, items[0..]); |
| 701 | var queue = PDQ.fromOwnedSlice(testing.allocator, queue_items[0..]); | 706 | var queue = PDQ.fromOwnedSlice(testing.allocator, queue_items[0..], {}); |
| 702 | defer queue.deinit(); | 707 | defer queue.deinit(); |
| 703 | | 708 | |
| 704 | const sorted_items = [_]u32{ 1, 2, 5, 6, 7, 7, 11, 12, 13, 14, 15, 15, 16, 21, 22, 24, 24, 25 }; | 709 | const sorted_items = [_]u32{ 1, 2, 5, 6, 7, 7, 11, 12, 13, 14, 15, 15, 16, 21, 22, 24, 24, 25 }; |
| ... | @@ -708,7 +713,7 @@ test "std.PriorityDequeue: fromOwnedSlice" { | ... | @@ -708,7 +713,7 @@ test "std.PriorityDequeue: fromOwnedSlice" { |
| 708 | } | 713 | } |
| 709 | | 714 | |
| 710 | test "std.PriorityDequeue: update min queue" { | 715 | test "std.PriorityDequeue: update min queue" { |
| 711 | var queue = PDQ.init(testing.allocator); | 716 | var queue = PDQ.init(testing.allocator, {}); |
| 712 | defer queue.deinit(); | 717 | defer queue.deinit(); |
| 713 | | 718 | |
| 714 | try queue.add(55); | 719 | try queue.add(55); |
| ... | @@ -723,7 +728,7 @@ test "std.PriorityDequeue: update min queue" { | ... | @@ -723,7 +728,7 @@ test "std.PriorityDequeue: update min queue" { |
| 723 | } | 728 | } |
| 724 | | 729 | |
| 725 | test "std.PriorityDequeue: update same min queue" { | 730 | test "std.PriorityDequeue: update same min queue" { |
| 726 | var queue = PDQ.init(testing.allocator); | 731 | var queue = PDQ.init(testing.allocator, {}); |
| 727 | defer queue.deinit(); | 732 | defer queue.deinit(); |
| 728 | | 733 | |
| 729 | try queue.add(1); | 734 | try queue.add(1); |
| ... | @@ -739,7 +744,7 @@ test "std.PriorityDequeue: update same min queue" { | ... | @@ -739,7 +744,7 @@ test "std.PriorityDequeue: update same min queue" { |
| 739 | } | 744 | } |
| 740 | | 745 | |
| 741 | test "std.PriorityDequeue: update max queue" { | 746 | test "std.PriorityDequeue: update max queue" { |
| 742 | var queue = PDQ.init(testing.allocator); | 747 | var queue = PDQ.init(testing.allocator, {}); |
| 743 | defer queue.deinit(); | 748 | defer queue.deinit(); |
| 744 | | 749 | |
| 745 | try queue.add(55); | 750 | try queue.add(55); |
| ... | @@ -755,7 +760,7 @@ test "std.PriorityDequeue: update max queue" { | ... | @@ -755,7 +760,7 @@ test "std.PriorityDequeue: update max queue" { |
| 755 | } | 760 | } |
| 756 | | 761 | |
| 757 | test "std.PriorityDequeue: update same max queue" { | 762 | test "std.PriorityDequeue: update same max queue" { |
| 758 | var queue = PDQ.init(testing.allocator); | 763 | var queue = PDQ.init(testing.allocator, {}); |
| 759 | defer queue.deinit(); | 764 | defer queue.deinit(); |
| 760 | | 765 | |
| 761 | try queue.add(1); | 766 | try queue.add(1); |
| ... | @@ -771,7 +776,7 @@ test "std.PriorityDequeue: update same max queue" { | ... | @@ -771,7 +776,7 @@ test "std.PriorityDequeue: update same max queue" { |
| 771 | } | 776 | } |
| 772 | | 777 | |
| 773 | test "std.PriorityDequeue: iterator" { | 778 | test "std.PriorityDequeue: iterator" { |
| 774 | var queue = PDQ.init(testing.allocator); | 779 | var queue = PDQ.init(testing.allocator, {}); |
| 775 | var map = std.AutoHashMap(u32, void).init(testing.allocator); | 780 | var map = std.AutoHashMap(u32, void).init(testing.allocator); |
| 776 | defer { | 781 | defer { |
| 777 | queue.deinit(); | 782 | queue.deinit(); |
| ... | @@ -793,7 +798,7 @@ test "std.PriorityDequeue: iterator" { | ... | @@ -793,7 +798,7 @@ test "std.PriorityDequeue: iterator" { |
| 793 | } | 798 | } |
| 794 | | 799 | |
| 795 | test "std.PriorityDequeue: remove at index" { | 800 | test "std.PriorityDequeue: remove at index" { |
| 796 | var queue = PDQ.init(testing.allocator); | 801 | var queue = PDQ.init(testing.allocator, {}); |
| 797 | defer queue.deinit(); | 802 | defer queue.deinit(); |
| 798 | | 803 | |
| 799 | try queue.add(3); | 804 | try queue.add(3); |
| ... | @@ -816,7 +821,7 @@ test "std.PriorityDequeue: remove at index" { | ... | @@ -816,7 +821,7 @@ test "std.PriorityDequeue: remove at index" { |
| 816 | } | 821 | } |
| 817 | | 822 | |
| 818 | test "std.PriorityDequeue: iterator while empty" { | 823 | test "std.PriorityDequeue: iterator while empty" { |
| 819 | var queue = PDQ.init(testing.allocator); | 824 | var queue = PDQ.init(testing.allocator, {}); |
| 820 | defer queue.deinit(); | 825 | defer queue.deinit(); |
| 821 | | 826 | |
| 822 | var it = queue.iterator(); | 827 | var it = queue.iterator(); |
| ... | @@ -825,7 +830,7 @@ test "std.PriorityDequeue: iterator while empty" { | ... | @@ -825,7 +830,7 @@ test "std.PriorityDequeue: iterator while empty" { |
| 825 | } | 830 | } |
| 826 | | 831 | |
| 827 | test "std.PriorityDequeue: shrinkAndFree" { | 832 | test "std.PriorityDequeue: shrinkAndFree" { |
| 828 | var queue = PDQ.init(testing.allocator); | 833 | var queue = PDQ.init(testing.allocator, {}); |
| 829 | defer queue.deinit(); | 834 | defer queue.deinit(); |
| 830 | | 835 | |
| 831 | try queue.ensureTotalCapacity(4); | 836 | try queue.ensureTotalCapacity(4); |
| ... | @@ -864,7 +869,7 @@ fn fuzzTestMin(rng: std.rand.Random, comptime queue_size: usize) !void { | ... | @@ -864,7 +869,7 @@ fn fuzzTestMin(rng: std.rand.Random, comptime queue_size: usize) !void { |
| 864 | const allocator = testing.allocator; | 869 | const allocator = testing.allocator; |
| 865 | const items = try generateRandomSlice(allocator, rng, queue_size); | 870 | const items = try generateRandomSlice(allocator, rng, queue_size); |
| 866 | | 871 | |
| 867 | var queue = PDQ.fromOwnedSlice(allocator, items); | 872 | var queue = PDQ.fromOwnedSlice(allocator, items, {}); |
| 868 | defer queue.deinit(); | 873 | defer queue.deinit(); |
| 869 | | 874 | |
| 870 | var last_removed: ?u32 = null; | 875 | var last_removed: ?u32 = null; |
| ... | @@ -893,7 +898,7 @@ fn fuzzTestMax(rng: std.rand.Random, queue_size: usize) !void { | ... | @@ -893,7 +898,7 @@ fn fuzzTestMax(rng: std.rand.Random, queue_size: usize) !void { |
| 893 | const allocator = testing.allocator; | 898 | const allocator = testing.allocator; |
| 894 | const items = try generateRandomSlice(allocator, rng, queue_size); | 899 | const items = try generateRandomSlice(allocator, rng, queue_size); |
| 895 | | 900 | |
| 896 | var queue = PDQ.fromOwnedSlice(testing.allocator, items); | 901 | var queue = PDQ.fromOwnedSlice(testing.allocator, items, {}); |
| 897 | defer queue.deinit(); | 902 | defer queue.deinit(); |
| 898 | | 903 | |
| 899 | var last_removed: ?u32 = null; | 904 | var last_removed: ?u32 = null; |
| ... | @@ -922,7 +927,7 @@ fn fuzzTestMinMax(rng: std.rand.Random, queue_size: usize) !void { | ... | @@ -922,7 +927,7 @@ fn fuzzTestMinMax(rng: std.rand.Random, queue_size: usize) !void { |
| 922 | const allocator = testing.allocator; | 927 | const allocator = testing.allocator; |
| 923 | const items = try generateRandomSlice(allocator, rng, queue_size); | 928 | const items = try generateRandomSlice(allocator, rng, queue_size); |
| 924 | | 929 | |
| 925 | var queue = PDQ.fromOwnedSlice(allocator, items); | 930 | var queue = PDQ.fromOwnedSlice(allocator, items, {}); |
| 926 | defer queue.deinit(); | 931 | defer queue.deinit(); |
| 927 | | 932 | |
| 928 | var last_min: ?u32 = null; | 933 | var last_min: ?u32 = null; |
| ... | @@ -957,3 +962,31 @@ fn generateRandomSlice(allocator: std.mem.Allocator, rng: std.rand.Random, size: | ... | @@ -957,3 +962,31 @@ fn generateRandomSlice(allocator: std.mem.Allocator, rng: std.rand.Random, size: |
| 957 | | 962 | |
| 958 | return array.toOwnedSlice(); | 963 | return array.toOwnedSlice(); |
| 959 | } | 964 | } |
| | 965 | |
| | 966 | fn contextLessThanComparison(context: []const u32, a: usize, b: usize) Order { |
| | 967 | return std.math.order(context[a], context[b]); |
| | 968 | } |
| | 969 | |
| | 970 | const CPDQ = PriorityDequeue(usize, []const u32, contextLessThanComparison); |
| | 971 | |
| | 972 | test "std.PriorityDequeue: add and remove" { |
| | 973 | const context = [_]u32{ 5, 3, 4, 2, 2, 8, 0 }; |
| | 974 | |
| | 975 | var queue = CPDQ.init(testing.allocator, context[0..]); |
| | 976 | defer queue.deinit(); |
| | 977 | |
| | 978 | try queue.add(0); |
| | 979 | try queue.add(1); |
| | 980 | try queue.add(2); |
| | 981 | try queue.add(3); |
| | 982 | try queue.add(4); |
| | 983 | try queue.add(5); |
| | 984 | try queue.add(6); |
| | 985 | try expectEqual(@as(usize, 6), queue.removeMin()); |
| | 986 | try expectEqual(@as(usize, 5), queue.removeMax()); |
| | 987 | try expectEqual(@as(usize, 3), queue.removeMin()); |
| | 988 | try expectEqual(@as(usize, 0), queue.removeMax()); |
| | 989 | try expectEqual(@as(usize, 4), queue.removeMin()); |
| | 990 | try expectEqual(@as(usize, 2), queue.removeMax()); |
| | 991 | try expectEqual(@as(usize, 1), queue.removeMin()); |
| | 992 | } |