hashset

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

README

hashset

Hash-based set implementations.

Types

For Comparable Elements

Use when elements support == comparison:

// Create with initial elements
s := hashset.NewComparable("a", "b", "c")

// Empty set
s := hashset.NewComparable[string]()
For Hashable Elements

Use when elements implement ds.Hashable[T]:

s := hashset.NewHashable(elem1, elem2, elem3)
Thread-Safe Sets

Wrap any mutable set for concurrent access:

inner := hashset.NewComparable[string]()
s := hashset.NewConcurrentSet(inner)

// Atomic operations
s.Compute("elem", func(e string, exists bool) (string, bool) {
    return e, true  // add or keep
})

s.ComputeIfAbsent("elem", func(e string) (string, bool) {
    return e, true  // add if missing
})

s.ComputeIfPresent("elem", func(e string) (string, bool) {
    return e, false // remove if present
})

Common Operations

// Add/Remove
s.Add("x")
s.AddAll("x", "y", "z")
s.Remove("x")
s.RemoveAll("x", "y")
s.Clear()

// Query
s.Contains("x")
s.Size()
s.IsEmpty()
s.List()  // []E

// Set Operations
union := s.Union(other)
inter := s.Intersection(other)
diff := s.Difference(other)
symDiff := s.SymmetricDifference(other)

// Subset Relations
s.IsSubSet(other)
s.IsProperSubSet(other)
s.IsSuperSet(other)
s.IsProperSuperSet(other)
s.Equal(other)

// Power Set
subsets := s.SubSets()           // []Set
for sub := range s.IterSubSets() // iterator

Iteration

// Elements only
for elem := range s.Iter() {
    // ...
}

// With index
for i, elem := range s.Iter2() {
    // ...
}

Freeze/Unfreeze

// Create immutable snapshot
frozen := s.Freeze()

// Create mutable copy from immutable
mutable := frozen.Unfreeze()

Documentation

Overview

Package hashset provides hash-based set implementations for the datastructures interfaces.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewComparable

func NewComparable[E comparable](xs ...E) ds.MutableSet[E]

NewComparable creates a new mutable set for comparable element types.

func NewHashable

func NewHashable[E ds.Hashable[E]](xs ...E) ds.MutableSet[E]

NewHashable creates a new mutable set for hashable element types.

Types

type ConcurrentSet

type ConcurrentSet[E any] struct {
	// contains filtered or unexported fields
}

ConcurrentSet is a thread-safe wrapper around a MutableSet. All operations are protected by a read-write mutex.

func NewConcurrentSet

func NewConcurrentSet[E any](innerSet ds.MutableSet[E]) *ConcurrentSet[E]

NewConcurrentSet creates a new thread-safe set wrapping the given mutable set.

func (*ConcurrentSet[E]) Add

func (s *ConcurrentSet[E]) Add(e E)

Add adds an element to the set.

func (*ConcurrentSet[E]) AddAll

func (s *ConcurrentSet[E]) AddAll(es ...E)

AddAll adds multiple elements to the set.

func (*ConcurrentSet[_]) Cardinality

func (s *ConcurrentSet[_]) Cardinality() int

Cardinality returns the number of elements in the set.

func (*ConcurrentSet[E]) Clear

func (s *ConcurrentSet[E]) Clear()

Clear removes all elements from the set.

func (*ConcurrentSet[E]) Clone

func (s *ConcurrentSet[E]) Clone() ds.ConcurrentSet[E]

Clone returns a new concurrent set with a copy of the data.

func (*ConcurrentSet[E]) Compute

func (s *ConcurrentSet[E]) Compute(e E, remappingFunction func(e E, exists bool) (E, bool)) E

Compute atomically computes a new value based on the element's presence. The remappingFunction receives the element and whether it exists, returning the new value and whether to store it.

func (*ConcurrentSet[E]) ComputeIfAbsent

func (s *ConcurrentSet[E]) ComputeIfAbsent(e E, mappingFunction func(e E) (E, bool)) E

ComputeIfAbsent atomically computes a value only if the element is absent. The mappingFunction returns the value to store and whether to store it. If the element exists, returns the element without calling mappingFunction.

