Documentation ¶
Overview ¶
Package hashset provides a hash bucket backed collection that contains an unordered collection of unique values.
Index ¶
- Variables
- type HashSet
- func (s *HashSet[T]) Add(value T) bool
- func (s *HashSet[T]) AddCollection(collection collections.Collection[T])
- func (s *HashSet[T]) AddRange(values []T)
- func (s *HashSet[T]) All(predicate functions.PredicateFunc[T]) bool
- func (s *HashSet[T]) Any(predicate functions.PredicateFunc[T]) bool
- func (s *HashSet[T]) Clear()
- func (s *HashSet[T]) Contains(value T) bool
- func (s *HashSet[T]) Count() int
- func (s *HashSet[T]) Difference(other sets.Set[T]) sets.Set[T]
- func (s *HashSet[T]) Find(predicate functions.PredicateFunc[T]) collections.Element[T]
- func (s *HashSet[T]) FindAll(predicate functions.PredicateFunc[T]) []collections.Element[T]
- func (s *HashSet[T]) ForEach(f func(collections.Element[T]))
- func (s *HashSet[T]) Get(value T) collections.Element[T]
- func (s *HashSet[T]) Intersection(other sets.Set[T]) sets.Set[T]
- func (s *HashSet[T]) IsEmpty() bool
- func (s *HashSet[T]) Iterator() collections.Iterator[T]
- func (s *HashSet[T]) Map(f func(T) T) collections.Collection[T]
- func (s *HashSet[T]) Max() T
- func (s *HashSet[T]) Min() T
- func (s *HashSet[T]) Remove(value T) bool
- func (s *HashSet[T]) Select(predicate functions.PredicateFunc[T]) collections.Collection[T]
- func (s *HashSet[T]) SelectDeep(predicate functions.PredicateFunc[T]) collections.Collection[T]
- func (s *HashSet[T]) String() string
- func (s *HashSet[T]) TakeWhile(predicate functions.PredicateFunc[T]) collections.Iterator[T]
- func (s *HashSet[T]) ToSlice() []T
- func (s *HashSet[T]) ToSliceDeep() []T
- func (*HashSet[T]) Type() collections.CollectionType
- func (s *HashSet[T]) Union(other sets.Set[T]) sets.Set[T]
- func (s *HashSet[T]) UnlockedContains(value T) bool
- type HashSetIterator
- type HashSetOptionFunc
- func WithCapacity[T any](capacity int) HashSetOptionFunc[T]
- func WithComparer[T any](comparer functions.ComparerFunc[T]) HashSetOptionFunc[T]
- func WithConcurrent[T any]() HashSetOptionFunc[T]
- func WithDeepCopy[T any](copier functions.DeepCopyFunc[T]) HashSetOptionFunc[T]
- func WithHashBucketCapacity[T any](bucketCapacity int) HashSetOptionFunc[T]
- func WithHasher[T any](hasher functions.HashFunc[T]) HashSetOptionFunc[T]
- func WithThreadSafe[T any]() HashSetOptionFunc[T]
Constants ¶
This section is empty.
Variables ¶
var HashBoolean = hashBoolean
HashBoolean is a variable containing a function that hashes a boolean value with the following signature
func (bool) uintptr
var HashByte = hashByte
HashByte is a variable containing a function that hashes an 8 bit integer value with the following signature
func (uint8) uintptr
var HashDword = hashDword
HashDword is a variable containing a function that hashes a 32 bit integer value with the following signature
func (unit32) uintptr
var HashFloat32 = hashFloat32
HashFloat32 is a variable containing a function that hashes a 32 bit float value with the following signature
func (float32) uintptr
var HashFloat64 = hashFloat64
HashFloat64 is a variable containing a function that hashes a 64 bit float value with the following signature
func (float64) uintptr
var HashQword = hashQword
HashDword is a variable containing a function that hashes a 64 bit integer value with the following signature
func (uint64) uintptr
var HashString = hashString
HashString is a variable containing a function that hashes a string value with the following signature
func (string) uintptr
var HashTime = hashTime
HashTime is a variable containing a function that hashes a time.Time struct with the following signature
func (time.Time) uintptr
var HashWord = hashWord
HashWord is a variable containing a function that hashes a 16 bit integer value with the following signature
func (uint16) uintptr
Functions ¶
This section is empty.
Types ¶
type HashSet ¶
type HashSet[T any] struct { local.InternalImpl // contains filtered or unexported fields }
HashSet stores an unordered collection of unique elements.
func New ¶
func New[T any](options ...HashSetOptionFunc[T]) *HashSet[T]
Constructs a new HashSet[T].
func (*HashSet[T]) Add ¶
Add adds a value into the set. Returns true if the value was added; else false if the value already exists in the set.
func (*HashSet[T]) AddCollection ¶
func (s *HashSet[T]) AddCollection(collection collections.Collection[T])
AddCollection inserts the values of the given collection into this set. Values are added in the order defined by the other collection.
func (*HashSet[T]) AddRange ¶
func (s *HashSet[T]) AddRange(values []T)
AddRange adds a slice of values to the set.
func (*HashSet[T]) All ¶
func (s *HashSet[T]) All(predicate functions.PredicateFunc[T]) bool
All applies the predicate function to every element in the collection, and returns true if all elements match the predicate.
func (*HashSet[T]) Any ¶
func (s *HashSet[T]) Any(predicate functions.PredicateFunc[T]) bool
Any returns true for the first element found where the predicate function returns true. It returns false if no element matches the predicate.
func (*HashSet[T]) Clear ¶
func (s *HashSet[T]) Clear()
Clear removes all values from the set, restoring it to its initial capacity.
func (*HashSet[T]) Contains ¶
Contains returns true if the given value exists in the set; else false
O(1) average and amortized. Up to O(n) if user hasher is poor.
func (*HashSet[T]) Difference ¶
Difference returns the difference between two sets. The new set consists of all elements that are in this set, but not other set.
The argument can be any implementation of Set[T]. The result is a new HashSet with the same properties as this one. Items are shallow-copied.
func (*HashSet[T]) Find ¶
func (s *HashSet[T]) Find(predicate functions.PredicateFunc[T]) collections.Element[T]
Find finds the first occurrence of an element matching the predicate.
The function returns nil if no match.
func (*HashSet[T]) FindAll ¶
func (s *HashSet[T]) FindAll(predicate functions.PredicateFunc[T]) []collections.Element[T]
FindAll finds all occurrences of an element matching the predicate.
The function returns an empty slice if none match.
func (*HashSet[T]) ForEach ¶
func (s *HashSet[T]) ForEach(f func(collections.Element[T]))
ForEach applies function f to all elements in the collection.
func (*HashSet[T]) Get ¶ added in v0.1.1
func (s *HashSet[T]) Get(value T) collections.Element[T]
Get returns the collection element that matches the given value, or nil if it is not found. Useful if the set contains struct elements you want to modify in-place.
func (*HashSet[T]) Intersection ¶
Intersection returns the intersection between two sets. The new set consists of all elements that are in both this set and the other.
The argument can be any implementation of Set[T]. The result is a new HashSet with the same properties as this one. Items are shallow-copied.
func (*HashSet[T]) Iterator ¶
func (s *HashSet[T]) Iterator() collections.Iterator[T]
Iterator returns a forward iterator that walks the set from first to last element
iter := set.Iterator() for e := iter.Start() ; e != nil; e = iter.Next() { // do something with e.Value() }
func (*HashSet[T]) Map ¶
func (s *HashSet[T]) Map(f func(T) T) collections.Collection[T]
Map applies function f to all elements in the collection and returns a new HashSet containing the result of f.
func (*HashSet[T]) Max ¶
func (s *HashSet[T]) Max() T
Max returns the maximum value in the collection according to the Comparer function.
func (*HashSet[T]) Min ¶
func (s *HashSet[T]) Min() T
Min returns the minimum value in the collection according to the Comparer function.
func (*HashSet[T]) Remove ¶
Remove removes a value from the set.
Returns true if the value was present and was removed; else false.
func (*HashSet[T]) Select ¶
func (s *HashSet[T]) Select(predicate functions.PredicateFunc[T]) collections.Collection[T]
Select returns a new HashSet containing only the items for which predicate is true.
func (*HashSet[T]) SelectDeep ¶
func (s *HashSet[T]) SelectDeep(predicate functions.PredicateFunc[T]) collections.Collection[T]
SelectDeep returns a new HashSet containing only the items for which predicate is true
Elements are deep copied to the new collection using the provided functions.DeepCopyFunc if any.
func (*HashSet[T]) TakeWhile ¶
func (s *HashSet[T]) TakeWhile(predicate functions.PredicateFunc[T]) collections.Iterator[T]
TakeWhile returns a forward iterater that walks the collection returning only those elements for which predicate returns true.
set := hashset.New[int]() // add values iter := set.TakeWhile(func (val int) bool { return val % 2 == 0 }) for e := iter.Start() ; e != nil; e = iter.Next() { // do something with e.Value() }
func (*HashSet[T]) ToSlice ¶
func (s *HashSet[T]) ToSlice() []T
ToSlice returns a copy of the set content as a slice.
func (*HashSet[T]) ToSliceDeep ¶
func (s *HashSet[T]) ToSliceDeep() []T
ToSliceDeep returns a copy of the set content as a slice using the provided functions.DeepCopyFunc if any.
func (*HashSet[T]) Type ¶
func (*HashSet[T]) Type() collections.CollectionType
Type returns the type of this collection.
func (*HashSet[T]) Union ¶
Union returns the union of two sets. The new set consists of all elements that are in both this and the other set.
The argument can be any implementation of Set[T]. The result is a new HashSet with the same properties as this one. Items are shallow-copied.
func (*HashSet[T]) UnlockedContains ¶
type HashSetIterator ¶
type HashSetIterator[T any] struct { util.IteratorBase[T] local.InternalImpl // contains filtered or unexported fields }
HashSetIterator implements an iterator over the elements in the set.
func (*HashSetIterator[T]) Next ¶
func (i *HashSetIterator[T]) Next() collections.Element[T]
Next returns the next element from the iterator, which will be nil if the end has been reached.
Panics if the underlying set is modified between calls to Next.
func (*HashSetIterator[T]) Start ¶
func (i *HashSetIterator[T]) Start() collections.Element[T]
Start begins iteration across the set returning the fisrt element, which will be nil if the set is empty.
Panics if the underlying set is modified between iteration creation and call to Start(),.
type HashSetOptionFunc ¶
Option function signature for HashSet contructor options.
func WithCapacity ¶
func WithCapacity[T any](capacity int) HashSetOptionFunc[T]
Option function for NewSet to set initial key capacity to something other than the default 16 elements. If you have some idea of hom many elements you will be storing, addition of values is approx 30% faster in a preallocated collection.
func WithComparer ¶
func WithComparer[T any](comparer functions.ComparerFunc[T]) HashSetOptionFunc[T]
Option function for NewSet to provide a comparer function for values of type T. Required if the element type is not numeric, bool, pointer or string.
func WithConcurrent ¶
func WithConcurrent[T any]() HashSetOptionFunc[T]
Option function to enable concurrency feature.
func WithDeepCopy ¶
func WithDeepCopy[T any](copier functions.DeepCopyFunc[T]) HashSetOptionFunc[T]
Option func to provide a deep copy implementation for collection elements.
func WithHashBucketCapacity ¶
func WithHashBucketCapacity[T any](bucketCapacity int) HashSetOptionFunc[T]
Option function for NewSet to set the initial hash bucket capacity associated with a new hash key. The default capacity is 2, which should be sufficient for the default hashing algorithms.
func WithHasher ¶
func WithHasher[T any](hasher functions.HashFunc[T]) HashSetOptionFunc[T]
Option function for NewSet to provide an alternative hash function for the type of values stored in the set. This is required for any type that is not one of the supported types. HashSet creation will panic if the type T is not one of the supported types and a hasher is not provided.
// Creates a HashSet[int] with a custom hasher. // Clearly _don't_ use a hash function like this :-) set := NewSet(WithHasher(func(v int) uintptr { return uintptr(v % 4) }))
func WithThreadSafe ¶
func WithThreadSafe[T any]() HashSetOptionFunc[T]
Option function for New to make the collection thread-safe. Adds overhead.