2022-08-11 04:32:12 +00:00
|
|
|
package set
|
|
|
|
|
|
|
|
type SetKey interface {
|
|
|
|
~int | ~string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Set[T SetKey] map[T]bool
|
|
|
|
|
2023-05-14 02:26:11 +00:00
|
|
|
// Add adds a key to the set.
|
2022-08-11 04:32:12 +00:00
|
|
|
func (s Set[T]) Add(key T) {
|
|
|
|
s[key] = true
|
|
|
|
}
|
|
|
|
|
2023-05-14 02:26:11 +00:00
|
|
|
// Contains returns true if the set contains the key.
|
2022-08-11 04:32:12 +00:00
|
|
|
func (s Set[T]) Contains(key T) bool {
|
|
|
|
_, ok := s[key]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2023-05-14 02:26:11 +00:00
|
|
|
// Remove removes a key from the set.
|
2022-08-11 04:32:12 +00:00
|
|
|
func (s Set[T]) Remove(key T) {
|
|
|
|
delete(s, key)
|
|
|
|
}
|
|
|
|
|
2023-05-14 02:26:11 +00:00
|
|
|
// Len returns the number of keys in the set.
|
2022-08-11 04:32:12 +00:00
|
|
|
func (s Set[T]) Len() int {
|
|
|
|
return len(s)
|
|
|
|
}
|
|
|
|
|
2023-05-14 02:26:11 +00:00
|
|
|
// IsEmpty returns true if the set is empty.
|
2022-08-11 04:32:12 +00:00
|
|
|
func (s Set[T]) IsEmpty() bool {
|
|
|
|
return len(s) == 0
|
|
|
|
}
|
|
|
|
|
2023-05-14 02:26:11 +00:00
|
|
|
// Clear removes all keys from the set.
|
2022-08-11 04:32:12 +00:00
|
|
|
func (s Set[T]) Keys() []T {
|
|
|
|
keys := make([]T, s.Len())
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for k := range s {
|
|
|
|
keys[i] = k
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
return keys
|
|
|
|
}
|
2023-05-14 02:26:11 +00:00
|
|
|
|
|
|
|
// Clear removes all keys from the set.
|
|
|
|
func (s Set[T]) Copy() Set[T] {
|
|
|
|
copy := make(Set[T])
|
|
|
|
|
|
|
|
for key := range s {
|
|
|
|
copy.Add(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return copy
|
|
|
|
}
|
|
|
|
|
|
|
|
// Difference returns a new set containing the keys that are in the first set but not in the second set.
|
|
|
|
func (set Set[T]) Difference(second Set[T]) Set[T] {
|
|
|
|
|
|
|
|
difference := set.Copy()
|
|
|
|
|
|
|
|
for key := range second {
|
|
|
|
difference.Remove(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return difference
|
|
|
|
}
|
|
|
|
|
|
|
|
// Union returns a new set containing the keys that are in either set.
|
|
|
|
func Union[T SetKey](sets ...Set[T]) Set[T] {
|
|
|
|
union := make(Set[T])
|
|
|
|
|
|
|
|
for _, set := range sets {
|
|
|
|
for key := range set {
|
|
|
|
union.Add(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return union
|
|
|
|
}
|
|
|
|
|
|
|
|
// Intersection returns a new set containing the keys that are in all sets.
|
|
|
|
func Intersection[T SetKey](sets ...Set[T]) Set[T] {
|
|
|
|
if len(sets) == 0 {
|
|
|
|
return make(Set[T])
|
|
|
|
}
|
|
|
|
|
|
|
|
intersection := sets[0].Copy()
|
|
|
|
|
|
|
|
for _, set := range sets[1:] {
|
|
|
|
for key := range intersection {
|
|
|
|
if !set.Contains(key) {
|
|
|
|
intersection.Remove(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return intersection
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToSet returns a new set containing the keys.
|
|
|
|
func ToSet[T SetKey](keys []T) Set[T] {
|
|
|
|
set := make(Set[T])
|
|
|
|
for _, key := range keys {
|
|
|
|
set.Add(key)
|
|
|
|
}
|
|
|
|
return set
|
|
|
|
}
|