From 57b66a9223d27bf4b35d3ae002e90385195216c3 Mon Sep 17 00:00:00 2001 From: Matthew Saltz Date: Mon, 16 Sep 2024 19:53:04 -0500 Subject: [PATCH 1/6] std.PriorityQueue: Convert to an 'unmanaged'-style API Resolves: #21432. Removed the allocator field from the PriorityQueue struct and adapted functions to take an allocator where needed. Updated tests accordingly. --- lib/std/priority_queue.zig | 373 +++++++++++++++++++------------------ 1 file changed, 196 insertions(+), 177 deletions(-) diff --git a/lib/std/priority_queue.zig b/lib/std/priority_queue.zig index a5ea649c467a..05fe862962ec 100644 --- a/lib/std/priority_queue.zig +++ b/lib/std/priority_queue.zig @@ -18,33 +18,26 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF return struct { const Self = @This(); - items: []T, - cap: usize, - allocator: Allocator, + items: []T = undefined, + /// The capacity of the queue. This may be read directly, but must not + /// be modified directly. + capacity: usize = 0, context: Context, - /// Initialize and return a priority queue. - pub fn init(allocator: Allocator, context: Context) Self { - return Self{ - .items = &[_]T{}, - .cap = 0, - .allocator = allocator, - .context = context, - }; - } - /// Free memory used by the queue. - pub fn deinit(self: Self) void { - self.allocator.free(self.allocatedSlice()); + pub fn deinit(self: Self, allocator: std.mem.Allocator) void { + allocator.free(self.allocatedSlice()); } /// Insert a new element, maintaining priority. - pub fn add(self: *Self, elem: T) !void { - try self.ensureUnusedCapacity(1); - addUnchecked(self, elem); + pub fn add(self: *Self, allocator: std.mem.Allocator, elem: T) !void { + try self.ensureUnusedCapacity(allocator, 1); + self.addAssumeCapacity(elem); } - fn addUnchecked(self: *Self, elem: T) void { + /// Insert a new element, maintaining priority. Assumes there is enough + /// capacity in the queue for the additional item. + pub fn addAssumeCapacity(self: *Self, elem: T) void { self.items.len += 1; self.items[self.items.len - 1] = elem; siftUp(self, self.items.len - 1); @@ -64,10 +57,16 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF } /// Add each element in `items` to the queue. - pub fn addSlice(self: *Self, items: []const T) !void { - try self.ensureUnusedCapacity(items.len); + pub fn addSlice(self: *Self, allocator: std.mem.Allocator, items: []const T) !void { + try self.ensureUnusedCapacity(allocator, items.len); + self.addSliceAssumeCapacity(items); + } + + /// Add each element in `items` to the queue. Assumes there is enough + /// capacity in the queue for the additional items. + pub fn addSliceAssumeCapacity(self: *Self, items: []const T) void { for (items) |e| { - self.addUnchecked(e); + self.addAssumeCapacity(e); } } @@ -122,17 +121,11 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF return self.items.len; } - /// Return the number of elements that can be added to the - /// queue before more memory is allocated. - pub fn capacity(self: Self) usize { - return self.cap; - } - /// Returns a slice of all the items plus the extra capacity, whose memory /// contents are `undefined`. fn allocatedSlice(self: Self) []T { // `items.len` is the length, not the capacity. - return self.items.ptr[0..self.cap]; + return self.items.ptr[0..self.capacity]; } fn siftDown(self: *Self, target_index: usize) void { @@ -155,14 +148,14 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF self.items[index] = target_element; } - /// PriorityQueue takes ownership of the passed in slice. The slice must have been - /// allocated with `allocator`. - /// Deinitialize with `deinit`. - pub fn fromOwnedSlice(allocator: Allocator, items: []T, context: Context) Self { + /// PriorityQueue takes ownership of the passed in slice. + /// + /// Deinitialize with `deinit(allocator)`, using the allocator used to + /// allocate the passed in slice. + pub fn fromOwnedSlice(items: []T, context: Context) Self { var self = Self{ .items = items, - .cap = items.len, - .allocator = allocator, + .capacity = items.len, .context = context, }; @@ -175,40 +168,43 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF } /// Ensure that the queue can fit at least `new_capacity` items. - pub fn ensureTotalCapacity(self: *Self, new_capacity: usize) !void { - var better_capacity = self.cap; + pub fn ensureTotalCapacity(self: *Self, allocator: std.mem.Allocator, new_capacity: usize) !void { + var better_capacity = self.capacity; if (better_capacity >= new_capacity) return; while (true) { better_capacity += better_capacity / 2 + 8; if (better_capacity >= new_capacity) break; } const old_memory = self.allocatedSlice(); - const new_memory = try self.allocator.realloc(old_memory, better_capacity); + const new_memory = try allocator.realloc(old_memory, better_capacity); self.items.ptr = new_memory.ptr; - self.cap = new_memory.len; + self.capacity = new_memory.len; } /// Ensure that the queue can fit at least `additional_count` **more** item. - pub fn ensureUnusedCapacity(self: *Self, additional_count: usize) !void { - return self.ensureTotalCapacity(self.items.len + additional_count); + pub fn ensureUnusedCapacity(self: *Self, allocator: std.mem.Allocator, additional_count: usize) !void { + return self.ensureTotalCapacity( + allocator, + self.items.len + additional_count, + ); } /// Reduce allocated capacity to `new_capacity`. - pub fn shrinkAndFree(self: *Self, new_capacity: usize) void { - assert(new_capacity <= self.cap); + pub fn shrinkAndFree(self: *Self, allocator: std.mem.Allocator, new_capacity: usize) void { + assert(new_capacity <= self.capacity); // Cannot shrink to smaller than the current queue size without invalidating the heap property assert(new_capacity >= self.items.len); const old_memory = self.allocatedSlice(); - const new_memory = self.allocator.realloc(old_memory, new_capacity) catch |e| switch (e) { + const new_memory = allocator.realloc(old_memory, new_capacity) catch |e| switch (e) { error.OutOfMemory => { // no problem, capacity is still correct then. return; }, }; self.items.ptr = new_memory.ptr; - self.cap = new_memory.len; + self.capacity = new_memory.len; } pub fn update(self: *Self, elem: T, new_elem: T) !void { @@ -267,7 +263,7 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF print("{}, ", .{e}); } print("len: {} ", .{self.items.len}); - print("capacity: {}", .{self.cap}); + print("capacity: {}", .{self.capacity}); print(" }}\n", .{}); } }; @@ -286,15 +282,16 @@ const PQlt = PriorityQueue(u32, void, lessThan); const PQgt = PriorityQueue(u32, void, greaterThan); test "add and remove min heap" { - var queue = PQlt.init(testing.allocator, {}); - defer queue.deinit(); - - try queue.add(54); - try queue.add(12); - try queue.add(7); - try queue.add(23); - try queue.add(25); - try queue.add(13); + const allocator = std.testing.allocator; + var queue: PQlt = .{ .context = {} }; + defer queue.deinit(allocator); + + try queue.add(allocator, 54); + try queue.add(allocator, 12); + try queue.add(allocator, 7); + try queue.add(allocator, 23); + try queue.add(allocator, 25); + try queue.add(allocator, 13); try expectEqual(@as(u32, 7), queue.remove()); try expectEqual(@as(u32, 12), queue.remove()); try expectEqual(@as(u32, 13), queue.remove()); @@ -304,15 +301,16 @@ test "add and remove min heap" { } test "add and remove same min heap" { - var queue = PQlt.init(testing.allocator, {}); - defer queue.deinit(); - - try queue.add(1); - try queue.add(1); - try queue.add(2); - try queue.add(2); - try queue.add(1); - try queue.add(1); + const allocator = std.testing.allocator; + var queue: PQlt = .{ .context = {} }; + defer queue.deinit(allocator); + + try queue.add(allocator, 1); + try queue.add(allocator, 1); + try queue.add(allocator, 2); + try queue.add(allocator, 2); + try queue.add(allocator, 1); + try queue.add(allocator, 1); try expectEqual(@as(u32, 1), queue.remove()); try expectEqual(@as(u32, 1), queue.remove()); try expectEqual(@as(u32, 1), queue.remove()); @@ -322,43 +320,46 @@ test "add and remove same min heap" { } test "removeOrNull on empty" { - var queue = PQlt.init(testing.allocator, {}); - defer queue.deinit(); + const allocator = std.testing.allocator; + var queue: PQlt = .{ .context = {} }; + defer queue.deinit(allocator); try expect(queue.removeOrNull() == null); } test "edge case 3 elements" { - var queue = PQlt.init(testing.allocator, {}); - defer queue.deinit(); + const allocator = std.testing.allocator; + var queue: PQlt = .{ .context = {} }; + defer queue.deinit(allocator); - try queue.add(9); - try queue.add(3); - try queue.add(2); + try queue.add(allocator, 9); + try queue.add(allocator, 3); + try queue.add(allocator, 2); try expectEqual(@as(u32, 2), queue.remove()); try expectEqual(@as(u32, 3), queue.remove()); try expectEqual(@as(u32, 9), queue.remove()); } test "peek" { - var queue = PQlt.init(testing.allocator, {}); - defer queue.deinit(); + const allocator = std.testing.allocator; + var queue: PQlt = .{ .context = {} }; + defer queue.deinit(allocator); try expect(queue.peek() == null); - try queue.add(9); - try queue.add(3); - try queue.add(2); + try queue.add(allocator, 9); + try queue.add(allocator, 3); + try queue.add(allocator, 2); try expectEqual(@as(u32, 2), queue.peek().?); try expectEqual(@as(u32, 2), queue.peek().?); } test "sift up with odd indices" { - var queue = PQlt.init(testing.allocator, {}); - defer queue.deinit(); + const allocator = std.testing.allocator; + var queue: PQlt = .{ .context = {} }; + defer queue.deinit(allocator); + const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 }; - for (items) |e| { - try queue.add(e); - } + try queue.addSlice(allocator, items[0..]); const sorted_items = [_]u32{ 1, 2, 5, 6, 7, 7, 11, 12, 13, 14, 15, 15, 16, 21, 22, 24, 24, 25 }; for (sorted_items) |e| { @@ -367,10 +368,12 @@ test "sift up with odd indices" { } test "addSlice" { - var queue = PQlt.init(testing.allocator, {}); - defer queue.deinit(); + const allocator = std.testing.allocator; + var queue: PQlt = .{ .context = {} }; + defer queue.deinit(allocator); + const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 }; - try queue.addSlice(items[0..]); + try queue.addSlice(allocator, items[0..]); const sorted_items = [_]u32{ 1, 2, 5, 6, 7, 7, 11, 12, 13, 14, 15, 15, 16, 21, 22, 24, 24, 25 }; for (sorted_items) |e| { @@ -379,19 +382,21 @@ test "addSlice" { } test "fromOwnedSlice trivial case 0" { + const allocator = testing.allocator; const items = [0]u32{}; - const queue_items = try testing.allocator.dupe(u32, &items); - var queue = PQlt.fromOwnedSlice(testing.allocator, queue_items[0..], {}); - defer queue.deinit(); + const queue_items = try allocator.dupe(u32, &items); + var queue = PQlt.fromOwnedSlice(queue_items[0..], {}); + defer queue.deinit(allocator); try expectEqual(@as(usize, 0), queue.count()); try expect(queue.removeOrNull() == null); } test "fromOwnedSlice trivial case 1" { + const allocator = testing.allocator; const items = [1]u32{1}; - const queue_items = try testing.allocator.dupe(u32, &items); - var queue = PQlt.fromOwnedSlice(testing.allocator, queue_items[0..], {}); - defer queue.deinit(); + const queue_items = try allocator.dupe(u32, &items); + var queue = PQlt.fromOwnedSlice(queue_items[0..], {}); + defer queue.deinit(allocator); try expectEqual(@as(usize, 1), queue.count()); try expectEqual(items[0], queue.remove()); @@ -399,10 +404,11 @@ test "fromOwnedSlice trivial case 1" { } test "fromOwnedSlice" { + const allocator = testing.allocator; const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 }; - const heap_items = try testing.allocator.dupe(u32, items[0..]); - var queue = PQlt.fromOwnedSlice(testing.allocator, heap_items[0..], {}); - defer queue.deinit(); + const heap_items = try allocator.dupe(u32, items[0..]); + var queue = PQlt.fromOwnedSlice(heap_items[0..], {}); + defer queue.deinit(allocator); const sorted_items = [_]u32{ 1, 2, 5, 6, 7, 7, 11, 12, 13, 14, 15, 15, 16, 21, 22, 24, 24, 25 }; for (sorted_items) |e| { @@ -411,15 +417,16 @@ test "fromOwnedSlice" { } test "add and remove max heap" { - var queue = PQgt.init(testing.allocator, {}); - defer queue.deinit(); - - try queue.add(54); - try queue.add(12); - try queue.add(7); - try queue.add(23); - try queue.add(25); - try queue.add(13); + const allocator = testing.allocator; + var queue: PQgt = .{ .context = {} }; + defer queue.deinit(allocator); + + try queue.add(allocator, 54); + try queue.add(allocator, 12); + try queue.add(allocator, 7); + try queue.add(allocator, 23); + try queue.add(allocator, 25); + try queue.add(allocator, 13); try expectEqual(@as(u32, 54), queue.remove()); try expectEqual(@as(u32, 25), queue.remove()); try expectEqual(@as(u32, 23), queue.remove()); @@ -429,15 +436,16 @@ test "add and remove max heap" { } test "add and remove same max heap" { - var queue = PQgt.init(testing.allocator, {}); - defer queue.deinit(); - - try queue.add(1); - try queue.add(1); - try queue.add(2); - try queue.add(2); - try queue.add(1); - try queue.add(1); + const allocator = testing.allocator; + var queue: PQgt = .{ .context = {} }; + defer queue.deinit(allocator); + + try queue.add(allocator, 1); + try queue.add(allocator, 1); + try queue.add(allocator, 2); + try queue.add(allocator, 2); + try queue.add(allocator, 1); + try queue.add(allocator, 1); try expectEqual(@as(u32, 2), queue.remove()); try expectEqual(@as(u32, 2), queue.remove()); try expectEqual(@as(u32, 1), queue.remove()); @@ -447,16 +455,16 @@ test "add and remove same max heap" { } test "iterator" { - var queue = PQlt.init(testing.allocator, {}); - var map = std.AutoHashMap(u32, void).init(testing.allocator); - defer { - queue.deinit(); - map.deinit(); - } + const allocator = testing.allocator; + var queue: PQlt = .{ .context = {} }; + defer queue.deinit(allocator); + + var map = std.AutoHashMap(u32, void).init(allocator); + defer map.deinit(); const items = [_]u32{ 54, 12, 7, 23, 25, 13 }; for (items) |e| { - _ = try queue.add(e); + _ = try queue.add(allocator, e); try map.put(e, {}); } @@ -469,13 +477,12 @@ test "iterator" { } test "remove at index" { - var queue = PQlt.init(testing.allocator, {}); - defer queue.deinit(); + const allocator = testing.allocator; + var queue: PQlt = .{ .context = {} }; + defer queue.deinit(allocator); const items = [_]u32{ 2, 1, 8, 9, 3, 4, 5 }; - for (items) |e| { - _ = try queue.add(e); - } + try queue.addSlice(allocator, items[0..]); var it = queue.iterator(); var idx: usize = 0; @@ -495,8 +502,9 @@ test "remove at index" { } test "iterator while empty" { - var queue = PQlt.init(testing.allocator, {}); - defer queue.deinit(); + const allocator = testing.allocator; + var queue: PQlt = .{ .context = {} }; + defer queue.deinit(allocator); var it = queue.iterator(); @@ -504,20 +512,21 @@ test "iterator while empty" { } test "shrinkAndFree" { - var queue = PQlt.init(testing.allocator, {}); - defer queue.deinit(); + const allocator = std.testing.allocator; + var queue: PQlt = .{ .context = {} }; + defer queue.deinit(allocator); - try queue.ensureTotalCapacity(4); - try expect(queue.capacity() >= 4); + try queue.ensureTotalCapacity(allocator, 4); + try expect(queue.capacity >= 4); - try queue.add(1); - try queue.add(2); - try queue.add(3); - try expect(queue.capacity() >= 4); + try queue.add(allocator, 1); + try queue.add(allocator, 2); + try queue.add(allocator, 3); + try expect(queue.capacity >= 4); try expectEqual(@as(usize, 3), queue.count()); - queue.shrinkAndFree(3); - try expectEqual(@as(usize, 3), queue.capacity()); + queue.shrinkAndFree(allocator, 3); + try expectEqual(@as(usize, 3), queue.capacity); try expectEqual(@as(usize, 3), queue.count()); try expectEqual(@as(u32, 1), queue.remove()); @@ -527,12 +536,13 @@ test "shrinkAndFree" { } test "update min heap" { - var queue = PQlt.init(testing.allocator, {}); - defer queue.deinit(); + const allocator = std.testing.allocator; + var queue: PQlt = .{ .context = {} }; + defer queue.deinit(allocator); - try queue.add(55); - try queue.add(44); - try queue.add(11); + try queue.add(allocator, 55); + try queue.add(allocator, 44); + try queue.add(allocator, 11); try queue.update(55, 5); try queue.update(44, 4); try queue.update(11, 1); @@ -542,13 +552,14 @@ test "update min heap" { } test "update same min heap" { - var queue = PQlt.init(testing.allocator, {}); - defer queue.deinit(); - - try queue.add(1); - try queue.add(1); - try queue.add(2); - try queue.add(2); + const allocator = std.testing.allocator; + var queue: PQlt = .{ .context = {} }; + defer queue.deinit(allocator); + + try queue.add(allocator, 1); + try queue.add(allocator, 1); + try queue.add(allocator, 2); + try queue.add(allocator, 2); try queue.update(1, 5); try queue.update(2, 4); try expectEqual(@as(u32, 1), queue.remove()); @@ -558,12 +569,13 @@ test "update same min heap" { } test "update max heap" { - var queue = PQgt.init(testing.allocator, {}); - defer queue.deinit(); + const allocator = std.testing.allocator; + var queue: PQgt = .{ .context = {} }; + defer queue.deinit(allocator); - try queue.add(55); - try queue.add(44); - try queue.add(11); + try queue.add(allocator, 55); + try queue.add(allocator, 44); + try queue.add(allocator, 11); try queue.update(55, 5); try queue.update(44, 1); try queue.update(11, 4); @@ -573,13 +585,14 @@ test "update max heap" { } test "update same max heap" { - var queue = PQgt.init(testing.allocator, {}); - defer queue.deinit(); - - try queue.add(1); - try queue.add(1); - try queue.add(2); - try queue.add(2); + const allocator = std.testing.allocator; + var queue: PQgt = .{ .context = {} }; + defer queue.deinit(allocator); + + try queue.add(allocator, 1); + try queue.add(allocator, 1); + try queue.add(allocator, 2); + try queue.add(allocator, 2); try queue.update(1, 5); try queue.update(2, 4); try expectEqual(@as(u32, 5), queue.remove()); @@ -589,19 +602,24 @@ test "update same max heap" { } test "update after remove" { - var queue = PQlt.init(testing.allocator, {}); - defer queue.deinit(); + const allocator = std.testing.allocator; + var queue: PQlt = .{ .context = {} }; + defer queue.deinit(allocator); - try queue.add(1); + try queue.add(allocator, 1); try expectEqual(@as(u32, 1), queue.remove()); try expectError(error.ElementNotFound, queue.update(1, 1)); } test "siftUp in remove" { - var queue = PQlt.init(testing.allocator, {}); - defer queue.deinit(); + const allocator = std.testing.allocator; + var queue: PQlt = .{ .context = {} }; + defer queue.deinit(allocator); - try queue.addSlice(&.{ 0, 1, 100, 2, 3, 101, 102, 4, 5, 6, 7, 103, 104, 105, 106, 8 }); + try queue.addSlice( + allocator, + &.{ 0, 1, 100, 2, 3, 101, 102, 4, 5, 6, 7, 103, 104, 105, 106, 8 }, + ); _ = queue.removeIndex(std.mem.indexOfScalar(u32, queue.items[0..queue.count()], 102).?); @@ -618,18 +636,19 @@ fn contextLessThan(context: []const u32, a: usize, b: usize) Order { const CPQlt = PriorityQueue(usize, []const u32, contextLessThan); test "add and remove min heap with context comparator" { - const context = [_]u32{ 5, 3, 4, 2, 2, 8, 0 }; - - var queue = CPQlt.init(testing.allocator, context[0..]); - defer queue.deinit(); - - try queue.add(0); - try queue.add(1); - try queue.add(2); - try queue.add(3); - try queue.add(4); - try queue.add(5); - try queue.add(6); + const allocator = std.testing.allocator; + var queue: CPQlt = .{ + .context = &[_]u32{ 5, 3, 4, 2, 2, 8, 0 }, + }; + defer queue.deinit(allocator); + + try queue.add(allocator, 0); + try queue.add(allocator, 1); + try queue.add(allocator, 2); + try queue.add(allocator, 3); + try queue.add(allocator, 4); + try queue.add(allocator, 5); + try queue.add(allocator, 6); try expectEqual(@as(usize, 6), queue.remove()); try expectEqual(@as(usize, 4), queue.remove()); try expectEqual(@as(usize, 3), queue.remove()); From 775840369ad940edd821392b86f8390245be40d1 Mon Sep 17 00:00:00 2001 From: Matthew Saltz Date: Tue, 17 Sep 2024 15:58:37 -0500 Subject: [PATCH 2/6] Add init() method back to PriorityQueue --- lib/std/priority_queue.zig | 62 +++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/lib/std/priority_queue.zig b/lib/std/priority_queue.zig index 05fe862962ec..ad7672cacb81 100644 --- a/lib/std/priority_queue.zig +++ b/lib/std/priority_queue.zig @@ -18,12 +18,21 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF return struct { const Self = @This(); - items: []T = undefined, + items: []T, /// The capacity of the queue. This may be read directly, but must not /// be modified directly. - capacity: usize = 0, + capacity: usize, context: Context, + /// Initialize and return an empty PriorityQueue. + pub fn init(context: Context) Self { + return .{ + .items = &[_]T{}, + .capacity = 0, + .context = context, + }; + } + /// Free memory used by the queue. pub fn deinit(self: Self, allocator: std.mem.Allocator) void { allocator.free(self.allocatedSlice()); @@ -168,7 +177,11 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF } /// Ensure that the queue can fit at least `new_capacity` items. - pub fn ensureTotalCapacity(self: *Self, allocator: std.mem.Allocator, new_capacity: usize) !void { + pub fn ensureTotalCapacity( + self: *Self, + allocator: std.mem.Allocator, + new_capacity: usize, + ) !void { var better_capacity = self.capacity; if (better_capacity >= new_capacity) return; while (true) { @@ -283,7 +296,7 @@ const PQgt = PriorityQueue(u32, void, greaterThan); test "add and remove min heap" { const allocator = std.testing.allocator; - var queue: PQlt = .{ .context = {} }; + var queue = PQlt.init({}); defer queue.deinit(allocator); try queue.add(allocator, 54); @@ -302,7 +315,7 @@ test "add and remove min heap" { test "add and remove same min heap" { const allocator = std.testing.allocator; - var queue: PQlt = .{ .context = {} }; + var queue = PQlt.init({}); defer queue.deinit(allocator); try queue.add(allocator, 1); @@ -321,7 +334,7 @@ test "add and remove same min heap" { test "removeOrNull on empty" { const allocator = std.testing.allocator; - var queue: PQlt = .{ .context = {} }; + var queue = PQlt.init({}); defer queue.deinit(allocator); try expect(queue.removeOrNull() == null); @@ -329,7 +342,7 @@ test "removeOrNull on empty" { test "edge case 3 elements" { const allocator = std.testing.allocator; - var queue: PQlt = .{ .context = {} }; + var queue = PQlt.init({}); defer queue.deinit(allocator); try queue.add(allocator, 9); @@ -342,7 +355,7 @@ test "edge case 3 elements" { test "peek" { const allocator = std.testing.allocator; - var queue: PQlt = .{ .context = {} }; + var queue = PQlt.init({}); defer queue.deinit(allocator); try expect(queue.peek() == null); @@ -355,7 +368,7 @@ test "peek" { test "sift up with odd indices" { const allocator = std.testing.allocator; - var queue: PQlt = .{ .context = {} }; + var queue = PQlt.init({}); defer queue.deinit(allocator); const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 }; @@ -369,7 +382,7 @@ test "sift up with odd indices" { test "addSlice" { const allocator = std.testing.allocator; - var queue: PQlt = .{ .context = {} }; + var queue = PQlt.init({}); defer queue.deinit(allocator); const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 }; @@ -418,7 +431,7 @@ test "fromOwnedSlice" { test "add and remove max heap" { const allocator = testing.allocator; - var queue: PQgt = .{ .context = {} }; + var queue = PQgt.init({}); defer queue.deinit(allocator); try queue.add(allocator, 54); @@ -437,7 +450,7 @@ test "add and remove max heap" { test "add and remove same max heap" { const allocator = testing.allocator; - var queue: PQgt = .{ .context = {} }; + var queue = PQgt.init({}); defer queue.deinit(allocator); try queue.add(allocator, 1); @@ -456,7 +469,7 @@ test "add and remove same max heap" { test "iterator" { const allocator = testing.allocator; - var queue: PQlt = .{ .context = {} }; + var queue = PQlt.init({}); defer queue.deinit(allocator); var map = std.AutoHashMap(u32, void).init(allocator); @@ -478,7 +491,7 @@ test "iterator" { test "remove at index" { const allocator = testing.allocator; - var queue: PQlt = .{ .context = {} }; + var queue = PQlt.init({}); defer queue.deinit(allocator); const items = [_]u32{ 2, 1, 8, 9, 3, 4, 5 }; @@ -503,7 +516,7 @@ test "remove at index" { test "iterator while empty" { const allocator = testing.allocator; - var queue: PQlt = .{ .context = {} }; + var queue = PQlt.init({}); defer queue.deinit(allocator); var it = queue.iterator(); @@ -513,7 +526,7 @@ test "iterator while empty" { test "shrinkAndFree" { const allocator = std.testing.allocator; - var queue: PQlt = .{ .context = {} }; + var queue = PQlt.init({}); defer queue.deinit(allocator); try queue.ensureTotalCapacity(allocator, 4); @@ -537,7 +550,7 @@ test "shrinkAndFree" { test "update min heap" { const allocator = std.testing.allocator; - var queue: PQlt = .{ .context = {} }; + var queue = PQlt.init({}); defer queue.deinit(allocator); try queue.add(allocator, 55); @@ -553,7 +566,7 @@ test "update min heap" { test "update same min heap" { const allocator = std.testing.allocator; - var queue: PQlt = .{ .context = {} }; + var queue = PQlt.init({}); defer queue.deinit(allocator); try queue.add(allocator, 1); @@ -570,7 +583,7 @@ test "update same min heap" { test "update max heap" { const allocator = std.testing.allocator; - var queue: PQgt = .{ .context = {} }; + var queue = PQgt.init({}); defer queue.deinit(allocator); try queue.add(allocator, 55); @@ -586,7 +599,7 @@ test "update max heap" { test "update same max heap" { const allocator = std.testing.allocator; - var queue: PQgt = .{ .context = {} }; + var queue = PQgt.init({}); defer queue.deinit(allocator); try queue.add(allocator, 1); @@ -603,7 +616,7 @@ test "update same max heap" { test "update after remove" { const allocator = std.testing.allocator; - var queue: PQlt = .{ .context = {} }; + var queue = PQlt.init({}); defer queue.deinit(allocator); try queue.add(allocator, 1); @@ -613,7 +626,7 @@ test "update after remove" { test "siftUp in remove" { const allocator = std.testing.allocator; - var queue: PQlt = .{ .context = {} }; + var queue = PQlt.init({}); defer queue.deinit(allocator); try queue.addSlice( @@ -637,9 +650,8 @@ const CPQlt = PriorityQueue(usize, []const u32, contextLessThan); test "add and remove min heap with context comparator" { const allocator = std.testing.allocator; - var queue: CPQlt = .{ - .context = &[_]u32{ 5, 3, 4, 2, 2, 8, 0 }, - }; + const context = [_]u32{ 5, 3, 4, 2, 2, 8, 0 }; + var queue = CPQlt.init(&context); defer queue.deinit(allocator); try queue.add(allocator, 0); From da0e21c5c62af7568a1d4cbaf6bfcff3030efd32 Mon Sep 17 00:00:00 2001 From: Matthew Saltz Date: Tue, 17 Sep 2024 15:58:49 -0500 Subject: [PATCH 3/6] std.PriorityDequeue: Convert to an 'unmanaged'-style API Removed the allocator field from the PriorityDequeue struct and adapted functions to take an allocator where needed. Updated tests accordingly. --- lib/std/priority_dequeue.zig | 400 +++++++++++++++++++---------------- 1 file changed, 218 insertions(+), 182 deletions(-) diff --git a/lib/std/priority_dequeue.zig b/lib/std/priority_dequeue.zig index 520288be7864..a932194d5078 100644 --- a/lib/std/priority_dequeue.zig +++ b/lib/std/priority_dequeue.zig @@ -21,39 +21,39 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar items: []T, len: usize, - allocator: Allocator, context: Context, /// Initialize and return a new priority dequeue. - pub fn init(allocator: Allocator, context: Context) Self { + pub fn init(context: Context) Self { return Self{ .items = &[_]T{}, .len = 0, - .allocator = allocator, .context = context, }; } /// Free memory used by the dequeue. - pub fn deinit(self: Self) void { - self.allocator.free(self.items); + pub fn deinit(self: Self, allocator: std.mem.Allocator) void { + allocator.free(self.items); } /// Insert a new element, maintaining priority. - pub fn add(self: *Self, elem: T) !void { - try self.ensureUnusedCapacity(1); - addUnchecked(self, elem); + pub fn add(self: *Self, allocator: std.mem.Allocator, elem: T) !void { + try self.ensureUnusedCapacity(allocator, 1); + self.addAssumeCapacity(elem); } /// Add each element in `items` to the dequeue. - pub fn addSlice(self: *Self, items: []const T) !void { - try self.ensureUnusedCapacity(items.len); + pub fn addSlice(self: *Self, allocator: std.mem.Allocator, items: []const T) !void { + try self.ensureUnusedCapacity(allocator, items.len); for (items) |e| { - self.addUnchecked(e); + self.addAssumeCapacity(e); } } - fn addUnchecked(self: *Self, elem: T) void { + /// Insert a new element, maintaining priority. Assumes there is enough + /// capacity in the queue for the additional item. + pub fn addAssumeCapacity(self: *Self, elem: T) void { self.items[self.len] = elem; if (self.len > 0) { @@ -335,12 +335,13 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar /// Dequeue takes ownership of the passed in slice. The slice must have been /// allocated with `allocator`. - /// De-initialize with `deinit`. - pub fn fromOwnedSlice(allocator: Allocator, items: []T, context: Context) Self { + /// + /// Deinitialize with `deinit(allocator)`, using the allocator used to + /// allocate the passed in slice. + pub fn fromOwnedSlice(items: []T, context: Context) Self { var queue = Self{ .items = items, .len = items.len, - .allocator = allocator, .context = context, }; @@ -356,29 +357,37 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar } /// Ensure that the dequeue can fit at least `new_capacity` items. - pub fn ensureTotalCapacity(self: *Self, new_capacity: usize) !void { + pub fn ensureTotalCapacity( + self: *Self, + allocator: std.mem.Allocator, + new_capacity: usize, + ) !void { var better_capacity = self.capacity(); if (better_capacity >= new_capacity) return; while (true) { better_capacity += better_capacity / 2 + 8; if (better_capacity >= new_capacity) break; } - self.items = try self.allocator.realloc(self.items, better_capacity); + self.items = try allocator.realloc(self.items, better_capacity); } /// Ensure that the dequeue can fit at least `additional_count` **more** items. - pub fn ensureUnusedCapacity(self: *Self, additional_count: usize) !void { - return self.ensureTotalCapacity(self.len + additional_count); + pub fn ensureUnusedCapacity( + self: *Self, + allocator: std.mem.Allocator, + additional_count: usize, + ) !void { + return self.ensureTotalCapacity(allocator, self.len + additional_count); } /// Reduce allocated capacity to `new_len`. - pub fn shrinkAndFree(self: *Self, new_len: usize) void { + pub fn shrinkAndFree(self: *Self, allocator: std.mem.Allocator, new_len: usize) void { assert(new_len <= self.items.len); // Cannot shrink to smaller than the current queue size without invalidating the heap property assert(new_len >= self.len); - self.items = self.allocator.realloc(self.items[0..], new_len) catch |e| switch (e) { + self.items = allocator.realloc(self.items[0..], new_len) catch |e| switch (e) { error.OutOfMemory => { // no problem, capacity is still correct then. self.items.len = new_len; return; @@ -396,7 +405,7 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar return error.ElementNotFound; }; _ = self.removeIndex(old_index); - self.addUnchecked(new_elem); + self.addAssumeCapacity(new_elem); } pub const Iterator = struct { @@ -468,15 +477,16 @@ fn lessThanComparison(context: void, a: u32, b: u32) Order { const PDQ = PriorityDequeue(u32, void, lessThanComparison); test "add and remove min" { - var queue = PDQ.init(testing.allocator, {}); - defer queue.deinit(); + const allocator = std.testing.allocator; + var queue = PDQ.init({}); + defer queue.deinit(allocator); - try queue.add(54); - try queue.add(12); - try queue.add(7); - try queue.add(23); - try queue.add(25); - try queue.add(13); + try queue.add(allocator, 54); + try queue.add(allocator, 12); + try queue.add(allocator, 7); + try queue.add(allocator, 23); + try queue.add(allocator, 25); + try queue.add(allocator, 13); try expectEqual(@as(u32, 7), queue.removeMin()); try expectEqual(@as(u32, 12), queue.removeMin()); @@ -487,6 +497,7 @@ test "add and remove min" { } test "add and remove min structs" { + const allocator = std.testing.allocator; const S = struct { size: u32, }; @@ -495,15 +506,15 @@ test "add and remove min structs" { _ = context; return std.math.order(a.size, b.size); } - }.order).init(testing.allocator, {}); - defer queue.deinit(); + }.order).init({}); + defer queue.deinit(allocator); - try queue.add(.{ .size = 54 }); - try queue.add(.{ .size = 12 }); - try queue.add(.{ .size = 7 }); - try queue.add(.{ .size = 23 }); - try queue.add(.{ .size = 25 }); - try queue.add(.{ .size = 13 }); + try queue.add(allocator, .{ .size = 54 }); + try queue.add(allocator, .{ .size = 12 }); + try queue.add(allocator, .{ .size = 7 }); + try queue.add(allocator, .{ .size = 23 }); + try queue.add(allocator, .{ .size = 25 }); + try queue.add(allocator, .{ .size = 13 }); try expectEqual(@as(u32, 7), queue.removeMin().size); try expectEqual(@as(u32, 12), queue.removeMin().size); @@ -514,15 +525,16 @@ test "add and remove min structs" { } test "add and remove max" { - var queue = PDQ.init(testing.allocator, {}); - defer queue.deinit(); + const allocator = std.testing.allocator; + var queue = PDQ.init({}); + defer queue.deinit(allocator); - try queue.add(54); - try queue.add(12); - try queue.add(7); - try queue.add(23); - try queue.add(25); - try queue.add(13); + try queue.add(allocator, 54); + try queue.add(allocator, 12); + try queue.add(allocator, 7); + try queue.add(allocator, 23); + try queue.add(allocator, 25); + try queue.add(allocator, 13); try expectEqual(@as(u32, 54), queue.removeMax()); try expectEqual(@as(u32, 25), queue.removeMax()); @@ -533,15 +545,16 @@ test "add and remove max" { } test "add and remove same min" { - var queue = PDQ.init(testing.allocator, {}); - defer queue.deinit(); + const allocator = std.testing.allocator; + var queue = PDQ.init({}); + defer queue.deinit(allocator); - try queue.add(1); - try queue.add(1); - try queue.add(2); - try queue.add(2); - try queue.add(1); - try queue.add(1); + try queue.add(allocator, 1); + try queue.add(allocator, 1); + try queue.add(allocator, 2); + try queue.add(allocator, 2); + try queue.add(allocator, 1); + try queue.add(allocator, 1); try expectEqual(@as(u32, 1), queue.removeMin()); try expectEqual(@as(u32, 1), queue.removeMin()); @@ -552,15 +565,16 @@ test "add and remove same min" { } test "add and remove same max" { - var queue = PDQ.init(testing.allocator, {}); - defer queue.deinit(); + const allocator = std.testing.allocator; + var queue = PDQ.init({}); + defer queue.deinit(allocator); - try queue.add(1); - try queue.add(1); - try queue.add(2); - try queue.add(2); - try queue.add(1); - try queue.add(1); + try queue.add(allocator, 1); + try queue.add(allocator, 1); + try queue.add(allocator, 2); + try queue.add(allocator, 2); + try queue.add(allocator, 1); + try queue.add(allocator, 1); try expectEqual(@as(u32, 2), queue.removeMax()); try expectEqual(@as(u32, 2), queue.removeMax()); @@ -571,20 +585,21 @@ test "add and remove same max" { } test "removeOrNull empty" { - var queue = PDQ.init(testing.allocator, {}); - defer queue.deinit(); + var queue = PDQ.init({}); + defer queue.deinit(testing.allocator); try expect(queue.removeMinOrNull() == null); try expect(queue.removeMaxOrNull() == null); } test "edge case 3 elements" { - var queue = PDQ.init(testing.allocator, {}); - defer queue.deinit(); + const allocator = std.testing.allocator; + var queue = PDQ.init({}); + defer queue.deinit(allocator); - try queue.add(9); - try queue.add(3); - try queue.add(2); + try queue.add(allocator, 9); + try queue.add(allocator, 3); + try queue.add(allocator, 2); try expectEqual(@as(u32, 2), queue.removeMin()); try expectEqual(@as(u32, 3), queue.removeMin()); @@ -592,12 +607,13 @@ test "edge case 3 elements" { } test "edge case 3 elements max" { - var queue = PDQ.init(testing.allocator, {}); - defer queue.deinit(); + const allocator = std.testing.allocator; + var queue = PDQ.init({}); + defer queue.deinit(allocator); - try queue.add(9); - try queue.add(3); - try queue.add(2); + try queue.add(allocator, 9); + try queue.add(allocator, 3); + try queue.add(allocator, 2); try expectEqual(@as(u32, 9), queue.removeMax()); try expectEqual(@as(u32, 3), queue.removeMax()); @@ -605,40 +621,41 @@ test "edge case 3 elements max" { } test "peekMin" { - var queue = PDQ.init(testing.allocator, {}); - defer queue.deinit(); + const allocator = std.testing.allocator; + var queue = PDQ.init({}); + defer queue.deinit(allocator); try expect(queue.peekMin() == null); - try queue.add(9); - try queue.add(3); - try queue.add(2); + try queue.add(allocator, 9); + try queue.add(allocator, 3); + try queue.add(allocator, 2); try expect(queue.peekMin().? == 2); try expect(queue.peekMin().? == 2); } test "peekMax" { - var queue = PDQ.init(testing.allocator, {}); - defer queue.deinit(); + const allocator = std.testing.allocator; + var queue = PDQ.init({}); + defer queue.deinit(allocator); try expect(queue.peekMin() == null); - try queue.add(9); - try queue.add(3); - try queue.add(2); + try queue.add(allocator, 9); + try queue.add(allocator, 3); + try queue.add(allocator, 2); try expect(queue.peekMax().? == 9); try expect(queue.peekMax().? == 9); } test "sift up with odd indices, removeMin" { - var queue = PDQ.init(testing.allocator, {}); - defer queue.deinit(); + const allocator = std.testing.allocator; + var queue = PDQ.init({}); + defer queue.deinit(allocator); const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 }; - for (items) |e| { - try queue.add(e); - } + try queue.addSlice(allocator, items[0..]); const sorted_items = [_]u32{ 1, 2, 5, 6, 7, 7, 11, 12, 13, 14, 15, 15, 16, 21, 22, 24, 24, 25 }; for (sorted_items) |e| { @@ -647,12 +664,11 @@ test "sift up with odd indices, removeMin" { } test "sift up with odd indices, removeMax" { - var queue = PDQ.init(testing.allocator, {}); - defer queue.deinit(); + const allocator = std.testing.allocator; + var queue = PDQ.init({}); + defer queue.deinit(allocator); const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 }; - for (items) |e| { - try queue.add(e); - } + try queue.addSlice(allocator, items[0..]); const sorted_items = [_]u32{ 25, 24, 24, 22, 21, 16, 15, 15, 14, 13, 12, 11, 7, 7, 6, 5, 2, 1 }; for (sorted_items) |e| { @@ -661,10 +677,11 @@ test "sift up with odd indices, removeMax" { } test "addSlice min" { - var queue = PDQ.init(testing.allocator, {}); - defer queue.deinit(); + const allocator = std.testing.allocator; + var queue = PDQ.init({}); + defer queue.deinit(allocator); const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 }; - try queue.addSlice(items[0..]); + try queue.addSlice(allocator, items[0..]); const sorted_items = [_]u32{ 1, 2, 5, 6, 7, 7, 11, 12, 13, 14, 15, 15, 16, 21, 22, 24, 24, 25 }; for (sorted_items) |e| { @@ -673,10 +690,11 @@ test "addSlice min" { } test "addSlice max" { - var queue = PDQ.init(testing.allocator, {}); - defer queue.deinit(); + const allocator = std.testing.allocator; + var queue = PDQ.init({}); + defer queue.deinit(allocator); const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 }; - try queue.addSlice(items[0..]); + try queue.addSlice(allocator, items[0..]); const sorted_items = [_]u32{ 25, 24, 24, 22, 21, 16, 15, 15, 14, 13, 12, 11, 7, 7, 6, 5, 2, 1 }; for (sorted_items) |e| { @@ -685,19 +703,24 @@ test "addSlice max" { } test "fromOwnedSlice trivial case 0" { + const allocator = std.testing.allocator; const items = [0]u32{}; - const queue_items = try testing.allocator.dupe(u32, &items); - var queue = PDQ.fromOwnedSlice(testing.allocator, queue_items[0..], {}); - defer queue.deinit(); + const queue_items = try allocator.dupe(u32, &items); + + var queue = PDQ.fromOwnedSlice(queue_items[0..], {}); + defer queue.deinit(allocator); + try expectEqual(@as(usize, 0), queue.len); try expect(queue.removeMinOrNull() == null); } test "fromOwnedSlice trivial case 1" { + const allocator = std.testing.allocator; const items = [1]u32{1}; - const queue_items = try testing.allocator.dupe(u32, &items); - var queue = PDQ.fromOwnedSlice(testing.allocator, queue_items[0..], {}); - defer queue.deinit(); + const queue_items = try allocator.dupe(u32, &items); + + var queue = PDQ.fromOwnedSlice(queue_items[0..], {}); + defer queue.deinit(allocator); try expectEqual(@as(usize, 1), queue.len); try expectEqual(items[0], queue.removeMin()); @@ -705,10 +728,12 @@ test "fromOwnedSlice trivial case 1" { } test "fromOwnedSlice" { + const allocator = std.testing.allocator; const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 }; - const queue_items = try testing.allocator.dupe(u32, items[0..]); - var queue = PDQ.fromOwnedSlice(testing.allocator, queue_items[0..], {}); - defer queue.deinit(); + const queue_items = try allocator.dupe(u32, items[0..]); + + var queue = PDQ.fromOwnedSlice(queue_items[0..], {}); + defer queue.deinit(allocator); const sorted_items = [_]u32{ 1, 2, 5, 6, 7, 7, 11, 12, 13, 14, 15, 15, 16, 21, 22, 24, 24, 25 }; for (sorted_items) |e| { @@ -717,12 +742,13 @@ test "fromOwnedSlice" { } test "update min queue" { - var queue = PDQ.init(testing.allocator, {}); - defer queue.deinit(); + const allocator = std.testing.allocator; + var queue = PDQ.init({}); + defer queue.deinit(allocator); - try queue.add(55); - try queue.add(44); - try queue.add(11); + try queue.add(allocator, 55); + try queue.add(allocator, 44); + try queue.add(allocator, 11); try queue.update(55, 5); try queue.update(44, 4); try queue.update(11, 1); @@ -732,13 +758,14 @@ test "update min queue" { } test "update same min queue" { - var queue = PDQ.init(testing.allocator, {}); - defer queue.deinit(); - - try queue.add(1); - try queue.add(1); - try queue.add(2); - try queue.add(2); + const allocator = std.testing.allocator; + var queue = PDQ.init({}); + defer queue.deinit(allocator); + + try queue.add(allocator, 1); + try queue.add(allocator, 1); + try queue.add(allocator, 2); + try queue.add(allocator, 2); try queue.update(1, 5); try queue.update(2, 4); try expectEqual(@as(u32, 1), queue.removeMin()); @@ -748,12 +775,13 @@ test "update same min queue" { } test "update max queue" { - var queue = PDQ.init(testing.allocator, {}); - defer queue.deinit(); + const allocator = std.testing.allocator; + var queue = PDQ.init({}); + defer queue.deinit(allocator); - try queue.add(55); - try queue.add(44); - try queue.add(11); + try queue.add(allocator, 55); + try queue.add(allocator, 44); + try queue.add(allocator, 11); try queue.update(55, 5); try queue.update(44, 1); try queue.update(11, 4); @@ -764,13 +792,14 @@ test "update max queue" { } test "update same max queue" { - var queue = PDQ.init(testing.allocator, {}); - defer queue.deinit(); - - try queue.add(1); - try queue.add(1); - try queue.add(2); - try queue.add(2); + const allocator = std.testing.allocator; + var queue = PDQ.init({}); + defer queue.deinit(allocator); + + try queue.add(allocator, 1); + try queue.add(allocator, 1); + try queue.add(allocator, 2); + try queue.add(allocator, 2); try queue.update(1, 5); try queue.update(2, 4); try expectEqual(@as(u32, 5), queue.removeMax()); @@ -780,25 +809,27 @@ test "update same max queue" { } test "update after remove" { - var queue = PDQ.init(testing.allocator, {}); - defer queue.deinit(); + const allocator = std.testing.allocator; + var queue = PDQ.init({}); + defer queue.deinit(allocator); - try queue.add(1); + try queue.add(allocator, 1); try expectEqual(@as(u32, 1), queue.removeMin()); try expectError(error.ElementNotFound, queue.update(1, 1)); } test "iterator" { - var queue = PDQ.init(testing.allocator, {}); - var map = std.AutoHashMap(u32, void).init(testing.allocator); - defer { - queue.deinit(); - map.deinit(); - } + const allocator = std.testing.allocator; + + var queue = PDQ.init({}); + defer queue.deinit(allocator); + + var map = std.AutoHashMap(u32, void).init(allocator); + defer map.deinit(); const items = [_]u32{ 54, 12, 7, 23, 25, 13 }; for (items) |e| { - _ = try queue.add(e); + _ = try queue.add(allocator, e); _ = try map.put(e, {}); } @@ -811,12 +842,13 @@ test "iterator" { } test "remove at index" { - var queue = PDQ.init(testing.allocator, {}); - defer queue.deinit(); + const allocator = std.testing.allocator; + var queue = PDQ.init({}); + defer queue.deinit(allocator); - try queue.add(3); - try queue.add(2); - try queue.add(1); + try queue.add(allocator, 3); + try queue.add(allocator, 2); + try queue.add(allocator, 1); var it = queue.iterator(); var elem = it.next(); @@ -834,8 +866,9 @@ test "remove at index" { } test "iterator while empty" { - var queue = PDQ.init(testing.allocator, {}); - defer queue.deinit(); + const allocator = std.testing.allocator; + var queue = PDQ.init({}); + defer queue.deinit(allocator); var it = queue.iterator(); @@ -843,19 +876,20 @@ test "iterator while empty" { } test "shrinkAndFree" { - var queue = PDQ.init(testing.allocator, {}); - defer queue.deinit(); + const allocator = std.testing.allocator; + var queue = PDQ.init({}); + defer queue.deinit(allocator); - try queue.ensureTotalCapacity(4); + try queue.ensureTotalCapacity(allocator, 4); try expect(queue.capacity() >= 4); - try queue.add(1); - try queue.add(2); - try queue.add(3); + try queue.add(allocator, 1); + try queue.add(allocator, 2); + try queue.add(allocator, 3); try expect(queue.capacity() >= 4); try expectEqual(@as(usize, 3), queue.len); - queue.shrinkAndFree(3); + queue.shrinkAndFree(allocator, 3); try expectEqual(@as(usize, 3), queue.capacity()); try expectEqual(@as(usize, 3), queue.len); @@ -882,8 +916,8 @@ fn fuzzTestMin(rng: std.Random, comptime queue_size: usize) !void { const allocator = testing.allocator; const items = try generateRandomSlice(allocator, rng, queue_size); - var queue = PDQ.fromOwnedSlice(allocator, items, {}); - defer queue.deinit(); + var queue = PDQ.fromOwnedSlice(items, {}); + defer queue.deinit(allocator); var last_removed: ?u32 = null; while (queue.removeMinOrNull()) |next| { @@ -911,8 +945,8 @@ fn fuzzTestMax(rng: std.Random, queue_size: usize) !void { const allocator = testing.allocator; const items = try generateRandomSlice(allocator, rng, queue_size); - var queue = PDQ.fromOwnedSlice(testing.allocator, items, {}); - defer queue.deinit(); + var queue = PDQ.fromOwnedSlice(items, {}); + defer queue.deinit(allocator); var last_removed: ?u32 = null; while (queue.removeMaxOrNull()) |next| { @@ -940,8 +974,8 @@ fn fuzzTestMinMax(rng: std.Random, queue_size: usize) !void { const allocator = testing.allocator; const items = try generateRandomSlice(allocator, rng, queue_size); - var queue = PDQ.fromOwnedSlice(allocator, items, {}); - defer queue.deinit(); + var queue = PDQ.fromOwnedSlice(items, {}); + defer queue.deinit(allocator); var last_min: ?u32 = null; var last_max: ?u32 = null; @@ -985,16 +1019,17 @@ const CPDQ = PriorityDequeue(usize, []const u32, contextLessThanComparison); test "add and remove" { const context = [_]u32{ 5, 3, 4, 2, 2, 8, 0 }; - var queue = CPDQ.init(testing.allocator, context[0..]); - defer queue.deinit(); - - try queue.add(0); - try queue.add(1); - try queue.add(2); - try queue.add(3); - try queue.add(4); - try queue.add(5); - try queue.add(6); + const allocator = std.testing.allocator; + var queue = CPDQ.init(context[0..]); + defer queue.deinit(allocator); + + try queue.add(allocator, 0); + try queue.add(allocator, 1); + try queue.add(allocator, 2); + try queue.add(allocator, 3); + try queue.add(allocator, 4); + try queue.add(allocator, 5); + try queue.add(allocator, 6); try expectEqual(@as(usize, 6), queue.removeMin()); try expectEqual(@as(usize, 5), queue.removeMax()); try expectEqual(@as(usize, 3), queue.removeMin()); @@ -1007,20 +1042,21 @@ test "add and remove" { var all_cmps_unique = true; test "don't compare a value to a copy of itself" { + const allocator = std.testing.allocator; var depq = PriorityDequeue(u32, void, struct { fn uniqueLessThan(_: void, a: u32, b: u32) Order { all_cmps_unique = all_cmps_unique and (a != b); return std.math.order(a, b); } - }.uniqueLessThan).init(testing.allocator, {}); - defer depq.deinit(); - - try depq.add(1); - try depq.add(2); - try depq.add(3); - try depq.add(4); - try depq.add(5); - try depq.add(6); + }.uniqueLessThan).init({}); + defer depq.deinit(allocator); + + try depq.add(allocator, 1); + try depq.add(allocator, 2); + try depq.add(allocator, 3); + try depq.add(allocator, 4); + try depq.add(allocator, 5); + try depq.add(allocator, 6); _ = depq.removeIndex(2); try expectEqual(all_cmps_unique, true); From 42981fec07e68ef0aff365dc012134c4cf4e9f62 Mon Sep 17 00:00:00 2001 From: Matthew Saltz Date: Tue, 17 Sep 2024 16:25:15 -0500 Subject: [PATCH 4/6] Change PriorityQueue to store current length rather than capacity --- lib/std/priority_dequeue.zig | 4 +- lib/std/priority_queue.zig | 88 ++++++++++++++++-------------------- 2 files changed, 43 insertions(+), 49 deletions(-) diff --git a/lib/std/priority_dequeue.zig b/lib/std/priority_dequeue.zig index a932194d5078..7d2273522abb 100644 --- a/lib/std/priority_dequeue.zig +++ b/lib/std/priority_dequeue.zig @@ -34,7 +34,9 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar /// Free memory used by the dequeue. pub fn deinit(self: Self, allocator: std.mem.Allocator) void { - allocator.free(self.items); + if (self.items.len > 0) { + allocator.free(self.items); + } } /// Insert a new element, maintaining priority. diff --git a/lib/std/priority_queue.zig b/lib/std/priority_queue.zig index ad7672cacb81..6e61f62cbb45 100644 --- a/lib/std/priority_queue.zig +++ b/lib/std/priority_queue.zig @@ -19,23 +19,25 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF const Self = @This(); items: []T, - /// The capacity of the queue. This may be read directly, but must not + /// The current size of the queue. This may be read directly, but must not /// be modified directly. - capacity: usize, + len: usize, context: Context, /// Initialize and return an empty PriorityQueue. pub fn init(context: Context) Self { return .{ .items = &[_]T{}, - .capacity = 0, + .len = 0, .context = context, }; } /// Free memory used by the queue. pub fn deinit(self: Self, allocator: std.mem.Allocator) void { - allocator.free(self.allocatedSlice()); + if (self.items.len > 0) { + allocator.free(self.items); + } } /// Insert a new element, maintaining priority. @@ -47,9 +49,13 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF /// Insert a new element, maintaining priority. Assumes there is enough /// capacity in the queue for the additional item. pub fn addAssumeCapacity(self: *Self, elem: T) void { - self.items.len += 1; - self.items[self.items.len - 1] = elem; - siftUp(self, self.items.len - 1); + self.items[self.len] = elem; + + if (self.len > 0) { + self.siftUp(self.len); + } + + self.len += 1; } fn siftUp(self: *Self, start_index: usize) void { @@ -82,13 +88,13 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF /// Look at the highest priority element in the queue. Returns /// `null` if empty. pub fn peek(self: *Self) ?T { - return if (self.items.len > 0) self.items[0] else null; + return if (self.len > 0) self.items[0] else null; } /// Pop the highest priority element from the queue. Returns /// `null` if empty. pub fn removeOrNull(self: *Self) ?T { - return if (self.items.len > 0) self.remove() else null; + return if (self.len > 0) self.remove() else null; } /// Remove and return the highest priority element from the @@ -101,23 +107,23 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF /// same order as iterator, which is not necessarily priority /// order. pub fn removeIndex(self: *Self, index: usize) T { - assert(self.items.len > index); - const last = self.items[self.items.len - 1]; + assert(self.len > index); + const last = self.items[self.len - 1]; const item = self.items[index]; self.items[index] = last; - self.items.len -= 1; + self.len -= 1; - if (index == self.items.len) { + if (index == self.len) { // Last element removed, nothing more to do. } else if (index == 0) { - siftDown(self, index); + self.siftDown(index); } else { const parent_index = ((index - 1) >> 1); const parent = self.items[parent_index]; if (compareFn(self.context, last, parent) == .gt) { - siftDown(self, index); + self.siftDown(index); } else { - siftUp(self, index); + self.siftUp(index); } } @@ -127,14 +133,7 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF /// Return the number of elements remaining in the priority /// queue. pub fn count(self: Self) usize { - return self.items.len; - } - - /// Returns a slice of all the items plus the extra capacity, whose memory - /// contents are `undefined`. - fn allocatedSlice(self: Self) []T { - // `items.len` is the length, not the capacity. - return self.items.ptr[0..self.capacity]; + return self.len; } fn siftDown(self: *Self, target_index: usize) void { @@ -142,10 +141,10 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF var index = target_index; while (true) { var lesser_child_i = (std.math.mul(usize, index, 2) catch break) | 1; - if (!(lesser_child_i < self.items.len)) break; + if (!(lesser_child_i < self.len)) break; const next_child_i = lesser_child_i + 1; - if (next_child_i < self.items.len and compareFn(self.context, self.items[next_child_i], self.items[lesser_child_i]) == .lt) { + if (next_child_i < self.len and compareFn(self.context, self.items[next_child_i], self.items[lesser_child_i]) == .lt) { lesser_child_i = next_child_i; } @@ -164,11 +163,11 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF pub fn fromOwnedSlice(items: []T, context: Context) Self { var self = Self{ .items = items, - .capacity = items.len, + .len = items.len, .context = context, }; - var i = self.items.len >> 1; + var i = self.len >> 1; while (i > 0) { i -= 1; self.siftDown(i); @@ -182,48 +181,41 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF allocator: std.mem.Allocator, new_capacity: usize, ) !void { - var better_capacity = self.capacity; + var better_capacity = self.items.len; if (better_capacity >= new_capacity) return; while (true) { better_capacity += better_capacity / 2 + 8; if (better_capacity >= new_capacity) break; } - const old_memory = self.allocatedSlice(); - const new_memory = try allocator.realloc(old_memory, better_capacity); - self.items.ptr = new_memory.ptr; - self.capacity = new_memory.len; + self.items = try allocator.realloc(self.items, better_capacity); } /// Ensure that the queue can fit at least `additional_count` **more** item. pub fn ensureUnusedCapacity(self: *Self, allocator: std.mem.Allocator, additional_count: usize) !void { return self.ensureTotalCapacity( allocator, - self.items.len + additional_count, + self.len + additional_count, ); } /// Reduce allocated capacity to `new_capacity`. pub fn shrinkAndFree(self: *Self, allocator: std.mem.Allocator, new_capacity: usize) void { - assert(new_capacity <= self.capacity); + assert(new_capacity <= self.items.len); // Cannot shrink to smaller than the current queue size without invalidating the heap property - assert(new_capacity >= self.items.len); + assert(new_capacity >= self.len); - const old_memory = self.allocatedSlice(); - const new_memory = allocator.realloc(old_memory, new_capacity) catch |e| switch (e) { + self.items = allocator.realloc(self.items, new_capacity) catch |e| switch (e) { error.OutOfMemory => { // no problem, capacity is still correct then. return; }, }; - - self.items.ptr = new_memory.ptr; - self.capacity = new_memory.len; } pub fn update(self: *Self, elem: T, new_elem: T) !void { const update_index = blk: { var idx: usize = 0; - while (idx < self.items.len) : (idx += 1) { + while (idx < self.len) : (idx += 1) { const item = self.items[idx]; if (compareFn(self.context, item, elem) == .eq) break :blk idx; } @@ -243,7 +235,7 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF count: usize, pub fn next(it: *Iterator) ?T { - if (it.count >= it.queue.items.len) return null; + if (it.count >= it.queue.len) return null; const out = it.count; it.count += 1; return it.queue.items[out]; @@ -275,8 +267,8 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF for (self.items) |e| { print("{}, ", .{e}); } - print("len: {} ", .{self.items.len}); - print("capacity: {}", .{self.capacity}); + print("len: {} ", .{self.len}); + print("capacity: {}", .{self.items.len}); print(" }}\n", .{}); } }; @@ -530,16 +522,16 @@ test "shrinkAndFree" { defer queue.deinit(allocator); try queue.ensureTotalCapacity(allocator, 4); - try expect(queue.capacity >= 4); + try expect(queue.items.len >= 4); try queue.add(allocator, 1); try queue.add(allocator, 2); try queue.add(allocator, 3); - try expect(queue.capacity >= 4); + try expect(queue.items.len >= 4); try expectEqual(@as(usize, 3), queue.count()); queue.shrinkAndFree(allocator, 3); - try expectEqual(@as(usize, 3), queue.capacity); + try expectEqual(@as(usize, 3), queue.items.len); try expectEqual(@as(usize, 3), queue.count()); try expectEqual(@as(u32, 1), queue.remove()); From 621be32c5f20eb40e89a133a52e092b172c4188a Mon Sep 17 00:00:00 2001 From: Matthew Saltz Date: Tue, 17 Sep 2024 17:26:48 -0500 Subject: [PATCH 5/6] Revert "Change PriorityQueue to store current length rather than capacity" This reverts commit 42981fec07e68ef0aff365dc012134c4cf4e9f62. --- lib/std/priority_dequeue.zig | 4 +- lib/std/priority_queue.zig | 88 ++++++++++++++++++++---------------- 2 files changed, 49 insertions(+), 43 deletions(-) diff --git a/lib/std/priority_dequeue.zig b/lib/std/priority_dequeue.zig index 7d2273522abb..a932194d5078 100644 --- a/lib/std/priority_dequeue.zig +++ b/lib/std/priority_dequeue.zig @@ -34,9 +34,7 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar /// Free memory used by the dequeue. pub fn deinit(self: Self, allocator: std.mem.Allocator) void { - if (self.items.len > 0) { - allocator.free(self.items); - } + allocator.free(self.items); } /// Insert a new element, maintaining priority. diff --git a/lib/std/priority_queue.zig b/lib/std/priority_queue.zig index 6e61f62cbb45..ad7672cacb81 100644 --- a/lib/std/priority_queue.zig +++ b/lib/std/priority_queue.zig @@ -19,25 +19,23 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF const Self = @This(); items: []T, - /// The current size of the queue. This may be read directly, but must not + /// The capacity of the queue. This may be read directly, but must not /// be modified directly. - len: usize, + capacity: usize, context: Context, /// Initialize and return an empty PriorityQueue. pub fn init(context: Context) Self { return .{ .items = &[_]T{}, - .len = 0, + .capacity = 0, .context = context, }; } /// Free memory used by the queue. pub fn deinit(self: Self, allocator: std.mem.Allocator) void { - if (self.items.len > 0) { - allocator.free(self.items); - } + allocator.free(self.allocatedSlice()); } /// Insert a new element, maintaining priority. @@ -49,13 +47,9 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF /// Insert a new element, maintaining priority. Assumes there is enough /// capacity in the queue for the additional item. pub fn addAssumeCapacity(self: *Self, elem: T) void { - self.items[self.len] = elem; - - if (self.len > 0) { - self.siftUp(self.len); - } - - self.len += 1; + self.items.len += 1; + self.items[self.items.len - 1] = elem; + siftUp(self, self.items.len - 1); } fn siftUp(self: *Self, start_index: usize) void { @@ -88,13 +82,13 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF /// Look at the highest priority element in the queue. Returns /// `null` if empty. pub fn peek(self: *Self) ?T { - return if (self.len > 0) self.items[0] else null; + return if (self.items.len > 0) self.items[0] else null; } /// Pop the highest priority element from the queue. Returns /// `null` if empty. pub fn removeOrNull(self: *Self) ?T { - return if (self.len > 0) self.remove() else null; + return if (self.items.len > 0) self.remove() else null; } /// Remove and return the highest priority element from the @@ -107,23 +101,23 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF /// same order as iterator, which is not necessarily priority /// order. pub fn removeIndex(self: *Self, index: usize) T { - assert(self.len > index); - const last = self.items[self.len - 1]; + assert(self.items.len > index); + const last = self.items[self.items.len - 1]; const item = self.items[index]; self.items[index] = last; - self.len -= 1; + self.items.len -= 1; - if (index == self.len) { + if (index == self.items.len) { // Last element removed, nothing more to do. } else if (index == 0) { - self.siftDown(index); + siftDown(self, index); } else { const parent_index = ((index - 1) >> 1); const parent = self.items[parent_index]; if (compareFn(self.context, last, parent) == .gt) { - self.siftDown(index); + siftDown(self, index); } else { - self.siftUp(index); + siftUp(self, index); } } @@ -133,7 +127,14 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF /// Return the number of elements remaining in the priority /// queue. pub fn count(self: Self) usize { - return self.len; + return self.items.len; + } + + /// Returns a slice of all the items plus the extra capacity, whose memory + /// contents are `undefined`. + fn allocatedSlice(self: Self) []T { + // `items.len` is the length, not the capacity. + return self.items.ptr[0..self.capacity]; } fn siftDown(self: *Self, target_index: usize) void { @@ -141,10 +142,10 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF var index = target_index; while (true) { var lesser_child_i = (std.math.mul(usize, index, 2) catch break) | 1; - if (!(lesser_child_i < self.len)) break; + if (!(lesser_child_i < self.items.len)) break; const next_child_i = lesser_child_i + 1; - if (next_child_i < self.len and compareFn(self.context, self.items[next_child_i], self.items[lesser_child_i]) == .lt) { + if (next_child_i < self.items.len and compareFn(self.context, self.items[next_child_i], self.items[lesser_child_i]) == .lt) { lesser_child_i = next_child_i; } @@ -163,11 +164,11 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF pub fn fromOwnedSlice(items: []T, context: Context) Self { var self = Self{ .items = items, - .len = items.len, + .capacity = items.len, .context = context, }; - var i = self.len >> 1; + var i = self.items.len >> 1; while (i > 0) { i -= 1; self.siftDown(i); @@ -181,41 +182,48 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF allocator: std.mem.Allocator, new_capacity: usize, ) !void { - var better_capacity = self.items.len; + var better_capacity = self.capacity; if (better_capacity >= new_capacity) return; while (true) { better_capacity += better_capacity / 2 + 8; if (better_capacity >= new_capacity) break; } - self.items = try allocator.realloc(self.items, better_capacity); + const old_memory = self.allocatedSlice(); + const new_memory = try allocator.realloc(old_memory, better_capacity); + self.items.ptr = new_memory.ptr; + self.capacity = new_memory.len; } /// Ensure that the queue can fit at least `additional_count` **more** item. pub fn ensureUnusedCapacity(self: *Self, allocator: std.mem.Allocator, additional_count: usize) !void { return self.ensureTotalCapacity( allocator, - self.len + additional_count, + self.items.len + additional_count, ); } /// Reduce allocated capacity to `new_capacity`. pub fn shrinkAndFree(self: *Self, allocator: std.mem.Allocator, new_capacity: usize) void { - assert(new_capacity <= self.items.len); + assert(new_capacity <= self.capacity); // Cannot shrink to smaller than the current queue size without invalidating the heap property - assert(new_capacity >= self.len); + assert(new_capacity >= self.items.len); - self.items = allocator.realloc(self.items, new_capacity) catch |e| switch (e) { + const old_memory = self.allocatedSlice(); + const new_memory = allocator.realloc(old_memory, new_capacity) catch |e| switch (e) { error.OutOfMemory => { // no problem, capacity is still correct then. return; }, }; + + self.items.ptr = new_memory.ptr; + self.capacity = new_memory.len; } pub fn update(self: *Self, elem: T, new_elem: T) !void { const update_index = blk: { var idx: usize = 0; - while (idx < self.len) : (idx += 1) { + while (idx < self.items.len) : (idx += 1) { const item = self.items[idx]; if (compareFn(self.context, item, elem) == .eq) break :blk idx; } @@ -235,7 +243,7 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF count: usize, pub fn next(it: *Iterator) ?T { - if (it.count >= it.queue.len) return null; + if (it.count >= it.queue.items.len) return null; const out = it.count; it.count += 1; return it.queue.items[out]; @@ -267,8 +275,8 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF for (self.items) |e| { print("{}, ", .{e}); } - print("len: {} ", .{self.len}); - print("capacity: {}", .{self.items.len}); + print("len: {} ", .{self.items.len}); + print("capacity: {}", .{self.capacity}); print(" }}\n", .{}); } }; @@ -522,16 +530,16 @@ test "shrinkAndFree" { defer queue.deinit(allocator); try queue.ensureTotalCapacity(allocator, 4); - try expect(queue.items.len >= 4); + try expect(queue.capacity >= 4); try queue.add(allocator, 1); try queue.add(allocator, 2); try queue.add(allocator, 3); - try expect(queue.items.len >= 4); + try expect(queue.capacity >= 4); try expectEqual(@as(usize, 3), queue.count()); queue.shrinkAndFree(allocator, 3); - try expectEqual(@as(usize, 3), queue.items.len); + try expectEqual(@as(usize, 3), queue.capacity); try expectEqual(@as(usize, 3), queue.count()); try expectEqual(@as(u32, 1), queue.remove()); From cec35ac08db95e8ab18e9eb997bd2eb59ecdfa0a Mon Sep 17 00:00:00 2001 From: Matthew Saltz Date: Tue, 24 Sep 2024 12:19:06 -0400 Subject: [PATCH 6/6] Update capacity comment in lib/std/priority_queue.zig Co-authored-by: Andrew Kelley --- lib/std/priority_queue.zig | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/std/priority_queue.zig b/lib/std/priority_queue.zig index ad7672cacb81..607a6157ac3f 100644 --- a/lib/std/priority_queue.zig +++ b/lib/std/priority_queue.zig @@ -19,8 +19,7 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF const Self = @This(); items: []T, - /// The capacity of the queue. This may be read directly, but must not - /// be modified directly. + /// Tracks the allocated slice of memory when combined with `items.ptr`. capacity: usize, context: Context,