mirror of
https://github.com/ziglang/zig.git
synced 2026-02-12 20:37:54 +00:00
std.sort: give comparator functions a context parameter
This commit is contained in:
parent
c405844b0a
commit
d4d954abd2
@ -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);
|
||||
|
||||
|
||||
@ -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();
|
||||
}
|
||||
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
570
lib/std/sort.zig
570
lib/std/sort.zig
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user