Documentation ¶
Overview ¶
Sets and Set type and operations in go 1.18+ generics. (pending built in support in golang core)
Index ¶
- func RemoveCommon[T comparable](a, b Set[T])
- func Sort[Q constraints.Ordered](s Set[Q]) []Q
- func XOR[T comparable](a, b Set[T])
- type Set
- func (s Set[T]) Add(item ...T)
- func (s Set[T]) Clear()
- func (s Set[T]) Clone() Set[T]
- func (s Set[T]) Elements() []T
- func (s Set[T]) Equals(other Set[T]) bool
- func (s Set[T]) Has(item T) bool
- func (s Set[T]) Len() int
- func (s Set[T]) MarshalJSON() ([]byte, error)
- func (s Set[T]) Minus(other Set[T]) Set[T]
- func (s Set[T]) Plus(others ...Set[T]) Set[T]
- func (s Set[T]) Remove(item ...T)
- func (s Set[T]) String() string
- func (s Set[T]) Subset(bigger Set[T]) bool
- func (s *Set[T]) UnmarshalJSON(data []byte) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RemoveCommon ¶
func RemoveCommon[T comparable](a, b Set[T])
RemoveCommon removes elements from both sets that are in both, leaving only the delta. Useful when a is an old value and b is new and you want to apply some operation on all removed and added elements.
func Sort ¶ added in v0.3.0
func Sort[Q constraints.Ordered](s Set[Q]) []Q
func XOR ¶ added in v0.4.0
func XOR[T comparable](a, b Set[T])
XOR is an alias for RemoveCommon, efficiently removes from each set the common elements.
Types ¶
type Set ¶
type Set[T comparable] map[T]struct{}
func FromSlice ¶
func FromSlice[T comparable](items []T) Set[T]
FromSlice constructs a Set from a slice. [Elements] is the inverse function, getting back a slice from the Set. This is a short cut/alias for New[T](items...).
func Intersection ¶ added in v0.4.0
func Intersection[T comparable](sets ...Set[T]) Set[T]
func New ¶
func New[T comparable](item ...T) Set[T]
New returns a new set containing the given elements.
func Union ¶ added in v0.4.0
func Union[T comparable](sets ...Set[T]) Set[T]
Union returns a new set that has all the elements of all the sets. Note that Union(s1) == s1.Clone() and Union[T]() == New[T]().
func (Set[T]) MarshalJSON ¶ added in v1.0.0
MarshalJSON implements the json.Marshaler interface and only gets the elements as an array.
func (Set[T]) Minus ¶ added in v1.0.0
Minus mutates the receiver to remove all the elements of the passed in set. If you want a copy use s.Clone().Minus(other). Returns the receiver for chaining.
func (Set[T]) Plus ¶ added in v1.0.0
Plus is similar to Union but mutates the receiver. Added for symmetry with Minus. Returns the receiver for chaining.
func (Set[T]) String ¶
String() returns a coma separated list of the elements in the set. This is mostly for troubleshooting/debug output unless the [T] serializes to a string that doesn't contain commas.
func (Set[T]) Subset ¶ added in v1.0.0
Subset returns true if all elements of s are in the passed in set.
func (*Set[T]) UnmarshalJSON ¶ added in v1.0.0
UnmarshalJSON implements the json.Unmarshaler interface turns the slice back to a Set.