-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathset.go
124 lines (110 loc) · 2.6 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
// genericset provides a simple map-based implementation of generic set.
package genericset
import (
"fmt"
"sync"
)
type Set[K comparable] struct {
m sync.RWMutex
data map[K]struct{}
}
// New returns new set of type K.
func New[K comparable]() Set[K] {
return Set[K]{
m: sync.RWMutex{},
data: make(map[K]struct{}),
}
}
// Add — add elements to a set.
func (s *Set[T]) Add(elements ...T) {
s.m.Lock()
defer s.m.Unlock()
for _, elem := range elements {
s.data[elem] = struct{}{}
}
}
// Del – remove element from set.
func (s *Set[T]) Del(elem T) {
s.m.Lock()
defer s.m.Unlock()
delete(s.data, elem)
}
// Size returns size of the set.
func (s *Set[T]) Size() int {
s.m.RLock()
defer s.m.RUnlock()
return len(s.data)
}
// String returns string representation of the set.
func (s Set[T]) String() string {
return fmt.Sprintf("%v", s.ToSlice())
}
// ToSlice returns slice with elements of the set.
func (s *Set[T]) ToSlice() []T {
s.m.RLock()
defer s.m.RUnlock()
result := make([]T, 0, len(s.data))
for elem, _ := range s.data {
result = append(result, elem)
}
return result
}
func (s *Set[T]) IsElement(elem T) bool {
s.m.RLock()
defer s.m.RUnlock()
_, exists := s.data[elem]
return exists
}
// IsEmpty returns true if there are no elements in set.
func (s *Set[T]) IsEmpty() bool {
s.m.RLock()
defer s.m.RUnlock()
return s.Size() == 0
}
// IsSubset returns true when every element of set `s` is also a member of set `another`, otherwise false.
func (s *Set[T]) IsSubset(another *Set[T]) bool {
s.m.RLock()
defer s.m.RUnlock()
another.m.RLock()
defer another.m.RUnlock()
for elem, _ := range s.data {
if _, exists := another.data[elem]; !exists {
return false
}
}
return true
}
// Intersection returns the intersection of set `s` and set `another`.
func (s *Set[T]) Intersection(another *Set[T]) *Set[T] {
result := New[T]()
s.m.RLock()
defer s.m.RUnlock()
another.m.RLock()
defer another.m.RUnlock()
for elem, _ := range s.data {
if _, exists := another.data[elem]; exists {
result.Add(elem)
}
}
return &result
}
// IsDisjoint returns true if set `s` and set `another` are disjoint (have no elements in common), otherwise false.
func (s *Set[T]) IsDisjoint(another *Set[T]) bool {
s.m.RLock()
defer s.m.RUnlock()
another.m.RLock()
defer another.m.RUnlock()
for elem, _ := range s.data {
if _, exists := another.data[elem]; exists {
return false
}
}
return true
}
// Union returns the merged (union) set of `s` and `another`.
func (s *Set[T]) Union(another *Set[T]) *Set[T] {
res := New[T]()
res.Add(s.ToSlice()...)
res.Add(another.ToSlice()...)
return &res
}