set

package
v0.16.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 31, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

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]) Add

func (s *HashSet[T]) Add(elements ...T) bool

Add adds one or more elements to the set.

func (*HashSet[T]) All

func (s *HashSet[T]) All(predicate func(element T) bool) bool

All returns true if all elements satisfy the predicate.

func (*HashSet[T]) Any

func (s *HashSet[T]) Any(predicate func(element T) bool) bool

Any returns true if at least one element satisfies the predicate.

func (*HashSet[T]) Clear

func (s *HashSet[T]) Clear()

Clear removes all elements from the set.

func (*HashSet[T]) Clone

func (s *HashSet[T]) Clone() Set[T]

Clone returns a shallow copy of the set.

func (*HashSet[T]) Contains

func (s *HashSet[T]) Contains(element T) bool

Contains checks if the set contains the specified element.

func (*HashSet[T]) ContainsAll

func (s *HashSet[T]) ContainsAll(elements ...T) bool

ContainsAll checks if the set contains all specified elements.

func (*HashSet[T]) ContainsAny

func (s *HashSet[T]) ContainsAny(elements ...T) bool

ContainsAny checks if the set contains any of the specified elements.

func (*HashSet[T]) Difference

func (s *HashSet[T]) Difference(other Set[T]) Set[T]

Difference returns a new set containing elements present in this set but not in the other.

func (*HashSet[T]) Each

func (s *HashSet[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.

func (*HashSet[T]) Equal

func (s *HashSet[T]) Equal(other Set[T]) bool

Equal returns true if both sets contain exactly the same elements.

func (*HashSet[T]) Filter

func (s *HashSet[T]) Filter(predicate func(element T) bool) Set[T]

Filter returns a new set containing only elements that satisfy the predicate.

func (*HashSet[T]) GobDecode

func (s *HashSet[T]) GobDecode(data []byte) error

func (*HashSet[T]) GobEncode

func (s *HashSet[T]) GobEncode() ([]byte, error)

func (*HashSet[T]) Intersection

func (s *HashSet[T]) Intersection(other Set[T]) Set[T]

Intersection returns a new set containing only elements present in both sets.

func (*HashSet[T]) IsEmpty

func (s *HashSet[T]) IsEmpty() bool

IsEmpty returns true if the set contains no elements.

func (*HashSet[T]) IsSubset

func (s *HashSet[T]) IsSubset(other Set[T]) bool

IsSubset returns true if this set is a subset of the other set.

func (*HashSet[T]) IsSuperset

func (s *HashSet[T]) IsSuperset(other Set[T]) bool

IsSuperset returns true if this set is a superset of the other set.

func (*HashSet[T]) Map

func (s *HashSet[T]) Map(transform func(element T) T) Set[T]

Map transforms each element using the provided function and returns a new set.

func (*HashSet[T]) MarshalJSON

func (s *HashSet[T]) MarshalJSON() ([]byte, error)

func (*HashSet[T]) Remove

func (s *HashSet[T]) Remove(elements ...T) bool

Remove removes one or more elements from the set.

func (*HashSet[T]) RemoveIf

func (s *HashSet[T]) RemoveIf(predicate func(element T) bool) int

RemoveIf removes all elements that satisfy the predicate. Returns the number of elements removed.

func (*HashSet[T]) Seq

func (s *HashSet[T]) Seq() iter.Seq[T]

Seq returns an iterator sequence over the set in unspecified order.

func (*HashSet[T]) Size

func (s *HashSet[T]) Size() int

Size returns the number of elements in the set.

func (*HashSet[T]) SymmetricDifference

func (s *HashSet[T]) SymmetricDifference(other Set[T]) Set[T]

SymmetricDifference returns a new set containing elements present in either set but not in both.

func (*HashSet[T]) Union

func (s *HashSet[T]) Union(other Set[T]) Set[T]

Union returns a new set containing all elements from both sets.

func (*HashSet[T]) UnmarshalJSON

func (s *HashSet[T]) UnmarshalJSON(data []byte) error

func (*HashSet[T]) Values

func (s *HashSet[T]) Values() []T

Values returns all elements in the set as a slice. The order is random and not guaranteed to be consistent.

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

func NewTreeSet[T cmp.Ordered]() *TreeSet[T]

NewTreeSet creates a new empty TreeSet using the default comparator for ordered types. T must satisfy cmp.Ordered constraint (implements < operator).

func NewTreeSetFromSlice

func NewTreeSetFromSlice[T cmp.Ordered](elements []T) *TreeSet[T]

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]) Add

func (s *TreeSet[T]) Add(elements ...T) bool

Add adds one or more elements to the set.

func (*TreeSet[T]) All

func (s *TreeSet[T]) All(predicate func(element T) bool) bool

All returns true if all elements satisfy the predicate.

func (*TreeSet[T]) Any

func (s *TreeSet[T]) Any(predicate func(element T) bool) bool

Any returns true if at least one element satisfies the predicate.

func (*TreeSet[T]) Clear

