Fast, seeded, reversible permutations over integer domains of any size. Use
it to obfuscate IDs, shuffle lazily, or sample without collisions. Not
cryptographic.
Installation
implementation("com.eignex:kpermute:1.3.0")
Usage
Obfuscate an ID:
val idPerm = longPermutation(seed = 1L)
val longId = 49102490812045Lval encoded = idPerm.encode(longId)
println("encoded: $encoded (always prints 3631103739497407856)")
The size selects the domain 0 until size. A size of -1 means every 32- or
64-bit value, and other negative sizes are read as unsigned.
Sizes up to 4096 use a shuffled lookup table. Larger sizes mix within the
enclosing power-of-two block and use
cycle-walking,
repeating until the output is in range. The full 32- or 64-bit domain mixes
directly. Range factories wrap any of these in a view over a non-empty range.
Each mixing round multiplies by an odd constant, adds or xors a per-round key,
and xor-shifts the value right, in the style of
Jenkins' integer hashes.
Every step is invertible.
Int
override
funget(index: Int)
val
val
"shuffled: ${shuffled.take(20)}"
val
"unshuffled: ${unshuffled.take(20)}"
Related libraries
Surfaced from shared tags and platforms — no rankings paid for.