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