-
Notifications
You must be signed in to change notification settings - Fork 0
/
uint32.go
200 lines (179 loc) · 3.79 KB
/
uint32.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
192
193
194
195
196
197
198
199
200
package menge
import (
"fmt"
"strings"
)
// UInt32Set represents a set of uint32 elements.
type UInt32Set map[uint32]struct{}
// Add adds zero or more elements to the set.
func (s UInt32Set) Add(elems ...uint32) {
for _, e := range elems {
s[e] = struct{}{}
}
}
// Remove removes zero or more elements from the set.
func (s UInt32Set) Remove(elems ...uint32) {
for _, e := range elems {
delete(s, e)
}
}
// Empty empties the set.
func (s UInt32Set) Empty() {
for e := range s {
delete(s, e)
}
}
// Has indicates whether the set has an element.
func (s UInt32Set) Has(elem uint32) bool {
_, ok := s[elem]
return ok
}
// Size returns the size of the set.
func (s UInt32Set) Size() int {
return len(s)
}
// IsEmpty indicates whether the set is empty.
func (s UInt32Set) IsEmpty() bool {
return len(s) == 0
}
// Clone returns a clone of the set.
func (s UInt32Set) Clone() UInt32Set {
c := make(UInt32Set, len(s))
for e := range s {
c[e] = struct{}{}
}
return c
}
// AsSlice returns an equivalent slice with no specific order of the elements.
func (s UInt32Set) AsSlice() []uint32 {
a := make([]uint32, len(s))
i := 0
for e := range s {
a[i] = e
i++
}
return a
}
// String returns a string representation of the set.
func (s UInt32Set) String() string {
b := &strings.Builder{}
b.Grow(len(s) * 8)
fmt.Fprint(b, "{")
first := true
for e := range s {
if first {
first = false
fmt.Fprintf(b, "%v", e)
} else {
fmt.Fprintf(b, " %v", e)
}
}
fmt.Fprint(b, "}")
return b.String()
}
// Equals indicates whether s and t are equal.
func (s UInt32Set) Equals(t UInt32Set) bool {
if len(s) != len(t) {
return false
}
for e := range s {
if _, ok := t[e]; !ok {
return false
}
}
return true
}
// Union returns the union of s and t.
func (s UInt32Set) Union(t UInt32Set) UInt32Set {
r := make(UInt32Set, len(s)+len(t))
for e := range s {
r[e] = struct{}{}
}
for e := range t {
r[e] = struct{}{}
}
return r
}
// Intersection returns the intersection of s and t.
func (s UInt32Set) Intersection(t UInt32Set) UInt32Set {
var small, large UInt32Set
if len(s) <= len(t) {
small, large = s, t
} else {
small, large = t, s
}
r := make(UInt32Set, len(small))
for e := range small {
if _, ok := large[e]; ok {
r[e] = struct{}{}
}
}
return r
}
// Difference returns the difference of s and t, i.e., s - t.
func (s UInt32Set) Difference(t UInt32Set) UInt32Set {
r := make(UInt32Set, len(s))
for e := range s {
if _, ok := t[e]; !ok {
r[e] = struct{}{}
}
}
return r
}
// IsSubsetOf indicates whether s is a subset of t.
func (s UInt32Set) IsSubsetOf(t UInt32Set) bool {
for e := range s {
if _, ok := t[e]; !ok {
return false
}
}
return true
}
// IsProperSubsetOf indicates whether s is a proper subset of t.
func (s UInt32Set) IsProperSubsetOf(t UInt32Set) bool {
for e := range s {
if _, ok := t[e]; !ok {
return false
}
}
return len(s) != len(t)
}
// IsSupersetOf indicates whether s is a superset of t.
func (s UInt32Set) IsSupersetOf(t UInt32Set) bool {
for e := range t {
if _, ok := s[e]; !ok {
return false
}
}
return true
}
// IsProperSupersetOf indicates whether s is a proper superset of t.
func (s UInt32Set) IsProperSupersetOf(t UInt32Set) bool {
for e := range t {
if _, ok := s[e]; !ok {
return false
}
}
return len(s) != len(t)
}
// IsDisjointFrom indicates whether s and t are disjoint.
func (s UInt32Set) IsDisjointFrom(t UInt32Set) bool {
var small, large UInt32Set
if len(s) <= len(t) {
small, large = s, t
} else {
small, large = t, s
}
for e := range small {
if _, ok := large[e]; ok {
return false
}
}
return true
}
// NewUInt32Set returns a new UInt32Set containing zero or more elements.
func NewUInt32Set(elems ...uint32) UInt32Set {
s := make(UInt32Set, len(elems))
s.Add(elems...)
return s
}