authorgravatar for nathan@nmichaels.orgNathan Michaels <nathan@nmichaels.org> 2019-10-29 01:22:04-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-10-30 01:05:00-04:00
log1f0bcefe4a1b00882f59982bd641f9adb927234f
treec4c9f00f691eb265671c83ecb3ebe635e14d7fb9
parent48b5dc051622350acb18bd726cd59627e02c5efc

Document PriorityQueue.


1 files changed, 20 insertions(+), 0 deletions(-)

lib/std/priority_queue.zig+20
......@@ -4,6 +4,7 @@ const debug = std.debug;
44const expect = std.testing.expect;
55const expectEqual = std.testing.expectEqual;
66
7/// Priority queue for storing generic data. Initialize with `init`.
78pub fn PriorityQueue(comptime T: type) type {
89 return struct {
910 const Self = @This();
......@@ -13,6 +14,12 @@ pub fn PriorityQueue(comptime T: type) type {
1314 allocator: *Allocator,
1415 compareFn: fn (a: T, b: T) bool,
1516
17 /// Initialize and return a priority queue. Provide
18 /// `compareFn` that returns `true` when its first argument
19 /// should get popped before its second argument. For example,
20 /// to make `pop` return the minimum value, provide
21 ///
22 /// `fn lessThan(a: T, b: T) bool { return a < b; }`
1623 pub fn init(allocator: *Allocator, compareFn: fn (a: T, b: T) bool) Self {
1724 return Self{
1825 .items = [_]T{},
......@@ -22,10 +29,12 @@ pub fn PriorityQueue(comptime T: type) type {
2229 };
2330 }
2431
32 /// Free memory used by the queue.
2533 pub fn deinit(self: Self) void {
2634 self.allocator.free(self.items);
2735 }
2836
37 /// Insert a new element, maintaining priority.
2938 pub fn add(self: *Self, elem: T) !void {
3039 try ensureCapacity(self, self.len + 1);
3140 addUnchecked(self, elem);
......@@ -48,6 +57,7 @@ pub fn PriorityQueue(comptime T: type) type {
4857 self.len += 1;
4958 }
5059
60 /// Add each element in `items` to the queue.
5161 pub fn addSlice(self: *Self, items: []const T) !void {
5262 try self.ensureCapacity(self.len + items.len);
5363 for (items) |e| {
......@@ -55,10 +65,14 @@ pub fn PriorityQueue(comptime T: type) type {
5565 }
5666 }
5767
68 /// Look at the highest priority element in the queue. Returns
69 /// `null` if empty.
5870 pub fn peek(self: *Self) ?T {
5971 return if (self.len > 0) self.items[0] else null;
6072 }
6173
74 /// Pop the highest priority element from the queue. Returns
75 /// `null` if empty.
6276 pub fn removeOrNull(self: *Self) ?T {
6377 return if (self.len > 0) self.remove() else null;
6478 }
......@@ -72,10 +86,14 @@ pub fn PriorityQueue(comptime T: type) type {
7286 return first;
7387 }
7488
89 /// Return the number of elements remaining in the priority
90 /// queue.
7591 pub fn count(self: Self) usize {
7692 return self.len;
7793 }
7894
95 /// Return the number of elements that can be added to the
96 /// queue before more memory is allocated.
7997 pub fn capacity(self: Self) usize {
8098 return self.items.len;
8199 }
......@@ -171,6 +189,8 @@ pub fn PriorityQueue(comptime T: type) type {
171189 }
172190 };
173191
192 /// Return an iterator that walks the queue without consuming
193 /// it. Invalidated if the heap is modified.
174194 pub fn iterator(self: *Self) Iterator {
175195 return Iterator{
176196 .queue = self,