-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.go
69 lines (60 loc) · 1.28 KB
/
hash.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
package PPM
import (
"container/list"
"fmt"
)
type HashTable struct {
hash []*list.List
N int
r float64
}
const HASHMAX = 1000000000
func min(x, y int) int {
if x > y {
return y
} else {
return x
}
}
func NewHashTable(hp *list.List, num int, r float64) *HashTable {
r *= 2
t := &HashTable{make([]*list.List, min(2*num, HASHMAX)), min(2*num, HASHMAX), r}
for i := 0; i < t.N; i++ {
t.hash[i] = list.New()
}
one := *NewV(1, 1, 1)
bar := Pbar{}
bar.Init(num)
fmt.Println("gen hashtable...")
for e := hp.Front(); e != nil; e = e.Next() {
hpp := e.Value.(*HPoint)
p := hpp.pos
mn := p.Div(r).Sub(one)
mx := p.Div(r).Add(one)
for i := int(mn.X); i <= int(mx.X); i++ {
for j := int(mn.Y); j <= int(mx.Y); j++ {
for k := int(mn.Z); k <= int(mx.Z); k++ {
t.insert(i, j, k, hpp)
}
}
}
bar.Tick()
}
fmt.Println()
return t
}
func (t *HashTable) GetTable(pos V) *list.List {
return t.hash[t.get(pos)]
}
func (t *HashTable) insert(i, j, k int, hp *HPoint) {
idx := t.gett(i, j, k)
t.hash[idx].PushFront(hp)
}
func (t *HashTable) get(v V) int {
x := v.Div(t.r)
return t.gett(int(x.X), int(x.Y), int(x.Z))
}
func (t *HashTable) gett(x, y, z int) int {
sum := uint((x * 73856093) ^ (y * 19349663) ^ (z * 83492791))
return int(sum % uint(t.N))
}