| ... | @@ -23,10 +23,17 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar | ... | @@ -23,10 +23,17 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar |
| 23 | len: usize, | 23 | len: usize, |
| 24 | context: Context, | 24 | context: Context, |
| 25 | | 25 | |
| 26 | /// Initialize and return a new priority dequeue. | 26 | /// A priority dequeue containing no elements. |
| 27 | pub fn init(context: Context) Self { | 27 | pub const empty: Self = .{ |
| | 28 | .items = &.{}, |
| | 29 | .len = 0, |
| | 30 | .context = undefined, |
| | 31 | }; |
| | 32 | |
| | 33 | /// Initialize and return a new priority dequeue with context. |
| | 34 | pub fn initContext(context: Context) Self { |
| 28 | return Self{ | 35 | return Self{ |
| 29 | .items = &[_]T{}, | 36 | .items = &.{}, |
| 30 | .len = 0, | 37 | .len = 0, |
| 31 | .context = context, | 38 | .context = context, |
| 32 | }; | 39 | }; |
| ... | @@ -451,7 +458,8 @@ const PDQ = PriorityDequeue(u32, void, lessThanComparison); | ... | @@ -451,7 +458,8 @@ const PDQ = PriorityDequeue(u32, void, lessThanComparison); |
| 451 | | 458 | |
| 452 | test "push and pop min" { | 459 | test "push and pop min" { |
| 453 | const gpa = std.testing.allocator; | 460 | const gpa = std.testing.allocator; |
| 454 | var queue = PDQ.init({}); | 461 | |
| | 462 | var queue: PDQ = .empty; |
| 455 | defer queue.deinit(gpa); | 463 | defer queue.deinit(gpa); |
| 456 | | 464 | |
| 457 | try queue.push(gpa, 54); | 465 | try queue.push(gpa, 54); |
| ... | @@ -480,7 +488,7 @@ test "push and pop min structs" { | ... | @@ -480,7 +488,7 @@ test "push and pop min structs" { |
| 480 | _ = context; | 488 | _ = context; |
| 481 | return std.math.order(a.size, b.size); | 489 | return std.math.order(a.size, b.size); |
| 482 | } | 490 | } |
| 483 | }.order).init({}); | 491 | }.order).initContext({}); |
| 484 | defer queue.deinit(gpa); | 492 | defer queue.deinit(gpa); |
| 485 | | 493 | |
| 486 | try queue.push(gpa, .{ .size = 54 }); | 494 | try queue.push(gpa, .{ .size = 54 }); |
| ... | @@ -501,7 +509,7 @@ test "push and pop min structs" { | ... | @@ -501,7 +509,7 @@ test "push and pop min structs" { |
| 501 | test "push and pop max" { | 509 | test "push and pop max" { |
| 502 | const gpa = std.testing.allocator; | 510 | const gpa = std.testing.allocator; |
| 503 | | 511 | |
| 504 | var queue = PDQ.init({}); | 512 | var queue: PDQ = .empty; |
| 505 | defer queue.deinit(gpa); | 513 | defer queue.deinit(gpa); |
| 506 | | 514 | |
| 507 | try queue.push(gpa, 54); | 515 | try queue.push(gpa, 54); |
| ... | @@ -522,7 +530,7 @@ test "push and pop max" { | ... | @@ -522,7 +530,7 @@ test "push and pop max" { |
| 522 | test "push and pop same min" { | 530 | test "push and pop same min" { |
| 523 | const gpa = std.testing.allocator; | 531 | const gpa = std.testing.allocator; |
| 524 | | 532 | |
| 525 | var queue = PDQ.init({}); | 533 | var queue: PDQ = .empty; |
| 526 | defer queue.deinit(gpa); | 534 | defer queue.deinit(gpa); |
| 527 | | 535 | |
| 528 | try queue.push(gpa, 1); | 536 | try queue.push(gpa, 1); |
| ... | @@ -543,7 +551,7 @@ test "push and pop same min" { | ... | @@ -543,7 +551,7 @@ test "push and pop same min" { |
| 543 | test "push and pop same max" { | 551 | test "push and pop same max" { |
| 544 | const gpa = std.testing.allocator; | 552 | const gpa = std.testing.allocator; |
| 545 | | 553 | |
| 546 | var queue = PDQ.init({}); | 554 | var queue: PDQ = .empty; |
| 547 | defer queue.deinit(gpa); | 555 | defer queue.deinit(gpa); |
| 548 | | 556 | |
| 549 | try queue.push(gpa, 1); | 557 | try queue.push(gpa, 1); |
| ... | @@ -564,7 +572,7 @@ test "push and pop same max" { | ... | @@ -564,7 +572,7 @@ test "push and pop same max" { |
| 564 | test "popOrNull empty" { | 572 | test "popOrNull empty" { |
| 565 | const gpa = std.testing.allocator; | 573 | const gpa = std.testing.allocator; |
| 566 | | 574 | |
| 567 | var queue = PDQ.init({}); | 575 | var queue: PDQ = .empty; |
| 568 | defer queue.deinit(gpa); | 576 | defer queue.deinit(gpa); |
| 569 | | 577 | |
| 570 | try expect(queue.popMin() == null); | 578 | try expect(queue.popMin() == null); |
| ... | @@ -574,7 +582,7 @@ test "popOrNull empty" { | ... | @@ -574,7 +582,7 @@ test "popOrNull empty" { |
| 574 | test "edge case 3 elements" { | 582 | test "edge case 3 elements" { |
| 575 | const gpa = std.testing.allocator; | 583 | const gpa = std.testing.allocator; |
| 576 | | 584 | |
| 577 | var queue = PDQ.init({}); | 585 | var queue: PDQ = .empty; |
| 578 | defer queue.deinit(gpa); | 586 | defer queue.deinit(gpa); |
| 579 | | 587 | |
| 580 | try queue.push(gpa, 9); | 588 | try queue.push(gpa, 9); |
| ... | @@ -589,7 +597,7 @@ test "edge case 3 elements" { | ... | @@ -589,7 +597,7 @@ test "edge case 3 elements" { |
| 589 | test "edge case 3 elements max" { | 597 | test "edge case 3 elements max" { |
| 590 | const gpa = std.testing.allocator; | 598 | const gpa = std.testing.allocator; |
| 591 | | 599 | |
| 592 | var queue = PDQ.init({}); | 600 | var queue: PDQ = .empty; |
| 593 | defer queue.deinit(gpa); | 601 | defer queue.deinit(gpa); |
| 594 | | 602 | |
| 595 | try queue.push(gpa, 9); | 603 | try queue.push(gpa, 9); |
| ... | @@ -604,7 +612,7 @@ test "edge case 3 elements max" { | ... | @@ -604,7 +612,7 @@ test "edge case 3 elements max" { |
| 604 | test "peekMin" { | 612 | test "peekMin" { |
| 605 | const gpa = std.testing.allocator; | 613 | const gpa = std.testing.allocator; |
| 606 | | 614 | |
| 607 | var queue = PDQ.init({}); | 615 | var queue: PDQ = .empty; |
| 608 | defer queue.deinit(gpa); | 616 | defer queue.deinit(gpa); |
| 609 | | 617 | |
| 610 | try expect(queue.peekMin() == null); | 618 | try expect(queue.peekMin() == null); |
| ... | @@ -620,7 +628,7 @@ test "peekMin" { | ... | @@ -620,7 +628,7 @@ test "peekMin" { |
| 620 | test "peekMax" { | 628 | test "peekMax" { |
| 621 | const gpa = std.testing.allocator; | 629 | const gpa = std.testing.allocator; |
| 622 | | 630 | |
| 623 | var queue = PDQ.init({}); | 631 | var queue: PDQ = .empty; |
| 624 | defer queue.deinit(gpa); | 632 | defer queue.deinit(gpa); |
| 625 | | 633 | |
| 626 | try expect(queue.peekMin() == null); | 634 | try expect(queue.peekMin() == null); |
| ... | @@ -636,7 +644,7 @@ test "peekMax" { | ... | @@ -636,7 +644,7 @@ test "peekMax" { |
| 636 | test "sift up with odd indices, popMin" { | 644 | test "sift up with odd indices, popMin" { |
| 637 | const gpa = std.testing.allocator; | 645 | const gpa = std.testing.allocator; |
| 638 | | 646 | |
| 639 | var queue = PDQ.init({}); | 647 | var queue: PDQ = .empty; |
| 640 | defer queue.deinit(gpa); | 648 | defer queue.deinit(gpa); |
| 641 | | 649 | |
| 642 | const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 }; | 650 | const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 }; |
| ... | @@ -653,7 +661,7 @@ test "sift up with odd indices, popMin" { | ... | @@ -653,7 +661,7 @@ test "sift up with odd indices, popMin" { |
| 653 | test "sift up with odd indices, popMax" { | 661 | test "sift up with odd indices, popMax" { |
| 654 | const gpa = std.testing.allocator; | 662 | const gpa = std.testing.allocator; |
| 655 | | 663 | |
| 656 | var queue = PDQ.init({}); | 664 | var queue: PDQ = .empty; |
| 657 | defer queue.deinit(gpa); | 665 | defer queue.deinit(gpa); |
| 658 | | 666 | |
| 659 | const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 }; | 667 | const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 }; |
| ... | @@ -670,7 +678,7 @@ test "sift up with odd indices, popMax" { | ... | @@ -670,7 +678,7 @@ test "sift up with odd indices, popMax" { |
| 670 | test "pushSlice min" { | 678 | test "pushSlice min" { |
| 671 | const gpa = std.testing.allocator; | 679 | const gpa = std.testing.allocator; |
| 672 | | 680 | |
| 673 | var queue = PDQ.init({}); | 681 | var queue: PDQ = .empty; |
| 674 | defer queue.deinit(gpa); | 682 | defer queue.deinit(gpa); |
| 675 | | 683 | |
| 676 | const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 }; | 684 | const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 }; |
| ... | @@ -685,7 +693,7 @@ test "pushSlice min" { | ... | @@ -685,7 +693,7 @@ test "pushSlice min" { |
| 685 | test "pushSlice max" { | 693 | test "pushSlice max" { |
| 686 | const gpa = std.testing.allocator; | 694 | const gpa = std.testing.allocator; |
| 687 | | 695 | |
| 688 | var queue = PDQ.init({}); | 696 | var queue: PDQ = .empty; |
| 689 | defer queue.deinit(gpa); | 697 | defer queue.deinit(gpa); |
| 690 | | 698 | |
| 691 | const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 }; | 699 | const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 }; |
| ... | @@ -742,7 +750,7 @@ test "fromOwnedSlice" { | ... | @@ -742,7 +750,7 @@ test "fromOwnedSlice" { |
| 742 | test "update min queue" { | 750 | test "update min queue" { |
| 743 | const gpa = std.testing.allocator; | 751 | const gpa = std.testing.allocator; |
| 744 | | 752 | |
| 745 | var queue = PDQ.init({}); | 753 | var queue: PDQ = .empty; |
| 746 | defer queue.deinit(gpa); | 754 | defer queue.deinit(gpa); |
| 747 | | 755 | |
| 748 | try queue.push(gpa, 55); | 756 | try queue.push(gpa, 55); |
| ... | @@ -759,7 +767,7 @@ test "update min queue" { | ... | @@ -759,7 +767,7 @@ test "update min queue" { |
| 759 | test "update same min queue" { | 767 | test "update same min queue" { |
| 760 | const gpa = std.testing.allocator; | 768 | const gpa = std.testing.allocator; |
| 761 | | 769 | |
| 762 | var queue = PDQ.init({}); | 770 | var queue: PDQ = .empty; |
| 763 | defer queue.deinit(gpa); | 771 | defer queue.deinit(gpa); |
| 764 | | 772 | |
| 765 | try queue.push(gpa, 1); | 773 | try queue.push(gpa, 1); |
| ... | @@ -777,7 +785,7 @@ test "update same min queue" { | ... | @@ -777,7 +785,7 @@ test "update same min queue" { |
| 777 | test "update max queue" { | 785 | test "update max queue" { |
| 778 | const gpa = std.testing.allocator; | 786 | const gpa = std.testing.allocator; |
| 779 | | 787 | |
| 780 | var queue = PDQ.init({}); | 788 | var queue: PDQ = .empty; |
| 781 | defer queue.deinit(gpa); | 789 | defer queue.deinit(gpa); |
| 782 | | 790 | |
| 783 | try queue.push(gpa, 55); | 791 | try queue.push(gpa, 55); |
| ... | @@ -795,7 +803,7 @@ test "update max queue" { | ... | @@ -795,7 +803,7 @@ test "update max queue" { |
| 795 | test "update same max queue" { | 803 | test "update same max queue" { |
| 796 | const gpa = std.testing.allocator; | 804 | const gpa = std.testing.allocator; |
| 797 | | 805 | |
| 798 | var queue = PDQ.init({}); | 806 | var queue: PDQ = .empty; |
| 799 | defer queue.deinit(gpa); | 807 | defer queue.deinit(gpa); |
| 800 | | 808 | |
| 801 | try queue.push(gpa, 1); | 809 | try queue.push(gpa, 1); |
| ... | @@ -813,7 +821,7 @@ test "update same max queue" { | ... | @@ -813,7 +821,7 @@ test "update same max queue" { |
| 813 | test "update after pop" { | 821 | test "update after pop" { |
| 814 | const gpa = std.testing.allocator; | 822 | const gpa = std.testing.allocator; |
| 815 | | 823 | |
| 816 | var queue = PDQ.init({}); | 824 | var queue: PDQ = .empty; |
| 817 | defer queue.deinit(gpa); | 825 | defer queue.deinit(gpa); |
| 818 | | 826 | |
| 819 | try queue.push(gpa, 1); | 827 | try queue.push(gpa, 1); |
| ... | @@ -824,7 +832,7 @@ test "update after pop" { | ... | @@ -824,7 +832,7 @@ test "update after pop" { |
| 824 | test "iterator" { | 832 | test "iterator" { |
| 825 | const gpa = std.testing.allocator; | 833 | const gpa = std.testing.allocator; |
| 826 | | 834 | |
| 827 | var queue = PDQ.init({}); | 835 | var queue: PDQ = .empty; |
| 828 | var map = std.AutoHashMap(u32, void).init(testing.allocator); | 836 | var map = std.AutoHashMap(u32, void).init(testing.allocator); |
| 829 | defer { | 837 | defer { |
| 830 | queue.deinit(gpa); | 838 | queue.deinit(gpa); |
| ... | @@ -848,7 +856,7 @@ test "iterator" { | ... | @@ -848,7 +856,7 @@ test "iterator" { |
| 848 | test "pop at index" { | 856 | test "pop at index" { |
| 849 | const gpa = std.testing.allocator; | 857 | const gpa = std.testing.allocator; |
| 850 | | 858 | |
| 851 | var queue = PDQ.init({}); | 859 | var queue: PDQ = .empty; |
| 852 | defer queue.deinit(gpa); | 860 | defer queue.deinit(gpa); |
| 853 | | 861 | |
| 854 | try queue.push(gpa, 3); | 862 | try queue.push(gpa, 3); |
| ... | @@ -873,7 +881,7 @@ test "pop at index" { | ... | @@ -873,7 +881,7 @@ test "pop at index" { |
| 873 | test "iterator while empty" { | 881 | test "iterator while empty" { |
| 874 | const gpa = std.testing.allocator; | 882 | const gpa = std.testing.allocator; |
| 875 | | 883 | |
| 876 | var queue = PDQ.init({}); | 884 | var queue: PDQ = .empty; |
| 877 | defer queue.deinit(gpa); | 885 | defer queue.deinit(gpa); |
| 878 | | 886 | |
| 879 | var it = queue.iterator(); | 887 | var it = queue.iterator(); |
| ... | @@ -884,7 +892,7 @@ test "iterator while empty" { | ... | @@ -884,7 +892,7 @@ test "iterator while empty" { |
| 884 | test "shrinkAndFree" { | 892 | test "shrinkAndFree" { |
| 885 | const gpa = std.testing.allocator; | 893 | const gpa = std.testing.allocator; |
| 886 | | 894 | |
| 887 | var queue = PDQ.init({}); | 895 | var queue: PDQ = .empty; |
| 888 | defer queue.deinit(gpa); | 896 | defer queue.deinit(gpa); |
| 889 | | 897 | |
| 890 | try queue.ensureTotalCapacity(gpa, 4); | 898 | try queue.ensureTotalCapacity(gpa, 4); |
| ... | @@ -1031,7 +1039,7 @@ test "push and pop" { | ... | @@ -1031,7 +1039,7 @@ test "push and pop" { |
| 1031 | | 1039 | |
| 1032 | const context = [_]u32{ 5, 3, 4, 2, 2, 8, 0 }; | 1040 | const context = [_]u32{ 5, 3, 4, 2, 2, 8, 0 }; |
| 1033 | | 1041 | |
| 1034 | var queue = CPDQ.init(context[0..]); | 1042 | var queue: CPDQ = .initContext(context[0..]); |
| 1035 | defer queue.deinit(gpa); | 1043 | defer queue.deinit(gpa); |
| 1036 | | 1044 | |
| 1037 | try queue.push(gpa, 0); | 1045 | try queue.push(gpa, 0); |
| ... | @@ -1060,7 +1068,7 @@ test "don't compare a value to a copy of itself" { | ... | @@ -1060,7 +1068,7 @@ test "don't compare a value to a copy of itself" { |
| 1060 | all_cmps_unique = all_cmps_unique and (a != b); | 1068 | all_cmps_unique = all_cmps_unique and (a != b); |
| 1061 | return std.math.order(a, b); | 1069 | return std.math.order(a, b); |
| 1062 | } | 1070 | } |
| 1063 | }.uniqueLessThan).init({}); | 1071 | }.uniqueLessThan).initContext({}); |
| 1064 | defer depq.deinit(gpa); | 1072 | defer depq.deinit(gpa); |
| 1065 | | 1073 | |
| 1066 | try depq.push(gpa, 1); | 1074 | try depq.push(gpa, 1); |