Skip to content

Instantly share code, notes, and snippets.

@paniq
Created April 14, 2024 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paniq/62d7041de3e200848d11bc690ae08484 to your computer and use it in GitHub Desktop.
Save paniq/62d7041de3e200848d11bc690ae08484 to your computer and use it in GitHub Desktop.
# optimally encoding a set of two integers
N := 32
N/2 := N // 2
NUMVALS := N * (N + 1) // 2
fn trienc (x y)
assert (x < N)
assert (y < N)
assert (x >= y)
x y := if (y >= N/2)
pass
y - N/2
x - N/2
else
pass (x + 1) y
y * (N + 1) + x
fn tridec (i)
assert (i < NUMVALS)
y := i // (N + 1)
x := i - y * (N + 1)
if (y >= x)
pass
y + N/2
x + N/2
else
pass (x - 1) y
# optimally encoding a set of two integers, assuming x != y we save one bit.
NUMVALS := N * (N - 1) // 2
fn triencne (x y)
assert (x < N)
assert (y < N)
assert (x > y)
x y := if (y >= N/2)
pass
y - N/2
x - N/2
else
pass (x - 1) y
y * (N - 1) + x
fn tridecne (i)
assert (i < NUMVALS)
y := i // (N - 1)
x := i - y * (N - 1)
if (y > x)
pass
y + N/2
x + N/2
else
pass (x + 1) y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment