Documentation
¶
Overview ¶
Package set implements a container that holds a set of elements.
Index ¶
- type Set
- func (s *Set[T]) Add(e ...T)
- func (s *Set[T]) AddSet(o *Set[T])
- func (s *Set[T]) All() iter.Seq2[int, T]
- func (s *Set[T]) Clear()
- func (s *Set[T]) Clone() Set[T]
- func (s *Set[T]) Contains(v T) bool
- func (s *Set[T]) ContainsAll(o *Set[T]) bool
- func (s *Set[T]) ContainsAny(o *Set[T]) bool
- func (s *Set[T]) Equal(o *Set[T]) bool
- func (s *Set[T]) Filter(keep func(T) bool)
- func (s *Set[T]) ForEach(fn func(T))
- func (s *Set[T]) Len() int
- func (s *Set[T]) Remove(e ...T)
- func (s *Set[T]) RemoveSet(o *Set[T])
- func (s *Set[T]) String() string
- func (s *Set[T]) Values() iter.Seq[T]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Set ¶
type Set[T comparable] struct { // contains filtered or unexported fields }
Set implements a container which holds a set of elements.
Reading from a set is safe for concurrent use. However, modification of a set is not.
func Difference ¶
func Difference[T comparable](a, b *Set[T]) *Set[T]
Difference returns a new set containing all elements in a that are not present in b.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/slewiskelly/x/container/set"
)
func main() {
a, b := set.New(1, 2, 3, 4, 5, 6), set.New(4, 5, 6, 7, 8, 9)
difference := set.Difference(a, b)
fmt.Println(slices.Sorted(difference.Values()))
}
Output: [1 2 3]
func Intersection ¶
func Intersection[T comparable](a, b *Set[T]) *Set[T]
Intersection returns a new set containing the intersection of a and b.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/slewiskelly/x/container/set"
)
func main() {
a, b := set.New(1, 2, 3, 4, 5, 6), set.New(4, 5, 6, 7, 8, 9)
intersection := set.Intersection(a, b)
fmt.Println(slices.Sorted(intersection.Values()))
}
Output: [4 5 6]
func New ¶
func New[T comparable](e ...T) *Set[T]
New returns a new set containing the given elements.
func Union ¶
func Union[T comparable](a, b *Set[T]) *Set[T]
Union returns a new set containing the union of a and b.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/slewiskelly/x/container/set"
)
func main() {
a, b := set.New(1, 2, 3, 4, 5, 6), set.New(4, 5, 6, 7, 8, 9)
union := set.Union(a, b)
fmt.Println(slices.Sorted(union.Values()))
}
Output: [1 2 3 4 5 6 7 8 9]
func (*Set[T]) All ¶
All returns an iterator over (index, value) pairs of the set.
Elements are yielded in an indeterminate order.
func (*Set[T]) Contains ¶
Contains returns true if the given value is contained in the set; otherwise false.
func (*Set[T]) ContainsAll ¶
ContainsAll returns true if the set contains all of the elements in given set; otherwise false.
func (*Set[T]) ContainsAny ¶
ContainsAny returns true if the set contains any of the elements in the given set; otherwise false.
func (*Set[T]) Equal ¶
Equal returns true if the set is equal to the given set.
Sets are considered equal if they are of the same length and all elements are equal.
func (*Set[T]) Filter ¶
Filter removes any elements from the set in which the given function returns false.
func (*Set[T]) ForEach ¶
func (s *Set[T]) ForEach(fn func(T))
ForEach executes the given function for each element of the set.
func (*Set[T]) Remove ¶
func (s *Set[T]) Remove(e ...T)
Remove removes the given elements from the set.