Skip to content

Commit 7b0451c

Browse files
committed
std: add unstable sorting to array hash maps
closes #17426
1 parent 833de10 commit 7b0451c

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

lib/std/array_hash_map.zig

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,11 +1232,33 @@ pub fn ArrayHashMapUnmanaged(
12321232
pub inline fn sort(self: *Self, sort_ctx: anytype) void {
12331233
if (@sizeOf(ByIndexContext) != 0)
12341234
@compileError("Cannot infer context " ++ @typeName(Context) ++ ", call sortContext instead.");
1235-
return self.sortContext(sort_ctx, undefined);
1235+
return sortContextInternal(self, .stable, sort_ctx, undefined);
12361236
}
12371237

1238-
pub fn sortContext(self: *Self, sort_ctx: anytype, ctx: Context) void {
1239-
self.entries.sort(sort_ctx);
1238+
pub inline fn sortUnstable(self: *Self, sort_ctx: anytype) void {
1239+
if (@sizeOf(ByIndexContext) != 0)
1240+
@compileError("Cannot infer context " ++ @typeName(Context) ++ ", call sortContext instead.");
1241+
return self.sortContextInternal(.unstable, sort_ctx, undefined);
1242+
}
1243+
1244+
pub inline fn sortContext(self: *Self, sort_ctx: anytype, ctx: Context) void {
1245+
return sortContextInternal(self, .stable, sort_ctx, ctx);
1246+
}
1247+
1248+
pub inline fn sortUnstableContext(self: *Self, sort_ctx: anytype, ctx: Context) void {
1249+
return sortContextInternal(self, .unstable, sort_ctx, ctx);
1250+
}
1251+
1252+
fn sortContextInternal(
1253+
self: *Self,
1254+
comptime mode: std.sort.Mode,
1255+
sort_ctx: anytype,
1256+
ctx: Context,
1257+
) void {
1258+
switch (mode) {
1259+
.stable => self.entries.sort(sort_ctx),
1260+
.unstable => self.entries.sortUnstable(sort_ctx),
1261+
}
12401262
const header = self.index_header orelse return;
12411263
header.reset();
12421264
self.insertAllEntriesIntoNewHeader(if (store_hash) {} else ctx, header);

lib/std/multi_array_list.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ pub fn MultiArrayList(comptime T: type) type {
467467

468468
/// `ctx` has the following method:
469469
/// `fn lessThan(ctx: @TypeOf(ctx), a_index: usize, b_index: usize) bool`
470-
fn sortInternal(self: Self, a: usize, b: usize, ctx: anytype, comptime mode: enum { stable, unstable }) void {
470+
fn sortInternal(self: Self, a: usize, b: usize, ctx: anytype, comptime mode: std.sort.Mode) void {
471471
const sort_context: struct {
472472
sub_ctx: @TypeOf(ctx),
473473
slice: Slice,

lib/std/sort.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const testing = std.testing;
44
const mem = std.mem;
55
const math = std.math;
66

7+
pub const Mode = enum { stable, unstable };
8+
79
pub const block = @import("sort/block.zig").block;
810
pub const pdq = @import("sort/pdq.zig").pdq;
911
pub const pdqContext = @import("sort/pdq.zig").pdqContext;

0 commit comments

Comments
 (0)