-
Notifications
You must be signed in to change notification settings - Fork 0
/
int8.go
200 lines (179 loc) · 3.69 KB
/
int8.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"
)
// Int8Set represents a set of int8 elements.
type Int8Set map[int8]struct{}
// Add adds zero or more elements to the set.
func (s Int8Set) Add(elems ...int8) {
for _, e := range elems {
s[e] = struct{}{}
}
}
// Remove removes zero or more elements from the set.
func (s Int8Set) Remove(elems ...int8) {
for _, e := range elems {
delete(s, e)
}
}
// Empty empties the set.
func (s Int8Set) Empty() {
for e := range s {
delete(s, e)
}
}
// Has indicates whether the set has an element.
func (s Int8Set) Has(elem int8) bool {
_, ok := s[elem]
return ok
}
// Size returns the size of the set.
func (s Int8Set) Size() int {
return len(s)
}
// IsEmpty indicates whether the set is empty.
func (s Int8Set) IsEmpty() bool {
return len(s) == 0
}
// Clone returns a clone of the set.
func (s Int8Set) Clone() Int8Set {
c := make(Int8Set, 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 Int8Set) AsSlice() []int8 {
a := make([]int8, len(s))
i := 0
for e := range s {
a[i] = e
i++
}
return a
}
// String returns a string representation of the set.
func (s Int8Set) 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 Int8Set) Equals(t Int8Set) 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 Int8Set) Union(t Int8Set) Int8Set {
r := make(Int8Set, 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 Int8Set) Intersection(t Int8Set) Int8Set {
var small, large Int8Set
if len(s) <= len(t) {
small, large = s, t
} else {
small, large = t, s
}
r := make(Int8Set, 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 Int8Set) Difference(t Int8Set) Int8Set {
r := make(Int8Set, 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 Int8Set) IsSubsetOf(t Int8Set) 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 Int8Set) IsProperSubsetOf(t Int8Set) 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 Int8Set) IsSupersetOf(t Int8Set) 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 Int8Set) IsProperSupersetOf(t Int8Set) 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 Int8Set) IsDisjointFrom(t Int8Set) bool {
var small, large Int8Set
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
}
// NewInt8Set returns a new Int8Set containing zero or more elements.
func NewInt8Set(elems ...int8) Int8Set {
s := make(Int8Set, len(elems))
s.Add(elems...)
return s
}