|
| 1 | +package immutable |
| 2 | + |
| 3 | +type Set[T comparable] struct { |
| 4 | + m *Map[T, struct{}] |
| 5 | +} |
| 6 | + |
| 7 | +func NewSet[T comparable](hasher Hasher[T]) Set[T] { |
| 8 | + return Set[T]{ |
| 9 | + m: NewMap[T, struct{}](hasher), |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +func (s Set[T]) Set(val T) Set[T] { |
| 14 | + return Set[T]{ |
| 15 | + m: s.m.Set(val, struct{}{}), |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +func (s Set[T]) Delete(val T) Set[T] { |
| 20 | + return Set[T]{ |
| 21 | + m: s.m.Delete(val), |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +func (s Set[T]) Has(val T) bool { |
| 26 | + _, ok := s.m.Get(val) |
| 27 | + return ok |
| 28 | +} |
| 29 | + |
| 30 | +func (s Set[K]) Len() int { |
| 31 | + return s.m.Len() |
| 32 | +} |
| 33 | + |
| 34 | +func (s Set[T]) Iterator() *SetIterator[T] { |
| 35 | + itr := &SetIterator[T]{mi: s.m.Iterator()} |
| 36 | + itr.mi.First() |
| 37 | + return itr |
| 38 | +} |
| 39 | + |
| 40 | +type SetIterator[T comparable] struct { |
| 41 | + mi *MapIterator[T, struct{}] |
| 42 | +} |
| 43 | + |
| 44 | +func (itr *SetIterator[T]) Done() bool { |
| 45 | + return itr.mi.Done() |
| 46 | +} |
| 47 | + |
| 48 | +func (itr *SetIterator[T]) First() { |
| 49 | + itr.mi.First() |
| 50 | +} |
| 51 | + |
| 52 | +func (itr *SetIterator[T]) Next() (val T, ok bool) { |
| 53 | + val, _, ok = itr.mi.Next() |
| 54 | + return |
| 55 | +} |
| 56 | + |
| 57 | +type SetBuilder[T comparable] struct { |
| 58 | + s Set[T] |
| 59 | +} |
| 60 | + |
| 61 | +func NewSetBuilder[T comparable](hasher Hasher[T]) *SetBuilder[T] { |
| 62 | + return &SetBuilder[T]{s: NewSet(hasher)} |
| 63 | +} |
| 64 | + |
| 65 | +func (s SetBuilder[T]) Set(val T) { |
| 66 | + s.s.m = s.s.m.set(val, struct{}{}, true) |
| 67 | +} |
| 68 | + |
| 69 | +func (s SetBuilder[T]) Delete(val T) { |
| 70 | + s.s.m = s.s.m.delete(val, true) |
| 71 | +} |
| 72 | + |
| 73 | +func (s SetBuilder[T]) Has(val T) bool { |
| 74 | + return s.s.Has(val) |
| 75 | +} |
| 76 | + |
| 77 | +func (s SetBuilder[T]) Len() int { |
| 78 | + return s.s.Len() |
| 79 | +} |
| 80 | + |
| 81 | +type SortedSet[T comparable] struct { |
| 82 | + m *SortedMap[T, struct{}] |
| 83 | +} |
| 84 | + |
| 85 | +func NewSortedSet[T comparable](comparer Comparer[T]) SortedSet[T] { |
| 86 | + return SortedSet[T]{ |
| 87 | + m: NewSortedMap[T, struct{}](comparer), |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func (s SortedSet[T]) Put(val T) SortedSet[T] { |
| 92 | + return SortedSet[T]{ |
| 93 | + m: s.m.Set(val, struct{}{}), |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func (s SortedSet[T]) Delete(val T) SortedSet[T] { |
| 98 | + return SortedSet[T]{ |
| 99 | + m: s.m.Delete(val), |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func (s SortedSet[T]) Has(val T) bool { |
| 104 | + _, ok := s.m.Get(val) |
| 105 | + return ok |
| 106 | +} |
| 107 | + |
| 108 | +func (s SortedSet[K]) Len() int { |
| 109 | + return s.m.Len() |
| 110 | +} |
| 111 | + |
| 112 | +func (s SortedSet[T]) Iterator() *SortedSetIterator[T] { |
| 113 | + itr := &SortedSetIterator[T]{mi: s.m.Iterator()} |
| 114 | + itr.mi.First() |
| 115 | + return itr |
| 116 | +} |
| 117 | + |
| 118 | +type SortedSetIterator[T comparable] struct { |
| 119 | + mi *SortedMapIterator[T, struct{}] |
| 120 | +} |
| 121 | + |
| 122 | +func (itr *SortedSetIterator[T]) Done() bool { |
| 123 | + return itr.mi.Done() |
| 124 | +} |
| 125 | + |
| 126 | +func (itr *SortedSetIterator[T]) First() { |
| 127 | + itr.mi.First() |
| 128 | +} |
| 129 | + |
| 130 | +func (itr *SortedSetIterator[T]) Last() { |
| 131 | + itr.mi.Last() |
| 132 | +} |
| 133 | + |
| 134 | +func (itr *SortedSetIterator[T]) Next() (val T, ok bool) { |
| 135 | + val, _, ok = itr.mi.Next() |
| 136 | + return |
| 137 | +} |
| 138 | + |
| 139 | +func (itr *SortedSetIterator[T]) Prev() (val T, ok bool) { |
| 140 | + val, _, ok = itr.mi.Prev() |
| 141 | + return |
| 142 | +} |
| 143 | + |
| 144 | +func (itr *SortedSetIterator[T]) Seek(val T) { |
| 145 | + itr.mi.Seek(val) |
| 146 | +} |
| 147 | + |
| 148 | +type SortedSetBuilder[T comparable] struct { |
| 149 | + s SortedSet[T] |
| 150 | +} |
| 151 | + |
| 152 | +func NewSortedSetBuilder[T comparable](comparer Comparer[T]) *SortedSetBuilder[T] { |
| 153 | + return &SortedSetBuilder[T]{s: NewSortedSet(comparer)} |
| 154 | +} |
| 155 | + |
| 156 | +func (s SortedSetBuilder[T]) Set(val T) { |
| 157 | + s.s.m = s.s.m.set(val, struct{}{}, true) |
| 158 | +} |
| 159 | + |
| 160 | +func (s SortedSetBuilder[T]) Delete(val T) { |
| 161 | + s.s.m = s.s.m.delete(val, true) |
| 162 | +} |
| 163 | + |
| 164 | +func (s SortedSetBuilder[T]) Has(val T) bool { |
| 165 | + return s.s.Has(val) |
| 166 | +} |
| 167 | + |
| 168 | +func (s SortedSetBuilder[T]) Len() int { |
| 169 | + return s.s.Len() |
| 170 | +} |
0 commit comments