hashset

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2024 License: MIT Imports: 15 Imported by: 0

README

HashSet

Interface Implementations

Interface Implemented
Collection[T]
Enumerable [T]
Iterable[T]
ReverseIterable[T]
Sortable[T]

Documentation

Overview

Package hashset provides a hash bucket backed collection that contains an unordered collection of unique values.

Index

Constants

This section is empty.

Variables

View Source
var HashBoolean = hashBoolean

HashBoolean is a variable containing a function that hashes a boolean value with the following signature

func (bool) uintptr
View Source
var HashByte = hashByte

HashByte is a variable containing a function that hashes an 8 bit integer value with the following signature

func (uint8) uintptr
View Source
var HashDword = hashDword

HashDword is a variable containing a function that hashes a 32 bit integer value with the following signature

func (unit32) uintptr
View Source
var HashFloat32 = hashFloat32

HashFloat32 is a variable containing a function that hashes a 32 bit float value with the following signature

func (float32) uintptr
View Source
var HashFloat64 = hashFloat64

HashFloat64 is a variable containing a function that hashes a 64 bit float value with the following signature

func (float64) uintptr
View Source
var HashQword = hashQword

HashDword is a variable containing a function that hashes a 64 bit integer value with the following signature

func (uint64) uintptr
View Source
var HashString = hashString

HashString is a variable containing a function that hashes a string value with the following signature

func (string) uintptr
View Source
var HashTime = hashTime

HashTime is a variable containing a function that hashes a time.Time struct with the following signature

func (time.Time) uintptr
View Source
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

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

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

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

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

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

Count returns the number of elements stored in the set.

func (*HashSet[T]) Difference

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

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

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

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

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

IsEmpty returns true if the collection has no elements.

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

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

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

func (s *HashSet[T]) String() string

String returns a string representation of container.

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

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

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

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

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

type HashSetOptionFunc[T any] func(*HashSet[T])

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.

Jump to

Keyboard shortcuts

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