|
| 1 | +package typeutil |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "sort" |
| 7 | + |
| 8 | + "golang.org/x/exp/constraints" |
| 9 | +) |
| 10 | + |
| 11 | +// Set implements a set data structure based on built-in maps. This is not |
| 12 | +// optimized for large data, but to be convient. |
| 13 | +type Set[T constraints.Ordered] struct { |
| 14 | + data map[T]struct{} |
| 15 | +} |
| 16 | + |
| 17 | +// NewSet initializes a new Set with the given values. If there are no values |
| 18 | +// provides this is equivalent to new(Set[T]). |
| 19 | +func NewSet[T constraints.Ordered](values ...T) *Set[T] { |
| 20 | + s := new(Set[T]) |
| 21 | + for i := range values { |
| 22 | + s.Add(values[i]) |
| 23 | + } |
| 24 | + return s |
| 25 | +} |
| 26 | + |
| 27 | +// Add puts a single value to the set. The set will be the same, if it already |
| 28 | +// contains the value. |
| 29 | +func (s *Set[T]) Add(value T) { |
| 30 | + if s.data == nil { |
| 31 | + s.data = map[T]struct{}{} |
| 32 | + } |
| 33 | + |
| 34 | + s.data[value] = struct{}{} |
| 35 | +} |
| 36 | + |
| 37 | +// Contains returns true, if the given value is part of the set. |
| 38 | +func (s *Set[T]) Contains(value T) bool { |
| 39 | + if s.data == nil { |
| 40 | + return false |
| 41 | + } |
| 42 | + |
| 43 | + _, found := s.data[value] |
| 44 | + return found |
| 45 | +} |
| 46 | + |
| 47 | +// Remove removes the given value from the set. The set will be the same, if |
| 48 | +// the value is not part of it. |
| 49 | +func (s *Set[T]) Remove(value T) { |
| 50 | + if s.data == nil { |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + delete(s.data, value) |
| 55 | +} |
| 56 | + |
| 57 | +// Substract removes every element from the given set from the set. |
| 58 | +func (s *Set[T]) Subtract(other *Set[T]) { |
| 59 | + if s.data == nil { |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + for value := range other.data { |
| 64 | + delete(s.data, value) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// Len returns the number of all values in the set. |
| 69 | +func (s Set[T]) Len() int { |
| 70 | + return len(s.data) |
| 71 | +} |
| 72 | + |
| 73 | +// ToList converts the set into a slice. Since the set uses a map as an |
| 74 | +// underlying data structure, this will copy each value. So it might be memory |
| 75 | +// intensive. Also it sorts the slice to ensure a cosistent result. |
| 76 | +func (s *Set[T]) ToList() []T { |
| 77 | + if s == nil || s.data == nil { |
| 78 | + return nil |
| 79 | + } |
| 80 | + |
| 81 | + list := make([]T, 0, len(s.data)) |
| 82 | + |
| 83 | + if len(s.data) > 0 { |
| 84 | + for v := range s.data { |
| 85 | + list = append(list, v) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + sort.Slice(list, func(i, j int) bool { return list[i] < list[j] }) |
| 90 | + |
| 91 | + return list |
| 92 | +} |
| 93 | + |
| 94 | +// AddSet adds each value from the given set to the set. |
| 95 | +func (s *Set[T]) AddSet(other *Set[T]) { |
| 96 | + if other == nil { |
| 97 | + return |
| 98 | + } |
| 99 | + |
| 100 | + for o := range other.data { |
| 101 | + s.Add(o) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +// MarshalJSON adds support for mashaling the set into a JSON list. |
| 106 | +func (s Set[T]) MarshalJSON() ([]byte, error) { |
| 107 | + list := s.ToList() |
| 108 | + return json.Marshal(list) |
| 109 | +} |
| 110 | + |
| 111 | +// MarshalJSON adds support for unmashaling the set from a JSON list. |
| 112 | +func (s *Set[T]) UnmarshalJSON(data []byte) error { |
| 113 | + list := []T{} |
| 114 | + err := json.Unmarshal(data, &list) |
| 115 | + if err != nil { |
| 116 | + return fmt.Errorf("unmarshal set: %w", err) |
| 117 | + } |
| 118 | + |
| 119 | + for _, v := range list { |
| 120 | + s.Add(v) |
| 121 | + } |
| 122 | + |
| 123 | + return nil |
| 124 | +} |
| 125 | + |
| 126 | +func SetUnion[T constraints.Ordered](sets ...*Set[T]) *Set[T] { |
| 127 | + result := new(Set[T]) |
| 128 | + |
| 129 | + for s := range sets { |
| 130 | + result.AddSet(sets[s]) |
| 131 | + } |
| 132 | + |
| 133 | + return result |
| 134 | +} |
| 135 | + |
| 136 | +// SetIntersect returns a set that only contains elements which exist in all |
| 137 | +// sets. |
| 138 | +func SetIntersect[T constraints.Ordered](sets ...*Set[T]) *Set[T] { |
| 139 | + result := new(Set[T]) |
| 140 | + |
| 141 | + for _, s := range sets { |
| 142 | + if s == nil || s.data == nil { |
| 143 | + return result |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + if len(sets) == 0 { |
| 148 | + return result |
| 149 | + } |
| 150 | + |
| 151 | + result.AddSet(sets[0]) |
| 152 | + |
| 153 | + for _, s := range sets[1:] { |
| 154 | + for e := range result.data { |
| 155 | + if !s.Contains(e) { |
| 156 | + delete(result.data, e) |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + return result |
| 162 | +} |
0 commit comments