hashset

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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 New

func New[T comparable]() *HashSet[T]

Create a new HashSet.

func (*HashSet[T]) Add

func (set *HashSet[T]) Add(item T) bool

Add an item to the set. Returns true if the item was *not* already present.

func (*HashSet[T]) Contains

func (set *HashSet[T]) Contains(item T) bool

Checks if an item is already present in the set.

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

func (set *HashSet[T]) Iterator() iter.Seq[T]

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().

func (*HashSet[T]) Remove

func (set *HashSet[T]) Remove(item T) error

Remove an item from the set. Returns an error if the item is not contained in the set.

func (*HashSet[T]) Size

func (set *HashSet[T]) Size() int

Return the size of the set, the number of items contained.

Jump to

Keyboard shortcuts

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