Kwker for Zig
The module kwker over Kwker's C ABI (libkwker_c): slices sorted in place without copies, for u8, i8,
u16, i16, u32, i32, f32, u64, i64, f64 and f16 keys. Run-time engine dispatch (AVX-512, AVX2, NEON, portable) comes from
the C library.
const kwk = @import("kwker");
kwk.sort(f64, values); // ascending, -0.0 before +0.0, NaNs last
kwk.sortOrder(i32, keys, .{ .descending = true }); // .nans_first = true puts NaNs first
try kwk.select(u64, keys, k, .{}); // the k-th key at keys[k]
kwk.partialSort(u32, keys, 100, .{}); // the 100 smallest, in order, at the front
const ix = try kwk.argsortAlloc(allocator, f32, keys, .{}); // the stable sorting permutation ([]u64)
const m = try kwk.topK(f32, scores, 10, .{ .descending = true }, &top, &top_ix);
try kwk.sortKV(u64, Record, keys, records, .{}); // values of any type move with their keys
Build: install the C library (ss capi --install <prefix> from rust/, or a release tarball), then
zig build test -Dkwker-lib=<prefix>/lib. As a dependency: zig fetch --save <url>, then in build.zig
const kwk_dep = b.dependency("kwker", .{ .target = target, .optimize = optimize, .@"kwker-lib" = lib_dir }); and
exe.root_module.addImport("kwker", kwk_dep.module("kwker"));.
Tested with Zig 0.16 (ss native --lang zig from rust/: the tests against std.mem.sort on every engine, then 1M-key
timings against std.mem.sortUnstable).