uset

package
v3.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2026 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

uset

Set implementations and set algebra helpers.

Includes hash sets, synchronized sets, sharded concurrent sets, ordered sets, comparable-key sets, and helpers for union, intersection, difference, subset, and superset checks.

Example

set := uset.NewHashSet("read", "write")
set.Add("admin")
allowed := set.Contains("read")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsSubset

func IsSubset[T comparable](left, right Set[T]) bool

IsSubset returns true when every value from left is present in right.

func IsSuperset

func IsSuperset[T comparable](left, right Set[T]) bool

IsSuperset returns true when every value from right is present in left.

Types

type ComparableHashSet

type ComparableHashSet[T uconst.UniqueKey[K], K comparable] struct {
	// contains filtered or unexported fields
}

ComparableHashSet is the same as HashSet, but allows for custom UniqueKey definition.

func NewComparableHashSet

func NewComparableHashSet[T uconst.UniqueKey[K], K comparable](values ...T) *ComparableHashSet[T, K]

NewComparableHashSet creates a new instance of HashSet with initial capacity equal to values length.

func NewComparableHashSetWithSize

func NewComparableHashSetWithSize[T uconst.UniqueKey[K], K comparable](size int) *ComparableHashSet[T, K]

NewComparableHashSetWithSize creates a new instance of HashSet with a specified initial size.

func (*ComparableHashSet[T, K]) Add

func (s *ComparableHashSet[T, K]) Add(value T) bool

Add inserts a value into the set and returns true if the value was not already present.

func (*ComparableHashSet[T, K]) Clear

func (s *ComparableHashSet[T, K]) Clear()

Clear removes all elements from the set.

func (*ComparableHashSet[T, K]) Contains

func (s *ComparableHashSet[T, K]) Contains(value T) bool

Contains checks if a value is present in the set.

func (*ComparableHashSet[T, K]) Remove

func (s *ComparableHashSet[T, K]) Remove(value T) bool

Remove deletes a value from the set and returns true if the value was present.

func (*ComparableHashSet[T, K]) Size

func (s *ComparableHashSet[T, K]) Size() int

Size returns the number of elements in the set.

func (*ComparableHashSet[T, K]) Values

func (s *ComparableHashSet[T, K]) Values() []T

type ConcurrentHashSet

type ConcurrentHashSet[T comparable] struct {
	// contains filtered or unexported fields
}

ConcurrentHashSet is a concurrent-safe hash set implementation that uses sharding. Each shard is protected by its own RWMutex which allows high concurrency for operations on different keys.

func NewConcurrentHashSet

func NewConcurrentHashSet[T comparable]() *ConcurrentHashSet[T]

NewConcurrentHashSet creates a ConcurrentHashSet with a default Farm64-based hash and a default number of shards.

func NewConcurrentHashSetWithHash

func NewConcurrentHashSetWithHash[T comparable](hash func(T) uint64) *ConcurrentHashSet[T]

NewConcurrentHashSetWithHash creates a ConcurrentHashSet with a custom hash function and a default number of shards.

func NewConcurrentHashSetWithHashAndShards

func NewConcurrentHashSetWithHashAndShards[T comparable](hash func(T) uint64, shardCount int) *ConcurrentHashSet[T]

NewConcurrentHashSetWithHashAndShards creates a ConcurrentHashSet with a custom hash function and a custom number of shards.

func NewCustomConcurrentHashSet

func NewCustomConcurrentHashSet[T comparable](hash func(T) uint64) *ConcurrentHashSet[T]

NewCustomConcurrentHashSet is kept for backward compatibility. It is equivalent to NewConcurrentHashSetWithHash with the default shard count.

func (*ConcurrentHashSet[T]) Add

func (s *ConcurrentHashSet[T]) Add(value T) bool

Add inserts a value into the set and returns true if the value was not already present.

func (*ConcurrentHashSet[T]) Clear

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

Clear removes all elements from the set.

func (*ConcurrentHashSet[T]) Contains

func (s *ConcurrentHashSet[T]) Contains(value T) bool

Contains checks if a value is present in the set.

func (*ConcurrentHashSet[T]) Remove

func (s *ConcurrentHashSet[T]) Remove(value T) bool

Remove deletes a value from the set and returns true if the value was present.

func (*ConcurrentHashSet[T]) Size

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

Size returns the number of elements in the set.

func (*ConcurrentHashSet[T]) Values

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

Values retrieves all values from the set. The snapshot is not atomic across shards but is safe to call concurrently.

type HashSet

type HashSet[T comparable] struct {
	// contains filtered or unexported fields
}

HashSet is a generic set data structure that ensures all elements are unique. It uses a map to provide efficient operations for adding, removing, and checking elements.

func Difference

func Difference[T comparable](left, right Set[T]) *HashSet[T]

Difference returns a new set containing values from left that are not present in right.

func Intersect

func Intersect[T comparable](left, right Set[T]) *HashSet[T]

