collections

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DiffOrderedMaps

func DiffOrderedMaps[K comparable, V comparable](m1 *OrderedMap[K, V], m2 *OrderedMap[K, V], onAdded func(key K, value V), onRemoved func(key K, value V), onModified func(key K, oldValue V, newValue V))

func DiffOrderedMapsFunc

func DiffOrderedMapsFunc[K comparable, V any](m1 *OrderedMap[K, V], m2 *OrderedMap[K, V], equalValues func(a, b V) bool, onAdded func(key K, value V), onRemoved func(key K, value V), onModified func(key K, oldValue V, newValue V))

Types

type CopyOnWriteMap added in v0.15.0

type CopyOnWriteMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

CopyOnWriteMap is a map that defers cloning of an inherited backing map until the first mutation, and supports nested scopes that share the parent's map for reads but get their own clone on write.

The zero value is an empty map ready to use.

func (*CopyOnWriteMap[K, V]) EnterScope added in v0.15.0

func (c *CopyOnWriteMap[K, V]) EnterScope() func()

EnterScope returns a function that restores this map to its current state. While the scope is active, the map shares its current backing storage with the parent scope: reads see the inherited entries, and the first mutation transparently clones the storage so the parent's view is not modified.

func (*CopyOnWriteMap[K, V]) Get added in v0.15.0

func (c *CopyOnWriteMap[K, V]) Get(k K) (V, bool)

Get returns the value for k and whether it was present.

func (*CopyOnWriteMap[K, V]) Has added in v0.15.0

func (c *CopyOnWriteMap[K, V]) Has(k K) bool

Has reports whether k is in the map.

func (*CopyOnWriteMap[K, V]) Set added in v0.15.0

func (c *CopyOnWriteMap[K, V]) Set(k K, v V)

Set assigns v to k, cloning the inherited backing map first if necessary.

type CopyOnWriteSet added in v0.15.0

type CopyOnWriteSet[K comparable] struct {
	// contains filtered or unexported fields
}

func (*CopyOnWriteSet[K]) Add added in v0.15.0

func (c *CopyOnWriteSet[K]) Add(k K)

Set adds k to the set, cloning the inherited backing map first if necessary.

func (*CopyOnWriteSet[K]) EnterScope added in v0.15.0

func (c *CopyOnWriteSet[K]) EnterScope() func()

EnterScope returns a function that restores this set to its current state. While the scope is active, the set shares its current backing storage with the parent scope: reads see the inherited entries, and the first mutation transparently clones the storage so the parent's view is not modified.

func (*CopyOnWriteSet[K]) Has added in v0.15.0

func (c *CopyOnWriteSet[K]) Has(k K) bool

Has reports whether k is in the set.

type MapEntry

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

type MultiMap

type MultiMap[K comparable, V comparable] struct {
	M map[K][]V
}

func GroupBy

func GroupBy[K comparable, V comparable](items []V, groupId func(V) K) *MultiMap[K, V]

func NewMultiMapWithSizeHint

func NewMultiMapWithSizeHint[K comparable, V comparable](hint int) *MultiMap[K, V]

func (*MultiMap[K, V]) Add

func (s *MultiMap[K, V]) Add(key K, value V)

func (*MultiMap[K, V]) Clear

func (s *MultiMap[K, V]) Clear()

func (*MultiMap[K, V]) Get

func (s *MultiMap[K, V]) Get(key K) []V

func (*MultiMap[K, V]) Has

func (s *MultiMap[K, V]) Has(key K) bool

func (*MultiMap[K, V]) Keys

func (s *MultiMap[K, V]) Keys() iter.Seq[K]

func (*MultiMap[K, V]) Len

func (s *MultiMap[K, V]) Len() int

func (*MultiMap[K, V]) Remove

func (s *MultiMap[K, V]) Remove(key K, value V)

func (*MultiMap[K, V]) RemoveAll

func (s *MultiMap[K, V]) RemoveAll(key K)

func (*MultiMap[K, V]) Values

func (s *MultiMap[K, V]) Values() iter.Seq[[]V]

type OrderedMap

type OrderedMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

OrderedMap is an insertion ordered map.

func NewOrderedMapFromList

