2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2022-11-28 18:34:35 +00:00
|
|
|
|
|
|
|
// Package set contains set types.
|
|
|
|
package set
|
|
|
|
|
2023-11-01 00:15:40 +00:00
|
|
|
import (
|
2023-11-20 16:00:31 +00:00
|
|
|
"encoding/json"
|
2023-11-01 00:15:40 +00:00
|
|
|
"maps"
|
|
|
|
)
|
|
|
|
|
2023-05-11 21:59:36 +00:00
|
|
|
// Set is a set of T.
|
|
|
|
type Set[T comparable] map[T]struct{}
|
|
|
|
|
2023-09-29 21:31:02 +00:00
|
|
|
// SetOf returns a new set constructed from the elements in slice.
|
|
|
|
func SetOf[T comparable](slice []T) Set[T] {
|
|
|
|
s := make(Set[T])
|
|
|
|
s.AddSlice(slice)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2023-11-01 00:15:40 +00:00
|
|
|
// Clone returns a new set cloned from the elements in s.
|
2023-11-01 17:20:38 +00:00
|
|
|
func (s Set[T]) Clone() Set[T] {
|
2023-11-01 00:15:40 +00:00
|
|
|
return maps.Clone(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add adds e to s.
|
2023-05-11 21:59:36 +00:00
|
|
|
func (s Set[T]) Add(e T) { s[e] = struct{}{} }
|
|
|
|
|
2023-11-01 00:15:40 +00:00
|
|
|
// AddSlice adds each element of es to s.
|
2023-09-29 21:31:02 +00:00
|
|
|
func (s Set[T]) AddSlice(es []T) {
|
|
|
|
for _, e := range es {
|
|
|
|
s.Add(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-01 00:15:40 +00:00
|
|
|
// AddSet adds each element of es to s.
|
|
|
|
func (s Set[T]) AddSet(es Set[T]) {
|
|
|
|
for e := range es {
|
|
|
|
s.Add(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-29 21:31:02 +00:00
|
|
|
// Slice returns the elements of the set as a slice. The elements will not be
|
|
|
|
// in any particular order.
|
|
|
|
func (s Set[T]) Slice() []T {
|
|
|
|
es := make([]T, 0, s.Len())
|
|
|
|
for k := range s {
|
|
|
|
es = append(es, k)
|
|
|
|
}
|
|
|
|
return es
|
|
|
|
}
|
|
|
|
|
2023-09-09 16:55:57 +00:00
|
|
|
// Delete removes e from the set.
|
|
|
|
func (s Set[T]) Delete(e T) { delete(s, e) }
|
|
|
|
|
2023-05-11 21:59:36 +00:00
|
|
|
// Contains reports whether s contains e.
|
|
|
|
func (s Set[T]) Contains(e T) bool {
|
|
|
|
_, ok := s[e]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len reports the number of items in s.
|
|
|
|
func (s Set[T]) Len() int { return len(s) }
|
2023-11-01 00:15:40 +00:00
|
|
|
|
|
|
|
// Equal reports whether s is equal to other.
|
|
|
|
func (s Set[T]) Equal(other Set[T]) bool {
|
|
|
|
return maps.Equal(s, other)
|
|
|
|
}
|
2023-11-20 16:00:31 +00:00
|
|
|
|
|
|
|
func (s Set[T]) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(s.Slice())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Set[T]) UnmarshalJSON(buf []byte) error {
|
|
|
|
var ss []T
|
|
|
|
if err := json.Unmarshal(buf, &ss); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*s = SetOf(ss)
|
|
|
|
return nil
|
|
|
|
}
|