Skip to content

Instantly share code, notes, and snippets.

@likern
Last active August 4, 2023 17:01
Show Gist options
  • Save likern/dbbe4be8e03010dc37e0a9668263dae2 to your computer and use it in GitHub Desktop.
Save likern/dbbe4be8e03010dc37e0a9668263dae2 to your computer and use it in GitHub Desktop.
pdq.zig
pub fn pdq(
comptime T: type,
items: []T,
context: anytype,
comptime lessThanFn: fn (context: @TypeOf(context), lhs: T, rhs: T) bool,
) void {
const Context = struct {
items: []T,
sub_ctx: @TypeOf(context),
pub fn lessThan(ctx: @This(), a: usize, b: usize) bool {
return lessThanFn(ctx.sub_ctx, ctx.items[a], ctx.items[b]);
}
pub fn swap(ctx: @This(), a: usize, b: usize) void {
return mem.swap(T, &ctx.items[a], &ctx.items[b]);
}
};
pdqContext(0, items.len, Context{ .items = items, .sub_ctx = context });
}
const Hint = enum {
increasing,
decreasing,
unknown,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment