-
Notifications
You must be signed in to change notification settings - Fork 0
/
subindustry_set.go
172 lines (150 loc) · 4.26 KB
/
subindustry_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
// Generated by: main
// TypeWriter: set
// Directive: +gen on SubIndustry
package gics
// Set is a modification of https://github.com/deckarep/golang-set
// The MIT License (MIT)
// Copyright (c) 2013 Ralph Caraveo (deckarep@gmail.com)
// SubIndustrySet is the primary type that represents a set
type SubIndustrySet map[SubIndustry]struct{}
// NewSubIndustrySet creates and returns a reference to an empty set.
func NewSubIndustrySet(a ...SubIndustry) SubIndustrySet {
s := make(SubIndustrySet)
for _, i := range a {
s.Add(i)
}
return s
}
// ToSlice returns the elements of the current set as a slice
func (set SubIndustrySet) ToSlice() []SubIndustry {
var s []SubIndustry
for v := range set {
s = append(s, v)
}
return s
}
// Add adds an item to the current set if it doesn't already exist in the set.
func (set SubIndustrySet) Add(i SubIndustry) bool {
_, found := set[i]
set[i] = struct{}{}
return !found //False if it existed already
}
// Contains determines if a given item is already in the set.
func (set SubIndustrySet) Contains(i SubIndustry) bool {
_, found := set[i]
return found
}
// ContainsAll determines if the given items are all in the set
func (set SubIndustrySet) ContainsAll(i ...SubIndustry) bool {
for _, v := range i {
if !set.Contains(v) {
return false
}
}
return true
}
// IsSubset determines if every item in the other set is in this set.
func (set SubIndustrySet) IsSubset(other SubIndustrySet) bool {
for elem := range set {
if !other.Contains(elem) {
return false
}
}
return true
}
// IsSuperset determines if every item of this set is in the other set.
func (set SubIndustrySet) IsSuperset(other SubIndustrySet) bool {
return other.IsSubset(set)
}
// Union returns a new set with all items in both sets.
func (set SubIndustrySet) Union(other SubIndustrySet) SubIndustrySet {
unionedSet := NewSubIndustrySet()
for elem := range set {
unionedSet.Add(elem)
}
for elem := range other {
unionedSet.Add(elem)
}
return unionedSet
}
// Intersect returns a new set with items that exist only in both sets.
func (set SubIndustrySet) Intersect(other SubIndustrySet) SubIndustrySet {
intersection := NewSubIndustrySet()
// loop over smaller set
if set.Cardinality() < other.Cardinality() {
for elem := range set {
if other.Contains(elem) {
intersection.Add(elem)
}
}
} else {
for elem := range other {
if set.Contains(elem) {
intersection.Add(elem)
}
}
}
return intersection
}
// Difference returns a new set with items in the current set but not in the other set
func (set SubIndustrySet) Difference(other SubIndustrySet) SubIndustrySet {
differencedSet := NewSubIndustrySet()
for elem := range set {
if !other.Contains(elem) {
differencedSet.Add(elem)
}
}
return differencedSet
}
// SymmetricDifference returns a new set with items in the current set or the other set but not in both.
func (set SubIndustrySet) SymmetricDifference(other SubIndustrySet) SubIndustrySet {
aDiff := set.Difference(other)
bDiff := other.Difference(set)
return aDiff.Union(bDiff)
}
// Clear clears the entire set to be the empty set.
func (set *SubIndustrySet) Clear() {
*set = make(SubIndustrySet)
}
// Remove allows the removal of a single item in the set.
func (set SubIndustrySet) Remove(i SubIndustry) {
delete(set, i)
}
// Cardinality returns how many items are currently in the set.
func (set SubIndustrySet) Cardinality() int {
return len(set)
}
// Iter returns a channel of type SubIndustry that you can range over.
func (set SubIndustrySet) Iter() <-chan SubIndustry {
ch := make(chan SubIndustry)
go func() {
for elem := range set {
ch <- elem
}
close(ch)
}()
return ch
}
// Equal determines if two sets are equal to each other.
// If they both are the same size and have the same items they are considered equal.
// Order of items is not relevent for sets to be equal.
func (set SubIndustrySet) Equal(other SubIndustrySet) bool {
if set.Cardinality() != other.Cardinality() {
return false
}
for elem := range set {
if !other.Contains(elem) {
return false
}
}
return true
}
// Clone returns a clone of the set.
// Does NOT clone the underlying elements.
func (set SubIndustrySet) Clone() SubIndustrySet {
clonedSet := NewSubIndustrySet()
for elem := range set {
clonedSet.Add(elem)
}
return clonedSet
}