func (*ConcurrentSet[E]) ComputeIfPresent

func (s *ConcurrentSet[E]) ComputeIfPresent(e E, remappingFunction func(e E) (E, bool)) E

ComputeIfPresent atomically computes a new value only if the element is present. The remappingFunction returns the new value and whether to keep it (false removes the element). If the element is absent, returns the element without calling remappingFunction.

func (*ConcurrentSet[E]) Contains

func (s *ConcurrentSet[E]) Contains(e E) bool

Contains returns true if the element is in the set.

func (*ConcurrentSet[E]) Difference

func (s *ConcurrentSet[E]) Difference(other ds.Set[E]) ds.ConcurrentSet[E]

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

func (*ConcurrentSet[E]) Equal

func (s *ConcurrentSet[E]) Equal(other ds.Set[E]) bool

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

func (*ConcurrentSet[E]) Intersection

func (s *ConcurrentSet[E]) Intersection(other ds.Set[E]) ds.ConcurrentSet[E]

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

func (*ConcurrentSet[_]) IsEmpty

func (s *ConcurrentSet[_]) IsEmpty() bool

IsEmpty returns true if the set contains no elements.

func (*ConcurrentSet[E]) IsProperSubSet

func (s *ConcurrentSet[E]) IsProperSubSet(other ds.Set[E]) bool

IsProperSubSet returns true if this is a subset of other and they are not equal.

func (*ConcurrentSet[E]) IsProperSuperSet

func (s *ConcurrentSet[E]) IsProperSuperSet(other ds.Set[E]) bool

IsProperSuperSet returns true if this is a superset of other and they are not equal.

func (*ConcurrentSet[E]) IsSubSet

func (s *ConcurrentSet[E]) IsSubSet(other ds.Set[E]) bool

IsSubSet returns true if all elements of this set are in the other set.

func (*ConcurrentSet[E]) IsSuperSet

func (s *ConcurrentSet[E]) IsSuperSet(other ds.Set[E]) bool

IsSuperSet returns true if all elements of the other set are in this set.

func (*ConcurrentSet[E]) Iter

func (s *ConcurrentSet[E]) Iter() iter.Seq[E]

Iter returns an iterator over all elements in the set.

func (*ConcurrentSet[E]) Iter2

func (s *ConcurrentSet[E]) Iter2() iter.Seq2[int, E]

Iter2 returns an iterator with index and element pairs.

func (*ConcurrentSet[E]) IterSubSets

func (s *ConcurrentSet[E]) IterSubSets() iter.Seq[ds.Set[E]]

IterSubSets returns an iterator over all possible subsets of this set.

func (*ConcurrentSet[E]) List

func (s *ConcurrentSet[E]) List() []E

List returns a slice of all elements in the set.

func (*ConcurrentSet[E]) Remove

func (s *ConcurrentSet[E]) Remove(e E)

Remove removes an element from the set.

func (*ConcurrentSet[E]) RemoveAll

func (s *ConcurrentSet[E]) RemoveAll(es ...E)

RemoveAll removes multiple elements from the set.

func (*ConcurrentSet[_]) Size

func (s *ConcurrentSet[_]) Size() int

Size returns the number of elements in the set.

func (*ConcurrentSet[E]) SubSets

func (s *ConcurrentSet[E]) SubSets() []ds.Set[E]

SubSets returns all possible subsets of this set (power set).

func (*ConcurrentSet[E]) SymmetricDifference

func (s *ConcurrentSet[E]) SymmetricDifference(other ds.Set[E]) ds.ConcurrentSet[E]

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

func (*ConcurrentSet[E]) Union

func (s *ConcurrentSet[E]) Union(other ds.Set[E]) ds.ConcurrentSet[E]

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

type ImmutableSet

type ImmutableSet[E any] struct {
	// contains filtered or unexported fields
}

ImmutableSet is an immutable wrapper around a MutableSet.

func (*ImmutableSet[E]) Cardinality

func (s *ImmutableSet[E]) Cardinality() int

Cardinality returns the number of elements in the set.

func (*ImmutableSet[E]) Clone

func (s *ImmutableSet[E]) Clone() ds.Set[E]

