Documentation
¶
Overview ¶
Package set provides an implementation of a set which accepts any values, even if not comparable.
Index ¶
- func Compare[T any](s1, s2 *Set[T]) bool
- type Set
- func (set *Set[T]) Add(elems ...T)
- func (set *Set[T]) All(elems ...T) bool
- func (set *Set[T]) Any(elems ...T) bool
- func (set *Set[T]) Clear()
- func (set *Set[T]) Contains(elem T) bool
- func (set *Set[T]) Copy() *Set[T]
- func (set *Set[T]) Difference(other *Set[T]) *Set[T]
- func (set *Set[T]) Each(fn func(T))
- func (set *Set[T]) Equals(other *Set[T]) bool
- func (set *Set[T]) Intersection(other *Set[T]) *Set[T]
- func (set *Set[T]) IsEmpty() bool
- func (set *Set[T]) Len() int
- func (set *Set[T]) List() []T
- func (set *Set[T]) MarshalJSON() ([]byte, error)
- func (set *Set[T]) Pop() T
- func (set *Set[T]) Remove(elems ...T)
- func (set *Set[T]) Size() int
- func (set *Set[T]) Union(other *Set[T]) *Set[T]
- func (set *Set[T]) UnmarshalJSON(data []byte) error
- func (set *Set[T]) Values() iter.Seq[T]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Set ¶
type Set[T any] struct { // contains filtered or unexported fields }
Set holds a collection of unique values.
It makes use of maphash.Hash to allow for adding values of any type, even ones that are uncomparable. The seed is set as a package global to ensure that all sets will produce hashes compatible for comparison.
All methods lazily initialize the underlying storage, so the zero value of Set can be used.
Example ¶
package main
import (
"fmt"
"go.chrisrx.dev/x/set"
"go.chrisrx.dev/x/slices"
)
func main() {
var s set.Set[int]
for range 10 {
s.Add(1)
}
s.Add(2)
fmt.Println(slices.Sort(s.List()))
}
Output: [1 2]
Example (New) ¶
package main
import (
"fmt"
"go.chrisrx.dev/x/set"
"go.chrisrx.dev/x/slices"
)
func main() {
s := set.New(1, 2, 2, 3, 3, 3, 4, 5, 5)
fmt.Println(slices.Sort(s.List()))
}
Output: [1 2 3 4 5]
Example (Uncomparable) ¶
package main
import (
"fmt"
"go.chrisrx.dev/x/set"
"go.chrisrx.dev/x/slices"
)
func main() {
var s set.Set[[]byte]
s.Add([]byte("1"))
s.Add([]byte("1"))
s.Add([]byte("2"))
s.Add([]byte("2"))
s.Add([]byte("3"))
fmt.Println(slices.Sort(slices.Map(s.List(), func(elem []byte) string {
return string(elem)
})))
}
Output: [1 2 3]
func New ¶
New constructs a new set. If any elements are provided the set is initialized with those values.
func (*Set[T]) All ¶
All checks that the set contains all of the elements provided. If any are not part of the set, this returns false.
func (*Set[T]) Copy ¶
Copy constructs a new set, copying the internal state of this set into the new returned set.
func (*Set[T]) Difference ¶
Difference returns a new set containing all of the elements in this set that are not present in the other set.
func (*Set[T]) Each ¶
func (set *Set[T]) Each(fn func(T))
Each applies the provided function to every element of the set.
func (*Set[T]) Intersection ¶
Intersection returns a new set made up of all of the elements that are contained in both sets.
func (*Set[T]) List ¶
func (set *Set[T]) List() []T
List returns all the values of the set as a slice.
func (*Set[T]) MarshalJSON ¶
func (*Set[T]) Pop ¶
func (set *Set[T]) Pop() T
Pop removes and returns one value from the set. The element returned is indeterministically selected.
func (*Set[T]) Remove ¶
func (set *Set[T]) Remove(elems ...T)
Removes an element from the set, if present.
func (*Set[T]) Size ¶
Size returns how many elements are current in the set. This is an alias to Set.Len