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