std.sort: give comparator functions a context parameter

This commit is contained in:
Andrew Kelley 2020-06-03 18:41:56 -04:00
parent c405844b0a
commit d4d954abd2
4 changed files with 379 additions and 247 deletions

View File

@ -17,18 +17,18 @@ pub fn ComptimeStringMap(comptime V: type, comptime kvs: var) type {
};
var sorted_kvs: [kvs.len]KV = undefined;
const lenAsc = (struct {
fn lenAsc(a: KV, b: KV) bool {
fn lenAsc(context: void, a: KV, b: KV) bool {
return a.key.len < b.key.len;
}
}).lenAsc;
for (kvs) |kv, i| {
if (V != void) {
sorted_kvs[i] = .{.key = kv.@"0", .value = kv.@"1"};
sorted_kvs[i] = .{ .key = kv.@"0", .value = kv.@"1" };
} else {
sorted_kvs[i] = .{.key = kv.@"0", .value = {}};
sorted_kvs[i] = .{ .key = kv.@"0", .value = {} };
}
}
std.sort.sort(KV, &sorted_kvs, lenAsc);
std.sort.sort(KV, &sorted_kvs, {}, lenAsc);
const min_len = sorted_kvs[0].key.len;
const max_len = sorted_kvs[sorted_kvs.len - 1].key.len;
var len_indexes: [max_len + 1]usize = undefined;
@ -83,11 +83,11 @@ const TestEnum = enum {
test "ComptimeStringMap list literal of list literals" {
const map = ComptimeStringMap(TestEnum, .{
.{"these", .D},
.{"have", .A},
.{"nothing", .B},
.{"incommon", .C},
.{"samelen", .E},
.{ "these", .D },
.{ "have", .A },
.{ "nothing", .B },
.{ "incommon", .C },
.{ "samelen", .E },
});
testMap(map);
@ -99,11 +99,11 @@ test "ComptimeStringMap array of structs" {
@"1": TestEnum,
};
const map = ComptimeStringMap(TestEnum, [_]KV{
.{.@"0" = "these", .@"1" = .D},
.{.@"0" = "have", .@"1" = .A},
.{.@"0" = "nothing", .@"1" = .B},
.{.@"0" = "incommon", .@"1" = .C},
.{.@"0" = "samelen", .@"1" = .E},
.{ .@"0" = "these", .@"1" = .D },
.{ .@"0" = "have", .@"1" = .A },
.{ .@"0" = "nothing", .@"1" = .B },
.{ .@"0" = "incommon", .@"1" = .C },
.{ .@"0" = "samelen", .@"1" = .E },
});
testMap(map);
@ -115,11 +115,11 @@ test "ComptimeStringMap slice of structs" {
@"1": TestEnum,
};
const slice: []const KV = &[_]KV{
.{.@"0" = "these", .@"1" = .D},
.{.@"0" = "have", .@"1" = .A},
.{.@"0" = "nothing", .@"1" = .B},
.{.@"0" = "incommon", .@"1" = .C},
.{.@"0" = "samelen", .@"1" = .E},
.{ .@"0" = "these", .@"1" = .D },
.{ .@"0" = "have", .@"1" = .A },
.{ .@"0" = "nothing", .@"1" = .B },
.{ .@"0" = "incommon", .@"1" = .C },
.{ .@"0" = "samelen", .@"1" = .E },
};
const map = ComptimeStringMap(TestEnum, slice);
@ -142,11 +142,11 @@ test "ComptimeStringMap void value type, slice of structs" {
@"0": []const u8,
};
const slice: []const KV = &[_]KV{
.{.@"0" = "these"},
.{.@"0" = "have"},
.{.@"0" = "nothing"},
.{.@"0" = "incommon"},
.{.@"0" = "samelen"},
.{ .@"0" = "these" },
.{ .@"0" = "have" },
.{ .@"0" = "nothing" },
.{ .@"0" = "incommon" },
.{ .@"0" = "samelen" },
};
const map = ComptimeStringMap(void, slice);

View File

@ -58,7 +58,7 @@ const HeaderEntry = struct {
self.never_index = never_index orelse never_index_default(self.name);
}
fn compare(a: HeaderEntry, b: HeaderEntry) bool {
fn compare(context: void, a: HeaderEntry, b: HeaderEntry) bool {
if (a.name.ptr != b.name.ptr and a.name.len != b.name.len) {
// Things beginning with a colon *must* be before others
const a_is_colon = a.name[0] == ':';
@ -342,7 +342,7 @@ pub const Headers = struct {
}
pub fn sort(self: *Self) void {
std.sort.sort(HeaderEntry, self.data.items, HeaderEntry.compare);
std.sort.sort(HeaderEntry, self.data.items, {}, HeaderEntry.compare);
self.rebuild_index();
}

View File

@ -836,7 +836,7 @@ fn linuxLookupName(
key |= (MAXADDRS - @intCast(i32, i)) << DAS_ORDER_SHIFT;
addr.sortkey = key;
}
std.sort.sort(LookupAddr, addrs.span(), addrCmpLessThan);
std.sort.sort(LookupAddr, addrs.span(), {}, addrCmpLessThan);
}
const Policy = struct {
@ -953,7 +953,7 @@ fn IN6_IS_ADDR_SITELOCAL(a: [16]u8) bool {
}
// Parameters `b` and `a` swapped to make this descending.
fn addrCmpLessThan(b: LookupAddr, a: LookupAddr) bool {
fn addrCmpLessThan(context: void, b: LookupAddr, a: LookupAddr) bool {
return a.sortkey < b.sortkey;
}

File diff suppressed because it is too large Load Diff