maps

package
v1.2.117 Latest Latest
Warning

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

Go to latest
Published: May 13, 2024 License: MIT Imports: 1 Imported by: 2

Documentation

Overview

Package maps defines various functions useful with maps of any type.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Filter added in v1.2.59

func Filter[M ~map[K]V, K comparable, V comparable](m M) M

Filter returns a map satisfying c != zero within all c in the map. Filter modifies the contents of the map s; it does not create a new map.

func FilterFunc added in v1.2.59

func FilterFunc[M ~map[K]V, K comparable, V any](m M, f func(K, V) bool) M

FilterFunc returns a map satisfying f(c) within all c in the map. FilterFunc modifies the contents of the map s; it does not create a new map.

func Set added in v1.2.26

func Set[M map[K]struct{}, K comparable](ks ...K) (m M)

Set implements a non-thread safe Set

func Split added in v1.2.40

func Split[M ~map[K]V, K comparable, V any](m M, sep int) []M

Split slices s into all submaps separated by sep and returns a slice of the submaps between those separators.

If s is less than sep and sep is more than zero, Split returns a slice of length 1 whose only element is s.

If s is nil, Split returns nil (zero submaps).

If both s and sep are empty or zero, Split returns an empty slice.

If sep is <= zero, Split splits after each element, as chunk size is 1.

It is equivalent to SplitN with a count of -1.

func SplitN

func SplitN[M ~map[K]V, K comparable, V any](m M, sep int, n int) []M

SplitN slices s into submaps and returns a slice of the submaps.

The count determines the number of submaps to return:

  n > 0: at most n submaps; the last submaps will be the unsplit remainder.
		The count determines the number of submaps to return:
  		sep > 0: Split splits every sep as chunk size; the last submaps will be the unsplit remainder.
  		sep <= 0: take len(S)/n as chunk size
  n == 0: the result is nil (zero submaps)
  n < 0: all submaps as n == len(s)

Edge cases for s and sep (for example, zero) are handled as described in the documentation for Split.

func TypeAssertFilter added in v1.2.59

func TypeAssertFilter[M ~map[K]V, M2 ~map[K2]V2, K comparable, V comparable, K2 comparable, V2 comparable](m M) M2

TypeAssertFilter returns a map satisfying r, ok := any(c).(R); ok == true within all r in the map. TypeAssertFilter does not modify the contents of the map m; it creates a new map.

func TypeAssertFilterFunc added in v1.2.59

func TypeAssertFilterFunc[M ~map[K]V, M2 ~map[K2]V2, K comparable, V any, K2 comparable, V2 any](m M, f func(K, V) (K2, V2, bool)) M2

TypeAssertFilterFunc returns a map satisfying f(c) within all c in the map. TypeAssertFilterFunc does not modify the contents of the map m; it creates a new map.

Types

type NestedMap added in v1.2.67

type NestedMap[K comparable] map[K]any

A NestedMap is a map which, when it cannot find an element in itself, defers to another map. Modifications, however, are passed on to the supermap. It is used to implement nested namespaces, such as those which store local-variable bindings.

func (NestedMap[K]) CompareAndDelete added in v1.2.67

func (m NestedMap[K]) CompareAndDelete(keys []K, old any) (deleted bool)

CompareAndDelete deletes the entry for key if its value is equal to old. The old value must be of a comparable type.

If there is no current value for key in the map, CompareAndDelete returns false (even if the old value is the nil interface value).

func (NestedMap[K]) CompareAndSwap added in v1.2.67

func (m NestedMap[K]) CompareAndSwap(keys []K, old, new any) bool

CompareAndSwap swaps the old and new values for key if the value stored in the map is equal to old. The old value must be of a comparable type.

func (NestedMap[K]) Delete added in v1.2.67

func (m NestedMap[K]) Delete(keys []K)

Delete deletes the value for a key.

func (NestedMap[K]) Load added in v1.2.67

func (m NestedMap[K]) Load(keys []K) (value any, ok bool)

Load returns the value stored in the map for a key, or nil if no value is present. The ok result indicates whether value was found in the map.

func (NestedMap[K]) LoadAndDelete added in v1.2.67

func (m NestedMap[K]) LoadAndDelete(keys []K) (value any, loaded bool)

LoadAndDelete deletes the value for a key, returning the previous value if any. The loaded result reports whether the key was present.

func (NestedMap[K]) LoadOrStore added in v1.2.67

func (m NestedMap[K]) LoadOrStore(keys []K, value any) (actual any, loaded bool)

LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.

func (NestedMap[K]) Range added in v1.2.67

func (m NestedMap[K]) Range(f func(keys []K, value any) bool)

Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.

Range does not necessarily correspond to any consistent snapshot of the Map's contents: no key will be visited more than once, but if the value for any key is stored or deleted concurrently (including by f), Range may reflect any mapping for that key from any point during the Range call. Range does not block other methods on the receiver; even f itself may call any method on m.

Range may be O(N) with the number of elements in the map even if f returns false after a constant number of calls.

func (NestedMap[K]) Store added in v1.2.67

func (m NestedMap[K]) Store(keys []K, value any)

Store sets the value for a key.

func (NestedMap[K]) Swap added in v1.2.67

func (m NestedMap[K]) Swap(keys []K, value any) (previous any, loaded bool)

Swap swaps the value for a key and returns the previous value if any. The loaded result reports whether the key was present.

Jump to

Keyboard shortcuts

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