-
Notifications
You must be signed in to change notification settings - Fork 21
/
fvn.go
39 lines (35 loc) · 1.14 KB
/
fvn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package common
import (
"errors"
"hash/fnv"
"reflect"
)
// Fowler–Noll–Vo is a non-cryptographic hash function created by Glenn Fowler, Landon Curt Noll, and Kiem-Phong Vo.
//The basis of the FNV hash algorithm was taken from an idea sent as reviewer comments to the
//IEEE POSIX P1003.2 committee by Glenn Fowler and Phong Vo in 1991. In a subsequent ballot round,
//Landon Curt Noll improved on their algorithm. In an email message to Landon,
//they named it the Fowler/Noll/Vo or FNV hash.
// https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
func Fnv_hash_to_byte(data ...[]byte) []byte {
d := fnv.New32()
for _, b := range data {
d.Write(b)
}
return d.Sum(nil)
}
func Contains(obj interface{}, target interface{}) (bool, error) {
targetValue := reflect.ValueOf(target)
switch reflect.TypeOf(target).Kind() {
case reflect.Slice, reflect.Array:
for i := 0; i < targetValue.Len(); i++ {
if targetValue.Index(i).Interface() == obj {
return true, nil
}
}
case reflect.Map:
if targetValue.MapIndex(reflect.ValueOf(obj)).IsValid() {
return true, nil
}
}
return false, errors.New("not in array")
}