forked from vova616/chipmunk
-
Notifications
You must be signed in to change notification settings - Fork 2
/
HashValue.go
51 lines (39 loc) · 775 Bytes
/
HashValue.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
package chipmunk
import (
"reflect"
"sync/atomic"
)
var hashCounter = uint64(0)
type HashValue uint32
const hashCoef = HashValue(3344921057)
func hashPair(a, b HashValue) HashValue {
return (a * hashCoef) ^ (b * hashCoef)
}
type HashPair struct {
A *Shape
B *Shape
}
func newPair(A *Shape, B *Shape) HashPair {
if A.Hash() > B.Hash() {
return HashPair{A, B}
}
return HashPair{B, A}
}
type DefaultHash struct {
hash HashValue
}
func ToHash(ptr interface{}) HashValue {
return HashValue(reflect.ValueOf(ptr).Pointer())
}
func (h *DefaultHash) Hash() HashValue {
if h.hash == 0 {
h.hash = HashValue(atomic.AddUint64(&hashCounter, 1))
if h.hash == 0 {
panic("Hash overflowed")
}
}
return h.hash
}
func (h *DefaultHash) Reset() {
h.hash = 0
}