Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var (
ErrorItemNotContained = errors.New("item not in set")
)
Functions ¶
func Apply ¶ added in v1.1.0
func Apply[T comparable](set *HashSet[T], f func(item T))
Iterate over the items of the hash set and apply a function to each item.
Idiomatic Go should likely use Iterator() rather than functional methods.
BEWARE: Iteration over a hashset does not guarantee a specific order --- you may find elements in any order, not the order they were inserted! Ensure your function accounts for this.
To accumulate values over items, use Fold.
func Fold ¶ added in v1.1.0
func Fold[T comparable, G any](set *HashSet[T], initialAccumulator G, f func(item T, accumulator G) G) G
Iterate over set items and apply the function f. The function f also takes the current value of the accumulator. The results of f become the new value of the accumulator at each step.
Idiomatic Go should likely use Iterator() rather than functional methods.
BEWARE: Iteration over a hashset does not guarantee a specific order --- you may find elements in any order, not the order they were inserted! Ensure your function accounts for this. This is especially important for a fold!
This function is not a method on HashSet to allow for generic accumulators.
Types ¶
type HashSet ¶
type HashSet[T comparable] struct { // contains filtered or unexported fields }
An implementation of a set using maps as the underlying data structure.
func (*HashSet[T]) Add ¶
Add an item to the set. Returns true if the item was *not* already present.
func (*HashSet[T]) Items ¶ added in v1.1.0
func (set *HashSet[T]) Items() []T
Get all items from the hashset. This method allocates an array of length equal to the number of items. The items are not guaranteed to be in the order they were inserted into the hashset.
func (*HashSet[T]) Iterator ¶ added in v1.2.0
Iterate over the items of the hashset. Note the iteration order may not be the insertion order. This method is not concurrency safe. For concurrent applications, consider using a mutex, or pull the data out using Items().