Documentation
¶
Overview ¶
Package set provides a generic `Set` type for working with unordered collections of unique, comparable values.
A Set is implemented as a map[T]struct{}, offering O(1) average-case performance for insertion, deletion, and membership checks.
Basic usage:
s := set.New(1, 2, 3)
s.Add(4)
if s.Has(2) {
fmt.Println("contains 2")
}
Set operations:
a := set.New(1, 2, 3)
b := set.New(3, 4, 5)
union := a.Union(b) // {1, 2, 3, 4, 5}
inter := a.Intersect(b) // {3}
diff := a.Diff(b) // {1, 2}
Mutating operations:
a.UnionInto(b) // a now contains all elements from both sets
Iteration:
for v := range s.Iter() {
fmt.Println(v)
}
Predicates:
s.Any(func(v int) bool { return v%2 == 0 })
s.All(func(v int) bool { return v > 0 })
Construction from iterators:
s := set.Collect(iter.Seq[int]{...})
Reduction and accumulation:
sets := []set.Set[int]{
set.New(1, 2),
set.New(2, 3),
set.New(3, 4),
}
Reduce combines sets left-to-right using a binary operation.
r := set.Reduce(sets, func(a, b set.Set[int]) set.Set[int] {
return a.Union(b)
}) // {1,2,3,4}
ReduceWhile stops when the predicate fails.
r = set.ReduceWhile(
sets,
func(a, b set.Set[int]) set.Set[int] { return a.Union(b) },
func(s set.Set[int]) bool { return s.Len() < 4 },
)
ReduceTry allows the reducer to control early termination.
r = set.ReduceTry(
sets,
func(a, b set.Set[int]) (set.Set[int], bool) {
r := a.Intersect(b)
return r, !r.IsEmpty()
},
)
Accumulate produces intermediate results.
for v := range set.Accumulate(sets, func(a, b set.Set[int]) set.Set[int] {
return a.Union(b)
}) {
fmt.Println(v)
}
The iteration order of a Set is not defined and may vary between runs.
This package is designed to be small, predictable, and idiomatic, providing a practical set abstraction without additional dependencies or complex semantics.
Package set provides a generic set type and operations for working with sets of comparable elements. A set is implemented as a map[T]struct{} for memory efficiency and O(1) average-case operations.
Index ¶
- func Accumulate[T comparable, S SetLike[T]](sets []S, fn func(a, b S) S) iter.Seq[S]
- func AccumulateTry[T comparable, S SetLike[T]](sets []S, fn func(a, b S) (S, bool)) iter.Seq[S]
- func IntersectIter[T comparable](sets ...SetLike[T]) iter.Seq[T]
- func Reduce[T comparable, S SetLike[T]](sets []S, fn func(a, b S) S) S
- func ReduceTry[T comparable, S SetLike[T]](sets []S, fn func(a, b S) (S, bool)) S
- func ReduceUntil[T comparable, S SetLike[T]](sets []S, fn func(a, b S) S, pred func(s S) bool) S
- func ReduceWhile[T comparable, S SetLike[T]](sets []S, fn func(a, b S) S, pred func(s S) bool) S
- func UnionIter[T comparable](sets ...SetLike[T]) iter.Seq[T]
- type FrozenSet
- func (fs FrozenSet[T]) All(fn func(T) bool) bool
- func (fs FrozenSet[T]) Any(fn func(T) bool) bool
- func (fs FrozenSet[T]) AsSet() Set[T]
- func (fs FrozenSet[T]) AsSlice() []T
- func (fs FrozenSet[T]) Clone() FrozenSet[T]
- func (fs FrozenSet[T]) Diff(o FrozenSet[T]) FrozenSet[T]
- func (fs FrozenSet[T]) Equal(o FrozenSet[T]) bool
- func (fs FrozenSet[T]) Filter(fn func(T) bool) FrozenSet[T]
- func (fs FrozenSet[T]) Find(fn func(T) bool) (T, bool)
- func (fs FrozenSet[T]) First() (T, bool)
- func (fs FrozenSet[T]) Has(item T) bool
- func (fs FrozenSet[T]) HasAll(item ...T) bool
- func (fs FrozenSet[T]) HasAny(item ...T) bool
- func (fs FrozenSet[T]) Intersect(o FrozenSet[T]) FrozenSet[T]
- func (fs FrozenSet[T]) IntersectIter(o FrozenSet[T]) iter.Seq[T]
- func (fs FrozenSet[T]) IsEmpty() bool
- func (fs FrozenSet[T]) IsSubsetOf(o FrozenSet[T]) bool
- func (fs FrozenSet[T]) IsSupersetOf(o FrozenSet[T]) bool
- func (fs FrozenSet[T]) Iter() iter.Seq[T]
- func (fs FrozenSet[T]) Len() int
- func (fs FrozenSet[T]) String() string
- func (fs FrozenSet[T]) SymDiff(o FrozenSet[T]) FrozenSet[T]
- func (fs FrozenSet[T]) Union(o FrozenSet[T]) FrozenSet[T]
- func (fs FrozenSet[T]) UnionIter(o FrozenSet[T]) iter.Seq[T]
- type Set
- func (s Set[T]) Add(item T)
- func (s Set[T]) AddAll(items ...T)
- func (s Set[T]) AddCheck(item T) bool
- func (s Set[T]) All(fn func(T) bool) bool
- func (s Set[T]) Any(fn func(T) bool) bool
- func (s Set[T]) AsSlice() []T
- func (s Set[T]) Clear()
- func (s Set[T]) Clone() Set[T]
- func (s Set[T]) Delete(item T)
- func (s Set[T]) Diff(o Set[T]) Set[T]
- func (s Set[T]) Equal(o Set[T]) bool
- func (s Set[T]) Filter(fn func(T) bool) Set[T]
- func (s Set[T]) Find(fn func(T) bool) (T, bool)
- func (s Set[T]) First() (T, bool)
- func (s Set[T]) Freeze() FrozenSet[T]
- func (s Set[T]) Has(item T) bool
- func (s Set[T]) HasAll(item ...T) bool
- func (s Set[T]) HasAny(item ...T) bool
- func (s Set[T]) Intersect(o Set[T]) Set[T]
- func (s Set[T]) IntersectIter(o Set[T]) iter.Seq[T]
- func (s Set[T]) IsEmpty() bool
- func (s Set[T]) IsSubsetOf(o Set[T]) bool
- func (s Set[T]) IsSupersetOf(o Set[T]) bool
- func (s Set[T]) Iter() iter.Seq[T]
- func (s Set[T]) Len() int
- func (s Set[T]) Pop() (T, bool)
- func (s Set[T]) String() string
- func (s Set[T]) SymDiff(o Set[T]) Set[T]
- func (s Set[T]) Union(o Set[T]) Set[T]
- func (s Set[T]) UnionInto(o Set[T])
- func (s Set[T]) UnionIter(o Set[T]) iter.Seq[T]
- type SetLike
- type SyncSet
- func (ss *SyncSet[T]) Add(item T)
- func (ss *SyncSet[T]) All(fn func(T) bool) bool
- func (ss *SyncSet[T]) Any(fn func(T) bool) bool
- func (ss *SyncSet[T]) AsSet() Set[T]
- func (ss *SyncSet[T]) AsSlice() []T
- func (ss *SyncSet[T]) Clear()
- func (ss *SyncSet[T]) Clone() FrozenSet[T]
- func (ss *SyncSet[T]) Delete(item T)
- func (ss *SyncSet[T]) Diff(o *SyncSet[T]) FrozenSet[T]
- func (ss *SyncSet[T]) Equal(o *SyncSet[T]) bool
- func (ss *SyncSet[T]) Filter(fn func(T) bool) FrozenSet[T]
- func (ss *SyncSet[T]) Find(fn func(T) bool) (T, bool)
- func (ss *SyncSet[T]) First() (T, bool)
- func (ss *SyncSet[T]) Has(item T) bool
- func (ss *SyncSet[T]) HasAdd(item T) bool
- func (ss *SyncSet[T]) HasAll(item ...T) bool
- func (ss *SyncSet[T]) HasAny(item ...T) bool
- func (ss *SyncSet[T]) Intersect(o *SyncSet[T]) FrozenSet[T]
- func (ss *SyncSet[T]) IntersectIter(o *SyncSet[T]) iter.Seq[T]
- func (ss *SyncSet[T]) IsEmpty() bool
- func (ss *SyncSet[T]) IsSubsetOf(o *SyncSet[T]) bool
- func (ss *SyncSet[T]) IsSupersetOf(o *SyncSet[T]) bool
- func (ss *SyncSet[T]) Iter() iter.Seq[T]
- func (ss *SyncSet[T]) Len() int
- func (ss *SyncSet[T]) String() string
- func (ss *SyncSet[T]) SymDiff(o *SyncSet[T]) FrozenSet[T]
- func (ss *SyncSet[T]) Union(o *SyncSet[T]) FrozenSet[T]
- func (ss *SyncSet[T]) UnionInto(o *SyncSet[T])
- func (ss *SyncSet[T]) UnionIter(o *SyncSet[T]) iter.Seq[T]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Accumulate ¶ added in v0.0.2
func Accumulate[T comparable, S SetLike[T]]( sets []S, fn func(a, b S) S, ) iter.Seq[S]
Accumulate returns a iterator of intermediate accumulation results. Each step applies fn to the current accumulator and the next set. fn must return a new set and must not mutate its arguments. Iteration stops if the consumer stops early. For zero sets, a single zero value is yielded. For one set, no values are yielded.
func AccumulateTry ¶ added in v0.0.2
func AccumulateTry[T comparable, S SetLike[T]]( sets []S, fn func(a, b S) (S, bool), ) iter.Seq[S]
AccumulateTry returns an iterator of intermediate accumulation results. Each step applies fn to the current accumulator and the next set. If fn returns false, the result of that step is yielded and iteration stops. Iteration also stops if the consumer stops early. For zero sets, a single zero value is yielded. For one set, no values are yielded.
func IntersectIter ¶ added in v0.0.3
func IntersectIter[T comparable](sets ...SetLike[T]) iter.Seq[T]
IntersectIter returns a lazy sequence of elements present in all provided sets. It iterates the smallest input set and yields elements that exist in every other set. Iteration stops early if the consumer returns false.
No intermediate sets are allocated. Membership checks are performed on demand. For zero sets, the sequence yields nothing.
func Reduce ¶ added in v0.0.2
func Reduce[T comparable, S SetLike[T]]( sets []S, fn func(a, b S) S, ) S
Reduce reduces sets by applying fn pairwise from left to right. The final accumulated value is returned. If no sets are provided, a zero value is returned.
func ReduceTry ¶ added in v0.0.2
func ReduceTry[T comparable, S SetLike[T]]( sets []S, fn func(a, b S) (S, bool), ) S
ReduceTry reduces sets using fn, stopping early if fn returns false. The result of the step that signalled stop is returned as the final value. If no sets are provided, a zero value is returned.
func ReduceUntil ¶ added in v0.0.3
func ReduceUntil[T comparable, S SetLike[T]]( sets []S, fn func(a, b S) S, pred func(s S) bool, ) S
ReduceUntil reduces sets using fn, stopping when pred returns true for a result. The first result that passes pred is returned. If no sets are provided, a zero value is returned.
func ReduceWhile ¶ added in v0.0.2
func ReduceWhile[T comparable, S SetLike[T]]( sets []S, fn func(a, b S) S, pred func(s S) bool, ) S
ReduceWhile reduces sets using fn, stopping when pred returns false for a result. The last result that passes pred is returned. If no sets are provided, a zero value is returned.
Types ¶
type FrozenSet ¶ added in v0.0.3
type FrozenSet[T comparable] struct { // contains filtered or unexported fields }
func (FrozenSet[T]) IntersectIter ¶ added in v0.0.3
func (FrozenSet[T]) IsSubsetOf ¶ added in v0.0.3
func (FrozenSet[T]) IsSupersetOf ¶ added in v0.0.3
type Set ¶
type Set[T comparable] map[T]struct{}
set represents a mathematical set of comparable elements. The zero value is ready to use but prefer using New() for initialization.
func Clone ¶ added in v0.0.2
func Clone[T comparable](s Set[T]) Set[T]
Clone returns a shallow copy of the set.
func Collect ¶ added in v0.0.2
func Collect[T comparable](it iter.Seq[T], size ...int) Set[T]
Collect returns a new Set containing all elements produced by the iterator. If a positive capacity hint is provided, it is used to preallocate the set.
func Intersect ¶ added in v0.0.2
func Intersect[T comparable](sets ...Set[T]) Set[T]
Intersect returns a new Set containing elements present in all input sets. Evaluation stops early if the result becomes empty.
func Make ¶ added in v0.0.3
func Make[T comparable](size ...int) Set[T]
Make returns a new Set with a . If no items are provided, it returns an empty set.
func New ¶
func New[T comparable](items ...T) Set[T]
New returns a new Set containing the provided items. If no items are provided, it returns an empty set.
func Union ¶ added in v0.0.2
func Union[T comparable](sets ...Set[T]) Set[T]
Union returns a new Set containing all elements from the provided sets.
func (Set[T]) AddAll ¶ added in v0.0.2
func (s Set[T]) AddAll(items ...T)
AddAll inserts one or more items into the set.
func (Set[T]) AddCheck ¶ added in v0.0.3
AddCheck inserts an item into the set. Returns true if item was already present.
func (Set[T]) Intersect ¶
Intersect returns a new set containing elements present in both s and o. It iterates the smaller set and allocates only for actual matches.
func (Set[T]) IntersectIter ¶ added in v0.0.3
IntersectIter returns a lazy sequence of elements present in both s and o. The smaller set is selected before iteration to avoid branching in the hot path. No intermediate set is allocated and iteration may stop early.
func (Set[T]) IsSubsetOf ¶
IsSubsetOf reports whether s ⊆ o.
func (Set[T]) IsSupersetOf ¶
IsSupersetOf reports whether s ⊇ o.
func (Set[T]) Union ¶
Union returns a new set containing all elements from s and o. It clones the smaller set and inserts elements from the larger, minimizing total copy and insert work.
func (Set[T]) UnionInto ¶
UnionInto inserts all elements from o into s in place. It performs a single pass over o with no allocations.
type SyncSet ¶ added in v0.0.3
type SyncSet[T comparable] struct { // contains filtered or unexported fields }
SyncSet is a concurrency-safe wrapper around Set. All operations are guarded by a RWMutex. Iteration holds a read lock for the duration of the sequence.
func FromSet ¶ added in v0.0.3
func FromSet[T comparable](s Set[T]) *SyncSet[T]
FromSet creates a SyncSet initialized with a clone of s.
func NewSync ¶ added in v0.0.3
func NewSync[T comparable]() *SyncSet[T]
NewSync returns an empty SyncSet.
func (*SyncSet[T]) Add ¶ added in v0.0.3
func (ss *SyncSet[T]) Add(item T)
Add inserts item into the set.
func (*SyncSet[T]) AsSet ¶ added in v0.0.3
AsSet creates a new mutable thread-unsafe Set. The new Set is initialized with a clone of the contents of ss.
func (*SyncSet[T]) Clear ¶ added in v0.0.3
func (ss *SyncSet[T]) Clear()
Clear removes all elements from the set.
func (*SyncSet[T]) Delete ¶ added in v0.0.3
func (ss *SyncSet[T]) Delete(item T)
Delete removes item from the set.
func (*SyncSet[T]) HasAdd ¶ added in v0.0.3
HasAdd inserts item and reports whether it was already present.
func (*SyncSet[T]) IntersectIter ¶ added in v0.0.3
IntersectIter returns an iterator of elements present in both sets. Locks are held only during snapshot creation.
func (*SyncSet[T]) IsSubsetOf ¶ added in v0.0.3
func (*SyncSet[T]) IsSupersetOf ¶ added in v0.0.3
func (*SyncSet[T]) Iter ¶ added in v0.0.3
Iter returns a sequence of elements in the set. A read lock is held for the duration of iteration.