Clone returns a copy of this set.

func (*ImmutableSet[E]) Contains

func (s *ImmutableSet[E]) Contains(e E) bool

Contains returns true if the element is in the set.

func (*ImmutableSet[E]) Difference

func (s *ImmutableSet[E]) Difference(other ds.Set[E]) ds.Set[E]

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

func (*ImmutableSet[E]) Equal

func (s *ImmutableSet[E]) Equal(other ds.Set[E]) bool

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

func (*ImmutableSet[E]) Intersection

func (s *ImmutableSet[E]) Intersection(other ds.Set[E]) ds.Set[E]

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

func (*ImmutableSet[E]) IsEmpty

func (s *ImmutableSet[E]) IsEmpty() bool

IsEmpty returns true if the set contains no elements.

func (*ImmutableSet[E]) IsProperSubSet

func (s *ImmutableSet[E]) IsProperSubSet(of ds.Set[E]) bool

IsProperSubSet returns true if this is a subset of other and they are not equal.

func (*ImmutableSet[E]) IsProperSuperSet

func (s *ImmutableSet[E]) IsProperSuperSet(of ds.Set[E]) bool

IsProperSuperSet returns true if this is a superset of other and they are not equal.

func (*ImmutableSet[E]) IsSubSet

func (s *ImmutableSet[E]) IsSubSet(of ds.Set[E]) bool

IsSubSet returns true if all elements of this set are in the other set.

func (*ImmutableSet[E]) IsSuperSet

func (s *ImmutableSet[E]) IsSuperSet(of ds.Set[E]) bool

IsSuperSet returns true if all elements of the other set are in this set.

func (*ImmutableSet[E]) Iter

func (s *ImmutableSet[E]) Iter() iter.Seq[E]

Iter returns an iterator over all elements in the set.

func (*ImmutableSet[E]) Iter2

func (s *ImmutableSet[E]) Iter2() iter.Seq2[int, E]

Iter2 returns an iterator with index and element pairs.

func (*ImmutableSet[E]) IterSubSets

func (s *ImmutableSet[E]) IterSubSets() iter.Seq[ds.Set[E]]

IterSubSets returns an iterator over all possible subsets of this set.

func (*ImmutableSet[E]) List

func (s *ImmutableSet[E]) List() []E

List returns a slice of all elements in the set.

func (*ImmutableSet[E]) Size

func (s *ImmutableSet[E]) Size() int

Size returns the number of elements in the set.

func (*ImmutableSet[E]) SubSets

func (s *ImmutableSet[E]) SubSets() []ds.Set[E]

SubSets returns all possible subsets of this set (power set).

func (*ImmutableSet[E]) SymmetricDifference

func (s *ImmutableSet[E]) SymmetricDifference(other ds.Set[E]) ds.Set[E]

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

func (*ImmutableSet[E]) Unfreeze

func (s *ImmutableSet[E]) Unfreeze() ds.MutableSet[E]

Unfreeze returns a mutable copy of this set.

func (*ImmutableSet[E]) Union

func (s *ImmutableSet[E]) Union(other ds.Set[E]) ds.Set[E]

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

type MutableComparableSet

type MutableComparableSet[E comparable] struct {
	// contains filtered or unexported fields
}

MutableComparableSet is a mutable hash set for comparable element types.

func (*MutableComparableSet[E]) Add

func (s *MutableComparableSet[E]) Add(e E)

Add adds an element to the set.

func (*MutableComparableSet[E]) AddAll

func (s *MutableComparableSet[E]) AddAll(es ...E)

AddAll adds multiple elements to the set.

func (*MutableComparableSet[_]) Cardinality

func (s *MutableComparableSet[_]) Cardinality() int

Cardinality returns the number of elements in the set.

func (*MutableComparableSet[E]) Clear

func (s *MutableComparableSet[E]) Clear()

Clear removes all elements from the set.

func (*MutableComparableSet[E]) Clone

func (s *MutableComparableSet[E]) Clone() ds.MutableSet[E]

Clone returns a mutable copy of this set.

func (*MutableComparableSet[E]) Contains