func (s *TreeSet[T]) Clear()

Clear removes all elements from the set.

func (*TreeSet[T]) Clone

func (s *TreeSet[T]) Clone() Set[T]

Clone returns a shallow copy of the set.

func (*TreeSet[T]) Contains

func (s *TreeSet[T]) Contains(element T) bool

Contains checks if the set contains the specified element.

func (*TreeSet[T]) ContainsAll

func (s *TreeSet[T]) ContainsAll(elements ...T) bool

ContainsAll checks if the set contains all specified elements.

func (*TreeSet[T]) ContainsAny

func (s *TreeSet[T]) ContainsAny(elements ...T) bool

ContainsAny checks if the set contains any of the specified elements.

func (*TreeSet[T]) Difference

func (s *TreeSet[T]) Difference(other Set[T]) Set[T]

Difference returns a new set containing elements present in this set but not in the other.

func (*TreeSet[T]) Each

func (s *TreeSet[T]) Each(fn func(element T) bool)

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

func (s *TreeSet[T]) EachIndexed(fn func(index int, element T) bool)

EachIndexed iterates over elements in sorted order with their position.

func (*TreeSet[T]) Equal

func (s *TreeSet[T]) Equal(other Set[T]) bool

Equal returns true if both sets contain exactly the same elements.

func (*TreeSet[T]) Filter

func (s *TreeSet[T]) Filter(predicate func(element T) bool) Set[T]

Filter returns a new set containing only elements that satisfy the predicate.

func (*TreeSet[T]) GobDecode

func (s *TreeSet[T]) GobDecode(data []byte) error

func (*TreeSet[T]) GobEncode

func (s *TreeSet[T]) GobEncode() ([]byte, error)

func (*TreeSet[T]) Intersection

func (s *TreeSet[T]) Intersection(other Set[T]) Set[T]

Intersection returns a new set containing only elements present in both sets.

func (*TreeSet[T]) IsEmpty

func (s *TreeSet[T]) IsEmpty() bool

IsEmpty returns true if the set contains no elements.

func (*TreeSet[T]) IsSubset

func (s *TreeSet[T]) IsSubset(other Set[T]) bool

IsSubset returns true if this set is a subset of the other set.

func (*TreeSet[T]) IsSuperset

func (s *TreeSet[T]) IsSuperset(other Set[T]) bool

IsSuperset returns true if this set is a superset of the other set.

func (*TreeSet[T]) Iterator

func (s *TreeSet[T]) Iterator() Iterator[T]

Iterator returns an iterator for traversing the set in sorted order.

func (*TreeSet[T]) Map

func (s *TreeSet[T]) Map(transform func(element T) T) Set[T]

Map transforms each element using the provided function and returns a new set.

func (*TreeSet[T]) MarshalJSON

func (s *TreeSet[T]) MarshalJSON() ([]byte, error)

func (*TreeSet[T]) Max

func (s *TreeSet[T]) Max() (T, bool)

Max returns the maximum element in the set. Returns zero value and false if the set is empty.

func (*TreeSet[T]) Min

func (s *TreeSet[T]) Min() (T, bool)

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]) Remove

func (s *TreeSet[T]) Remove(elements ...T) bool

Remove removes one or more elements from the set.

func (*TreeSet[T]) RemoveIf

func (s *TreeSet[T]) RemoveIf(predicate func(element T) bool) int

RemoveIf removes all elements that satisfy the predicate. Returns the number of elements removed.

func (*TreeSet[T]) ReverseSeq

func (s *TreeSet[T]) ReverseSeq() iter.Seq[T]

ReverseSeq returns an iterator sequence over the set in descending order.

func (*TreeSet[T]) ReverseSeqWithIndex

func (s *TreeSet[T]) ReverseSeqWithIndex() iter.Seq2[int, T]

ReverseSeqWithIndex returns an iterator sequence yielding index-element pairs in descending order.

func (*TreeSet[T]) Seq

func (s *TreeSet[T]) Seq() iter.Seq[T]

Seq returns an iterator sequence over the set in ascending order.

func (*TreeSet[T]) SeqWithIndex

func (s *TreeSet[T]) SeqWithIndex() iter.Seq2[int, T]

SeqWithIndex returns an iterator sequence yielding index-element pairs in ascending order.

func (*TreeSet[T]) Size

func (s *TreeSet[T]) Size() int

Size returns the number of elements in the set.

func (*TreeSet[T]) SymmetricDifference

func (s *TreeSet[T]) SymmetricDifference(other Set[T]) Set[T]

SymmetricDifference returns a new set containing elements present in either set but not in both.

func (*TreeSet[T]) Union

func (s *TreeSet[T]) Union(other Set[T]) Set[T]

Union returns a new set containing all elements from both sets.

func (*TreeSet[T]) UnmarshalJSON

func (s *TreeSet[T]) UnmarshalJSON(data []byte) error

func (*TreeSet[T]) Values

func (s *TreeSet[T]) Values() []T

Values returns all elements in the set as a slice in sorted order.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL