-
Notifications
You must be signed in to change notification settings - Fork 1
/
set.go
172 lines (147 loc) · 2.94 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
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
package plot
import (
"fmt"
"sort"
)
// -------------------------------------------------------------------------
// Float Set
// Float set is a set of float64 values.
type FloatSet map[float64]struct{}
func NewFloatSet() FloatSet {
return make(FloatSet)
}
func (s FloatSet) String() string {
var t = "[ "
for x, _ := range s {
t += fmt.Sprintf("%.1f ", x)
}
return t + "]"
}
// Add adds x to s.
func (s FloatSet) Add(x float64) {
s[x] = struct{}{}
}
// Del removes x from s.
func (s FloatSet) Del(x float64) {
delete(s, x)
}
// Contains reports membership of x in s.
func (s FloatSet) Contains(x float64) bool {
_, ok := s[x]
return ok
}
// Join adds all elements of t to s.
func (s FloatSet) Join(t FloatSet) {
for x := range t {
s[x] = struct{}{}
}
}
// Intersect returns the intersection of s and t.
func (s FloatSet) Intersect(t FloatSet) FloatSet {
intersection := NewFloatSet()
for x := range s {
if t.Contains(x) {
intersection.Add(x)
}
}
return intersection
}
// Remove removes all elements of t from s. (Set difference)
func (s FloatSet) Remove(t FloatSet) {
for x := range t {
delete(s, x)
}
}
// Equals compares s to a slice t.
func (s FloatSet) Equals(t []float64) bool {
if len(s) != len(t) {
return false
}
for _, x := range t {
if _, ok := s[x]; !ok {
return false
}
}
return true
}
func (s FloatSet) Elements() []float64 {
elems := make([]float64, len(s))
i := 0
for x := range s {
elems[i] = x
i++
}
sort.Float64s(elems)
return elems
}
// -------------------------------------------------------------------------
// String Set
// String set is a set of string values.
type StringSet map[string]struct{}
func NewStringSet() StringSet {
return make(StringSet)
}
func NewStringSetFrom(init []string) StringSet {
s := NewStringSet()
for _, v := range init {
s.Add(v)
}
return s
}
// Add adds x to s.
func (s StringSet) Add(x string) {
s[x] = struct{}{}
}
// Del removes x from s.
func (s StringSet) Del(x string) {
delete(s, x)
}
// Contains reports membership of x in s.
func (s StringSet) Contains(x string) bool {
_, ok := s[x]
return ok
}
// Join adds all elements of t to s.
func (s StringSet) Join(t StringSet) {
for x := range t {
s[x] = struct{}{}
}
}
// Intersect returns the intersection of s and t.
func (s StringSet) Intersect(t StringSet) StringSet {
intersection := NewStringSet()
for x := range s {
if t.Contains(x) {
intersection.Add(x)
}
}
return intersection
}
// Remove removes all elements of t from s. (Set difference)
func (s StringSet) Remove(t StringSet) {
for x := range t {
delete(s, x)
}
}
// Equals compares s to a slice t.
func (s StringSet) Equals(t []string) bool {
if len(s) != len(t) {
return false
}
for _, x := range t {
if _, ok := s[x]; !ok {
return false
}
}
return true
}
func (s StringSet) Elements() []string {
elems := make([]string, len(s))
i := 0
for x := range s {
elems[i] = x
i++
}
sort.Strings(elems)
return elems
}