-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashset.go
207 lines (189 loc) · 4.72 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package hashset
import (
"encoding/json"
"fmt"
"reflect"
"sync"
)
// Set represents a thread-safe collection of unique elements.
type Set struct {
mu sync.RWMutex // Guards access to the internal hash map.
hash map[interface{}]bool // Stores elements as keys with a boolean value.
}
// New initializes and returns a new Set with optional initial elements.
func New(initialValue ...interface{}) *Set {
s := &Set{
hash: make(map[interface{}]bool),
}
for _, iv := range initialValue {
v := reflect.ValueOf(iv)
if v.Kind() == reflect.Slice {
for i := 0; i < v.Len(); i++ {
// TODO: Optimize destructuring slice
s.Add(v.Index(i).Interface())
}
} else {
s.Add(iv)
}
}
return s
}
// Add inserts an element into the Set. If the element is already present, it does nothing.
func (s *Set) Add(element interface{}) {
s.mu.Lock()
defer s.mu.Unlock()
if !IsComparable(element) {
element = MakeComparable(element)
}
s.hash[element] = true
}
// Remove deletes an element from the Set. If the element is not present, it does nothing.
func (s *Set) Remove(element interface{}) {
s.mu.Lock()
defer s.mu.Unlock()
if !IsComparable(element) {
element = MakeComparable(element)
}
delete(s.hash, element)
}
// Contains checks if an element is present in the Set.
func (s *Set) Contains(element interface{}) bool {
s.mu.RLock()
defer s.mu.RUnlock()
if !IsComparable(element) {
element = MakeComparable(element)
}
_, exists := s.hash[element]
return exists
}
// Difference returns a new Set containing elements present in the original Set but not in the given Set.
func (s *Set) Difference(set *Set) *Set {
diff := make(map[interface{}]bool)
s.mu.RLock()
defer s.mu.RUnlock()
for k, _ := range s.hash {
if _, exists := set.hash[k]; !exists {
diff[k] = true
}
}
return &Set{hash: diff}
}
// Do applies a given function to each element of the Set. This can be used for iterating over the Set.
func (s *Set) Do(f func(interface{})) {
s.mu.RLock()
defer s.mu.RUnlock()
for k, _ := range s.hash {
f(k)
}
}
// Intersection returns a new Set containing elements that are present in both Sets.
func (s *Set) Intersection(set *Set) *Set {
intersect := make(map[interface{}]bool)
s.mu.RLock()
defer s.mu.RUnlock()
for k, _ := range s.hash {
if _, exists := set.hash[k]; exists {
intersect[k] = true
}
}
return &Set{hash: intersect}
}
// Len returns the number of elements in the Set.
func (s *Set) Len() int {
s.mu.RLock()
defer s.mu.RUnlock()
return len(s.hash)
}
// SubsetOf checks if the Set is a subset of the given Set.
func (s *Set) SubsetOf(set *Set) bool {
s.mu.RLock()
defer s.mu.RUnlock()
if s.Len() > set.Len() {
return false
}
for k, _ := range s.hash {
if _, exists := set.hash[k]; !exists {
return false
}
}
return true
}
// Union returns a new Set containing all elements that are present in either Set.
func (s *Set) Union(set *Set) *Set {
union := make(map[interface{}]bool)
s.mu.Lock()
for k, _ := range s.hash {
union[k] = true
}
s.mu.Unlock()
s.mu.Lock()
for k, _ := range set.hash {
union[k] = true
}
s.mu.Unlock()
return &Set{hash: union}
}
// ToSlice function returns converted slice from this set
func (s *Set) ToSlice() []interface{} {
uniTypeSlice := make([]interface{}, 0)
s.mu.RLock()
for key, _ := range s.hash {
uniTypeSlice = append(uniTypeSlice, key)
}
s.mu.RUnlock()
return uniTypeSlice
}
func (s *Set) MarshalJSON() ([]byte, error) {
stringMap := make(map[string]bool)
// s.mu.RLock()
for k, v := range s.hash {
if reflect.TypeOf(k).Kind() == reflect.Func {
fmt.Printf("[WARN] Skipped function pointer value in set: %v (hashset - MarshalJSON)", k)
continue
}
key := fmt.Sprintf("%v", k)
stringMap[key] = v
}
// s.mu.RUnlock()
jsonByte, err := json.Marshal(stringMap)
if err != nil {
return nil, err
}
return jsonByte, nil
}
func (s *Set) UnmarshalJSON(data []byte) error {
stringMap := make(map[string]bool)
// Here, it is guaranteed that Unmarshal will be appropriate.
s.mu.Lock()
if err := json.Unmarshal(data, &stringMap); err != nil {
return err
}
s.mu.Unlock()
for k := range stringMap {
s.Add(k)
}
return nil
}
// MakeComparable returns pointer(address) not comparable types: slice, map, function
func MakeComparable(element interface{}) interface{} {
/*
Not comparable types: slice, map, function
*/
elementType := reflect.TypeOf(element)
switch elementType.Kind() {
case reflect.Slice, reflect.Map, reflect.Func:
return reflect.ValueOf(element).Pointer()
default:
return element
}
}
// Powerful assertion comparable type by generic on compile time
func IsComparable[T comparable](element T) bool {
defer func() bool {
if r := recover(); r != nil {
return false
}
return true
}()
return element == element
}