|
1 | 1 | package immutable
|
2 | 2 |
|
| 3 | +// Set represents a collection of unique values. The set uses a Hasher |
| 4 | +// to generate hashes and check for equality of key values. |
| 5 | +// |
| 6 | +// Internally, the Set stores values as keys of a Map[T,struct{}] |
3 | 7 | type Set[T comparable] struct {
|
4 | 8 | m *Map[T, struct{}]
|
5 | 9 | }
|
6 | 10 |
|
7 |
| -func NewSet[T comparable](hasher Hasher[T]) Set[T] { |
8 |
| - return Set[T]{ |
| 11 | +// NewSet returns a new instance of Set. |
| 12 | +// |
| 13 | +// If hasher is nil, a default hasher implementation will automatically be chosen based on the first key added. |
| 14 | +// Default hasher implementations only exist for int, string, and byte slice types. |
| 15 | +// NewSet can also take some initial values as varargs. |
| 16 | +func NewSet[T comparable](hasher Hasher[T], values ...T) Set[T] { |
| 17 | + s := Set[T]{ |
9 | 18 | m: NewMap[T, struct{}](hasher),
|
10 | 19 | }
|
| 20 | + for _, value := range values { |
| 21 | + s.m.set(value, struct{}{}, true) |
| 22 | + } |
| 23 | + return s |
11 | 24 | }
|
12 | 25 |
|
13 |
| -func (s Set[T]) Set(val T) Set[T] { |
14 |
| - return Set[T]{ |
15 |
| - m: s.m.Set(val, struct{}{}), |
| 26 | +// Set returns a set containing the new value. |
| 27 | +// |
| 28 | +// This function will return a new set even if the set already contains the value. |
| 29 | +func (s Set[T]) Set(values ...T) Set[T] { |
| 30 | + n := Set[T]{ |
| 31 | + m: s.m.clone(), |
| 32 | + } |
| 33 | + for _, value := range values { |
| 34 | + n.m.set(value, struct{}{}, true) |
16 | 35 | }
|
| 36 | + return n |
17 | 37 | }
|
18 | 38 |
|
19 |
| -func (s Set[T]) Delete(val T) Set[T] { |
20 |
| - return Set[T]{ |
21 |
| - m: s.m.Delete(val), |
| 39 | +// Delete returns a set with the given key removed. |
| 40 | +func (s Set[T]) Delete(values ...T) Set[T] { |
| 41 | + n := Set[T]{ |
| 42 | + m: s.m.clone(), |
| 43 | + } |
| 44 | + for _, value := range values { |
| 45 | + n.m.delete(value, true) |
22 | 46 | }
|
| 47 | + return n |
23 | 48 | }
|
24 | 49 |
|
| 50 | +// Has returns true when the set contains the given value |
25 | 51 | func (s Set[T]) Has(val T) bool {
|
26 | 52 | _, ok := s.m.Get(val)
|
27 | 53 | return ok
|
28 | 54 | }
|
29 | 55 |
|
| 56 | +// Len returns the number of elements in the underlying map. |
30 | 57 | func (s Set[K]) Len() int {
|
31 | 58 | return s.m.Len()
|
32 | 59 | }
|
33 | 60 |
|
| 61 | +// Items returns a slice of the items inside the set |
| 62 | +func (s Set[T]) Items() []T { |
| 63 | + r := make([]T, 0, s.Len()) |
| 64 | + itr := s.Iterator() |
| 65 | + for !itr.Done() { |
| 66 | + v, _ := itr.Next() |
| 67 | + r = append(r, v) |
| 68 | + } |
| 69 | + return r |
| 70 | +} |
| 71 | + |
| 72 | +// Iterator returns a new iterator for this set positioned at the first value. |
34 | 73 | func (s Set[T]) Iterator() *SetIterator[T] {
|
35 | 74 | itr := &SetIterator[T]{mi: s.m.Iterator()}
|
36 | 75 | itr.mi.First()
|
37 | 76 | return itr
|
38 | 77 | }
|
39 | 78 |
|
| 79 | +// SetIterator represents an iterator over a set. |
| 80 | +// Iteration can occur in natural or reverse order based on use of Next() or Prev(). |
40 | 81 | type SetIterator[T comparable] struct {
|
41 | 82 | mi *MapIterator[T, struct{}]
|
42 | 83 | }
|
43 | 84 |
|
| 85 | +// Done returns true if no more values remain in the iterator. |
44 | 86 | func (itr *SetIterator[T]) Done() bool {
|
45 | 87 | return itr.mi.Done()
|
46 | 88 | }
|
47 | 89 |
|
| 90 | +// First moves the iterator to the first value. |
48 | 91 | func (itr *SetIterator[T]) First() {
|
49 | 92 | itr.mi.First()
|
50 | 93 | }
|
51 | 94 |
|
| 95 | +// Next moves the iterator to the next value. |
52 | 96 | func (itr *SetIterator[T]) Next() (val T, ok bool) {
|
53 | 97 | val, _, ok = itr.mi.Next()
|
54 | 98 | return
|
@@ -82,65 +126,112 @@ type SortedSet[T comparable] struct {
|
82 | 126 | m *SortedMap[T, struct{}]
|
83 | 127 | }
|
84 | 128 |
|
85 |
| -func NewSortedSet[T comparable](comparer Comparer[T]) SortedSet[T] { |
86 |
| - return SortedSet[T]{ |
| 129 | +// NewSortedSet returns a new instance of SortedSet. |
| 130 | +// |
| 131 | +// If comparer is nil then |
| 132 | +// a default comparer is set after the first key is inserted. Default comparers |
| 133 | +// exist for int, string, and byte slice keys. |
| 134 | +// NewSortedSet can also take some initial values as varargs. |
| 135 | +func NewSortedSet[T comparable](comparer Comparer[T], values ...T) SortedSet[T] { |
| 136 | + s := SortedSet[T]{ |
87 | 137 | m: NewSortedMap[T, struct{}](comparer),
|
88 | 138 | }
|
| 139 | + for _, value := range values { |
| 140 | + s.m.set(value, struct{}{}, true) |
| 141 | + } |
| 142 | + return s |
89 | 143 | }
|
90 | 144 |
|
91 |
| -func (s SortedSet[T]) Put(val T) SortedSet[T] { |
92 |
| - return SortedSet[T]{ |
93 |
| - m: s.m.Set(val, struct{}{}), |
| 145 | +// Set returns a set containing the new value. |
| 146 | +// |
| 147 | +// This function will return a new set even if the set already contains the value. |
| 148 | +func (s SortedSet[T]) Set(values ...T) SortedSet[T] { |
| 149 | + n := SortedSet[T]{ |
| 150 | + m: s.m.clone(), |
| 151 | + } |
| 152 | + for _, value := range values { |
| 153 | + n.m.set(value, struct{}{}, true) |
94 | 154 | }
|
| 155 | + return n |
95 | 156 | }
|
96 | 157 |
|
97 |
| -func (s SortedSet[T]) Delete(val T) SortedSet[T] { |
98 |
| - return SortedSet[T]{ |
99 |
| - m: s.m.Delete(val), |
| 158 | +// Delete returns a set with the given key removed. |
| 159 | +func (s SortedSet[T]) Delete(values ...T) SortedSet[T] { |
| 160 | + n := SortedSet[T]{ |
| 161 | + m: s.m.clone(), |
| 162 | + } |
| 163 | + for _, value := range values { |
| 164 | + n.m.delete(value, true) |
100 | 165 | }
|
| 166 | + return n |
101 | 167 | }
|
102 | 168 |
|
| 169 | +// Has returns true when the set contains the given value |
103 | 170 | func (s SortedSet[T]) Has(val T) bool {
|
104 | 171 | _, ok := s.m.Get(val)
|
105 | 172 | return ok
|
106 | 173 | }
|
107 | 174 |
|
| 175 | +// Len returns the number of elements in the underlying map. |
108 | 176 | func (s SortedSet[K]) Len() int {
|
109 | 177 | return s.m.Len()
|
110 | 178 | }
|
111 | 179 |
|
| 180 | +// Items returns a slice of the items inside the set |
| 181 | +func (s SortedSet[T]) Items() []T { |
| 182 | + r := make([]T, 0, s.Len()) |
| 183 | + itr := s.Iterator() |
| 184 | + for !itr.Done() { |
| 185 | + v, _ := itr.Next() |
| 186 | + r = append(r, v) |
| 187 | + } |
| 188 | + return r |
| 189 | +} |
| 190 | + |
| 191 | +// Iterator returns a new iterator for this set positioned at the first value. |
112 | 192 | func (s SortedSet[T]) Iterator() *SortedSetIterator[T] {
|
113 | 193 | itr := &SortedSetIterator[T]{mi: s.m.Iterator()}
|
114 | 194 | itr.mi.First()
|
115 | 195 | return itr
|
116 | 196 | }
|
117 | 197 |
|
| 198 | +// SortedSetIterator represents an iterator over a sorted set. |
| 199 | +// Iteration can occur in natural or reverse order based on use of Next() or Prev(). |
118 | 200 | type SortedSetIterator[T comparable] struct {
|
119 | 201 | mi *SortedMapIterator[T, struct{}]
|
120 | 202 | }
|
121 | 203 |
|
| 204 | +// Done returns true if no more values remain in the iterator. |
122 | 205 | func (itr *SortedSetIterator[T]) Done() bool {
|
123 | 206 | return itr.mi.Done()
|
124 | 207 | }
|
125 | 208 |
|
| 209 | +// First moves the iterator to the first value. |
126 | 210 | func (itr *SortedSetIterator[T]) First() {
|
127 | 211 | itr.mi.First()
|
128 | 212 | }
|
129 | 213 |
|
| 214 | +// Last moves the iterator to the last value. |
130 | 215 | func (itr *SortedSetIterator[T]) Last() {
|
131 | 216 | itr.mi.Last()
|
132 | 217 | }
|
133 | 218 |
|
| 219 | +// Next moves the iterator to the next value. |
134 | 220 | func (itr *SortedSetIterator[T]) Next() (val T, ok bool) {
|
135 | 221 | val, _, ok = itr.mi.Next()
|
136 | 222 | return
|
137 | 223 | }
|
138 | 224 |
|
| 225 | +// Next moves the iterator to the previous value. |
139 | 226 | func (itr *SortedSetIterator[T]) Prev() (val T, ok bool) {
|
140 | 227 | val, _, ok = itr.mi.Prev()
|
141 | 228 | return
|
142 | 229 | }
|
143 | 230 |
|
| 231 | +// Next moves the iterator to the given value. |
| 232 | +// |
| 233 | +// If the value does not exist then the next value is used. If no more keys exist |
| 234 | +// then the iterator is marked as done. |
144 | 235 | func (itr *SortedSetIterator[T]) Seek(val T) {
|
145 | 236 | itr.mi.Seek(val)
|
146 | 237 | }
|
|
0 commit comments