collections

package
v0.58.1 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2025 License: MPL-2.0, MPL-2.0 Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Set

type Set[T any] struct {
	// contains filtered or unexported fields
}

Set represents an unordered set of values of a particular type.

A caller-provided "key function" defines how to produce a comparable unique key for each distinct value of type T.

Set operations are not concurrency-safe. Use external locking if multiple goroutines might modify the set concurrently or if one goroutine might read a set while another is modifying it.

func NewSet

func NewSet[T UniqueKeyer[T]](elems ...T) Set[T]

NewSet constructs a new set whose element type knows how to calculate its own unique keys, by implementing UniqueKeyer of itself.

func NewSetCmp

func NewSetCmp[T comparable](elems ...T) Set[T]

NewSetCmp constructs a new set for any comparable type, using the built-in == operator as the definition of element equivalence.

func NewSetFunc

func NewSetFunc[T any](keyFunc func(T) UniqueKey[T], elems ...T) Set[T]

NewSetFunc constructs a new set with the given "key function".

A valid key function must produce only values of types that can be compared for equality using the Go == operator, and must guarantee that each unique value of T has a corresponding key that uniquely identifies it. The implementer of the key function can decide what constitutes a "unique value of T", based on the meaning of type T.

func (Set[T]) Add

func (s Set[T]) Add(vs ...T)

Add inserts new members into the set.

If any existing member of the set is considered to be equivalent to a given value per the rules in the set's "key function", the old value will be discarded and replaced by the new value.

If multiple of the given arguments is considered to be equivalent then only the later one is retained.

func (Set[T]) All added in v0.55.0

func (s Set[T]) All() iter.Seq[T]

All returns an iterator over the elements of the set, in an unspecified order.

The result of this function is part of the internal state of the set and so callers MUST NOT modify it. If a caller is using locks to ensure safe concurrent access then any reads of the resulting map must be guarded by the same lock as would be used for other methods that read data from the set.

All returns an iterator over the elements of the set, in an unspecified order.

for elem := range set.All() {
	// do something with elem
}

Modifying the set during iteration causes unspecified results. Modifying the set concurrently with advancing the iterator causes undefined behavior including possible memory unsafety.

func (Set[T]) Has

func (s Set[T]) Has(v T) bool

Has returns true if the given value is present in the set, or false otherwise.

func (Set[T]) Len

func (s Set[T]) Len() int

Len returns the number of unique elements in the set.

func (Set[T]) Remove

func (s Set[T]) Remove(v T)

Remove removes the given member from the set, or does nothing if no equivalent value was present.

type UniqueKey

type UniqueKey[T any] interface {
	// Implementations must include an IsUniqueKey method with an empty body
	// just as a compile-time assertion that they are intended to behave as
	// unique keys for a particular other type.
	//
	// This method is never actually called by the collection types. Other
	// callers could potentially call it, but it would be strange and pointless
	// to do so.
	IsUniqueKey(T)
}

UniqueKey represents a value that is comparable and uniquely identifies another value of type T.

The Go type system offers no way to guarantee at compile time that implementations of this type are comparable, but if this interface is implemented by an uncomparable type then that will cause runtime panics when inserting elements into collection types that use unique keys.

We use this to help with correctness of the unique-key-generator callbacks used with the collection types in this package, so help with type parameter inference and to raise compile-time errors if an inappropriate callback is used as the key generator for a particular collection.

type UniqueKeyer

type UniqueKeyer[T any] interface {
	// UniqueKey returns the unique key of the receiver.
	//
	// A correct implementation of UniqueKey must return a distinct value
	// for each unique value of T, where the uniqueness of T values is decided
	// by the implementer. See [UniqueKey] for more information.
	//
	// Although not enforced directly by the Go type system, it doesn't make
	// sense for a type to implement [UniqueKeyer] for any type other than
	// itself. Such a nonsensical implementation will not be accepted by
	// functions like [NewSet] and [NewMap].
	UniqueKey() UniqueKey[T]
}

A UniqueKeyer is a type that knows how to calculate a unique key itself.

Jump to

Keyboard shortcuts

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