Commit cbb9b5d9f0
Changed files (3)
lib/std/array_hash_map.zig
@@ -1229,14 +1229,41 @@ pub fn ArrayHashMapUnmanaged(
/// Sorts the entries and then rebuilds the index.
/// `sort_ctx` must have this method:
/// `fn lessThan(ctx: @TypeOf(ctx), a_index: usize, b_index: usize) bool`
+ /// Uses a stable sorting algorithm.
pub inline fn sort(self: *Self, sort_ctx: anytype) void {
if (@sizeOf(ByIndexContext) != 0)
@compileError("Cannot infer context " ++ @typeName(Context) ++ ", call sortContext instead.");
- return self.sortContext(sort_ctx, undefined);
+ return sortContextInternal(self, .stable, sort_ctx, undefined);
}
- pub fn sortContext(self: *Self, sort_ctx: anytype, ctx: Context) void {
- self.entries.sort(sort_ctx);
+ /// Sorts the entries and then rebuilds the index.
+ /// `sort_ctx` must have this method:
+ /// `fn lessThan(ctx: @TypeOf(ctx), a_index: usize, b_index: usize) bool`
+ /// Uses an unstable sorting algorithm.
+ pub inline fn sortUnstable(self: *Self, sort_ctx: anytype) void {
+ if (@sizeOf(ByIndexContext) != 0)
+ @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call sortUnstableContext instead.");
+ return self.sortContextInternal(.unstable, sort_ctx, undefined);
+ }
+
+ pub inline fn sortContext(self: *Self, sort_ctx: anytype, ctx: Context) void {
+ return sortContextInternal(self, .stable, sort_ctx, ctx);
+ }
+
+ pub inline fn sortUnstableContext(self: *Self, sort_ctx: anytype, ctx: Context) void {
+ return sortContextInternal(self, .unstable, sort_ctx, ctx);
+ }
+
+ fn sortContextInternal(
+ self: *Self,
+ comptime mode: std.sort.Mode,
+ sort_ctx: anytype,
+ ctx: Context,
+ ) void {
+ switch (mode) {
+ .stable => self.entries.sort(sort_ctx),
+ .unstable => self.entries.sortUnstable(sort_ctx),
+ }
const header = self.index_header orelse return;
header.reset();
self.insertAllEntriesIntoNewHeader(if (store_hash) {} else ctx, header);
lib/std/multi_array_list.zig
@@ -467,7 +467,7 @@ pub fn MultiArrayList(comptime T: type) type {
/// `ctx` has the following method:
/// `fn lessThan(ctx: @TypeOf(ctx), a_index: usize, b_index: usize) bool`
- fn sortInternal(self: Self, a: usize, b: usize, ctx: anytype, comptime mode: enum { stable, unstable }) void {
+ fn sortInternal(self: Self, a: usize, b: usize, ctx: anytype, comptime mode: std.sort.Mode) void {
const sort_context: struct {
sub_ctx: @TypeOf(ctx),
slice: Slice,
lib/std/sort.zig
@@ -4,6 +4,8 @@ const testing = std.testing;
const mem = std.mem;
const math = std.math;
+pub const Mode = enum { stable, unstable };
+
pub const block = @import("sort/block.zig").block;
pub const pdq = @import("sort/pdq.zig").pdq;
pub const pdqContext = @import("sort/pdq.zig").pdqContext;