-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashset.go
184 lines (158 loc) · 3.82 KB
/
hashset.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package set
import (
"go-algorithms/algorithms/hashes"
. "go-algorithms/data_structures/types"
)
const (
DefaultHashSetCapacity = 20
DefaultHashSetUpperLoadFactor = 0.75
DefaultHashSetLowerLoadFactor = 0.10
)
type Node struct {
data T
next *Node
}
type Bucket struct {
dummyHead *Node
}
// Add 添加元素到bucket首部
//
// 当bucket中已经存在此元素时就不添加
// 返回是否新插入元素
func (b *Bucket) Add(data T) bool {
if !b.Contains(data) {
node := &Node{
data: data,
next: b.dummyHead.next,
}
b.dummyHead.next = node
return true
}
return false
}
// Delete 删除指定元素
//
// 当bucket中不存在此元素时就不删除
// 返回是否删除此元素
func (b *Bucket) Delete(data T) bool {
for pre, cur := b.dummyHead, b.dummyHead.next; cur != nil; pre, cur = cur, cur.next {
if cur.data == data {
pre.next = cur.next
cur.next = nil
return true
}
}
return false
}
// Contains 判断元素是否在bucket中
func (b *Bucket) Contains(data T) bool {
for cur := b.dummyHead.next; cur != nil; cur = cur.next {
if cur.data == data {
return true
}
}
return false
}
// Keys 返回bucket中所有的元素
func (b *Bucket) keys() []T {
var keys []T
for cur := b.dummyHead.next; cur != nil; cur = cur.next {
keys = append(keys, cur.data)
}
return keys
}
type Option func(*HashSet)
// HashSet 哈希集合表
type HashSet struct {
buckets []*Bucket // 集合的存储链表列表
initCapacity int64 // 集合的初始容量
capacity int64 // 集合的容量
size int64 // 集合中的元素个数
upperLoadFactor float64 // 集合最大负载因子
lowerLoadFactor float64 // 集合最小负载因子
}
// NewHashSet 返回一个新的哈希集合表
func NewHashSet(opts ...Option) *HashSet {
set := HashSet{
capacity: DefaultHashSetCapacity,
initCapacity: DefaultHashSetCapacity,
upperLoadFactor: DefaultHashSetUpperLoadFactor,
lowerLoadFactor: DefaultHashSetLowerLoadFactor,
}
for _, o := range opts {
o(&set)
}
set.buckets = make([]*Bucket, set.capacity)
for idx := range set.buckets {
set.buckets[idx] = &Bucket{
&Node{},
}
}
return &set
}
func NewHashSetWithCapacity(capacity int64) Option {
return func(s *HashSet) {
s.capacity = capacity
s.initCapacity = capacity
}
}
func NewHashSetWithUpperLoadFactor(upperLoadFactor float64) Option {
return func(s *HashSet) {
s.upperLoadFactor = upperLoadFactor
}
}
func NewHashSetWithLowerLoadFactor(lowerLoadFactor float64) Option {
return func(s *HashSet) {
s.lowerLoadFactor = lowerLoadFactor
}
}
func (s *HashSet) Add(data T) {
if s.loadFactor() >= s.upperLoadFactor {
s.resize(s.capacity * 2)
}
hashcode := s.hash(data)
added := s.buckets[hashcode].Add(data)
if added {
s.size++
}
}
func (s *HashSet) Remove(data T) {
hashcode := s.hash(data)
deleted := s.buckets[hashcode].Delete(data)
if deleted {
s.size--
if s.loadFactor() <= s.lowerLoadFactor && s.capacity>>2 >= s.initCapacity {
s.resize(s.capacity >> 2)
}
}
}
func (s *HashSet) Contains(data T) bool {
hashcode := s.hash(data)
return s.buckets[hashcode].Contains(data)
}
func (s *HashSet) IsEmpty() bool {
return s.size == 0
}
func (s *HashSet) Size() int {
return int(s.size)
}
// hash 集合的哈希函数
func (s *HashSet) hash(data T) uint64 {
return hashes.SimpleHash(string(rune(data)), uint64(s.capacity))
}
// loadFactor 集合当前的负载因子
func (s *HashSet) loadFactor() float64 {
return float64(s.size) / float64(s.capacity)
}
// resize 集合扩缩容
func (s *HashSet) resize(capacity int64) {
hashset := NewHashSet(NewHashSetWithCapacity(capacity))
for idx := range s.buckets {
for _, key := range s.buckets[idx].keys() {
hashset.Add(key)
}
}
s.buckets = hashset.buckets
s.capacity = capacity
s.initCapacity = capacity
}