-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
set.go
54 lines (46 loc) · 783 Bytes
/
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
package ecrm
type set map[string]struct{}
func newSet(members ...string) set {
s := make(map[string]struct{})
for _, m := range members {
s[m] = struct{}{}
}
return s
}
func (s set) add(v string) bool {
if _, ok := s[v]; ok {
return false // already exists
}
s[v] = struct{}{}
return true // added
}
func (s set) contains(v string) bool {
_, ok := s[v]
return ok
}
func (s set) remove(v string) {
delete(s, v)
}
func (s set) isEmpty() bool {
if s == nil {
return true
}
return len(s) == 0
}
func (s set) members() []string {
var members []string
for k := range s {
members = append(members, k)
}
return members
}
func (s set) union(o set) set {
if o == nil {
return s
}
u := newSet(s.members()...)
for k := range o {
u.add(k)
}
return u
}