Skip to content

Commit db8676e

Browse files
committed
std.sort: add pdqsort and heapsort
1 parent df909da commit db8676e

37 files changed

+1706
-1291
lines changed

lib/std/compress/deflate/huffman_code.zig

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pub const HuffmanEncoder = struct {
9393
return;
9494
}
9595
self.lfs = list;
96-
sort.sort(LiteralNode, self.lfs, {}, byFreq);
96+
mem.sort(LiteralNode, self.lfs, {}, byFreq);
9797

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

272272
self.lns = chunk;
273-
sort.sort(LiteralNode, self.lns, {}, byLiteral);
273+
mem.sort(LiteralNode, self.lns, {}, byLiteral);
274274

275275
for (chunk) |node| {
276276
self.codes[node.literal] = HuffCode{

lib/std/compress/zstandard/decode/fse.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ fn buildFseTable(values: []const u16, entries: []Table.Fse) !void {
107107
position &= entries.len - 1;
108108
}
109109
}
110-
std.sort.sort(u16, temp_states[0..probability], {}, std.sort.asc(u16));
110+
std.mem.sort(u16, temp_states[0..probability], {}, std.sort.asc(u16));
111111
for (0..probability) |i| {
112112
entries[temp_states[i]] = if (i < double_state_count) Table.Fse{
113113
.symbol = @intCast(u8, symbol),

lib/std/compress/zstandard/decode/huffman.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ fn assignSymbols(weight_sorted_prefixed_symbols: []LiteralsSection.HuffmanTree.P
124124
};
125125
}
126126

127-
std.sort.sort(
127+
std.mem.sort(
128128
LiteralsSection.HuffmanTree.PrefixedSymbol,
129129
weight_sorted_prefixed_symbols,
130130
weights,

lib/std/comptime_string_map.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub fn ComptimeStringMap(comptime V: type, comptime kvs_list: anytype) type {
2828
sorted_kvs[i] = .{ .key = kv.@"0", .value = {} };
2929
}
3030
}
31-
std.sort.sort(KV, &sorted_kvs, {}, lenAsc);
31+
mem.sort(KV, &sorted_kvs, {}, lenAsc);
3232
const min_len = sorted_kvs[0].key.len;
3333
const max_len = sorted_kvs[sorted_kvs.len - 1].key.len;
3434
var len_indexes: [max_len + 1]usize = undefined;

lib/std/debug.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ fn readMachODebugInfo(allocator: mem.Allocator, macho_file: File) !ModuleDebugIn
12111211
// Even though lld emits symbols in ascending order, this debug code
12121212
// should work for programs linked in any valid way.
12131213
// This sort is so that we can binary search later.
1214-
std.sort.sort(MachoSymbol, symbols, {}, MachoSymbol.addressLessThan);
1214+
mem.sort(MachoSymbol, symbols, {}, MachoSymbol.addressLessThan);
12151215

12161216
return ModuleDebugInfo{
12171217
.base_address = undefined,

lib/std/enums.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -1314,7 +1314,7 @@ pub fn EnumIndexer(comptime E: type) type {
13141314
}
13151315
};
13161316
}
1317-
std.sort.sort(EnumField, &fields, {}, ascByValue);
1317+
std.mem.sort(EnumField, &fields, {}, ascByValue);
13181318
const min = fields[0].value;
13191319
const max = fields[fields.len - 1].value;
13201320
const fields_len = fields.len;

lib/std/http/Headers.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ pub const Headers = struct {
191191

192192
/// Sorts the headers in lexicographical order.
193193
pub fn sort(headers: *Headers) void {
194-
std.sort.sort(Field, headers.list.items, {}, Field.lessThan);
194+
std.mem.sort(Field, headers.list.items, {}, Field.lessThan);
195195
headers.rebuildIndex();
196196
}
197197

lib/std/mem.zig

+28
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,34 @@ test "zeroInit" {
566566
}, nested_baz);
567567
}
568568

569+
pub fn sort(
570+
comptime T: type,
571+
items: []T,
572+
context: anytype,
573+
comptime lessThanFn: fn (@TypeOf(context), lhs: T, rhs: T) bool,
574+
) void {
575+
std.sort.block(T, items, context, lessThanFn);
576+
}
577+
578+
pub fn sortUnstable(
579+
comptime T: type,
580+
items: []T,
581+
context: anytype,
582+
comptime lessThanFn: fn (@TypeOf(context), lhs: T, rhs: T) bool,
583+
) void {
584+
std.sort.pdq(T, items, context, lessThanFn);
585+
}
586+
587+
/// TODO: currently this just calls `insertionSortContext`. The block sort implementation
588+
/// in this file needs to be adapted to use the sort context.
589+
pub fn sortContext(a: usize, b: usize, context: anytype) void {
590+
std.sort.insertionContext(a, b, context);
591+
}
592+
593+
pub fn sortUnstableContext(a: usize, b: usize, context: anytype) void {
594+
std.sort.pdqContext(a, b, context);
595+
}
596+
569597
/// Compares two slices of numbers lexicographically. O(n).
570598
pub fn order(comptime T: type, lhs: []const T, rhs: []const T) math.Order {
571599
const n = math.min(lhs.len, rhs.len);

lib/std/meta.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ pub fn declList(comptime Namespace: type, comptime Decl: type) []const *const De
985985
for (decls, 0..) |decl, i| {
986986
array[i] = &@field(Namespace, decl.name);
987987
}
988-
std.sort.sort(*const Decl, &array, {}, S.declNameLessThan);
988+
mem.sort(*const Decl, &array, {}, S.declNameLessThan);
989989
return &array;
990990
}
991991
}

lib/std/multi_array_list.zig

+2-5
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ pub fn MultiArrayList(comptime T: type) type {
160160
return lhs.alignment > rhs.alignment;
161161
}
162162
};
163-
std.sort.sort(Data, &data, {}, Sort.lessThan);
163+
mem.sort(Data, &data, {}, Sort.lessThan);
164164
var sizes_bytes: [fields.len]usize = undefined;
165165
var field_indexes: [fields.len]usize = undefined;
166166
for (data, 0..) |elem, i| {
@@ -488,10 +488,7 @@ pub fn MultiArrayList(comptime T: type) type {
488488
}
489489
};
490490

491-
std.sort.sortContext(self.len, SortContext{
492-
.sub_ctx = ctx,
493-
.slice = self.slice(),
494-
});
491+
mem.sortContext(0, self.len, SortContext{ .sub_ctx = ctx, .slice = self.slice() });
495492
}
496493

497494
fn capacityInBytes(capacity: usize) usize {

lib/std/net.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,7 @@ fn linuxLookupName(
10821082
key |= (MAXADDRS - @intCast(i32, i)) << DAS_ORDER_SHIFT;
10831083
addr.sortkey = key;
10841084
}
1085-
std.sort.sort(LookupAddr, addrs.items, {}, addrCmpLessThan);
1085+
mem.sort(LookupAddr, addrs.items, {}, addrCmpLessThan);
10861086
}
10871087

10881088
const Policy = struct {

0 commit comments

Comments
 (0)