forked from jakecoffman/cp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashset_collisionhandler.go
115 lines (96 loc) · 2.48 KB
/
hashset_collisionhandler.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
package cp
type HashSetEqualCollisionHandler func(ptr, elt interface{}) bool
type HashSetTransCollisionHandler func(ptr, data interface{}) interface{}
type HashSetIteratorCollisionHandler func(elt interface{})
type HashSetFilterCollisionHandler func(elt, data interface{}) bool
type HashSetBinCollisionHandler struct {
elt *CollisionHandler
hash HashValue
next *HashSetBinCollisionHandler
}
type HashSetCollisionHandler struct {
// number of bins in the table, not just table size
entries uint
defaultValue *CollisionHandler
size uint
table []*HashSetBinCollisionHandler
pooledBins *HashSetBinCollisionHandler
}
func NewHashSetCollisionHandler() *HashSetCollisionHandler {
size := nextPrime(0)
return &HashSetCollisionHandler{
size: size,
table: make([]*HashSetBinCollisionHandler, size),
}
}
func (set *HashSetCollisionHandler) Resize() {
newSize := nextPrime(set.size + 1)
newTable := make([]*HashSetBinCollisionHandler, newSize)
var i uint
for i = 0; i < set.size; i++ {
bin := set.table[i]
for bin != nil {
next := bin.next
idx := uint(bin.hash) % newSize
bin.next = newTable[idx]
newTable[idx] = bin
bin = next
}
}
set.table = newTable
set.size = newSize
}
func (set *HashSetCollisionHandler) Count() uint {
return set.entries
}
func handlerSetEql(check, pair *CollisionHandler) bool {
if check.TypeA == pair.TypeA && check.TypeB == pair.TypeB {
return true
}
if check.TypeB == pair.TypeA && check.TypeA == pair.TypeB {
return true
}
return false
}
func (set *HashSetCollisionHandler) Insert(hash HashValue, ptr *CollisionHandler) *CollisionHandler {
idx := uint(hash) % set.size
// Find the bin with the matching element.
bin := set.table[idx]
for bin != nil && !handlerSetEql(ptr, bin.elt) {
bin = bin.next
}
// Create it if necessary.
if bin == nil {
bin = &HashSetBinCollisionHandler{}
bin.hash = hash
bin.elt = ptr
bin.next = set.table[idx]
set.table[idx] = bin
set.entries++
if set.entries >= set.size {
set.Resize()
}
}
return bin.elt
}
func (set *HashSetCollisionHandler) Find(hash HashValue, ptr *CollisionHandler) *CollisionHandler {
idx := uint(hash) % set.size
bin := set.table[idx]
for bin != nil && !handlerSetEql(ptr, bin.elt) {
bin = bin.next
}
if bin != nil {
return bin.elt
} else {
return set.defaultValue
}
}
func (set *HashSetCollisionHandler) Each(f HashSetIterator) {
for _, bin := range set.table {
for bin != nil {
next := bin.next
f(bin.elt)
bin = next
}
}
}