Documentation
¶
Index ¶
- type HashSet
- func (s *HashSet[T]) Add(elements ...T) bool
- func (s *HashSet[T]) All(predicate func(element T) bool) bool
- func (s *HashSet[T]) Any(predicate func(element T) bool) bool
- func (s *HashSet[T]) Clear()
- func (s *HashSet[T]) Clone() Set[T]
- func (s *HashSet[T]) Contains(element T) bool
- func (s *HashSet[T]) ContainsAll(elements ...T) bool
- func (s *HashSet[T]) ContainsAny(elements ...T) bool
- func (s *HashSet[T]) Difference(other Set[T]) Set[T]
- func (s *HashSet[T]) Each(fn func(element T) bool)
- func (s *HashSet[T]) Equal(other Set[T]) bool
- func (s *HashSet[T]) Filter(predicate func(element T) bool) Set[T]
- func (s *HashSet[T]) GobDecode(data []byte) error
- func (s *HashSet[T]) GobEncode() ([]byte, error)
- func (s *HashSet[T]) Intersection(other Set[T]) Set[T]
- func (s *HashSet[T]) IsEmpty() bool
- func (s *HashSet[T]) IsSubset(other Set[T]) bool
- func (s *HashSet[T]) IsSuperset(other Set[T]) bool
- func (s *HashSet[T]) Map(transform func(element T) T) Set[T]
- func (s *HashSet[T]) MarshalJSON() ([]byte, error)
- func (s *HashSet[T]) Remove(elements ...T) bool
- func (s *HashSet[T]) RemoveIf(predicate func(element T) bool) int
- func (s *HashSet[T]) Seq() iter.Seq[T]
- func (s *HashSet[T]) Size() int
- func (s *HashSet[T]) SymmetricDifference(other Set[T]) Set[T]
- func (s *HashSet[T]) Union(other Set[T]) Set[T]
- func (s *HashSet[T]) UnmarshalJSON(data []byte) error
- func (s *HashSet[T]) Values() []T
- type Iterator
- type OrderedSet
- type Set
- type SyncHashSet
- func (s *SyncHashSet[T]) Add(elements ...T) bool
- func (s *SyncHashSet[T]) All(predicate func(element T) bool) bool
- func (s *SyncHashSet[T]) Any(predicate func(element T) bool) bool
- func (s *SyncHashSet[T]) Clear()
- func (s *SyncHashSet[T]) Clone() Set[T]
- func (s *SyncHashSet[T]) Contains(element T) bool
- func (s *SyncHashSet[T]) ContainsAll(elements ...T) bool
- func (s *SyncHashSet[T]) ContainsAny(elements ...T) bool
- func (s *SyncHashSet[T]) Difference(other Set[T]) Set[T]
- func (s *SyncHashSet[T]) Each(fn func(element T) bool)
- func (s *SyncHashSet[T]) Equal(other Set[T]) bool
- func (s *SyncHashSet[T]) Filter(predicate func(element T) bool) Set[T]
- func (s *SyncHashSet[T]) GobDecode(data []byte) error
- func (s *SyncHashSet[T]) GobEncode() ([]byte, error)
- func (s *SyncHashSet[T]) Intersection(other Set[T]) Set[T]
- func (s *SyncHashSet[T]) IsEmpty() bool
- func (s *SyncHashSet[T]) IsSubset(other Set[T]) bool
- func (s *SyncHashSet[T]) IsSuperset(other Set[T]) bool
- func (s *SyncHashSet[T]) Map(transform func(element T) T) Set[T]
- func (s *SyncHashSet[T]) MarshalJSON() ([]byte, error)
- func (s *SyncHashSet[T]) Remove(elements ...T) bool
- func (s *SyncHashSet[T]) RemoveIf(predicate func(element T) bool) int
- func (s *SyncHashSet[T]) Seq() iter.Seq[T]
- func (s *SyncHashSet[T]) Size() int
- func (s *SyncHashSet[T]) SymmetricDifference(other Set[T]) Set[T]
- func (s *SyncHashSet[T]) Union(other Set[T]) Set[T]
- func (s *SyncHashSet[T]) UnmarshalJSON(data []byte) error
- func (s *SyncHashSet[T]) Values() []T
- type TreeSet
- func NewTreeSet[T cmp.Ordered]() *TreeSet[T]
- func NewTreeSetFromSlice[T cmp.Ordered](elements []T) *TreeSet[T]
- func NewTreeSetFromSliceWithComparator[T comparable](elements []T, comparator func(a, b T) int) *TreeSet[T]
- func NewTreeSetWithComparator[T comparable](comparator func(a, b T) int) *TreeSet[T]
- func (s *TreeSet[T]) Add(elements ...T) bool
- func (s *TreeSet[T]) All(predicate func(element T) bool) bool
- func (s *TreeSet[T]) Any(predicate func(element T) bool) bool
- func (s *TreeSet[T]) Clear()
- func (s *TreeSet[T]) Clone() Set[T]
- func (s *TreeSet[T]) Contains(element T) bool
- func (s *TreeSet[T]) ContainsAll(elements ...T) bool
- func (s *TreeSet[T]) ContainsAny(elements ...T) bool
- func (s *TreeSet[T]) Difference(other Set[T]) Set[T]
- func (s *TreeSet[T]) Each(fn func(element T) bool)
- func (s *TreeSet[T]) EachIndexed(fn func(index int, element T) bool)
- func (s *TreeSet[T]) Equal(other Set[T]) bool
- func (s *TreeSet[T]) Filter(predicate func(element T) bool) Set[T]
- func (s *TreeSet[T]) GobDecode(data []byte) error
- func (s *TreeSet[T]) GobEncode() ([]byte, error)
- func (s *TreeSet[T]) Intersection(other Set[T]) Set[T]
- func (s *TreeSet[T]) IsEmpty() bool
- func (s *TreeSet[T]) IsSubset(other Set[T]) bool
- func (s *TreeSet[T]) IsSuperset(other Set[T]) bool
- func (s *TreeSet[T]) Iterator() Iterator[T]
- func (s *TreeSet[T]) Map(transform func(element T) T) Set[T]
- func (s *TreeSet[T]) MarshalJSON() ([]byte, error)
- func (s *TreeSet[T]) Max() (T, bool)
- func (s *TreeSet[T]) Min() (T, bool)
- func (s *TreeSet[T]) Range(from, to T) []T
- func (s *TreeSet[T]) Remove(elements ...T) bool
- func (s *TreeSet[T]) RemoveIf(predicate func(element T) bool) int
- func (s *TreeSet[T]) ReverseSeq() iter.Seq[T]
- func (s *TreeSet[T]) ReverseSeqWithIndex() iter.Seq2[int, T]
- func (s *TreeSet[T]) Seq() iter.Seq[T]
- func (s *TreeSet[T]) SeqWithIndex() iter.Seq2[int, T]
- func (s *TreeSet[T]) Size() int
- func (s *TreeSet[T]) SymmetricDifference(other Set[T]) Set[T]
- func (s *TreeSet[T]) Union(other Set[T]) Set[T]
- func (s *TreeSet[T]) UnmarshalJSON(data []byte) error
- func (s *TreeSet[T]) Values() []T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type HashSet ¶
type HashSet[T comparable] struct { // contains filtered or unexported fields }
HashSet is an unordered set implementation using a native Go map. It provides O(1) average time complexity for add, remove, and contains operations. HashSet is not thread-safe.
func NewHashSet ¶
func NewHashSet[T comparable]() *HashSet[T]
NewHashSet creates a new empty HashSet.
func NewHashSetFromSlice ¶
func NewHashSetFromSlice[T comparable](elements []T) *HashSet[T]
NewHashSetFromSlice creates a new HashSet from the given slice.
func NewHashSetWithCapacity ¶
func NewHashSetWithCapacity[T comparable](capacity int) *HashSet[T]
NewHashSetWithCapacity creates a new HashSet with the specified initial capacity.
func (*HashSet[T]) ContainsAll ¶
ContainsAll checks if the set contains all specified elements.
func (*HashSet[T]) ContainsAny ¶
ContainsAny checks if the set contains any of the specified elements.
func (*HashSet[T]) Difference ¶
Difference returns a new set containing elements present in this set but not in the other.
func (*HashSet[T]) Each ¶
Each iterates over all elements in the set and calls the provided function. Iteration stops if the function returns false.
func (*HashSet[T]) Filter ¶
Filter returns a new set containing only elements that satisfy the predicate.
func (*HashSet[T]) Intersection ¶
Intersection returns a new set containing only elements present in both sets.
func (*HashSet[T]) IsSuperset ¶
IsSuperset returns true if this set is a superset of the other set.
func (*HashSet[T]) Map ¶
Map transforms each element using the provided function and returns a new set.
func (*HashSet[T]) MarshalJSON ¶
func (*HashSet[T]) RemoveIf ¶
RemoveIf removes all elements that satisfy the predicate. Returns the number of elements removed.
func (*HashSet[T]) SymmetricDifference ¶
SymmetricDifference returns a new set containing elements present in either set but not in both.
func (*HashSet[T]) UnmarshalJSON ¶
type Iterator ¶
type Iterator[T any] interface { // Next moves the iterator to the next element. // Returns true if there is a next element, false otherwise. Next() bool // Prev moves the iterator to the previous element. // Returns true if there is a previous element, false otherwise. Prev() bool // Value returns the current element. // Should only be called after Next() or Prev() returns true. Value() T // Begin resets the iterator to the beginning (before the first element). // The next call to Next() will return the first element. Begin() // End resets the iterator to the end (after the last element). // The next call to Prev() will return the last element. End() }
Iterator defines the interface for iterating over set elements.
type OrderedSet ¶
type OrderedSet[T comparable] interface { Set[T] // Min returns the minimum element in the set. // Returns zero value and false if the set is empty. Min() (T, bool) // Max returns the maximum element in the set. // Returns zero value and false if the set is empty. Max() (T, bool) // Range returns elements between from (inclusive) and to (exclusive). Range(from, to T) []T // Iterator returns an iterator for traversing the set in order. Iterator() Iterator[T] // ReverseSeq returns a reverse-order iterator sequence over the set. ReverseSeq() iter.Seq[T] // EachIndexed iterates with the element index in sorted order. EachIndexed(func(index int, element T) bool) // SeqWithIndex returns a sequence yielding index-element pairs in ascending order. SeqWithIndex() iter.Seq2[int, T] // ReverseSeqWithIndex returns a sequence yielding index-element pairs in descending order. ReverseSeqWithIndex() iter.Seq2[int, T] }
OrderedSet extends Set with additional methods for ordered sets (like TreeSet).
type Set ¶
type Set[T comparable] interface { // Add adds one or more elements to the set. // Returns true if at least one element was added (wasn't already present). Add(elements ...T) bool // Remove removes one or more elements from the set. // Returns true if at least one element was removed (was present). Remove(elements ...T) bool // Contains checks if the set contains the specified element. Contains(element T) bool // ContainsAll checks if the set contains all specified elements. ContainsAll(elements ...T) bool // ContainsAny checks if the set contains any of the specified elements. ContainsAny(elements ...T) bool // Size returns the number of elements in the set. Size() int // IsEmpty returns true if the set contains no elements. IsEmpty() bool // Clear removes all elements from the set. Clear() // RemoveIf removes all elements that satisfy the predicate. // Returns the number of elements removed. RemoveIf(predicate func(element T) bool) int // Values returns all elements in the set as a slice. // For unordered sets (HashSet), the order is random. // For ordered sets (TreeSet), the order is sorted. Values() []T // Union returns a new set containing all elements from both sets. Union(other Set[T]) Set[T] // Intersection returns a new set containing only elements present in both sets. Intersection(other Set[T]) Set[T] // Difference returns a new set containing elements present in this set but not in the other. Difference(other Set[T]) Set[T] // SymmetricDifference returns a new set containing elements present in either set but not in both. SymmetricDifference(other Set[T]) Set[T] // IsSubset returns true if this set is a subset of the other set. IsSubset(other Set[T]) bool // IsSuperset returns true if this set is a superset of the other set. IsSuperset(other Set[T]) bool // Equal returns true if both sets contain exactly the same elements. Equal(other Set[T]) bool // Clone returns a shallow copy of the set. Clone() Set[T] // Each iterates over all elements in the set and calls the provided function. // Iteration stops if the function returns false. Each(func(element T) bool) // Seq returns an iterator sequence over the set. // Hash-based sets yield elements without a defined order, ordered sets respect their ordering. Seq() iter.Seq[T] // Filter returns a new set containing only elements that satisfy the predicate. Filter(predicate func(element T) bool) Set[T] // Map transforms each element using the provided function and returns a new set. // Note: The transform function must return a comparable type. Map(transform func(element T) T) Set[T] // Any returns true if at least one element satisfies the predicate. Any(predicate func(element T) bool) bool // All returns true if all elements satisfy the predicate. All(predicate func(element T) bool) bool }
Set defines the interface for set data structures.
type SyncHashSet ¶
type SyncHashSet[T comparable] struct { // contains filtered or unexported fields }
SyncHashSet is a thread-safe unordered set implementation using xsync.Map. It provides O(1) average time complexity for add, remove, and contains operations. SyncHashSet is safe for concurrent use by multiple goroutines.
func NewSyncHashSet ¶
func NewSyncHashSet[T comparable]() *SyncHashSet[T]
NewSyncHashSet creates a new empty thread-safe SyncHashSet.
func NewSyncHashSetFromSlice ¶
func NewSyncHashSetFromSlice[T comparable](elements []T) *SyncHashSet[T]
NewSyncHashSetFromSlice creates a new thread-safe SyncHashSet from the given slice.
func (*SyncHashSet[T]) Add ¶
func (s *SyncHashSet[T]) Add(elements ...T) bool
Add adds one or more elements to the set. Thread-safe for concurrent use.
func (*SyncHashSet[T]) All ¶
func (s *SyncHashSet[T]) All(predicate func(element T) bool) bool
All returns true if all elements satisfy the predicate. Thread-safe for concurrent use.
func (*SyncHashSet[T]) Any ¶
func (s *SyncHashSet[T]) Any(predicate func(element T) bool) bool
Any returns true if at least one element satisfies the predicate. Thread-safe for concurrent use.
func (*SyncHashSet[T]) Clear ¶
func (s *SyncHashSet[T]) Clear()
Clear removes all elements from the set. Thread-safe for concurrent use.
func (*SyncHashSet[T]) Clone ¶
func (s *SyncHashSet[T]) Clone() Set[T]
Clone returns a shallow copy of the set. Thread-safe for concurrent use.
func (*SyncHashSet[T]) Contains ¶
func (s *SyncHashSet[T]) Contains(element T) bool
Contains checks if the set contains the specified element. Thread-safe for concurrent use.
func (*SyncHashSet[T]) ContainsAll ¶
func (s *SyncHashSet[T]) ContainsAll(elements ...T) bool
ContainsAll checks if the set contains all specified elements. Thread-safe for concurrent use.
func (*SyncHashSet[T]) ContainsAny ¶
func (s *SyncHashSet[T]) ContainsAny(elements ...T) bool
ContainsAny checks if the set contains any of the specified elements. Thread-safe for concurrent use.
func (*SyncHashSet[T]) Difference ¶
func (s *SyncHashSet[T]) Difference(other Set[T]) Set[T]
Difference returns a new set containing elements present in this set but not in the other. Thread-safe for concurrent use.
func (*SyncHashSet[T]) Each ¶
func (s *SyncHashSet[T]) Each(fn func(element T) bool)
Each iterates over all elements in the set and calls the provided function. Iteration stops if the function returns false. Thread-safe for concurrent use.
func (*SyncHashSet[T]) Equal ¶
func (s *SyncHashSet[T]) Equal(other Set[T]) bool
Equal returns true if both sets contain exactly the same elements. Thread-safe for concurrent use.
func (*SyncHashSet[T]) Filter ¶
func (s *SyncHashSet[T]) Filter(predicate func(element T) bool) Set[T]
Filter returns a new set containing only elements that satisfy the predicate. Thread-safe for concurrent use.
func (*SyncHashSet[T]) GobDecode ¶
func (s *SyncHashSet[T]) GobDecode(data []byte) error
func (*SyncHashSet[T]) GobEncode ¶
func (s *SyncHashSet[T]) GobEncode() ([]byte, error)
func (*SyncHashSet[T]) Intersection ¶
func (s *SyncHashSet[T]) Intersection(other Set[T]) Set[T]
Intersection returns a new set containing only elements present in both sets. Thread-safe for concurrent use.
func (*SyncHashSet[T]) IsEmpty ¶
func (s *SyncHashSet[T]) IsEmpty() bool
IsEmpty returns true if the set contains no elements. Thread-safe for concurrent use.
func (*SyncHashSet[T]) IsSubset ¶
func (s *SyncHashSet[T]) IsSubset(other Set[T]) bool
IsSubset returns true if this set is a subset of the other set. Thread-safe for concurrent use.
func (*SyncHashSet[T]) IsSuperset ¶
func (s *SyncHashSet[T]) IsSuperset(other Set[T]) bool
IsSuperset returns true if this set is a superset of the other set. Thread-safe for concurrent use.
func (*SyncHashSet[T]) Map ¶
func (s *SyncHashSet[T]) Map(transform func(element T) T) Set[T]
Map transforms each element using the provided function and returns a new set. Thread-safe for concurrent use.
func (*SyncHashSet[T]) MarshalJSON ¶
func (s *SyncHashSet[T]) MarshalJSON() ([]byte, error)
func (*SyncHashSet[T]) Remove ¶
func (s *SyncHashSet[T]) Remove(elements ...T) bool
Remove removes one or more elements from the set. Thread-safe for concurrent use.
func (*SyncHashSet[T]) RemoveIf ¶
func (s *SyncHashSet[T]) RemoveIf(predicate func(element T) bool) int
RemoveIf removes all elements that satisfy the predicate. Returns the number of elements removed. Thread-safe for concurrent use.
func (*SyncHashSet[T]) Seq ¶
func (s *SyncHashSet[T]) Seq() iter.Seq[T]
Seq returns an iterator sequence over the set in unspecified order. Thread-safe for concurrent use.
func (*SyncHashSet[T]) Size ¶
func (s *SyncHashSet[T]) Size() int
Size returns the number of elements in the set. Thread-safe for concurrent use.
func (*SyncHashSet[T]) SymmetricDifference ¶
func (s *SyncHashSet[T]) SymmetricDifference(other Set[T]) Set[T]
SymmetricDifference returns a new set containing elements present in either set but not in both. Thread-safe for concurrent use.
func (*SyncHashSet[T]) Union ¶
func (s *SyncHashSet[T]) Union(other Set[T]) Set[T]
Union returns a new set containing all elements from both sets. Thread-safe for concurrent use.
func (*SyncHashSet[T]) UnmarshalJSON ¶
func (s *SyncHashSet[T]) UnmarshalJSON(data []byte) error
func (*SyncHashSet[T]) Values ¶
func (s *SyncHashSet[T]) Values() []T
Values returns all elements in the set as a slice. The order is random and not guaranteed to be consistent. Thread-safe for concurrent use.
type TreeSet ¶
type TreeSet[T comparable] struct { // contains filtered or unexported fields }
TreeSet is an ordered set implementation using a red-black tree from gods v2. Elements are kept sorted according to the provided comparator. It provides O(log n) time complexity for add, remove, and contains operations. TreeSet is not thread-safe.
func NewTreeSet ¶
NewTreeSet creates a new empty TreeSet using the default comparator for ordered types. T must satisfy cmp.Ordered constraint (implements < operator).
func NewTreeSetFromSlice ¶
NewTreeSetFromSlice creates a new TreeSet from the given slice using the default comparator.
func NewTreeSetFromSliceWithComparator ¶
func NewTreeSetFromSliceWithComparator[T comparable](elements []T, comparator func(a, b T) int) *TreeSet[T]
NewTreeSetFromSliceWithComparator creates a new TreeSet from the given slice with a custom comparator.
func NewTreeSetWithComparator ¶
func NewTreeSetWithComparator[T comparable](comparator func(a, b T) int) *TreeSet[T]
NewTreeSetWithComparator creates a new empty TreeSet with a custom comparator. The comparator should return:
- negative value if a < b
- zero if a == b
- positive value if a > b
func (*TreeSet[T]) ContainsAll ¶
ContainsAll checks if the set contains all specified elements.
func (*TreeSet[T]) ContainsAny ¶
ContainsAny checks if the set contains any of the specified elements.
func (*TreeSet[T]) Difference ¶
Difference returns a new set containing elements present in this set but not in the other.
func (*TreeSet[T]) Each ¶
Each iterates over all elements in the set in sorted order and calls the provided function. Iteration stops if the function returns false.
func (*TreeSet[T]) EachIndexed ¶
EachIndexed iterates over elements in sorted order with their position.
func (*TreeSet[T]) Filter ¶
Filter returns a new set containing only elements that satisfy the predicate.
func (*TreeSet[T]) Intersection ¶
Intersection returns a new set containing only elements present in both sets.
func (*TreeSet[T]) IsSuperset ¶
IsSuperset returns true if this set is a superset of the other set.
func (*TreeSet[T]) Map ¶
Map transforms each element using the provided function and returns a new set.
func (*TreeSet[T]) MarshalJSON ¶
func (*TreeSet[T]) Max ¶
Max returns the maximum element in the set. Returns zero value and false if the set is empty.
func (*TreeSet[T]) Min ¶
Min returns the minimum element in the set. Returns zero value and false if the set is empty.
func (*TreeSet[T]) Range ¶
func (s *TreeSet[T]) Range(from, to T) []T
Range returns elements between from (inclusive) and to (exclusive).
func (*TreeSet[T]) RemoveIf ¶
RemoveIf removes all elements that satisfy the predicate. Returns the number of elements removed.
func (*TreeSet[T]) ReverseSeq ¶
ReverseSeq returns an iterator sequence over the set in descending order.
func (*TreeSet[T]) ReverseSeqWithIndex ¶
ReverseSeqWithIndex returns an iterator sequence yielding index-element pairs in descending order.
func (*TreeSet[T]) SeqWithIndex ¶
SeqWithIndex returns an iterator sequence yielding index-element pairs in ascending order.
func (*TreeSet[T]) SymmetricDifference ¶
SymmetricDifference returns a new set containing elements present in either set but not in both.