Merge remote-tracking branch 'origin/master' into std.net

This commit is contained in:
Andrew Kelley 2019-10-30 20:43:56 -04:00
commit d7f15aa2cb
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9
2 changed files with 21 additions and 21 deletions

View File

@ -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) {

View File

@ -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,