-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathupdaters.go
82 lines (70 loc) · 2.03 KB
/
updaters.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
package strset
/* Implementation note: The only methods that change a Set
after it is created are in this file. If you need an
immutable Set, delete this and updaters_test.go.
*/
// Add element to set.
func (s Set) Add(elem string) {
s.store[elem] = struct{}{}
}
// AddAll adds elements to set.
func (s Set) AddAll(elems ...string) {
for _, elem := range elems {
s.store[elem] = struct{}{}
}
}
// Remove element from set, if it is present.
func (s Set) Remove(elem string) {
delete(s.store, elem)
}
// RemoveAll removes elements from set, if they are present.
func (s Set) RemoveAll(elems ...string) {
for _, elem := range elems {
delete(s.store, elem)
}
}
// Pop tries to return some element of s, deleting it. If there was an element,
// the pair (element, true) is returned. Otherwise, the result is ("", false).
func (s Set) Pop() (elem string, found bool) {
for elem = range s.store {
delete(s.store, elem)
return elem, true
}
return "", false
}
// Clear removes all elements from s.
func (s *Set) Clear() {
s.store = make(map[string]struct{})
}
/* Set operations that change the receiver */
// IntersectionUpdate changes s in-place, keeping only elements
// that are in s AND in other. Math: S ∩ Z.
func (s Set) IntersectionUpdate(other Set) {
for elem := range s.store {
if !other.Contains(elem) {
delete(s.store, elem)
}
}
}
// UnionUpdate changes s in-place, gathering all elements that
// are in s OR in other. Math: S ∪ Z.
func (s Set) UnionUpdate(other Set) {
for elem := range other.store {
s.store[elem] = struct{}{}
}
}
// DifferenceUpdate changes s in-place, removing all elements
// that appear in other. Math: S \ Z.
func (s Set) DifferenceUpdate(other Set) {
for elem := range other.store {
delete(s.store, elem)
}
}
// SymmetricDifferenceUpdate changes s in-place, gathering only
// elements that are in either set but not on both.
// Think boolean XOR. Math: S ∆ Z.
func (s Set) SymmetricDifferenceUpdate(other Set) {
common := s.Intersection(other)
s.UnionUpdate(other)
s.DifferenceUpdate(common)
}