-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.go
163 lines (135 loc) · 3.05 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
package containers
// Credits: Make p set from github.com/badgerodon/collections generic
type (
Set[T comparable] struct {
hash map[T]nothing
}
nothing struct{}
)
// Create a new set
func NewSet[T comparable](initial ...T) *Set[T] {
s := &Set[T]{make(map[T]nothing)}
for _, v := range initial {
s.Insert(v)
}
return s
}
// Transforms Set to Slice
func (p *Set[T]) ToSlice() *Slice[T] {
n := []T{}
for k := range p.hash {
n = append(n, k)
}
return NewSlice(n...)
}
// Get the sets values
func (p *Set[T]) Values() []T {
n := []T{}
for k := range p.hash {
n = append(n, k)
}
return n
}
// Find the difference between two sets
func (p *Set[T]) Difference(set *Set[T]) *Set[T] {
n := make(map[T]nothing)
for k := range p.hash {
if _, exists := set.hash[k]; !exists {
n[k] = nothing{}
}
}
return &Set[T]{n}
}
// Do calls f for each item in the set
func (p *Set[T]) Do(f func(T)) {
for k := range p.hash {
f(k)
}
}
// ForEach calls the given function for each element in the set.
//
// The function takes a parameter 'f' of type 'func(T) error'. It should
// receive an element of type 'T' and return an error if any.
//
// It returns an error if the function 'f' returns an error during iteration.
// Otherwise, it returns nil.
func (p *Set[T]) ForEach(f func(T) error) error {
for k := range p.hash {
if err := f(k); err != nil {
return err
}
}
return nil
}
// Has checks if the given element exists in the Set.
//
// Parameters:
// - element: the element to check for existence in the Set.
//
// Returns:
// - bool: true if the element exists in the Set, false otherwise.
func (p *Set[T]) Has(element T) bool {
_, exists := p.hash[element]
return exists
}
// Test if Set has any Items
func (p *Set[T]) HasItems() bool {
return len(p.hash) > 0
}
// Add an element to the set
func (p *Set[T]) Insert(element T) {
p.hash[element] = nothing{}
}
// Find the intersection of two sets
func (p *Set[T]) Intersection(set *Set[T]) *Set[T] {
n := make(map[T]nothing)
for k := range p.hash {
if _, exists := set.hash[k]; exists {
n[k] = nothing{}
}
}
return &Set[T]{n}
}
// Return the number of items in the set
func (p *Set[T]) Len() int {
return len(p.hash)
}
// Test whether or not p set is a proper subset of "set"
func (p *Set[T]) ProperSubsetOf(set *Set[T]) bool {
return p.SubsetOf(set) && p.Len() < set.Len()
}
// Remove given elements from the set
func (p *Set[T]) Remove(elements ...T) {
for _, k := range elements {
delete(p.hash, k)
}
}
// Remove a set from the set
func (p *Set[T]) RemoveSet(set *Set[T]) {
for _, k := range set.Values() {
delete(p.hash, k)
}
}
// Test whether or not this set is a subset of "set"
func (p *Set[T]) SubsetOf(set *Set[T]) bool {
if p.Len() > set.Len() {
return false
}
for k := range p.hash {
if _, exists := set.hash[k]; !exists {
return false
}
}
return true
}
// Find the union of two sets
func (p *Set[T]) Union(set *Set[T]) *Set[T] {
n := make(map[T]nothing)
for k := range p.hash {
n[k] = nothing{}
}
for k := range set.hash {
n[k] = nothing{}
}
return &Set[T]{n}
}