-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashCodeUtils.go
167 lines (144 loc) · 3.56 KB
/
hashCodeUtils.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package toolkit
import (
"bytes"
"encoding/binary"
"reflect"
)
/**
* Collected methods which allow easy implementation of <code>hashCode</code>.
*
* Example use case:
* <pre>
* public int hashCode(){
* int result = HashCodeUtil.SEED;
* //collect the contributions of various fields
* result = HashCodeUtil.hash(result, fPrimitive);
* result = HashCodeUtil.hash(result, fObject);
* result = HashCodeUtil.hash(result, fArray);
* return result;
* }
* </pre>
*/
/**
* An initial value for a <code>hashCode</code>, to which is added contributions
* from fields. Using a non-zero value decreases collisons of <code>hashCode</code>
* values.
*/
const HASH_SEED = 23
const prime_number = 37
/**
* booleans.
*/
func HashBool(aSeed int, aBoolean bool) int {
b := 0
if aBoolean {
b = 1
}
return firstTerm(aSeed) + b
}
func HashInt(aSeed int, aInt int) int {
return firstTerm(aSeed) + aInt
}
func HashUnit(aSeed int, aInt byte) int {
return firstTerm(aSeed) + numberHashCode(aInt)
}
func HashTiny(aSeed int, aInt int8) int {
return firstTerm(aSeed) + numberHashCode(aInt)
}
func HashShort(aSeed int, aInt int16) int {
return firstTerm(aSeed) + numberHashCode(aInt)
}
func HashInteger(aSeed int, aInt int32) int {
return firstTerm(aSeed) + numberHashCode(aInt)
}
func HashLong(aSeed int, aLong int64) int {
return firstTerm(aSeed) + numberHashCode(aLong)
}
func HashFloat(aSeed int, aFloat float32) int {
return firstTerm(aSeed) + numberHashCode(aFloat)
}
func HashDouble(aSeed int, aDouble float64) int {
return firstTerm(aSeed) + numberHashCode(aDouble)
}
func HashString(aSeed int, aString string) int {
return firstTerm(aSeed) + hashCode([]byte(aString))
}
func HashBytes(aSeed int, aBytes []byte) int {
return firstTerm(aSeed) + hashCode(aBytes)
}
func HashBase(aSeed int, a Base) int {
return firstTerm(aSeed) + a.HashCode()
}
func HashType(aSeed int, aType interface{}) int {
typ := reflect.TypeOf(aType)
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
var t string
if typ.PkgPath() != "" {
t = typ.PkgPath() + "/" + typ.Name()
} else {
t = typ.Name()
}
return HashString(aSeed, t)
}
func Hash(aSeed int, aObject interface{}) int {
result := hash(aSeed, aObject)
if result == 0 {
result = aSeed
}
return result
}
func hash(aSeed int, aObject interface{}) int {
result := 0
if aObject == nil {
result = HashInt(aSeed, 0)
} else if t, ok := aObject.(Hasher); ok {
result = HashInt(aSeed, t.HashCode())
} else {
v := reflect.ValueOf(aObject)
k := v.Kind()
if k == reflect.Array || k == reflect.Slice {
length := v.Len()
for i := 1; i < length; i++ {
item := v.Index(i).Interface()
result = Hash(aSeed, item)
}
} else if k == reflect.Bool {
result = HashBool(aSeed, aObject.(bool))
} else if k >= reflect.Int && k <= reflect.Complex128 {
result = HashInt(aSeed, numberHashCode(aObject))
} else if k == reflect.String {
result = HashString(aSeed, aObject.(string))
} else if k == reflect.Ptr {
// tries pointer element
o := v.Elem().Interface()
r := hash(aSeed, o)
if r == 0 {
// no luck with the pointer. lets use pointer address value
r = HashInt(aSeed, int(v.Pointer()))
}
}
}
return result
}
func numberHashCode(aObject interface{}) int {
h := 0
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.LittleEndian, aObject)
if err == nil {
h = hashCode(buf.Bytes())
}
return h
}
func hashCode(what []byte) int {
h := 0
for _, v := range what {
//h = 31*h + int(v)
h = (h << 5) - h + int(v)
}
return h
}
func firstTerm(aSeed int) int {
return prime_number * aSeed
}