Skip to content

std.ArrayHashMap: add unstable sorting capability #17426

@andrewrk

Description

@andrewrk

These functions are available:

zig/lib/std/array_hash_map.zig

Lines 1229 to 1243 in d68f39b

/// 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`
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);
}
pub fn sortContext(self: *Self, sort_ctx: anytype, ctx: Context) void {
self.entries.sort(sort_ctx);
const header = self.index_header orelse return;
header.reset();
self.insertAllEntriesIntoNewHeader(if (store_hash) {} else ctx, header);
}

However, unstable sort is preferred when possible. This API should offer unstable sorting as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    contributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.enhancementSolving this issue will likely involve adding new logic or components to the codebase.optimizationstandard libraryThis issue involves writing Zig code for the standard library.

    Type

    No type

    Projects

    Status

    Uncategorized

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions