-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
32fef94
commit ca3061b
Showing
1 changed file
with
6 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,17 @@ | ||
package math | ||
|
||
import ( | ||
"fmt" | ||
"bytes" | ||
"encoding/gob" | ||
"hash/fnv" | ||
"reflect" | ||
) | ||
|
||
// Non-cryptographic hash (FNV-1a) | ||
func Hash(data any) uint { | ||
f := fnv.New64a() | ||
if reflect.ValueOf(data).Kind() == reflect.Ptr { | ||
f.Write([]byte(fmt.Sprintf("%p", interface{}(data)))) | ||
} else { | ||
f.Write([]byte(fmt.Sprintf("%v", data))) | ||
} | ||
var buf bytes.Buffer | ||
enc := gob.NewEncoder(&buf) | ||
enc.Encode(data) | ||
f.Write(buf.Bytes()) | ||
return uint(f.Sum64()) | ||
} |