Documentation
¶
Overview ¶
Package set provides a generic set backed by a map.
Index ¶
- func Sorted[T cmp.Ordered](s Set[T]) []T
- func SortedNatural[T ~string](s Set[T]) []T
- type Set
- func (s Set[T]) Add(items ...T)
- func (s Set[T]) All() iter.Seq[T]
- func (s Set[T]) Clone() Set[T]
- func (s Set[T]) Contains(item T) bool
- func (s Set[T]) Delete(items ...T)
- func (s Set[T]) Difference(others ...Set[T]) Set[T]
- func (s Set[T]) Equal(other Set[T]) bool
- func (s Set[T]) Intersect(others ...Set[T]) Set[T]
- func (s Set[T]) Len() int
- func (s Set[T]) Slice() []T
- func (s Set[T]) SubsetOf(other Set[T]) bool
- func (s Set[T]) Union(others ...Set[T]) Set[T]
- type SortedSet
- func (s *SortedSet[T]) Add(items ...T)
- func (s SortedSet[T]) All() iter.Seq[T]
- func (s SortedSet[T]) Clone() SortedSet[T]
- func (s SortedSet[T]) Contains(item T) bool
- func (s *SortedSet[T]) Delete(items ...T)
- func (s SortedSet[T]) Difference(others ...SortedSet[T]) SortedSet[T]
- func (s SortedSet[T]) Equal(other SortedSet[T]) bool
- func (s SortedSet[T]) Intersect(others ...SortedSet[T]) SortedSet[T]
- func (s SortedSet[T]) Len() int
- func (s SortedSet[T]) Slice() []T
- func (s SortedSet[T]) SubsetOf(other SortedSet[T]) bool
- func (s SortedSet[T]) Union(others ...SortedSet[T]) SortedSet[T]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Sorted ¶
Sorted returns the items of `s` as a slice in ascending order.
Sorted is a function rather than a Set method because it requires `T` to be ordered, not just comparable.
Example ¶
Sorted turns a Set's indeterminate iteration order into a stable one.
package main
import (
"fmt"
"github.com/gechr/x/set"
)
func main() {
s := set.New("banana", "apple", "cherry")
fmt.Println(set.Sorted(s))
}
Output: [apple banana cherry]
func SortedNatural ¶
SortedNatural returns the items of `s` as a slice in natural order, so embedded numbers compare by value ("item2" before "item10") rather than lexically. See github.com/gechr/x/strings.CompareNatural.
SortedNatural is a function rather than a Set method because it requires `T` to be string-like, not just comparable.
Example ¶
SortedNatural orders embedded numbers by value, so "item2" sorts before "item10".
package main
import (
"fmt"
"github.com/gechr/x/set"
)
func main() {
s := set.New("item10", "item2", "item1")
fmt.Println(set.Sorted(s))
fmt.Println(set.SortedNatural(s))
}
Output: [item1 item10 item2] [item1 item2 item10]
Types ¶
type Set ¶
type Set[T comparable] map[T]struct{}
Set is a set of comparable items backed by a map. Pointer types and structs containing pointer fields are compared using shallow equality. The zero value is nil: read operations work, but Set.Add panics - use New or Collect to create a usable Set.
func Collect ¶
func Collect[T comparable](seq iter.Seq[T]) Set[T]
Collect returns a Set containing the values of `seq`.
Example ¶
Collect builds a Set from any iter.Seq, such as slices.Values.
package main
import (
"fmt"
"slices"
"github.com/gechr/x/set"
)
func main() {
s := set.Collect(slices.Values([]int{3, 1, 3, 2}))
fmt.Println(set.Sorted(s))
}
Output: [1 2 3]
func New ¶
func New[T comparable](items ...T) Set[T]
New returns a Set containing `items`.
Example ¶
package main
import (
"fmt"
"github.com/gechr/x/set"
)
func main() {
s := set.New("a", "b", "a")
fmt.Println(s.Len())
fmt.Println(s.Contains("b"))
fmt.Println(s.Contains("c"))
}
Output: 2 true false
func (Set[T]) Difference ¶
Difference returns a new Set containing the items of `s` not present in any of `others`.
Example ¶
package main
import (
"fmt"
"github.com/gechr/x/set"
)
func main() {
a := set.New(1, 2, 3)
b := set.New(2, 4)
fmt.Println(set.Sorted(a.Difference(b)))
}
Output: [1 3]
func (Set[T]) Intersect ¶
Intersect returns a new Set containing the items of `s` present in every one of `others`.
Example ¶
package main
import (
"fmt"
"github.com/gechr/x/set"
)
func main() {
a := set.New(1, 2, 3)
b := set.New(2, 3, 4)
fmt.Println(set.Sorted(a.Intersect(b)))
}
Output: [2 3]
func (Set[T]) Slice ¶
func (s Set[T]) Slice() []T
Slice returns the items of `s` as a slice, in indeterminate order.
func (Set[T]) SubsetOf ¶
SubsetOf returns whether every item in `s` is present in `other`.
Example ¶
package main
import (
"fmt"
"github.com/gechr/x/set"
)
func main() {
a := set.New("x", "y")
b := set.New("x", "y", "z")
fmt.Println(a.SubsetOf(b))
fmt.Println(b.SubsetOf(a))
}
Output: true false
type SortedSet ¶
SortedSet is a set of ordered items, kept in ascending sorted order at all times: SortedSet.Add inserts in sorted position, and combining sets (SortedSet.Union/SortedSet.Intersect/SortedSet.Difference) always yields a sorted result. Unlike Set, SortedSet.Slice and SortedSet.All iterate in deterministic ascending order rather than indeterminate map order.
The zero value is an empty, usable SortedSet.
func CollectSorted ¶
CollectSorted returns a SortedSet containing the values of `seq`.
Example ¶
CollectSorted builds a SortedSet from any iter.Seq, such as slices.Values.
package main
import (
"fmt"
"slices"
"github.com/gechr/x/set"
)
func main() {
s := set.CollectSorted(slices.Values([]int{3, 1, 3, 2}))
fmt.Println(s.Slice())
}
Output: [1 2 3]
func NewSorted ¶
NewSorted returns a SortedSet containing `items`, sorted ascending with duplicates removed.
Example ¶
SortedSet keeps its items in ascending order at all times, so iteration is deterministic.
package main
import (
"fmt"
"github.com/gechr/x/set"
)
func main() {
s := set.NewSorted(3, 1, 2, 1)
s.Add(0)
s.Delete(2)
fmt.Println(s.Slice())
}
Output: [0 1 3]
func (*SortedSet[T]) Add ¶
func (s *SortedSet[T]) Add(items ...T)
Add adds `items` to `s`, inserting each in sorted position and ignoring duplicates.
func (SortedSet[T]) All ¶
All returns an iterator over the items of `s`, in ascending order.
Example ¶
package main
import (
"fmt"
"github.com/gechr/x/set"
)
func main() {
s := set.NewSorted("banana", "apple", "cherry")
for item := range s.All() {
fmt.Println(item)
}
}
Output: apple banana cherry
func (*SortedSet[T]) Delete ¶
func (s *SortedSet[T]) Delete(items ...T)
Delete removes `items` from `s`.
func (SortedSet[T]) Difference ¶
Difference returns a new SortedSet containing the items of `s` not present in any of `others`.
func (SortedSet[T]) Intersect ¶
Intersect returns a new SortedSet containing the items of `s` present in every one of `others`.
func (SortedSet[T]) Slice ¶
func (s SortedSet[T]) Slice() []T
Slice returns the items of `s` as a slice, in ascending order.