2 Open Source Initiative OSI - The MIT License (MIT):Licensing
5 Copyright (c) 2013 Ralph Caraveo (deckarep@gmail.com)
7 Permission is hereby granted, free of charge, to any person obtaining a copy of
8 this software and associated documentation files (the "Software"), to deal in
9 the Software without restriction, including without limitation the rights to
10 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
11 of the Software, and to permit persons to whom the Software is furnished to do
12 so, subject to the following conditions:
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 type threadUnsafeSet map[interface{}]struct{}
36 type orderedPair struct {
41 func newThreadUnsafeSet() threadUnsafeSet {
42 return make(threadUnsafeSet)
45 func (pair *orderedPair) Equal(other orderedPair) bool {
46 if pair.first == other.first &&
47 pair.second == other.second {
54 func (set *threadUnsafeSet) Add(i interface{}) bool {
56 (*set)[i] = struct{}{}
57 return !found //False if it existed already
60 func (set *threadUnsafeSet) Contains(i ...interface{}) bool {
61 for _, val := range i {
62 if _, ok := (*set)[val]; !ok {
69 func (set *threadUnsafeSet) IsSubset(other Set) bool {
70 _ = other.(*threadUnsafeSet)
71 for elem := range *set {
72 if !other.Contains(elem) {
79 func (set *threadUnsafeSet) IsSuperset(other Set) bool {
80 return other.IsSubset(set)
83 func (set *threadUnsafeSet) Union(other Set) Set {
84 o := other.(*threadUnsafeSet)
86 unionedSet := newThreadUnsafeSet()
88 for elem := range *set {
91 for elem := range *o {
97 func (set *threadUnsafeSet) Intersect(other Set) Set {
98 o := other.(*threadUnsafeSet)
100 intersection := newThreadUnsafeSet()
101 // loop over smaller set
102 if set.Cardinality() < other.Cardinality() {
103 for elem := range *set {
104 if other.Contains(elem) {
105 intersection.Add(elem)
109 for elem := range *o {
110 if set.Contains(elem) {
111 intersection.Add(elem)
118 func (set *threadUnsafeSet) Difference(other Set) Set {
119 _ = other.(*threadUnsafeSet)
121 difference := newThreadUnsafeSet()
122 for elem := range *set {
123 if !other.Contains(elem) {
130 func (set *threadUnsafeSet) SymmetricDifference(other Set) Set {
131 _ = other.(*threadUnsafeSet)
133 aDiff := set.Difference(other)
134 bDiff := other.Difference(set)
135 return aDiff.Union(bDiff)
138 func (set *threadUnsafeSet) Clear() {
139 *set = newThreadUnsafeSet()
142 func (set *threadUnsafeSet) Remove(i interface{}) {
146 func (set *threadUnsafeSet) Cardinality() int {
150 func (set *threadUnsafeSet) Iter() <-chan interface{} {
151 ch := make(chan interface{})
153 for elem := range *set {
162 func (set *threadUnsafeSet) Equal(other Set) bool {
163 _ = other.(*threadUnsafeSet)
165 if set.Cardinality() != other.Cardinality() {
168 for elem := range *set {
169 if !other.Contains(elem) {
176 func (set *threadUnsafeSet) Clone() Set {
177 clonedSet := newThreadUnsafeSet()
178 for elem := range *set {
184 func (set *threadUnsafeSet) String() string {
185 items := make([]string, 0, len(*set))
187 for elem := range *set {
188 items = append(items, fmt.Sprintf("%v", elem))
190 return fmt.Sprintf("Set{%s}", strings.Join(items, ", "))
193 func (pair orderedPair) String() string {
194 return fmt.Sprintf("(%v, %v)", pair.first, pair.second)
197 func (set *threadUnsafeSet) PowerSet() Set {
198 powSet := NewThreadUnsafeSet()
199 nullset := newThreadUnsafeSet()
202 for es := range *set {
203 u := newThreadUnsafeSet()
206 p := newThreadUnsafeSet()
207 if reflect.TypeOf(er).Name() == "" {
208 k := er.(*threadUnsafeSet)
209 for ek := range *(k) {
219 powSet = powSet.Union(&u)
225 func (set *threadUnsafeSet) CartesianProduct(other Set) Set {
226 o := other.(*threadUnsafeSet)
227 cartProduct := NewThreadUnsafeSet()
229 for i := range *set {
231 elem := orderedPair{first: i, second: j}
232 cartProduct.Add(elem)
239 func (set *threadUnsafeSet) ToSlice() []interface{} {
240 keys := make([]interface{}, 0, set.Cardinality())
241 for elem := range *set {
242 keys = append(keys, elem)