forked from Pryz/terraform-provider-ldap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.go
188 lines (173 loc) · 3.62 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"bytes"
"fmt"
"sort"
)
// Set represents a set of strings.
type Set struct {
data map[string]struct{}
}
// NewSet creates a new string set.
func NewSet(values ...string) *Set {
set := &Set{
data: map[string]struct{}{},
}
for _, value := range values {
set.Add(value)
}
return set
}
// Add adds a string to the set, returning whether the string was actually added;
// if the same value was already in the set, it will therefore return false.
func (s *Set) Add(attribute string) bool {
if attribute == "" {
return false
}
_, ok := s.data[attribute]
if !ok {
s.data[attribute] = struct{}{}
return true
}
return false
}
// Remove removes the given value from the set, returning if such a value was
// present and therefore actually removed.
func (s *Set) Remove(attribute string) bool {
if attribute == "" {
return false
}
_, ok := s.data[attribute]
if ok {
delete(s.data, attribute)
return true
}
return false
}
// Clear removes all values from the set.
func (s *Set) Clear() {
for k := range s.data {
delete(s.data, k)
}
}
// Equals checks if two sets have the same elements.
func (s *Set) Equals(other *Set) bool {
if other == nil {
return false
}
if s.Len() != other.Len() {
return false
}
for k := range s.data {
if !other.Contains(k) {
return false
}
}
return true
}
// Contains checks if the set contains the given value.
func (s *Set) Contains(value string) bool {
if value == "" {
return false
}
_, ok := s.data[value]
return ok
}
// Len returns the number of elements in the set.
func (s *Set) Len() int {
return len(s.data)
}
// Difference compares the set against another one and returns a new set
// containing the elements that are only in this set.
func (s *Set) Difference(other *Set) *Set {
if other == nil {
return nil
}
result := &Set{
data: map[string]struct{}{},
}
for k := range s.data {
if !other.Contains(k) {
result.Add(k)
}
}
return result
}
// Intersection compares the set against another one, returning a new set
// containing only the elements that are in both.
func (s *Set) Intersection(other *Set) *Set {
if other == nil {
return nil
}
result := &Set{
data: map[string]struct{}{},
}
for k := range s.data {
if other.Contains(k) {
result.Add(k)
}
}
return result
}
// Union returns a new set containing the elements in the set and in the other
// set.
func (s *Set) Union(other *Set) *Set {
if other == nil {
return nil
}
result := &Set{
data: map[string]struct{}{},
}
for k := range s.data {
result.Add(k)
}
for k := range other.data {
result.Add(k)
}
return result
}
// SymmetricDifference compares the set agains the other one and returns a new
// set containing only the elements that are not in common.
func (s *Set) SymmetricDifference(other *Set) *Set {
if other == nil {
return nil
}
result := &Set{
data: map[string]struct{}{},
}
for k := range s.data {
if !other.Contains(k) {
result.Add(k)
}
}
for k := range other.data {
if !s.Contains(k) {
result.Add(k)
}
}
return result
}
// List returns the elements in the set as a slice of strings.
func (s *Set) List() []string {
sorted := make([]string, 0, s.Len())
for k := range s.data {
sorted = append(sorted, k)
}
sort.Strings(sorted)
return sorted
}
// String returns a string representation of the set.
func (s *Set) String() string {
var buffer bytes.Buffer
buffer.WriteString("{ ")
sorted := make([]string, 0, s.Len())
for k := range s.data {
sorted = append(sorted, k)
}
sort.Strings(sorted)
for _, k := range sorted {
buffer.WriteString(fmt.Sprintf("%q, ", k))
}
buffer.WriteRune('}')
return buffer.String()
}