diff --git a/lib/std/fs/path.zig b/lib/std/fs/path.zig index f337e1df9a..1c9e3e100b 100644 --- a/lib/std/fs/path.zig +++ b/lib/std/fs/path.zig @@ -253,27 +253,7 @@ pub fn windowsParsePath(path: []const u8) WindowsPath { return relative_path; } - // TODO when I combined these together with `inline for` the compiler crashed - { - const this_sep = '/'; - const two_sep = [_]u8{ this_sep, this_sep }; - if (mem.startsWith(u8, path, two_sep)) { - if (path[2] == this_sep) { - return relative_path; - } - - var it = mem.tokenize(path, [_]u8{this_sep}); - _ = (it.next() orelse return relative_path); - _ = (it.next() orelse return relative_path); - return WindowsPath{ - .is_abs = isAbsoluteWindows(path), - .kind = WindowsPath.Kind.NetworkShare, - .disk_designator = path[0..it.index], - }; - } - } - { - const this_sep = '\\'; + inline for ("/\\") |this_sep| { const two_sep = [_]u8{ this_sep, this_sep }; if (mem.startsWith(u8, path, two_sep)) { if (path[2] == this_sep) { diff --git a/lib/std/priority_queue.zig b/lib/std/priority_queue.zig index e6f398a551..a081e03c5f 100644 --- a/lib/std/priority_queue.zig +++ b/lib/std/priority_queue.zig @@ -4,6 +4,7 @@ const debug = std.debug; const expect = std.testing.expect; const expectEqual = std.testing.expectEqual; +/// Priority queue for storing generic data. Initialize with `init`. pub fn PriorityQueue(comptime T: type) type { return struct { const Self = @This(); @@ -13,6 +14,12 @@ pub fn PriorityQueue(comptime T: type) type { allocator: *Allocator, compareFn: fn (a: T, b: T) bool, + /// Initialize and return a priority queue. Provide + /// `compareFn` that returns `true` when its first argument + /// should get popped before its second argument. For example, + /// to make `pop` return the minimum value, provide + /// + /// `fn lessThan(a: T, b: T) bool { return a < b; }` pub fn init(allocator: *Allocator, compareFn: fn (a: T, b: T) bool) Self { return Self{ .items = [_]T{}, @@ -22,10 +29,12 @@ pub fn PriorityQueue(comptime T: type) type { }; } + /// Free memory used by the queue. pub fn deinit(self: Self) void { self.allocator.free(self.items); } + /// Insert a new element, maintaining priority. pub fn add(self: *Self, elem: T) !void { try ensureCapacity(self, self.len + 1); addUnchecked(self, elem); @@ -48,6 +57,7 @@ pub fn PriorityQueue(comptime T: type) type { self.len += 1; } + /// Add each element in `items` to the queue. pub fn addSlice(self: *Self, items: []const T) !void { try self.ensureCapacity(self.len + items.len); for (items) |e| { @@ -55,10 +65,14 @@ pub fn PriorityQueue(comptime T: type) type { } } + /// 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; } + /// 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; } @@ -72,10 +86,14 @@ pub fn PriorityQueue(comptime T: type) type { return first; } + /// Return the number of elements remaining in the priority + /// queue. pub fn count(self: Self) usize { return self.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.items.len; } @@ -171,6 +189,8 @@ pub fn PriorityQueue(comptime T: type) type { } }; + /// Return an iterator that walks the queue without consuming + /// it. Invalidated if the heap is modified. pub fn iterator(self: *Self) Iterator { return Iterator{ .queue = self,