func NewOrderedMapFromList[K comparable, V any](items []MapEntry[K, V]) *OrderedMap[K, V]

func NewOrderedMapWithSizeHint

func NewOrderedMapWithSizeHint[K comparable, V any](hint int) *OrderedMap[K, V]

NewOrderedMapWithSizeHint creates a new OrderedMap with a hint for the number of elements it will contain.

func (*OrderedMap[K, V]) Clear

func (m *OrderedMap[K, V]) Clear()

Clear removes all key-value pairs from the map. The space allocated for the map will be reused.

func (*OrderedMap[K, V]) Clone

func (m *OrderedMap[K, V]) Clone() *OrderedMap[K, V]

Clone returns a shallow copy of the map.

func (*OrderedMap[K, V]) Delete

func (m *OrderedMap[K, V]) Delete(key K) (V, bool)

Delete removes a key-value pair from the map.

func (*OrderedMap[K, V]) Entries

func (m *OrderedMap[K, V]) Entries() iter.Seq2[K, V]

Entries returns an iterator over the key-value pairs in the map.

func (*OrderedMap[K, V]) EntryAt

func (m *OrderedMap[K, V]) EntryAt(index int) (K, V, bool)

EntryAt retrieves the key-value pair at the specified index.

func (*OrderedMap[K, V]) Get

func (m *OrderedMap[K, V]) Get(key K) (V, bool)

Get retrieves a value from the map.

func (*OrderedMap[K, V]) GetOrZero

func (m *OrderedMap[K, V]) GetOrZero(key K) V

GetOrZero retrieves a value from the map, or returns the zero value of the value type if the key is not present.

func (*OrderedMap[K, V]) Has

func (m *OrderedMap[K, V]) Has(key K) bool

Has returns true if the map contains the key.

func (*OrderedMap[K, V]) Keys

func (m *OrderedMap[K, V]) Keys() iter.Seq[K]

Keys returns an iterator over the keys in the map. A slice of the keys can be obtained by calling `slices.Collect`.

func (*OrderedMap[K, V]) MarshalJSONTo

func (m *OrderedMap[K, V]) MarshalJSONTo(enc *jsontext.Encoder) error

func (*OrderedMap[K, V]) Set

func (m *OrderedMap[K, V]) Set(key K, value V)

Set sets a key-value pair in the map.

func (*OrderedMap[K, V]) Size

func (m *OrderedMap[K, V]) Size() int

Size returns the number of key-value pairs in the map.

func (*OrderedMap[K, V]) UnmarshalJSONFrom

func (m *OrderedMap[K, V]) UnmarshalJSONFrom(dec *jsontext.Decoder) error

func (*OrderedMap[K, V]) Values

func (m *OrderedMap[K, V]) Values() iter.Seq[V]

Values returns an iterator over the values in the map. A slice of the values can be obtained by calling `slices.Collect`.

type OrderedSet

type OrderedSet[T comparable] struct {
	// contains filtered or unexported fields
}

OrderedSet an insertion ordered set.

func NewOrderedSetWithSizeHint

func NewOrderedSetWithSizeHint[T comparable](hint int) *OrderedSet[T]

NewOrderedSetWithSizeHint creates a new OrderedSet with a hint for the number of elements it will contain.

func (*OrderedSet[T]) Add

func (s *OrderedSet[T]) Add(value T)

Add adds a value to the set.

func (*OrderedSet[T]) Clear

func (s *OrderedSet[T]) Clear()

Clear removes all elements from the set. The space allocated for the set will be reused.

func (*OrderedSet[T]) Clone

func (s *OrderedSet[T]) Clone() *OrderedSet[T]

Clone returns a shallow copy of the set.

func (*OrderedSet[T]) Delete

func (s *OrderedSet[T]) Delete(value T) bool

Delete removes a value from the set.

func (*OrderedSet[T]) Has

func (s *OrderedSet[T]) Has(value T) bool

Has returns true if the set contains the value.

func (*OrderedSet[T]) Size

func (s *OrderedSet[T]) Size() int

Size returns the number of elements in the set.

func (*OrderedSet[T]) Values

func (s *OrderedSet[T]) Values() iter.Seq[T]

