| ... | @@ -349,19 +349,22 @@ pub fn PriorityDequeue(comptime T: type) type { | ... | @@ -349,19 +349,22 @@ pub fn PriorityDequeue(comptime T: type) type { |
| 349 | /// allocated with `allocator`. | 349 | /// allocated with `allocator`. |
| 350 | /// De-initialize with `deinit`. | 350 | /// De-initialize with `deinit`. |
| 351 | pub fn fromOwnedSlice(allocator: *Allocator, lessThanFn: fn (T, T) bool, items: []T) Self { | 351 | pub fn fromOwnedSlice(allocator: *Allocator, lessThanFn: fn (T, T) bool, items: []T) Self { |
| 352 | var dequeue = Self{ | 352 | var queue = Self{ |
| 353 | .items = items, | 353 | .items = items, |
| 354 | .len = items.len, | 354 | .len = items.len, |
| 355 | .allocator = allocator, | 355 | .allocator = allocator, |
| 356 | .lessThanFn = lessThanFn, | 356 | .lessThanFn = lessThanFn, |
| 357 | }; | 357 | }; |
| 358 | const half = (dequeue.len >> 1) - 1; | 358 | |
| | 359 | if (queue.len <= 1) return queue; |
| | 360 | |
| | 361 | const half = (queue.len >> 1) - 1; |
| 359 | var i: usize = 0; | 362 | var i: usize = 0; |
| 360 | while (i <= half) : (i += 1) { | 363 | while (i <= half) : (i += 1) { |
| 361 | const index = half - i; | 364 | const index = half - i; |
| 362 | dequeue.siftDown(index); | 365 | queue.siftDown(index); |
| 363 | } | 366 | } |
| 364 | return dequeue; | 367 | return queue; |
| 365 | } | 368 | } |
| 366 | | 369 | |
| 367 | pub fn ensureCapacity(self: *Self, new_capacity: usize) !void { | 370 | pub fn ensureCapacity(self: *Self, new_capacity: usize) !void { |
| ... | @@ -646,6 +649,26 @@ test "std.PriorityDequeue: addSlice max" { | ... | @@ -646,6 +649,26 @@ test "std.PriorityDequeue: addSlice max" { |
| 646 | } | 649 | } |
| 647 | } | 650 | } |
| 648 | | 651 | |
| | 652 | test "std.PriorityDequeue: fromOwnedSlice trivial case 0" { |
| | 653 | const items = [0]u32{}; |
| | 654 | const heap_items = try testing.allocator.dupe(u32, &items); |
| | 655 | var heap = Heap.fromOwnedSlice(testing.allocator, lessThanComparison, heap_items[0..]); |
| | 656 | defer heap.deinit(); |
| | 657 | expectEqual(@as(usize, 0), heap.len); |
| | 658 | expect(heap.removeMinOrNull() == null); |
| | 659 | } |
| | 660 | |
| | 661 | test "std.PriorityDequeue: fromOwnedSlice trivial case 1" { |
| | 662 | const items = [1]u32{1}; |
| | 663 | const heap_items = try testing.allocator.dupe(u32, &items); |
| | 664 | var heap = Heap.fromOwnedSlice(testing.allocator, lessThanComparison, heap_items[0..]); |
| | 665 | defer heap.deinit(); |
| | 666 | |
| | 667 | expectEqual(@as(usize, 1), heap.len); |
| | 668 | expectEqual(items[0], heap.removeMin()); |
| | 669 | expect(heap.removeMinOrNull() == null); |
| | 670 | } |
| | 671 | |
| 649 | test "std.PriorityDequeue: fromOwnedSlice" { | 672 | test "std.PriorityDequeue: fromOwnedSlice" { |
| 650 | const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 }; | 673 | const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 }; |
| 651 | const heap_items = try testing.allocator.dupe(u32, items[0..]); | 674 | const heap_items = try testing.allocator.dupe(u32, items[0..]); |