set

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package Set provides ordered and unordered set implementations for arbitrary comparable types.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddCheck

func AddCheck[T comparable](s Set[T], item T) bool

AddCheck adds an item from a set, returning a value if that item was already in the set.

func DeleteCheck

func DeleteCheck[T comparable](s Set[T], item T) bool

DeleteCheck deletes an item from a set, returning a value if that item was in the set.

func Equal

func Equal[T comparable](ctx context.Context, s1, s2 Set[T]) bool

Equal tests two sets, returning true if the sets have equal values, but will return early (and false) if the context is canceled.

func PopulateSet added in v0.7.0

func PopulateSet[T comparable](ctx context.Context, set Set[T], iter fun.Iterator[T])

PopulateSet adds all elements in the iterator to the provided Set.

Types

type Pair

type Pair[K comparable, V comparable] struct {
	Key   K
	Value V
}

Pair represents a key-value pair.

type Pairs

type Pairs[K comparable, V comparable] []Pair[K, V]

Pairs implements a collection of key-value pairs.

func MakePairs

func MakePairs[K comparable, V comparable](in map[K]V) Pairs[K, V]

MakePairs converts a map type into a slice of Pair types that can be usable in a set.

func (*Pairs[K, V]) Add

func (p *Pairs[K, V]) Add(k K, v V)

Add adds a new value to the underlying slice.

func (Pairs[K, V]) Append

func (p Pairs[K, V]) Append(new ...Pair[K, V]) Pairs[K, V]

Append, mirroring the semantics of the built in append() function adds one or more Pair items to a Pairs slice, and returns the new slice without changing the value of the original slice:

p = p.Append(pair, pare, pear)

func (Pairs[K, V]) Map

func (p Pairs[K, V]) Map() map[K]V

Map converts a list of pairs to the equivalent map. If there are duplicate keys in the Pairs list, only the first occurrence of the key is retained.

func (Pairs[K, V]) OrderedSet

func (p Pairs[K, V]) OrderedSet() Set[Pair[K, V]]

OrderedSet produces an order-preserving set based on the Pairs.

func (Pairs[K, V]) Set

func (p Pairs[K, V]) Set() Set[Pair[K, V]]

Set converts a Pairs object into a set.

type Set

type Set[T comparable] interface {
	// Add unconditionally adds an item to the set.
	Add(T)
	// Len Returns the number of items in the set
	Len() int
	// Delete removes an item from the set.
	Delete(T)
	// Check returns true if the item is in the set.
	Check(T) bool
	// Iterator produces an Iterator implementation for the
	// elements in the set.
	Iterator() fun.Iterator[T]
}

Set describes a basic set interface, and fun provdies a straightforward implementation backed by a `map[T]struct{}`, but other implementations are possible.

func BuildOrdered added in v0.7.0

func BuildOrdered[T comparable](ctx context.Context, iter fun.Iterator[T]) Set[T]

BuildOrdered creates an ordered set (new implementation) from the contents of the input iterator.

func BuildUnordered added in v0.7.0

func BuildUnordered[T comparable](ctx context.Context, iter fun.Iterator[T]) Set[T]

BuildUnordered produces a new unordered set from the elements in the iterator.

func MakeNewOrdered added in v0.3.0

func MakeNewOrdered[T comparable]() Set[T]

MakeNewOrdered constructs a "new" ordered set implementation. This is a more simple implementation based around a linked list, with identical semantics. Items are deleted from the list synchronously--the "old" implementation does a lazy deletion that batches--and this implementation may perform more predictably for use cases that do a large number of deletions.

This implementation does not permit iteration with concurrent deletes, as is the case with the old ordered implementation.

func MakeOrdered

func MakeOrdered[T comparable](size int) Set[T]

MakeOrdered produces an order-preserving set implementation, with pre-allocated capacity to the specified size. Iteration will reflect insertion order.

func MakeUnordered

func MakeUnordered[T comparable](len int) Set[T]

MakeUnordered constructs a set object, pre-allocating the specified length. Iteration order is randomized.

func NewOrdered

func NewOrdered[T comparable]() Set[T]

NewOrdered produces an order-preserving set implementation. Iteration order will reflect insertion order.

func NewUnordered

func NewUnordered[T comparable]() Set[T]

NewUnordered constructs a set object for the given type, without prealocation.

func Synchronize

func Synchronize[T comparable](s Set[T]) Set[T]

Synchronize wraps an existing set instance with a mutex. The underlying implementation provides an Unwrap method.

Jump to

Keyboard shortcuts

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