Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std.sort: add pdqsort and heapsort #15412

Merged
merged 1 commit into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/std/compress/deflate/huffman_code.zig
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub const HuffmanEncoder = struct {
return;
}
self.lfs = list;
sort.sort(LiteralNode, self.lfs, {}, byFreq);
mem.sort(LiteralNode, self.lfs, {}, byFreq);

// Get the number of literals for each bit count
var bit_count = self.bitCounts(list, max_bits);
Expand Down Expand Up @@ -270,7 +270,7 @@ pub const HuffmanEncoder = struct {
var chunk = list[list.len - @intCast(u32, bits) ..];

self.lns = chunk;
sort.sort(LiteralNode, self.lns, {}, byLiteral);
mem.sort(LiteralNode, self.lns, {}, byLiteral);

for (chunk) |node| {
self.codes[node.literal] = HuffCode{
Expand Down
2 changes: 1 addition & 1 deletion lib/std/compress/zstandard/decode/fse.zig
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ fn buildFseTable(values: []const u16, entries: []Table.Fse) !void {
position &= entries.len - 1;
}
}
std.sort.sort(u16, temp_states[0..probability], {}, std.sort.asc(u16));
std.mem.sort(u16, temp_states[0..probability], {}, std.sort.asc(u16));
for (0..probability) |i| {
entries[temp_states[i]] = if (i < double_state_count) Table.Fse{
.symbol = @intCast(u8, symbol),
Expand Down
2 changes: 1 addition & 1 deletion lib/std/compress/zstandard/decode/huffman.zig
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ fn assignSymbols(weight_sorted_prefixed_symbols: []LiteralsSection.HuffmanTree.P
};
}

std.sort.sort(
std.mem.sort(
LiteralsSection.HuffmanTree.PrefixedSymbol,
weight_sorted_prefixed_symbols,
weights,
Expand Down
2 changes: 1 addition & 1 deletion lib/std/comptime_string_map.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn ComptimeStringMap(comptime V: type, comptime kvs_list: anytype) type {
sorted_kvs[i] = .{ .key = kv.@"0", .value = {} };
}
}
std.sort.sort(KV, &sorted_kvs, {}, lenAsc);
mem.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;
Expand Down
2 changes: 1 addition & 1 deletion lib/std/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,7 @@ fn readMachODebugInfo(allocator: mem.Allocator, macho_file: File) !ModuleDebugIn
// Even though lld emits symbols in ascending order, this debug code
// should work for programs linked in any valid way.
// This sort is so that we can binary search later.
std.sort.sort(MachoSymbol, symbols, {}, MachoSymbol.addressLessThan);
mem.sort(MachoSymbol, symbols, {}, MachoSymbol.addressLessThan);

return ModuleDebugInfo{
.base_address = undefined,
Expand Down
2 changes: 1 addition & 1 deletion lib/std/enums.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1314,7 +1314,7 @@ pub fn EnumIndexer(comptime E: type) type {
}
};
}
std.sort.sort(EnumField, &fields, {}, ascByValue);
std.mem.sort(EnumField, &fields, {}, ascByValue);
const min = fields[0].value;
const max = fields[fields.len - 1].value;
const fields_len = fields.len;
Expand Down
2 changes: 1 addition & 1 deletion lib/std/http/Headers.zig
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ pub const Headers = struct {

/// Sorts the headers in lexicographical order.
pub fn sort(headers: *Headers) void {
std.sort.sort(Field, headers.list.items, {}, Field.lessThan);
std.mem.sort(Field, headers.list.items, {}, Field.lessThan);
headers.rebuildIndex();
}

Expand Down
28 changes: 28 additions & 0 deletions lib/std/mem.zig
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,34 @@ test "zeroInit" {
}, nested_baz);
}

pub fn sort(
comptime T: type,
items: []T,
context: anytype,
comptime lessThanFn: fn (@TypeOf(context), lhs: T, rhs: T) bool,
) void {
std.sort.block(T, items, context, lessThanFn);
}

pub fn sortUnstable(
comptime T: type,
items: []T,
context: anytype,
comptime lessThanFn: fn (@TypeOf(context), lhs: T, rhs: T) bool,
) void {
std.sort.pdq(T, items, context, lessThanFn);
}

/// TODO: currently this just calls `insertionSortContext`. The block sort implementation
/// in this file needs to be adapted to use the sort context.
pub fn sortContext(a: usize, b: usize, context: anytype) void {
std.sort.insertionContext(a, b, context);
}

pub fn sortUnstableContext(a: usize, b: usize, context: anytype) void {
std.sort.pdqContext(a, b, context);
}

/// Compares two slices of numbers lexicographically. O(n).
pub fn order(comptime T: type, lhs: []const T, rhs: []const T) math.Order {
const n = math.min(lhs.len, rhs.len);
Expand Down
2 changes: 1 addition & 1 deletion lib/std/meta.zig
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@ pub fn declList(comptime Namespace: type, comptime Decl: type) []const *const De
for (decls, 0..) |decl, i| {
array[i] = &@field(Namespace, decl.name);
}
std.sort.sort(*const Decl, &array, {}, S.declNameLessThan);
mem.sort(*const Decl, &array, {}, S.declNameLessThan);
return &array;
}
}
Expand Down
7 changes: 2 additions & 5 deletions lib/std/multi_array_list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ pub fn MultiArrayList(comptime T: type) type {
return lhs.alignment > rhs.alignment;
}
};
std.sort.sort(Data, &data, {}, Sort.lessThan);
mem.sort(Data, &data, {}, Sort.lessThan);
var sizes_bytes: [fields.len]usize = undefined;
var field_indexes: [fields.len]usize = undefined;
for (data, 0..) |elem, i| {
Expand Down Expand Up @@ -488,10 +488,7 @@ pub fn MultiArrayList(comptime T: type) type {
}
};

std.sort.sortContext(self.len, SortContext{
.sub_ctx = ctx,
.slice = self.slice(),
});
mem.sortContext(0, self.len, SortContext{ .sub_ctx = ctx, .slice = self.slice() });
}

fn capacityInBytes(capacity: usize) usize {
Expand Down
2 changes: 1 addition & 1 deletion lib/std/net.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ fn linuxLookupName(
key |= (MAXADDRS - @intCast(i32, i)) << DAS_ORDER_SHIFT;
addr.sortkey = key;
}
std.sort.sort(LookupAddr, addrs.items, {}, addrCmpLessThan);
mem.sort(LookupAddr, addrs.items, {}, addrCmpLessThan);
}

const Policy = struct {
Expand Down