func (s *MutableComparableSet[E]) Contains(e E) bool

Contains returns true if the element is in the set.

func (*MutableComparableSet[E]) Difference

func (s *MutableComparableSet[E]) Difference(other ds.MutableSet[E]) ds.MutableSet[E]

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

func (*MutableComparableSet[E]) Equal

func (s *MutableComparableSet[E]) Equal(other ds.MutableSet[E]) bool

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

func (*MutableComparableSet[E]) Freeze

func (s *MutableComparableSet[E]) Freeze() ds.Set[E]

Freeze returns an immutable snapshot of this set.

func (*MutableComparableSet[E]) Intersection

func (s *MutableComparableSet[E]) Intersection(other ds.MutableSet[E]) ds.MutableSet[E]

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

func (*MutableComparableSet[_]) IsEmpty

func (s *MutableComparableSet[_]) IsEmpty() bool

IsEmpty returns true if the set contains no elements.

func (*MutableComparableSet[E]) IsProperSubSet

func (s *MutableComparableSet[E]) IsProperSubSet(of ds.MutableSet[E]) bool

IsProperSubSet returns true if this is a subset of other and they are not equal.

func (*MutableComparableSet[E]) IsProperSuperSet

func (s *MutableComparableSet[E]) IsProperSuperSet(of ds.MutableSet[E]) bool

IsProperSuperSet returns true if this is a superset of other and they are not equal.

func (*MutableComparableSet[E]) IsSubSet

func (s *MutableComparableSet[E]) IsSubSet(of ds.MutableSet[E]) bool

IsSubSet returns true if all elements of this set are in the other set.

func (*MutableComparableSet[E]) IsSuperSet

func (s *MutableComparableSet[E]) IsSuperSet(of ds.MutableSet[E]) bool

IsSuperSet returns true if all elements of the other set are in this set.

func (*MutableComparableSet[E]) Iter

func (s *MutableComparableSet[E]) Iter() iter.Seq[E]

Iter returns an iterator over all elements in the set.

func (*MutableComparableSet[E]) Iter2

func (s *MutableComparableSet[E]) Iter2() iter.Seq2[int, E]

Iter2 returns an iterator with index and element pairs.

func (*MutableComparableSet[E]) IterSubSets

func (s *MutableComparableSet[E]) IterSubSets() iter.Seq[ds.MutableSet[E]]

IterSubSets returns an iterator over all possible subsets of this set.

func (*MutableComparableSet[E]) List

func (s *MutableComparableSet[E]) List() []E

List returns a slice of all elements in the set.

func (*MutableComparableSet[E]) Remove

func (s *MutableComparableSet[E]) Remove(e E)

Remove removes an element from the set.

func (*MutableComparableSet[E]) RemoveAll

func (s *MutableComparableSet[E]) RemoveAll(es ...E)

RemoveAll removes multiple elements from the set.

func (*MutableComparableSet[_]) Size

func (s *MutableComparableSet[_]) Size() int

Size returns the number of elements in the set.

func (*MutableComparableSet[E]) SubSets

func (s *MutableComparableSet[E]) SubSets() []ds.MutableSet[E]

SubSets returns all possible subsets of this set (power set).

func (*MutableComparableSet[E]) SymmetricDifference

func (s *MutableComparableSet[E]) SymmetricDifference(other ds.MutableSet[E]) ds.MutableSet[E]

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

func (*MutableComparableSet[E]) Union

func (s *MutableComparableSet[E]) Union(other ds.MutableSet[E]) ds.MutableSet[E]

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

type MutableHashableSet

type MutableHashableSet[E ds.Hashable[E]] struct {
	// contains filtered or unexported fields
}

MutableHashableSet is a mutable hash set for hashable element types.

func (*MutableHashableSet[E]) Add

func (s *MutableHashableSet[E]) Add(e E)

Add adds an element to the set.

func (*MutableHashableSet[E]) AddAll

func (s *MutableHashableSet[E]) AddAll(es ...E)

AddAll adds multiple elements to the set.

func (*MutableHashableSet[_]) Cardinality

func (s *MutableHashableSet[_]) Cardinality() int

Cardinality returns the number of elements in the set.