Values returns an iterator over the values in the set.

type Set

type Set[T comparable] struct {
	M map[T]struct{}
}

func NewSetFromItems

func NewSetFromItems[T comparable](items ...T) *Set[T]

func NewSetWithSizeHint

func NewSetWithSizeHint[T comparable](hint int) *Set[T]

NewSetWithSizeHint creates a new Set with a hint for the number of elements it will contain.

func (*Set[T]) Add

func (s *Set[T]) Add(key T)

func (*Set[T]) AddIfAbsent

func (s *Set[T]) AddIfAbsent(key T) bool

Returns true if the key was not already present in the set.

func (*Set[T]) Clear

func (s *Set[T]) Clear()

func (*Set[T]) Clone

func (s *Set[T]) Clone() *Set[T]

func (*Set[T]) Delete

func (s *Set[T]) Delete(key T)

func (*Set[T]) Equals

func (s *Set[T]) Equals(other *Set[T]) bool

func (*Set[T]) Has

func (s *Set[T]) Has(key T) bool

func (*Set[T]) Intersects

func (s *Set[T]) Intersects(other *Set[T]) bool

func (*Set[T]) IsSubsetOf

func (s *Set[T]) IsSubsetOf(other *Set[T]) bool

func (*Set[T]) Keys

func (s *Set[T]) Keys() map[T]struct{}

func (*Set[T]) Len

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

func (*Set[T]) Union

func (s *Set[T]) Union(other *Set[T])

func (*Set[T]) UnionedWith

func (s *Set[T]) UnionedWith(other *Set[T]) *Set[T]

type SyncMap

type SyncMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func (*SyncMap[K, V]) Clear

func (s *SyncMap[K, V]) Clear()

func (*SyncMap[K, V]) Clone

func (s *SyncMap[K, V]) Clone() *SyncMap[K, V]

func (*SyncMap[K, V]) Delete

func (s *SyncMap[K, V]) Delete(key K)

func (*SyncMap[K, V]) Keys

func (s *SyncMap[K, V]) Keys() iter.Seq[K]

func (*SyncMap[K, V]) Load

func (s *SyncMap[K, V]) Load(key K) (value V, ok bool)

func (*SyncMap[K, V]) LoadOrStore

func (s *SyncMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)

func (*SyncMap[K, V]) Range

func (s *SyncMap[K, V]) Range(f func(key K, value V) bool)

func (*SyncMap[K, V]) Size

func (s *SyncMap[K, V]) Size() int

Size returns the approximate number of items in the map. Note that this is not a precise count, as the map may be modified concurrently while this method is running.

func (*SyncMap[K, V]) Store

func (s *SyncMap[K, V]) Store(key K, value V)

func (*SyncMap[K, V]) ToMap

func (s *SyncMap[K, V]) ToMap() map[K]V

type SyncSet

type SyncSet[T comparable] struct {
	// contains filtered or unexported fields
}

func (*SyncSet[T]) Add

func (s *SyncSet[T]) Add(key T)

func (*SyncSet[T]) AddIfAbsent

func (s *SyncSet[T]) AddIfAbsent(key T) bool

AddIfAbsent adds the key to the set if it is not already present using LoadOrStore. It returns true if the key was not already present (opposite of the return value of LoadOrStore).

func (*SyncSet[T]) Delete

func (s *SyncSet[T]) Delete(key T)

func (*SyncSet[T]) Has

func (s *SyncSet[T]) Has(key T) bool

func (*SyncSet[T]) IsEmpty added in v0.12.1

func (s *SyncSet[T]) IsEmpty() bool

func (*SyncSet[T]) Keys

func (s *SyncSet[T]) Keys() iter.Seq[T]

func (*SyncSet[T]) Range

func (s *SyncSet[T]) Range(fn func(key T) bool)

func (*SyncSet[T]) Size

func (s *SyncSet[T]) Size() int

Size returns the approximate number of items in the map. Note that this is not a precise count, as the map may be modified concurrently while this method is running.

func (*SyncSet[T]) ToSlice

func (s *SyncSet[T]) ToSlice() []T

Jump to

Keyboard shortcuts

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