Documentation
¶
Overview ¶
Package set provides generic Set data structures (collections of unique elements). It implements a HashSet, an ArraySet and a DynamicSet, with a common interface between them.
Index ¶
- Constants
- type ArraySet
- func (set *ArraySet[E]) Add(element E)
- func (set *ArraySet[E]) AddFromSet(otherSet ComparableSet[E])
- func (set *ArraySet[E]) AddFromSlice(elements []E)
- func (set *ArraySet[E]) AddMultiple(elements ...E)
- func (set ArraySet[E]) All() Iterator[E]
- func (set *ArraySet[E]) Clear()
- func (set ArraySet[E]) Contains(element E) bool
- func (set ArraySet[E]) Copy() Set[E]
- func (set ArraySet[E]) CopyArraySet() ArraySet[E]
- func (set ArraySet[E]) Equals(otherSet ComparableSet[E]) bool
- func (set ArraySet[E]) Intersection(otherSet ComparableSet[E]) Set[E]
- func (set ArraySet[E]) IntersectionArraySet(otherSet ComparableSet[E]) ArraySet[E]
- func (set ArraySet[E]) IsEmpty() bool
- func (set ArraySet[E]) IsSubsetOf(otherSet ComparableSet[E]) bool
- func (set ArraySet[E]) IsSupersetOf(otherSet ComparableSet[E]) bool
- func (set *ArraySet[E]) Remove(element E)
- func (set ArraySet[E]) Size() int
- func (set ArraySet[E]) String() string
- func (set ArraySet[E]) ToMap() map[E]struct{}
- func (set ArraySet[E]) ToSlice() []E
- func (set ArraySet[E]) Union(otherSet ComparableSet[E]) Set[E]
- func (set ArraySet[E]) UnionArraySet(otherSet ComparableSet[E]) ArraySet[E]
- type ComparableSet
- type DynamicSet
- func (set *DynamicSet[E]) Add(element E)
- func (set *DynamicSet[E]) AddFromSet(otherSet ComparableSet[E])
- func (set *DynamicSet[E]) AddFromSlice(elements []E)
- func (set *DynamicSet[E]) AddMultiple(elements ...E)
- func (set DynamicSet[E]) All() Iterator[E]
- func (set *DynamicSet[E]) Clear()
- func (set DynamicSet[E]) Contains(element E) bool
- func (set DynamicSet[E]) Copy() Set[E]
- func (set DynamicSet[E]) CopyDynamicSet() DynamicSet[E]
- func (set DynamicSet[E]) Equals(otherSet ComparableSet[E]) bool
- func (set DynamicSet[E]) Intersection(otherSet ComparableSet[E]) Set[E]
- func (set DynamicSet[E]) IntersectionDynamicSet(otherSet ComparableSet[E]) DynamicSet[E]
- func (set DynamicSet[E]) IsArraySet() bool
- func (set DynamicSet[E]) IsEmpty() bool
- func (set DynamicSet[E]) IsHashSet() bool
- func (set DynamicSet[E]) IsSubsetOf(otherSet ComparableSet[E]) bool
- func (set DynamicSet[E]) IsSupersetOf(otherSet ComparableSet[E]) bool
- func (set *DynamicSet[E]) Remove(element E)
- func (set *DynamicSet[E]) SetSizeThreshold(sizeThreshold int)
- func (set DynamicSet[E]) Size() int
- func (set DynamicSet[E]) SizeThreshold() int
- func (set DynamicSet[E]) String() string
- func (set DynamicSet[E]) ToMap() map[E]struct{}
- func (set DynamicSet[E]) ToSlice() []E
- func (set DynamicSet[E]) Union(otherSet ComparableSet[E]) Set[E]
- func (set DynamicSet[E]) UnionDynamicSet(otherSet ComparableSet[E]) DynamicSet[E]
- type HashSet
- func (set *HashSet[E]) Add(element E)
- func (set *HashSet[E]) AddFromSet(otherSet ComparableSet[E])
- func (set *HashSet[E]) AddFromSlice(elements []E)
- func (set *HashSet[E]) AddMultiple(elements ...E)
- func (set HashSet[E]) All() Iterator[E]
- func (set HashSet[E]) Clear()
- func (set HashSet[E]) Contains(element E) bool
- func (set HashSet[E]) Copy() Set[E]
- func (set HashSet[E]) CopyHashSet() HashSet[E]
- func (set HashSet[E]) Equals(otherSet ComparableSet[E]) bool
- func (set HashSet[E]) Intersection(otherSet ComparableSet[E]) Set[E]
- func (set HashSet[E]) IntersectionHashSet(otherSet ComparableSet[E]) HashSet[E]
- func (set HashSet[E]) IsEmpty() bool
- func (set HashSet[E]) IsSubsetOf(otherSet ComparableSet[E]) bool
- func (set HashSet[E]) IsSupersetOf(otherSet ComparableSet[E]) bool
- func (set HashSet[E]) Remove(element E)
- func (set HashSet[E]) Size() int
- func (set HashSet[E]) String() string
- func (set HashSet[E]) ToMap() map[E]struct{}
- func (set HashSet[E]) ToSlice() []E
- func (set HashSet[E]) Union(otherSet ComparableSet[E]) Set[E]
- func (set HashSet[E]) UnionHashSet(otherSet ComparableSet[E]) HashSet[E]
- type Iterator
- type Set
Constants ¶
const DefaultDynamicSetSizeThreshold = 20
DefaultDynamicSetSizeThreshold is the default size at which a DynamicSet will transform from an ArraySet to a HashSet. From the benchmarks in benchmark_test.go, it appears that 20 elements is around where HashSet.Contains performs better than ArraySet.Contains, though this varies by the element type of the set.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ArraySet ¶ added in v0.2.0
type ArraySet[E comparable] struct { // contains filtered or unexported fields }
An ArraySet is a collection of unique elements of type E. It uses an array as its backing storage, optimized for small sets (up to around 20 elements - see benchmark_test.go for benchmarks).
The zero value for an ArraySet is ready to use. It must not be copied after first use.
ArraySet implements Set when passed by pointer, and ComparableSet when passed by value.
func ArraySetFromSlice ¶ added in v0.2.0
func ArraySetFromSlice[E comparable](elements []E) ArraySet[E]
ArraySetFromSlice creates a new ArraySet from the elements in the given slice. It must not be copied after first use. Duplicate elements in the slice are added only once.
func ArraySetOf ¶ added in v0.2.0
func ArraySetOf[E comparable](elements ...E) ArraySet[E]
ArraySetOf creates a new ArraySet from the given elements. It must not be copied after first use. Duplicate elements are added only once.
func ArraySetWithCapacity ¶ added in v0.2.0
func ArraySetWithCapacity[E comparable](capacity int) ArraySet[E]
ArraySetWithCapacity creates a new ArraySet, with at least the given initial capacity. It must not be copied after first use.
func NewArraySet ¶ added in v0.2.0
func NewArraySet[E comparable]() ArraySet[E]
NewArraySet creates a new ArraySet for elements of type E. It must not be copied after first use.
func (*ArraySet[E]) Add ¶ added in v0.2.0
func (set *ArraySet[E]) Add(element E)
Add adds the given element to the set. If the element is already present in the set, Add is a no-op.
func (*ArraySet[E]) AddFromSet ¶ added in v0.2.0
func (set *ArraySet[E]) AddFromSet(otherSet ComparableSet[E])
AddFromSet adds elements from the given other set to the set.
func (*ArraySet[E]) AddFromSlice ¶ added in v0.2.0
func (set *ArraySet[E]) AddFromSlice(elements []E)
AddFromSlice adds the elements from the given slice to the set. Duplicate elements are added only once, and elements already present in the set are not added.
func (*ArraySet[E]) AddMultiple ¶ added in v0.2.0
func (set *ArraySet[E]) AddMultiple(elements ...E)
AddMultiple adds the given elements to the set. Duplicate elements are added only once, and elements already present in the set are not added.
func (ArraySet[E]) All ¶ added in v0.2.0
All returns an Iterator function, which when called will loop over the elements in the set and call the given yield function on each element. If yield returns false, iteration stops.
Since sets are unordered, iteration order is non-deterministic.
func (*ArraySet[E]) Clear ¶ added in v0.2.0
func (set *ArraySet[E]) Clear()
Clear removes all elements from the set, leaving an empty set with the same capacity as before.
func (ArraySet[E]) Contains ¶ added in v0.2.0
Contains checks if given element is present in the set.
func (ArraySet[E]) Copy ¶ added in v0.2.0
Copy creates a new set with all the same elements and capacity as the original set. The underlying type of the returned set is an *ArraySet - to get a value type, use ArraySet.CopyArraySet instead.
func (ArraySet[E]) CopyArraySet ¶ added in v0.2.0
CopyArraySet creates a new ArraySet with all the same elements and capacity as the original set.
func (ArraySet[E]) Equals ¶ added in v0.2.0
func (set ArraySet[E]) Equals(otherSet ComparableSet[E]) bool
Equals checks if the set contains exactly the same elements as the other given set.
func (ArraySet[E]) Intersection ¶ added in v0.2.0
func (set ArraySet[E]) Intersection(otherSet ComparableSet[E]) Set[E]
Intersection creates a new set with only the elements that exist in both the receiver set and the other given set. The underlying type of the returned set is an *ArraySet - to get a value type, use ArraySet.IntersectionArraySet instead.
func (ArraySet[E]) IntersectionArraySet ¶ added in v0.2.0
func (set ArraySet[E]) IntersectionArraySet(otherSet ComparableSet[E]) ArraySet[E]
IntersectionArraySet creates a new ArraySet with only the elements that exist in both the receiver set and the other given set.
func (ArraySet[E]) IsSubsetOf ¶ added in v0.2.0
func (set ArraySet[E]) IsSubsetOf(otherSet ComparableSet[E]) bool
IsSubsetOf checks if all of the elements in the set exist in the other given set.
func (ArraySet[E]) IsSupersetOf ¶ added in v0.2.0
func (set ArraySet[E]) IsSupersetOf(otherSet ComparableSet[E]) bool
IsSupersetOf checks if the set contains all of the elements in the other given set.
func (*ArraySet[E]) Remove ¶ added in v0.2.0
func (set *ArraySet[E]) Remove(element E)
Remove removes the given element from the set. If the element is not present in the set, Remove is a no-op.
func (ArraySet[E]) String ¶ added in v0.2.0
String returns a string representation of the set, implementing fmt.Stringer.
An ArraySet of elements 1, 2 and 3 will be printed as: ArraySet{1, 2, 3}
func (ArraySet[E]) ToMap ¶ added in v0.2.0
func (set ArraySet[E]) ToMap() map[E]struct{}
ToMap creates a map with all the set's elements as keys.
func (ArraySet[E]) ToSlice ¶ added in v0.2.0
func (set ArraySet[E]) ToSlice() []E
ToSlice returns a slice with all the elements in the set.
Mutating the slice may invalidate the set, since it uses the same backing storage. To avoid this, call CopyArraySet first.
func (ArraySet[E]) Union ¶ added in v0.2.0
func (set ArraySet[E]) Union(otherSet ComparableSet[E]) Set[E]
Union creates a new set that contains all the elements of the receiver set and the other given set. The underlying type of the returned set is an *ArraySet - to get a value type, use ArraySet.UnionArraySet instead.
func (ArraySet[E]) UnionArraySet ¶ added in v0.2.0
func (set ArraySet[E]) UnionArraySet(otherSet ComparableSet[E]) ArraySet[E]
UnionArraySet creates a new ArraySet that contains all the elements of the receiver set and the other given set.
type ComparableSet ¶ added in v0.2.0
type ComparableSet[E comparable] interface { // Contains checks if given element is present in the set. Contains(element E) bool // Size returns the number of elements in the set. Size() int // IsEmpty checks if there are 0 elements in the set. IsEmpty() bool // Equals checks if the set contains exactly the same elements as the other given set. Equals(otherSet ComparableSet[E]) bool // IsSubsetOf checks if all of the elements in the set exist in the other given set. IsSubsetOf(otherSet ComparableSet[E]) bool // IsSupersetOf checks if the set contains all of the elements in the other given set. IsSupersetOf(otherSet ComparableSet[E]) bool // Union creates a new set that contains all the elements of the receiver set and the other // given set. The underlying type of the returned set will be the same as the receiver. Union(otherSet ComparableSet[E]) Set[E] // Intersection creates a new set with only the elements that exist in both the receiver set and // the other given set. The underlying type of the returned set will be the same as the // receiver. Intersection(otherSet ComparableSet[E]) Set[E] // ToSlice returns a slice with all the elements in the set. // // Since sets are unordered, the order of elements in the slice is non-deterministic, and may // vary even when called multiple times on the same set. // // If the underlying set type is an ArraySet, the returned slice uses the same backing storage, // so mutating it may invalidate the set. To avoid this, call Copy first. ToSlice() []E // ToMap returns a map with all the set's elements as keys. // // If the underlying set type is a HashSet, the returned map is the backing storage for the set, // so mutating it will also mutate the set. To avoid this, call Copy first. ToMap() map[E]struct{} // Copy creates a new set with all the same elements as the original set, and the same // underlying type. Copy() Set[E] // String returns a string representation of the set, implementing [fmt.Stringer]. // // Since sets are unordered, the order of elements in the string may differ each time it is // called. String() string // All returns an [Iterator] function, which when called will loop over the elements in the set // and call the given yield function on each element. If yield returns false, iteration stops. // // Since sets are unordered, iteration order is non-deterministic. All() Iterator[E] }
A ComparableSet is the value type for a Set, containing only non-mutating methods. This allows passing an ArraySet, HashSet or DynamicSet by value, whereas the full Set interface is only implemented when passing them by pointer.
type DynamicSet ¶ added in v0.2.0
type DynamicSet[E comparable] struct { // contains filtered or unexported fields }
A DynamicSet is a collection of unique elements of type E. It starts out as an ArraySet, optimized for small sets. But as elements are added to it and it reaches a certain size threshold, it transforms itself to a HashSet, optimized for large sets.
The size threshold defaults to DefaultDynamicSetSizeThreshold, but can be customized with DynamicSet.SetSizeThreshold.
The zero value for a DynamicSet is ready to use. It must not be copied after first use.
DynamicSet implements Set when passed by pointer, and ComparableSet when passed by value.
func DynamicSetFromSlice ¶ added in v0.2.0
func DynamicSetFromSlice[E comparable](elements []E) DynamicSet[E]
DynamicSetFromSlice creates a new DynamicSet from the elements in the given slice. It must not be copied after first use. Duplicate elements in the slice are added only once.
func DynamicSetOf ¶ added in v0.2.0
func DynamicSetOf[E comparable](elements ...E) DynamicSet[E]
DynamicSetOf creates a new DynamicSet from the given elements. It must not be copied after first use. Duplicate elements are added only once.
func DynamicSetWithCapacity ¶ added in v0.2.0
func DynamicSetWithCapacity[E comparable](capacity int) DynamicSet[E]
DynamicSetWithCapacity creates a new DynamicSet, with at least the given initial capacity. It must not be copied after first use.
func NewDynamicSet ¶ added in v0.2.0
func NewDynamicSet[E comparable]() DynamicSet[E]
NewDynamicSet creates a new DynamicSet for elements of type E. It must not be copied after first use.
func (*DynamicSet[E]) Add ¶ added in v0.2.0
func (set *DynamicSet[E]) Add(element E)
Add adds the given element to the set. If the element is already present in the set, Add is a no-op.
If the DynamicSet is an ArraySet, it transforms to a HashSet if adding the element brings it above the set's size threshold.
func (*DynamicSet[E]) AddFromSet ¶ added in v0.2.0
func (set *DynamicSet[E]) AddFromSet(otherSet ComparableSet[E])
AddFromSet adds elements from the given other set to the set.
If the DynamicSet is an ArraySet, it transforms to a HashSet if adding the elements brings it above the set's size threshold.
func (*DynamicSet[E]) AddFromSlice ¶ added in v0.2.0
func (set *DynamicSet[E]) AddFromSlice(elements []E)
AddFromSlice adds the elements from the given slice to the set. Duplicate elements are added only once, and elements already present in the set are not added.
If the DynamicSet is an ArraySet, it transforms to a HashSet if adding the elements brings it above the set's size threshold.
func (*DynamicSet[E]) AddMultiple ¶ added in v0.2.0
func (set *DynamicSet[E]) AddMultiple(elements ...E)
AddMultiple adds the given elements to the set. Duplicate elements are added only once, and elements already present in the set are not added.
If the DynamicSet is an ArraySet, it transforms to a HashSet if adding the elements brings it above the set's size threshold.
func (DynamicSet[E]) All ¶ added in v0.2.0
func (set DynamicSet[E]) All() Iterator[E]
All returns an Iterator function, which when called will loop over the elements in the set and call the given yield function on each element. If yield returns false, iteration stops.
Since sets are unordered, iteration order is non-deterministic.
func (*DynamicSet[E]) Clear ¶ added in v0.2.0
func (set *DynamicSet[E]) Clear()
Clear removes all elements from the set.
func (DynamicSet[E]) Contains ¶ added in v0.2.0
func (set DynamicSet[E]) Contains(element E) bool
Contains checks if given element is present in the set.
func (DynamicSet[E]) Copy ¶ added in v0.2.0
func (set DynamicSet[E]) Copy() Set[E]
Copy creates a new set with all the same elements and capacity as the original set. The underlying type of the returned set is a *DynamicSet - to get a value type, use DynamicSet.CopyDynamicSet instead.
func (DynamicSet[E]) CopyDynamicSet ¶ added in v0.2.0
func (set DynamicSet[E]) CopyDynamicSet() DynamicSet[E]
CopyDynamicSet creates a new DynamicSet with all the same elements and capacity as the original set.
func (DynamicSet[E]) Equals ¶ added in v0.2.0
func (set DynamicSet[E]) Equals(otherSet ComparableSet[E]) bool
Equals checks if the set contains exactly the same elements as the other given set.
func (DynamicSet[E]) Intersection ¶ added in v0.2.0
func (set DynamicSet[E]) Intersection(otherSet ComparableSet[E]) Set[E]
Intersection creates a new set with only the elements that exist in both the receiver set and the other given set. The underlying type of the returned set is a *DynamicSet - to get a value type, use DynamicSet.IntersectionDynamicSet instead.
func (DynamicSet[E]) IntersectionDynamicSet ¶ added in v0.2.0
func (set DynamicSet[E]) IntersectionDynamicSet(otherSet ComparableSet[E]) DynamicSet[E]
IntersectionDynamicSet creates a new DynamicSet with only the elements that exist in both the receiver set and the other given set.
func (DynamicSet[E]) IsArraySet ¶ added in v0.2.0
func (set DynamicSet[E]) IsArraySet() bool
IsArraySet checks if the DynamicSet is an ArraySet internally, i.e. that it is yet to transform to a HashSet due to being below its size threshold.
func (DynamicSet[E]) IsEmpty ¶ added in v0.2.0
func (set DynamicSet[E]) IsEmpty() bool
IsEmpty checks if there are 0 elements in the set.
func (DynamicSet[E]) IsHashSet ¶ added in v0.2.0
func (set DynamicSet[E]) IsHashSet() bool
IsHashSet checks if the DynamicSet is a HashSet internally, i.e. that is has transformed after reaching its size threshold.
func (DynamicSet[E]) IsSubsetOf ¶ added in v0.2.0
func (set DynamicSet[E]) IsSubsetOf(otherSet ComparableSet[E]) bool
IsSubsetOf checks if all of the elements in the set exist in the other given set.
func (DynamicSet[E]) IsSupersetOf ¶ added in v0.2.0
func (set DynamicSet[E]) IsSupersetOf(otherSet ComparableSet[E]) bool
IsSupersetOf checks if the set contains all of the elements in the other given set.
func (*DynamicSet[E]) Remove ¶ added in v0.2.0
func (set *DynamicSet[E]) Remove(element E)
Remove removes the given element from the set. If the element is not present in the set, Remove is a no-op.
If the DynamicSet is a HashSet, it transforms to an ArraySet if adding the elements brings it below half the set's size threshold.
func (*DynamicSet[E]) SetSizeThreshold ¶ added in v0.2.0
func (set *DynamicSet[E]) SetSizeThreshold(sizeThreshold int)
SetSizeThreshold sets the size at which the DynamicSet will transform from an ArraySet to a HashSet. A size threshold of 0 is ignored.
If the set is an ArraySet above the given size threshold, it transforms to a HashSet immediately. If the set is a HashSet below the given size threshold, it transforms to an ArraySet.
func (DynamicSet[E]) Size ¶ added in v0.2.0
func (set DynamicSet[E]) Size() int
Size returns the number of elements in the set.
func (DynamicSet[E]) SizeThreshold ¶ added in v0.2.0
func (set DynamicSet[E]) SizeThreshold() int
SizeThreshold returns the size at which the DynamicSet will transform from an ArraySet to a HashSet.
func (DynamicSet[E]) String ¶ added in v0.2.0
func (set DynamicSet[E]) String() string
String returns a string representation of the set, implementing fmt.Stringer.
Since sets are unordered, the order of elements in the string may differ each time it is called.
A DynamicSet of elements 1, 2 and 3 will be printed as: DynamicSet{1, 2, 3} (though the order may vary).
func (DynamicSet[E]) ToMap ¶ added in v0.2.0
func (set DynamicSet[E]) ToMap() map[E]struct{}
ToMap returns a map with all the set's elements as keys.
If the underlying set type is a HashSet, the returned map is the backing storage for the set, so mutating it will also mutate the set. To avoid this, call CopyDynamicSet first.
func (DynamicSet[E]) ToSlice ¶ added in v0.2.0
func (set DynamicSet[E]) ToSlice() []E
ToSlice returns a slice with all the elements in the set.
Since sets are unordered, the order of elements in the slice is non-deterministic, and may vary even when called multiple times on the same set.
If the underlying set type is an ArraySet, the returned slice uses the same backing storage, so mutating it may invalidate the set. To avoid this, call CopyDynamicSet first.
func (DynamicSet[E]) Union ¶ added in v0.2.0
func (set DynamicSet[E]) Union(otherSet ComparableSet[E]) Set[E]
Union creates a new set that contains all the elements of the receiver set and the other given set. The underlying type of the returned set is a *DynamicSet - to get a value type, use DynamicSet.UnionDynamicSet instead.
func (DynamicSet[E]) UnionDynamicSet ¶ added in v0.2.0
func (set DynamicSet[E]) UnionDynamicSet(otherSet ComparableSet[E]) DynamicSet[E]
UnionDynamicSet creates a new DynamicSet that contains all the elements of the receiver set and the other given set.
type HashSet ¶ added in v0.2.0
type HashSet[E comparable] struct { // contains filtered or unexported fields }
A HashSet is an unordered collection of unique elements of type E. It uses a hashmap (with empty values) as its backing storage, optimized for large sets (around 20 elements or larger - see benchmark_test.go for benchmarks).
The zero value for a HashSet is ready to use. It must not be copied after first use.
HashSet implements Set when passed by pointer, and ComparableSet when passed by value.
func HashSetFromSlice ¶ added in v0.2.0
func HashSetFromSlice[E comparable](elements []E) HashSet[E]
HashSetFromSlice creates a new HashSet from the elements in the given slice. It must not be copied after first use. Duplicate elements in the slice are added only once.
func HashSetOf ¶ added in v0.2.0
func HashSetOf[E comparable](elements ...E) HashSet[E]
HashSetOf creates a new HashSet from the given elements. It must not be copied after first use. Duplicate elements are added only once.
func HashSetWithCapacity ¶ added in v0.2.0
func HashSetWithCapacity[E comparable](capacity int) HashSet[E]
HashSetWithCapacity creates a new HashSet, with at least the given initial capacity. It must not be copied after first use.
func NewHashSet ¶ added in v0.2.0
func NewHashSet[E comparable]() HashSet[E]
NewHashSet creates a new HashSet for elements of type E. It must not be copied after first use.
func (*HashSet[E]) Add ¶ added in v0.2.0
func (set *HashSet[E]) Add(element E)
Add adds the given element to the set. If the element is already present in the set, Add is a no-op.
If the hash set was not previously initialized through one of the constructors in this package, it will be initialized here.
func (*HashSet[E]) AddFromSet ¶ added in v0.2.0
func (set *HashSet[E]) AddFromSet(otherSet ComparableSet[E])
AddFromSet adds elements from the given other set to the set.
If the hash set was not previously initialized through one of the constructors in this package, it will be initialized here.
func (*HashSet[E]) AddFromSlice ¶ added in v0.2.0
func (set *HashSet[E]) AddFromSlice(elements []E)
AddFromSlice adds the elements from the given slice to the set. Duplicate elements are added only once, and elements already present in the set are not added.
If the hash set was not previously initialized through one of the constructors in this package, it will be initialized here.
func (*HashSet[E]) AddMultiple ¶ added in v0.2.0
func (set *HashSet[E]) AddMultiple(elements ...E)
AddMultiple adds the given elements to the set. Duplicate elements are added only once, and elements already present in the set are not added.
If the hash set was not previously initialized through one of the constructors in this package, it will be initialized here.
func (HashSet[E]) All ¶ added in v0.2.0
All returns an Iterator function, which when called will loop over the elements in the set and call the given yield function on each element. If yield returns false, iteration stops.
Since sets are unordered, iteration order is non-deterministic.
func (HashSet[E]) Clear ¶ added in v0.2.0
func (set HashSet[E]) Clear()
Clear removes all elements from the set, leaving an empty set with the same capacity as before.
func (HashSet[E]) Contains ¶ added in v0.2.0
Contains checks if given element is present in the set.
func (HashSet[E]) Copy ¶ added in v0.2.0
Copy creates a new set with all the same elements and capacity as the original set. The underlying type of the returned set is a *HashSet - to get a value type, use HashSet.CopyHashSet instead.
func (HashSet[E]) CopyHashSet ¶ added in v0.2.0
CopyHashSet creates a new HashSet with all the same elements and capacity as the original set.
func (HashSet[E]) Equals ¶ added in v0.2.0
func (set HashSet[E]) Equals(otherSet ComparableSet[E]) bool
Equals checks if the set contains exactly the same elements as the other given set.
func (HashSet[E]) Intersection ¶ added in v0.2.0
func (set HashSet[E]) Intersection(otherSet ComparableSet[E]) Set[E]
Intersection creates a new set with only the elements that exist in both the receiver set and the other given set. The underlying type of the returned set is a *HashSet - to get a value type, use HashSet.IntersectionHashSet instead.
func (HashSet[E]) IntersectionHashSet ¶ added in v0.2.0
func (set HashSet[E]) IntersectionHashSet(otherSet ComparableSet[E]) HashSet[E]
IntersectionHashSet creates a new HashSet with only the elements that exist in both the receiver set and the other given set.
func (HashSet[E]) IsSubsetOf ¶ added in v0.2.0
func (set HashSet[E]) IsSubsetOf(otherSet ComparableSet[E]) bool
IsSubsetOf checks if all of the elements in the set exist in the other given set.
func (HashSet[E]) IsSupersetOf ¶ added in v0.2.0
func (set HashSet[E]) IsSupersetOf(otherSet ComparableSet[E]) bool
IsSupersetOf checks if the set contains all of the elements in the other given set.
func (HashSet[E]) Remove ¶ added in v0.2.0
func (set HashSet[E]) Remove(element E)
Remove removes the given element from the set. If the element is not present in the set, Remove is a no-op.
func (HashSet[E]) String ¶ added in v0.2.0
String returns a string representation of the set, implementing fmt.Stringer.
Since sets are unordered, the order of elements in the string may differ each time it is called.
A HashSet of elements 1, 2 and 3 will be printed as: HashSet{1, 2, 3} (though the order may vary).
func (HashSet[E]) ToMap ¶ added in v0.2.0
func (set HashSet[E]) ToMap() map[E]struct{}
ToMap returns a map with all the set's elements as keys.
Mutating the map will also mutate the set, since it uses the same backing storage. To avoid this, call CopyHashSet first.
func (HashSet[E]) ToSlice ¶ added in v0.2.0
func (set HashSet[E]) ToSlice() []E
ToSlice creates a slice with all the elements in the set.
Since sets are unordered, the order of elements in the slice is non-deterministic, and may vary even when called multiple times on the same set.
func (HashSet[E]) Union ¶ added in v0.2.0
func (set HashSet[E]) Union(otherSet ComparableSet[E]) Set[E]
Union creates a new set that contains all the elements of the receiver set and the other given set. The underlying type of the returned set is a *HashSet - to get a value type, use HashSet.UnionHashSet instead.
func (HashSet[E]) UnionHashSet ¶ added in v0.2.0
func (set HashSet[E]) UnionHashSet(otherSet ComparableSet[E]) HashSet[E]
UnionHashSet creates a new HashSet that contains all the elements of the receiver set and the other given set.
type Iterator ¶ added in v0.2.0
type Iterator[E comparable] func(yield func(element E) (continueIteration bool))
Iterator aims to satisfy the planned signature for range over func in Go, allowing iteration over sets like this in the future:
for element := range mySet.All() { fmt.Println(element) }
type Set ¶
type Set[E comparable] interface { ComparableSet[E] // Add adds the given element to the set. // If the element is already present in the set, Add is a no-op. Add(element E) // AddMultiple adds the given elements to the set. Duplicate elements are added only once, and // elements already present in the set are not added. AddMultiple(elements ...E) // AddFromSlice adds the elements from the given slice to the set. Duplicate elements are added // only once, and elements already present in the set are not added. AddFromSlice(elements []E) // AddFromSet adds elements from the given other set to the set. AddFromSet(otherSet ComparableSet[E]) // Remove removes the given element from the set. // If the element is not present in the set, Remove is a no-op. Remove(element E) // Clear removes all elements from the set. When possible, it will retain the same capacity as // before. Clear() }
A Set is an unordered collection of unique elements of type E.
Three types in this package implement Set:
- ArraySet uses an array as its backing storage, optimized for small sets
- HashSet uses a hashmap (with empty values) as its backing storage, optimized for large sets
- DynamicSet starts out as an ArraySet, but transforms itself to a HashSet once it reaches a size threshold