-
Notifications
You must be signed in to change notification settings - Fork 3
/
set.go
191 lines (154 loc) · 3.6 KB
/
set.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
package set
type (
Set[T comparable] struct {
hash map[T]nothing
}
nothing struct{}
)
// New Create a new set
// The capacity of created set is length of initial,
// if you want to customize it, please use WithCapacity and .Insert / .InsertAll
func New[T comparable](initial ...T) *Set[T] {
s := &Set[T]{make(map[T]nothing, len(initial))}
for _, v := range initial {
s.Insert(v)
}
return s
}
// WithCapacity Create an empty set with specific capacity
func WithCapacity[T comparable](capacity int) *Set[T] {
s := &Set[T]{make(map[T]nothing, capacity)}
return s
}
// Difference Find the difference between two sets
// 返回的是自己存在而传入的 set 不存在的元素集合
func (s *Set[T]) Difference(another *Set[T]) *Set[T] {
n := make(map[T]nothing)
for k := range s.hash {
if _, exists := another.hash[k]; !exists {
n[k] = nothing{}
}
}
return &Set[T]{n}
}
func (s *Set[T]) All() []T {
if s == nil || len(s.hash) == 0 {
return []T{}
}
all := make([]T, 0, len(s.hash))
for i := range s.hash {
all = append(all, i)
}
return all
}
// Do Call f for each item in the set
func (s *Set[T]) Do(f func(T)) {
for k := range s.hash {
f(k)
}
}
// DoE Call f for each item in the set
func (s *Set[T]) DoE(f func(T) error) error {
for k := range s.hash {
err := f(k)
if err != nil {
return err
}
}
return nil
}
// Contains Test to see whether the element is in the set
func (s *Set[T]) Contains(element T) bool {
return s.Has(element)
}
// Has Test to see whether the element is in the set
func (s *Set[T]) Has(element T) bool {
_, exists := s.hash[element]
return exists
}
// Add element(s) to the set
func (s *Set[T]) Add(elements ...T) {
s.InsertAll(elements)
}
// AddAll add elements to the set
func (s *Set[T]) AddAll(elements []T) {
s.InsertAll(elements)
}
// Insert add element(s) to the set
func (s *Set[T]) Insert(elements ...T) {
s.InsertAll(elements)
}
// InsertAll add elements to the set
func (s *Set[T]) InsertAll(elements []T) {
for _, e := range elements {
s.hash[e] = nothing{}
}
}
// Intersection Find the intersection of two sets
func (s *Set[T]) Intersection(another *Set[T]) *Set[T] {
n := make(map[T]nothing)
for k := range s.hash {
if _, exists := another.hash[k]; exists {
n[k] = nothing{}
}
}
return &Set[T]{n}
}
// Len Return the number of items in the set
func (s *Set[T]) Len() int {
return len(s.hash)
}
// Size Return the number of items in the set
func (s *Set[T]) Size() int {
return s.Len()
}
// ProperSubsetOf Test whether this set is a proper subset of "set"
func (s *Set[T]) ProperSubsetOf(another *Set[T]) bool {
return s.SubsetOf(another) && s.Len() < another.Len()
}
// Remove an element from the set
func (s *Set[T]) Remove(element T) {
delete(s.hash, element)
}
// SubsetOf Test whether this set is a subset of "set"
func (s *Set[T]) SubsetOf(set *Set[T]) bool {
if s.Len() > set.Len() {
return false
}
for k := range s.hash {
if _, exists := set.hash[k]; !exists {
return false
}
}
return true
}
// Union Find the union of two sets
func (s *Set[T]) Union(set *Set[T]) *Set[T] {
n := make(map[T]nothing)
for k := range s.hash {
n[k] = nothing{}
}
for k := range set.hash {
n[k] = nothing{}
}
return &Set[T]{n}
}
// ContainsOrAdd add element if not exist
func (s *Set[T]) ContainsOrAdd(element T) (added bool) {
if s.Has(element) {
return false
}
s.Add(element)
return true
}
// Intersection 返回若干个 set 的交集
func Intersection[T comparable](sets ...*Set[T]) *Set[T] {
if len(sets) == 0 {
return New[T]()
}
b := sets[0]
for _, s := range sets[1:] {
b = b.Intersection(s)
}
return b
}