mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 14:23:09 +00:00
parent
1ca4428324
commit
cbb9b5d9f0
@ -1229,14 +1229,41 @@ pub fn ArrayHashMapUnmanaged(
|
|||||||
/// Sorts the entries and then rebuilds the index.
|
/// Sorts the entries and then rebuilds the index.
|
||||||
/// `sort_ctx` must have this method:
|
/// `sort_ctx` must have this method:
|
||||||
/// `fn lessThan(ctx: @TypeOf(ctx), a_index: usize, b_index: usize) bool`
|
/// `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 {
|
pub inline fn sort(self: *Self, sort_ctx: anytype) void {
|
||||||
if (@sizeOf(ByIndexContext) != 0)
|
if (@sizeOf(ByIndexContext) != 0)
|
||||||
@compileError("Cannot infer context " ++ @typeName(Context) ++ ", call sortContext instead.");
|
@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 {
|
/// Sorts the entries and then rebuilds the index.
|
||||||
self.entries.sort(sort_ctx);
|
/// `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;
|
const header = self.index_header orelse return;
|
||||||
header.reset();
|
header.reset();
|
||||||
self.insertAllEntriesIntoNewHeader(if (store_hash) {} else ctx, header);
|
self.insertAllEntriesIntoNewHeader(if (store_hash) {} else ctx, header);
|
||||||
|
|||||||
@ -467,7 +467,7 @@ pub fn MultiArrayList(comptime T: type) type {
|
|||||||
|
|
||||||
/// `ctx` has the following method:
|
/// `ctx` has the following method:
|
||||||
/// `fn lessThan(ctx: @TypeOf(ctx), a_index: usize, b_index: usize) bool`
|
/// `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 {
|
const sort_context: struct {
|
||||||
sub_ctx: @TypeOf(ctx),
|
sub_ctx: @TypeOf(ctx),
|
||||||
slice: Slice,
|
slice: Slice,
|
||||||
|
|||||||
@ -4,6 +4,8 @@ const testing = std.testing;
|
|||||||
const mem = std.mem;
|
const mem = std.mem;
|
||||||
const math = std.math;
|
const math = std.math;
|
||||||
|
|
||||||
|
pub const Mode = enum { stable, unstable };
|
||||||
|
|
||||||
pub const block = @import("sort/block.zig").block;
|
pub const block = @import("sort/block.zig").block;
|
||||||
pub const pdq = @import("sort/pdq.zig").pdq;
|
pub const pdq = @import("sort/pdq.zig").pdq;
|
||||||
pub const pdqContext = @import("sort/pdq.zig").pdqContext;
|
pub const pdqContext = @import("sort/pdq.zig").pdqContext;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user