This repository has been archived by the owner on Aug 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 85
/
set_nots.go
189 lines (156 loc) · 4.11 KB
/
set_nots.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
package set
import (
"fmt"
"strings"
)
// Provides a common set baseline for both threadsafe and non-ts Sets.
type set struct {
m map[interface{}]struct{} // struct{} doesn't take up space
}
// SetNonTS defines a non-thread safe set data structure.
type SetNonTS struct {
set
}
// NewNonTS creates and initializes a new non-threadsafe Set.
func newNonTS() *SetNonTS {
s := &SetNonTS{}
s.m = make(map[interface{}]struct{})
// Ensure interface compliance
var _ Interface = s
return s
}
// Add includes the specified items (one or more) to the set. The underlying
// Set s is modified. If passed nothing it silently returns.
func (s *set) Add(items ...interface{}) {
if len(items) == 0 {
return
}
for _, item := range items {
s.m[item] = keyExists
}
}
// Remove deletes the specified items from the set. The underlying Set s is
// modified. If passed nothing it silently returns.
func (s *set) Remove(items ...interface{}) {
if len(items) == 0 {
return
}
for _, item := range items {
delete(s.m, item)
}
}
// Pop deletes and return an item from the set. The underlying Set s is
// modified. If set is empty, nil is returned.
func (s *set) Pop() interface{} {
for item := range s.m {
delete(s.m, item)
return item
}
return nil
}
// Has looks for the existence of items passed. It returns false if nothing is
// passed. For multiple items it returns true only if all of the items exist.
func (s *set) Has(items ...interface{}) bool {
// assume checked for empty item, which not exist
if len(items) == 0 {
return false
}
has := true
for _, item := range items {
if _, has = s.m[item]; !has {
break
}
}
return has
}
// Size returns the number of items in a set.
func (s *set) Size() int {
return len(s.m)
}
// Clear removes all items from the set.
func (s *set) Clear() {
s.m = make(map[interface{}]struct{})
}
// IsEmpty reports whether the Set is empty.
func (s *set) IsEmpty() bool {
return s.Size() == 0
}
// IsEqual test whether s and t are the same in size and have the same items.
func (s *set) IsEqual(t Interface) bool {
// Force locking only if given set is threadsafe.
if conv, ok := t.(*Set); ok {
conv.l.RLock()
defer conv.l.RUnlock()
}
// return false if they are no the same size
if sameSize := len(s.m) == t.Size(); !sameSize {
return false
}
equal := true
t.Each(func(item interface{}) bool {
_, equal = s.m[item]
return equal // if false, Each() will end
})
return equal
}
// IsSubset tests whether t is a subset of s.
func (s *set) IsSubset(t Interface) (subset bool) {
subset = true
t.Each(func(item interface{}) bool {
_, subset = s.m[item]
return subset
})
return
}
// IsSuperset tests whether t is a superset of s.
func (s *set) IsSuperset(t Interface) bool {
return t.IsSubset(s)
}
// Each traverses the items in the Set, calling the provided function for each
// set member. Traversal will continue until all items in the Set have been
// visited, or if the closure returns false.
func (s *set) Each(f func(item interface{}) bool) {
for item := range s.m {
if !f(item) {
break
}
}
}
// Copy returns a new Set with a copy of s.
func (s *set) Copy() Interface {
u := newNonTS()
for item := range s.m {
u.Add(item)
}
return u
}
// String returns a string representation of s
func (s *set) String() string {
t := make([]string, 0, len(s.List()))
for _, item := range s.List() {
t = append(t, fmt.Sprintf("%v", item))
}
return fmt.Sprintf("[%s]", strings.Join(t, ", "))
}
// List returns a slice of all items. There is also StringSlice() and
// IntSlice() methods for returning slices of type string or int.
func (s *set) List() []interface{} {
list := make([]interface{}, 0, len(s.m))
for item := range s.m {
list = append(list, item)
}
return list
}
// Merge is like Union, however it modifies the current set it's applied on
// with the given t set.
func (s *set) Merge(t Interface) {
t.Each(func(item interface{}) bool {
s.m[item] = keyExists
return true
})
}
// it's not the opposite of Merge.
// Separate removes the set items containing in t from set s. Please aware that
func (s *set) Separate(t Interface) {
s.Remove(t.List()...)
}