sync

package
v2.6.0 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2022 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Map

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

Map is like a Go map[interface{}]interface{} but is safe for concurrent use by multiple goroutines.

func NewMap

func NewMap[K comparable, V any]() *Map[K, V]

NewMap creates map.

func (*Map[K, V]) CopyData

func (m *Map[K, V]) CopyData() map[K]V

CopyData creates a deep copy of the internal map.

func (*Map[K, V]) Delete

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

Delete deletes the value for the key.

func (*Map[K, V]) DeleteWithFunc

func (m *Map[K, V]) DeleteWithFunc(key K, onDeleteFunc func(value V))

DeleteWithFunc removes the key from the map and if a value existed invokes the onDeleteFunc callback on the removed value.

The onReplaceFunc callback is invoked under a write lock.

func (*Map[K, V]) Length

func (m *Map[K, V]) Length() int

Length returns number of stored values.

func (*Map[K, V]) Load

func (m *Map[K, V]) Load(key K) (V, bool)

Load returns the value stored in the map for a key, or nil if no value is present. The loaded value is read-only and should not be modified. The ok result indicates whether value was found in the map.

func (*Map[K, V]) LoadOrStore

func (m *Map[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)

LoadOrStore returns the existing value for the key if present. The loaded value is read-only and should not be modified. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.

func (*Map[K, V]) LoadOrStoreWithFunc

func (m *Map[K, V]) LoadOrStoreWithFunc(key K, onLoadFunc func(value V) V, createFunc func() V) (actual V, loaded bool)

LoadOrStoreWithFunc loads an existing element from the map or creates a new element and stores it in the map

The onLoadFunc or createFunc are invoked under a write lock.

func (*Map[K, V]) LoadWithFunc

func (m *Map[K, V]) LoadWithFunc(key K, onLoadFunc func(value V) V) (V, bool)

LoadWithFunc tries to load element for key from the map, if it exists then the onload functions is invoked on it.

The onLoadFunc is invoked under a read lock.

func (*Map[K, V]) PullOut

func (m *Map[K, V]) PullOut(key K) (V, bool)

PullOut loads and deletes the value for the key.

func (*Map[K, V]) PullOutAll

func (m *Map[K, V]) PullOutAll() map[K]V

PullOutAll extracts internal map data and replace it with empty map.

func (*Map[K, V]) PullOutWithFunc

func (m *Map[K, V]) PullOutWithFunc(key K, onLoadFunc func(value V) V) (V, bool)

DeleteWithFunc removes the key from the map and if a value existed invokes the onLoadFunc callback on the removed value.

The onReplaceFunc callback is invoked under a write lock.

func (*Map[K, V]) Range

func (m *Map[K, V]) Range(f func(key K, value V) 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 copy the whole map, instead the read lock is locked on iteration of the map, and unlocked before f is called.

func (*Map[K, V]) Range2

func (m *Map[K, V]) Range2(f func(key K, value V) bool)

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

Range2 differs from Range by keepting a read lock locked during the whole call.

func (*Map[K, V]) Replace

func (m *Map[K, V]) Replace(key K, value V) (oldValue V, oldLoaded bool)

Replace replaces the existing value with a new value and returns old value for the key.

func (*Map[K, V]) ReplaceWithFunc

func (m *Map[K, V]) ReplaceWithFunc(key K, onReplaceFunc func(oldValue V, oldLoaded bool) (newValue V, doDelete bool)) (oldValue V, oldLoaded bool)

ReplaceWithFunc checks whether key exists in the map, invokes the onReplaceFunc callback on the pair (value, ok) and either deletes or stores the element in the map based on the returned values from the onReplaceFunc callback.

The onReplaceFunc callback is invoked under a write lock.

func (*Map[K, V]) Store

func (m *Map[K, V]) Store(key K, value V)

Store sets the value for a key.

func (*Map[K, V]) StoreWithFunc

func (m *Map[K, V]) StoreWithFunc(key K, createFunc func() V)

StoreWithFunc creates a new element and stores it in the map under the given key.

The createFunc is invoked under a write lock.

Jump to

Keyboard shortcuts

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