Documentation ¶
Overview ¶
Sets and Set type and operations of any comparable type (go 1.18+ generics) Intersection, Union, Set.Subset, difference aka Set.Minus, XOR, JSON serialization and deserialization and more.
Index ¶
- func RemoveCommon[T comparable](a, b Set[T])
- func Sort[Q constraints.Ordered](s Set[Q]) []Q
- func Tuplets[Q constraints.Ordered](s Set[Q], n int) [][]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
Sort returns a sorted slice of the elements in the set. Only applicable for when the type is sortable.
func Tuplets ¶ added in v1.1.0
func Tuplets[Q constraints.Ordered](s Set[Q], n int) [][]Q
Tuplets generates all the combinations of N of elements of the set. for n = 2, it would return all pairs of elements. for n = 3, all triplets, etc.
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{}
Set defines a low memory footprint set of any comparable type. Based on `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]
Intersection returns a new set that has the elements common to all the input sets.
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]) Add ¶
func (s Set[T]) Add(item ...T)
Add items to the set. Add and thus its callers will panic() if NaN is passed in.
func (Set[T]) Clear ¶ added in v1.0.0
func (s Set[T]) Clear()
Clear removes all elements from the set.
func (Set[T]) Elements ¶ added in v0.4.0
func (s Set[T]) Elements() []T
Elements returns a slice of the elements in the set.
func (Set[T]) Len ¶ added in v1.0.0
Len returns the number of elements in the set (same as len(s) but as a method).
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.