Skip to content

Instantly share code, notes, and snippets.

@likern
Last active August 4, 2023 17:01
Show Gist options
  • Select an option

  • Save likern/dbbe4be8e03010dc37e0a9668263dae2 to your computer and use it in GitHub Desktop.

Select an option

Save likern/dbbe4be8e03010dc37e0a9668263dae2 to your computer and use it in GitHub Desktop.

Revisions

  1. likern renamed this gist Aug 4, 2023. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. likern created this gist Aug 4, 2023.
    26 changes: 26 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    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,
    };