Skip to content

Instantly share code, notes, and snippets.

@maxymania
Created August 2, 2018 04:57
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 maxymania/1bf5cd9695f931da20e7e6d5a2a30093 to your computer and use it in GitHub Desktop.
Save maxymania/1bf5cd9695f931da20e7e6d5a2a30093 to your computer and use it in GitHub Desktop.
Integer hash
import "sort"
/*
Generates n unique numbers mod m. v is used as deterministic source.
*/
func Inthash(v, n, m int) []int {
if n>m { panic("less unique numbers than slots") }
r := make([]int,n)
for i := range r {
r[i] = v%m
v/=m
m--
}
for i := range r {
cv := r[i]
for j := i+1 ; j<n ; j++ {
if r[j]==cv { r[j]++ }
}
}
sort.Ints(r)
return r
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment