set

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2025 License: MIT Imports: 4 Imported by: 2

Documentation

Overview

Package set implements a generic programmatic Set data container.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrExist is returned by Set.Push when a matching entry is
	// already present.
	ErrExist = core.ErrExists
	// ErrNotExist is returned by Set.Pop and Set.Get when a matching entry
	// is not present.
	ErrNotExist = core.ErrNotExists
)

Functions

This section is empty.

Types

type Config

type Config[K, H comparable, T any] struct {
	// Hash computes the bucket identifier for the given K type
	Hash func(k K) (H, error)
	// ItemKey computes the K value from the T instance
	ItemKey func(v T) (K, error)
	// ItemMatch confirms the T instance matches the K value
	ItemMatch func(k K, v T) bool
}

Config defines the callbacks used by a Set.

func (Config[K, H, T]) Equal added in v0.1.4

func (cfg Config[K, H, T]) Equal(other Config[K, H, T]) bool

Equal determines if two Config instances use exactly the same callback functions by comparing their memory addresses using reflection. This is used to optimize Set operations like Copy() when configs are identical.

Equal isn't cheap but it saves Copy() from performing unnecessary rehashing.

func (Config[K, H, T]) Init

func (cfg Config[K, H, T]) Init(set *Set[K, H, T], items ...T) error

Init initializes a Set that wasn't created using Config.New.

func (Config[K, H, T]) Must

func (cfg Config[K, H, T]) Must(items ...T) *Set[K, H, T]

Must is equivalent to [New] but it panics on error.

func (Config[K, H, T]) New

func (cfg Config[K, H, T]) New(items ...T) (*Set[K, H, T], error)

New creates a Set based on the Config optionally receiving items. duplicates won't cause errors, just make sure to Set.Get the actually stored instances before assuming uniqueness.

func (Config[K, H, T]) Validate

func (cfg Config[K, H, T]) Validate() error

Validate confirms the Config is good for use.

type Set

type Set[K, H comparable, T any] struct {
	// contains filtered or unexported fields
}

Set implements a simple set using generics.

func (*Set[K, H, T]) Clone

func (set *Set[K, H, T]) Clone() *Set[K, H, T]

Clone creates a new Set containing the same values.

func (*Set[K, H, T]) Contains added in v0.1.2

func (set *Set[K, H, T]) Contains(key K) bool

Contains tells if there is a matching item in the Set. It returns false if: - the Set is nil or uninitialized - the key is invalid according to the Set's configuration - no matching item exists.

func (*Set[K, H, T]) Copy

func (set *Set[K, H, T]) Copy(dst *Set[K, H, T], cond func(T) bool) *Set[K, H, T]

Copy copies the values in the Set to another, optionally using a condition function.

If no destination is provided, one will be created. If an uninitialized destination is provided, it will be initialized using the source's Config and values copied in bulk.

func (*Set[K, H, T]) ForEach

func (set *Set[K, H, T]) ForEach(fn func(T) bool)

ForEach calls a function for each value in the Set until it returns false.

func (*Set[K, H, T]) Get

func (set *Set[K, H, T]) Get(key K) (T, error)

Get returns the item matching the key

func (*Set[K, H, T]) Pop

func (set *Set[K, H, T]) Pop(key K) (T, error)

Pop removes and return the item matching the given key from the Set.

func (*Set[K, H, T]) Push

func (set *Set[K, H, T]) Push(value T) (T, error)

Push adds entries to the set unless it already exist. It returns the value with matching key stored in the Set so it can be treated as a global reference.

func (*Set[K, H, T]) Reset

func (set *Set[K, H, T]) Reset() error

Reset removes all entires from the Set.

func (*Set[K, H, T]) Values

func (set *Set[K, H, T]) Values() []T

Values returns all values in the Set.

Jump to

Keyboard shortcuts

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