...
1
16
17
18
19
20
21
22
23 package sets
24
25 import (
26 "reflect"
27 "sort"
28 )
29
30
31 type String map[string]Empty
32
33
34 func NewString(items ...string) String {
35 ss := String{}
36 ss.Insert(items...)
37 return ss
38 }
39
40
41
42 func StringKeySet(theMap interface{}) String {
43 v := reflect.ValueOf(theMap)
44 ret := String{}
45
46 for _, keyValue := range v.MapKeys() {
47 ret.Insert(keyValue.Interface().(string))
48 }
49 return ret
50 }
51
52
53 func (s String) Insert(items ...string) {
54 for _, item := range items {
55 s[item] = Empty{}
56 }
57 }
58
59
60 func (s String) Delete(items ...string) {
61 for _, item := range items {
62 delete(s, item)
63 }
64 }
65
66
67 func (s String) Has(item string) bool {
68 _, contained := s[item]
69 return contained
70 }
71
72
73 func (s String) HasAll(items ...string) bool {
74 for _, item := range items {
75 if !s.Has(item) {
76 return false
77 }
78 }
79 return true
80 }
81
82
83 func (s String) HasAny(items ...string) bool {
84 for _, item := range items {
85 if s.Has(item) {
86 return true
87 }
88 }
89 return false
90 }
91
92
93
94
95
96
97
98 func (s String) Difference(s2 String) String {
99 result := NewString()
100 for key := range s {
101 if !s2.Has(key) {
102 result.Insert(key)
103 }
104 }
105 return result
106 }
107
108
109
110
111
112
113
114 func (s1 String) Union(s2 String) String {
115 result := NewString()
116 for key := range s1 {
117 result.Insert(key)
118 }
119 for key := range s2 {
120 result.Insert(key)
121 }
122 return result
123 }
124
125
126
127
128
129
130 func (s1 String) Intersection(s2 String) String {
131 var walk, other String
132 result := NewString()
133 if s1.Len() < s2.Len() {
134 walk = s1
135 other = s2
136 } else {
137 walk = s2
138 other = s1
139 }
140 for key := range walk {
141 if other.Has(key) {
142 result.Insert(key)
143 }
144 }
145 return result
146 }
147
148
149 func (s1 String) IsSuperset(s2 String) bool {
150 for item := range s2 {
151 if !s1.Has(item) {
152 return false
153 }
154 }
155 return true
156 }
157
158
159
160
161 func (s1 String) Equal(s2 String) bool {
162 return len(s1) == len(s2) && s1.IsSuperset(s2)
163 }
164
165 type sortableSliceOfString []string
166
167 func (s sortableSliceOfString) Len() int { return len(s) }
168 func (s sortableSliceOfString) Less(i, j int) bool { return lessString(s[i], s[j]) }
169 func (s sortableSliceOfString) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
170
171
172 func (s String) List() []string {
173 res := make(sortableSliceOfString, 0, len(s))
174 for key := range s {
175 res = append(res, key)
176 }
177 sort.Sort(res)
178 return []string(res)
179 }
180
181
182 func (s String) UnsortedList() []string {
183 res := make([]string, 0, len(s))
184 for key := range s {
185 res = append(res, key)
186 }
187 return res
188 }
189
190
191 func (s String) PopAny() (string, bool) {
192 for key := range s {
193 s.Delete(key)
194 return key, true
195 }
196 var zeroValue string
197 return zeroValue, false
198 }
199
200
201 func (s String) Len() int {
202 return len(s)
203 }
204
205 func lessString(lhs, rhs string) bool {
206 return lhs < rhs
207 }
208
View as plain text