Intersect returns a new set containing values present in both sets.

func NewHashSet

func NewHashSet[T comparable](values ...T) *HashSet[T]

NewHashSet creates a new instance of HashSet with initial capacity equal to values length.

func NewHashSetWithSize

func NewHashSetWithSize[T comparable](size int) *HashSet[T]

NewHashSetWithSize creates a new instance of HashSet with a specified initial size.

func Union

func Union[T comparable](left, right Set[T]) *HashSet[T]

Union returns a new set containing values from both sets.

func (*HashSet[T]) Add

func (s *HashSet[T]) Add(value T) bool

Add inserts a value into the set and returns true if the value was not already present.

func (*HashSet[T]) Clear

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

Clear removes all elements from the set.

func (*HashSet[T]) Contains

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

Contains checks if a value is present in the set.

func (*HashSet[T]) Remove

func (s *HashSet[T]) Remove(value T) bool

Remove deletes a value from the set and returns true if the value was present.

func (*HashSet[T]) Size

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

Size returns the number of elements in the set.

func (*HashSet[T]) Values

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

Values retrieves all the values.

type OrderedHashSet

type OrderedHashSet[T uconst.UniqueKey[K], K comparable] struct {
	// contains filtered or unexported fields
}

OrderedHashSet is a set implementation that preserves the order of elements as they were added. This implementation is slower than a traditional HashSet or ComparableHashSet, therefore it's recommended to use this only in cases you need to retrieve the items in their original order.

func NewOrderedHashSet

func NewOrderedHashSet[T uconst.UniqueKey[K], K comparable](values ...T) *OrderedHashSet[T, K]

NewOrderedHashSet creates a new instance of OrderedHashSet.

func (*OrderedHashSet[T, K]) Add

func (s *OrderedHashSet[T, K]) Add(value T) bool

Add inserts a value into the set and returns true if the value was not already present.

func (*OrderedHashSet[T, K]) Clear

func (s *OrderedHashSet[T, K]) Clear()

Clear removes all elements from the set.

func (*OrderedHashSet[T, K]) Contains

func (s *OrderedHashSet[T, K]) Contains(value T) bool

Contains checks if a value is present in the set.

func (*OrderedHashSet[T, K]) Delete

func (s *OrderedHashSet[T, K]) Delete(key K) bool

Delete deletes a value from the set by unique key and returns true if the value was present.

func (*OrderedHashSet[T, K]) Get

func (s *OrderedHashSet[T, K]) Get(key K) *T

Get retrieves the element by unique key.

func (*OrderedHashSet[T, K]) OrderedList

func (s *OrderedHashSet[T, K]) OrderedList() []T

OrderedList returns a slice of all elements in the set, in the order they were added.

func (*OrderedHashSet[T, K]) Remove

func (s *OrderedHashSet[T, K]) Remove(value T) bool

Remove deletes a value from the set and returns true if the value was present.

func (*OrderedHashSet[T, K]) Size

func (s *OrderedHashSet[T, K]) Size() int

Size returns the number of elements in the set.

type OrderedSet

type OrderedSet[T uconst.UniqueKey[K], K comparable] interface {
	Add(value T) bool
	Contains(value T) bool
	Get(key K) *T // Retrieve value by key
	Delete(key K) bool
	Remove(value T) bool
	Size() int
	Clear()
	OrderedList() []T // OrderedList returns the slice of ordered items while preserving their natural order
}

OrderedSet is an interface that represents a set of unique items that can be retrieved in the original insertion order. It is parameterized by T, which must implement uconst.UniqueKey[K], and K, which must be comparable.

type Set

type Set[T comparable] interface {
	Add(value T) bool
	Contains(value T) bool
	Remove(value T) bool
	Size() int
	Clear()
	Values() []T
}

Set that contains unique elements.

type SynchronizedHashSet

type SynchronizedHashSet[T comparable] struct {
	// contains filtered or unexported fields
}

SynchronizedHashSet is a thread-safe wrapper around HashSet that ensures concurrent safety.

func NewSynchronizedHashSet

func NewSynchronizedHashSet[T comparable](values ...T) *SynchronizedHashSet[T]

func NewSynchronizedHashSetFromSet

func NewSynchronizedHashSetFromSet[T comparable](hs *HashSet[T]) *SynchronizedHashSet[T]

func (*SynchronizedHashSet[T]) Add

func (s *SynchronizedHashSet[T]) Add(value T) bool

func (*SynchronizedHashSet[T]) Clear

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

func (*SynchronizedHashSet[T]) Contains

func (s *SynchronizedHashSet[T]) Contains(value T) bool

func (*SynchronizedHashSet[T]) Remove

func (s *SynchronizedHashSet[T]) Remove(value T) bool

func (*SynchronizedHashSet[T]) Size

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

func (*SynchronizedHashSet[T]) Values

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

Values retrieves all the values.

Jump to

Keyboard shortcuts

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