forked from Workiva/go-datastructures
/
dict.go
163 lines (124 loc) · 3.51 KB
/
dict.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
/*
Copyright 2014 Workiva, LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Package set is a simple unordered set implemented with a map. This set
is threadsafe which decreases performance.
TODO: Actually write custom hashmap using the hash/fnv hasher.
TODO: Our Set implementation Could be further optimized by getting the uintptr
of the generic interface{} used and using that as the key; Golang maps handle
uintptr much better than the generic interace{} key.
*/
package set
import "sync"
var pool = sync.Pool{}
// Set is an implementation of ISet using the builtin map type. Set is threadsafe.
type Set struct {
items map[interface{}]struct{}
lock sync.RWMutex
flattened []interface{}
}
// Add will add the provided items to the set.
func (set *Set) Add(items ...interface{}) {
set.lock.Lock()
defer set.lock.Unlock()
set.flattened = nil
for _, item := range items {
set.items[item] = struct{}{}
}
}
// Remove will remove the given items from the set.
func (set *Set) Remove(items ...interface{}) {
set.lock.Lock()
defer set.lock.Unlock()
set.flattened = nil
for _, item := range items {
delete(set.items, item)
}
}
// Exists returns a bool indicating if the given item exists in the set.
func (set *Set) Exists(item interface{}) bool {
set.lock.RLock()
_, ok := set.items[item]
set.lock.RUnlock()
return ok
}
// Flatten will return a list of the items in the set.
func (set *Set) Flatten() []interface{} {
set.lock.Lock()
defer set.lock.Unlock()
if set.flattened != nil {
return set.flattened
}
set.flattened = make([]interface{}, 0, len(set.items))
for item := range set.items {
set.flattened = append(set.flattened, item)
}
return set.flattened
}
// Len returns the number of items in the set.
func (set *Set) Len() int64 {
set.lock.RLock()
size := int64(len(set.items))
set.lock.RUnlock()
return size
}
// Clear will remove all items from the set.
func (set *Set) Clear() {
set.lock.Lock()
set.items = map[interface{}]struct{}{}
set.lock.Unlock()
}
// All returns a bool indicating if all of the supplied items exist in the set.
func (set *Set) All(items ...interface{}) bool {
set.lock.RLock()
defer set.lock.RUnlock()
for _, item := range items {
if _, ok := set.items[item]; !ok {
return false
}
}
return true
}
// Dispose will add this set back into the pool.
func (set *Set) Dispose() {
set.lock.Lock()
defer set.lock.Unlock()
for k := range set.items {
delete(set.items, k)
}
//this is so we don't hang onto any references
for i := 0; i < len(set.flattened); i++ {
set.flattened[i] = nil
}
set.flattened = set.flattened[:0]
pool.Put(set)
}
// New is the constructor for sets. It will pull from a reuseable memory pool if it can.
// Takes a list of items to initialize the set with.
func New(items ...interface{}) *Set {
set := pool.Get().(*Set)
for _, item := range items {
set.items[item] = struct{}{}
}
if len(items) > 0 {
set.flattened = nil
}
return set
}
func init() {
pool.New = func() interface{} {
return &Set{
items: make(map[interface{}]struct{}, 10),
}
}
}