...
1 package immutable
2
3
4
5
6
7 type Set[T any] struct {
8 m *Map[T, struct{}]
9 }
10
11
12
13
14
15
16 func NewSet[T any](hasher Hasher[T], values ...T) Set[T] {
17 m := NewMap[T, struct{}](hasher)
18 for _, value := range values {
19 m = m.set(value, struct{}{}, true)
20 }
21 return Set[T]{m}
22 }
23
24
25
26
27 func (s Set[T]) Add(value T) Set[T] {
28 return Set[T]{s.m.Set(value, struct{}{})}
29 }
30
31
32 func (s Set[T]) Delete(value T) Set[T] {
33 return Set[T]{s.m.Delete(value)}
34 }
35
36
37 func (s Set[T]) Has(val T) bool {
38 _, ok := s.m.Get(val)
39 return ok
40 }
41
42
43 func (s Set[K]) Len() int {
44 return s.m.Len()
45 }
46
47
48 func (s Set[T]) Items() []T {
49 r := make([]T, 0, s.Len())
50 itr := s.Iterator()
51 for !itr.Done() {
52 v, _ := itr.Next()
53 r = append(r, v)
54 }
55 return r
56 }
57
58
59 func (s Set[T]) Iterator() *SetIterator[T] {
60 itr := &SetIterator[T]{mi: s.m.Iterator()}
61 itr.mi.First()
62 return itr
63 }
64
65
66
67 type SetIterator[T any] struct {
68 mi *MapIterator[T, struct{}]
69 }
70
71
72 func (itr *SetIterator[T]) Done() bool {
73 return itr.mi.Done()
74 }
75
76
77 func (itr *SetIterator[T]) First() {
78 itr.mi.First()
79 }
80
81
82 func (itr *SetIterator[T]) Next() (val T, ok bool) {
83 val, _, ok = itr.mi.Next()
84 return
85 }
86
87 type SetBuilder[T any] struct {
88 s Set[T]
89 }
90
91 func NewSetBuilder[T any](hasher Hasher[T]) *SetBuilder[T] {
92 return &SetBuilder[T]{s: NewSet(hasher)}
93 }
94
95 func (s SetBuilder[T]) Set(val T) {
96 s.s.m = s.s.m.set(val, struct{}{}, true)
97 }
98
99 func (s SetBuilder[T]) Delete(val T) {
100 s.s.m = s.s.m.delete(val, true)
101 }
102
103 func (s SetBuilder[T]) Has(val T) bool {
104 return s.s.Has(val)
105 }
106
107 func (s SetBuilder[T]) Len() int {
108 return s.s.Len()
109 }
110
111 type SortedSet[T any] struct {
112 m *SortedMap[T, struct{}]
113 }
114
115
116
117
118
119
120
121 func NewSortedSet[T any](comparer Comparer[T], values ...T) SortedSet[T] {
122 m := NewSortedMap[T, struct{}](comparer)
123 for _, value := range values {
124 m = m.set(value, struct{}{}, true)
125 }
126 return SortedSet[T]{m}
127 }
128
129
130
131
132 func (s SortedSet[T]) Add(value T) SortedSet[T] {
133 return SortedSet[T]{s.m.Set(value, struct{}{})}
134 }
135
136
137 func (s SortedSet[T]) Delete(value T) SortedSet[T] {
138 return SortedSet[T]{s.m.Delete(value)}
139 }
140
141
142 func (s SortedSet[T]) Has(val T) bool {
143 _, ok := s.m.Get(val)
144 return ok
145 }
146
147
148 func (s SortedSet[K]) Len() int {
149 return s.m.Len()
150 }
151
152
153 func (s SortedSet[T]) Items() []T {
154 r := make([]T, 0, s.Len())
155 itr := s.Iterator()
156 for !itr.Done() {
157 v, _ := itr.Next()
158 r = append(r, v)
159 }
160 return r
161 }
162
163
164 func (s SortedSet[T]) Iterator() *SortedSetIterator[T] {
165 itr := &SortedSetIterator[T]{mi: s.m.Iterator()}
166 itr.mi.First()
167 return itr
168 }
169
170
171
172 type SortedSetIterator[T any] struct {
173 mi *SortedMapIterator[T, struct{}]
174 }
175
176
177 func (itr *SortedSetIterator[T]) Done() bool {
178 return itr.mi.Done()
179 }
180
181
182 func (itr *SortedSetIterator[T]) First() {
183 itr.mi.First()
184 }
185
186
187 func (itr *SortedSetIterator[T]) Last() {
188 itr.mi.Last()
189 }
190
191
192 func (itr *SortedSetIterator[T]) Next() (val T, ok bool) {
193 val, _, ok = itr.mi.Next()
194 return
195 }
196
197
198 func (itr *SortedSetIterator[T]) Prev() (val T, ok bool) {
199 val, _, ok = itr.mi.Prev()
200 return
201 }
202
203
204
205
206
207 func (itr *SortedSetIterator[T]) Seek(val T) {
208 itr.mi.Seek(val)
209 }
210
211 type SortedSetBuilder[T any] struct {
212 s SortedSet[T]
213 }
214
215 func NewSortedSetBuilder[T any](comparer Comparer[T]) *SortedSetBuilder[T] {
216 return &SortedSetBuilder[T]{s: NewSortedSet(comparer)}
217 }
218
219 func (s SortedSetBuilder[T]) Set(val T) {
220 s.s.m = s.s.m.set(val, struct{}{}, true)
221 }
222
223 func (s SortedSetBuilder[T]) Delete(val T) {
224 s.s.m = s.s.m.delete(val, true)
225 }
226
227 func (s SortedSetBuilder[T]) Has(val T) bool {
228 return s.s.Has(val)
229 }
230
231 func (s SortedSetBuilder[T]) Len() int {
232 return s.s.Len()
233 }
234
View as plain text