func (*MutableHashableSet[E]) Clear

func (s *MutableHashableSet[E]) Clear()

Clear removes all elements from the set.

func (*MutableHashableSet[E]) Clone

func (s *MutableHashableSet[E]) Clone() ds.MutableSet[E]

Clone returns a mutable copy of this set.

func (*MutableHashableSet[E]) Contains

func (s *MutableHashableSet[E]) Contains(e E) bool

Contains returns true if the element is in the set.

func (*MutableHashableSet[E]) Difference

func (s *MutableHashableSet[E]) Difference(other ds.MutableSet[E]) ds.MutableSet[E]

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

func (*MutableHashableSet[E]) Equal

func (s *MutableHashableSet[E]) Equal(other ds.MutableSet[E]) bool

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

func (*MutableHashableSet[E]) Freeze

func (s *MutableHashableSet[E]) Freeze() ds.Set[E]

Freeze returns an immutable snapshot of this set.

func (*MutableHashableSet[E]) HashCode

func (s *MutableHashableSet[E]) HashCode() base.HashCode

HashCode computes and returns the hash code for this set. The hash code is computed by XORing the hash codes of all elements.

func (*MutableHashableSet[E]) Intersection

func (s *MutableHashableSet[E]) Intersection(other ds.MutableSet[E]) ds.MutableSet[E]

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

func (*MutableHashableSet[_]) IsEmpty

func (s *MutableHashableSet[_]) IsEmpty() bool

IsEmpty returns true if the set contains no elements.

func (*MutableHashableSet[E]) IsProperSubSet

func (s *MutableHashableSet[E]) IsProperSubSet(of ds.MutableSet[E]) bool

IsProperSubSet returns true if this is a subset of other and they are not equal.

func (*MutableHashableSet[E]) IsProperSuperSet

func (s *MutableHashableSet[E]) IsProperSuperSet(of ds.MutableSet[E]) bool

IsProperSuperSet returns true if this is a superset of other and they are not equal.

func (*MutableHashableSet[E]) IsSubSet

func (s *MutableHashableSet[E]) IsSubSet(of ds.MutableSet[E]) bool

IsSubSet returns true if all elements of this set are in the other set.

func (*MutableHashableSet[E]) IsSuperSet

func (s *MutableHashableSet[E]) IsSuperSet(of ds.MutableSet[E]) bool

IsSuperSet returns true if all elements of the other set are in this set.

func (*MutableHashableSet[E]) Iter

func (s *MutableHashableSet[E]) Iter() iter.Seq[E]

Iter returns an iterator over all elements in the set.

func (*MutableHashableSet[E]) Iter2

func (s *MutableHashableSet[E]) Iter2() iter.Seq2[int, E]

Iter2 returns an iterator with index and element pairs.

func (*MutableHashableSet[E]) IterSubSets

func (s *MutableHashableSet[E]) IterSubSets() iter.Seq[ds.MutableSet[E]]

IterSubSets returns an iterator over all possible subsets of this set.

func (*MutableHashableSet[E]) List

func (s *MutableHashableSet[E]) List() []E

List returns a slice of all elements in the set.

func (*MutableHashableSet[E]) Remove

func (s *MutableHashableSet[E]) Remove(e E)

Remove removes an element from the set.

func (*MutableHashableSet[E]) RemoveAll

func (s *MutableHashableSet[E]) RemoveAll(es ...E)

RemoveAll removes multiple elements from the set.

func (*MutableHashableSet[_]) Size

func (s *MutableHashableSet[_]) Size() int

Size returns the number of elements in the set.

func (*MutableHashableSet[E]) SubSets

func (s *MutableHashableSet[E]) SubSets() []ds.MutableSet[E]

SubSets returns all possible subsets of this set (power set).

func (*MutableHashableSet[E]) SymmetricDifference

func (s *MutableHashableSet[E]) SymmetricDifference(other ds.MutableSet[E]) ds.MutableSet[E]

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

func (*MutableHashableSet[E]) Union

func (s *MutableHashableSet[E]) Union(other ds.MutableSet[E]) ds.MutableSet[E]

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

Jump to

Keyboard shortcuts

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