forked from iesreza/monday
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.go
67 lines (60 loc) · 1.53 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
package monday
import "sync"
var keyExists = struct{}{}
// Set is a thread safe set data structure.
//
// It is ported from https://github.com/fatih/set with only the required functionality.
type set struct {
m map[Locale]struct{}
l sync.RWMutex
}
// NewSet allocates and returns a new Set. It accepts a variable number of
// arguments to populate the initial set. If nothing is passed a Set with zero
// size is created.
func newSet(items ...Locale) *set {
s := set{
m: make(map[Locale]struct{}),
}
s.Add(items...)
return &s
}
// Add adds the specified items (one or more) to the set. The underlying
// Set s is modified. If no items are passed it silently returns.
func (s *set) Add(items ...Locale) {
if len(items) == 0 {
return
}
s.l.Lock()
defer s.l.Unlock()
for _, item := range items {
s.m[item] = keyExists
}
}
// Each traverses the items in the Set, calling the provided function f 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 Locale) bool) {
s.l.RLock()
defer s.l.RUnlock()
for item := range s.m {
if !f(item) {
break
}
}
}
// 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 ...Locale) bool {
if len(items) == 0 {
return false
}
s.l.RLock()
defer s.l.RUnlock()
has := true
for _, item := range items {
if _, has = s.m[item]; !has {
break
}
}
return has
}