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