pmaps

package
v0.4.117 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2023 License: ISC Imports: 11 Imported by: 1

Documentation

Overview

KeyInsOrderedMap is a mapping whose keys are provided in insertion order.

OrderedMap is a mapping whose values are provided in order.

RWMap is a one-liner thread-safe mapping. RWMap implements parli.ThreadSafeMap[K comparable, V any].

ThreadSafeOrderedMapFunc is a mapping whose values are provided in custom order. Thread-safe.

KeyByValueMap is a mapping whose keys are provided in value order.

KeyInsOrderedMap is a mapping whose keys are provided in insertion order.

Index

Constants

View Source
const (
	// with [ThreadSafeMap.Delete] sets the mapping value to the
	// zero-value prior to delete
	SetZeroValue = true
	// with [ThreadSafeMap.Clear], the map is cleared using range
	// and delete of all keys rather than re-created
	RangeDelete = true
)
View Source
const (
	BtreeDegree = 6 // each level has 2^6 children: 64
)

Variables

This section is empty.

Functions

func GoMapSize added in v0.4.75

func GoMapSize[K comparable, V any](m map[K]V) (size uint64)

GoMapSize returns the current size of the bucket array of Go map m

  • size is 0 for a nil map
  • size is 1 for an unallocated hash-table — rare case
  • otherwise size is a power of 2

About Go map:

  • Go map is a hash map
  • a hash table is a space-time trade-off compared to array access
  • size is how many slots m’s hash table currently has
  • size may grow or shrink as m is modified
  • a mapping of the hash value-space is used for hash-table array access
  • each map slot contains a linked list of key-value pairs
  • more slots is faster closing in on O(1) complexity, fewer slots saves memory
  • Load factor is number of hash-table entries including collisions divided by hash table size

Source code:

  • the map source code part of the runtime package is available online:
  • https://go.googlesource.com/go/+/refs/heads/master/src/runtime/map.go
  • runtime source is typically installed on a computer that has Go:
  • — module directory: …libexec/src, package directory: runtime
  • — on macOS homebrew similar to: …/homebrew/Cellar/go/1.20.2/libexec/src

func Less added in v0.4.117

func Less[V constraints.Ordered](a, b V) (aBeforeB bool)

Less is btree.LessFunc for ordered values

func NewRWMap

func NewRWMap[K comparable, V any]() (rwMap parli.ThreadSafeMap[K, V])

NewRWMap returns a thread-safe map implementation

Types

type BTreeMap added in v0.4.117

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

BTreeMap is a reusable and promotable mapping whose values are provided in custom order

  • mapping implementation is Go Map
  • ordering structure is B-tree
  • B-tree offers:
  • — avoiding vector-copy of large sorted slices which is slow and
  • — avoiding linear traversal of linked-lists which is slow and
  • — is a more efficient structure than binary tree
  • Put is implemented by consumers that can compare V values

func NewBTreeMap added in v0.4.117

func NewBTreeMap[K comparable, V btree.Ordered]() (orderedMap *BTreeMap[K, V])

NewBTreeMap returns a mapping whose values are provided in custom order

  • btree.Ordered does not include ~uintptr

func NewBTreeMapAny added in v0.4.117

func NewBTreeMapAny[K comparable, V any](less btree.LessFunc[V]) (orderedMap *BTreeMap[K, V])

NewBTreeMapAny returns a mapping whose values are provided in custom order

  • for uintptr

func (*BTreeMap[K, V]) Clear added in v0.4.117

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

Clear empties the map

  • clears by re-initializing the map
  • when instead ranging and deleting all keys, the unused size of the map is retained

func (*BTreeMap[K, V]) Clone added in v0.4.117

func (m *BTreeMap[K, V]) Clone() (clone *BTreeMap[K, V])

Clone returns a shallow clone of the map

  • clone is done by ranging all keys

func (*BTreeMap[K, V]) Delete added in v0.4.117

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

Delete removes mapping using key K.

  • if key K is not mapped, the map is unchanged.
  • O(log n)

func (*BTreeMap) Get added in v0.4.117

func (m *BTreeMap) Get(key K) (value V, ok bool)

func (*BTreeMap) Length added in v0.4.117

func (m *BTreeMap) Length() (length int)

func (*BTreeMap[K, V]) List added in v0.4.117

func (m *BTreeMap[K, V]) List(n ...int) (list []V)

List provides mapped values in order

  • n zero or missing means all items
  • n non-zero means this many items capped by length

func (*BTreeMap) Range added in v0.4.117

func (m *BTreeMap) Range(rangeFunc func(key K, value V) (keepGoing bool))

type BtreeIterator added in v0.4.107

type BtreeIterator[V any] struct {
	// contains filtered or unexported fields
}

BtreeIterator retrieves B-tree values in order

func NewBtreeIterator added in v0.4.107

func NewBtreeIterator[V any](tree *btree.BTreeG[V]) (iterator *BtreeIterator[V])

NewBtreeIterator returns an object that can retrieve elements in order

func (*BtreeIterator[V]) Iterate added in v0.4.107

func (b *BtreeIterator[V]) Iterate(n int) (list []V)

Iterate returns a sorted list of the first n elements

type BtreeOrdered added in v0.4.117

type BtreeOrdered interface {
	// ~int | ~int8 | ~int16 | ~int32 | ~int64 |
	// ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 |
	// ~float32 | ~float64 | ~string
	btree.Ordered
}

btree.Ordered does not include ~uintptr

type InsOrderedMap added in v0.4.83

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

InsOrderedMap is a mapping whose values are provided in insertion order.

func NewInsOrderedMap added in v0.4.83

func NewInsOrderedMap[K comparable, V any]() (orderedMap *InsOrderedMap[K, V])

NewInsOrderedMap is a mapping whose keys are provided in insertion order.

func (*InsOrderedMap[K, V]) Clone added in v0.4.83

func (mp *InsOrderedMap[K, V]) Clone() (clone *InsOrderedMap[K, V])

Clone returns a shallow clone of the map

func (*InsOrderedMap[K, V]) Delete added in v0.4.83

func (mp *InsOrderedMap[K, V]) Delete(key K)

Delete removes mapping using key K.

  • if key K is not mapped, the map is unchanged.
  • O(log n)

func (*InsOrderedMap[K, V]) Dump added in v0.4.85

func (mp *InsOrderedMap[K, V]) Dump() (s string)

func (*InsOrderedMap[K, V]) List added in v0.4.83

func (mp *InsOrderedMap[K, V]) List(n ...int) (list []V)

List provides the mapped values in order

  • O(n)

func (*InsOrderedMap[K, V]) Put added in v0.4.83

func (mp *InsOrderedMap[K, V]) Put(key K, value V)

Put saves or replaces a mapping

type KeyOrderedMap added in v0.4.32

type KeyOrderedMap[K constraints.Ordered, V any] struct {
	// contains filtered or unexported fields
}

KeyOrderedMap is a mapping whose keys are provided in order

  • native Go Map functions: Get Put Delete Length Range
  • convenience methods: Clear Clone
  • order method: List
  • — those methods are implemented because they require access to the underlying Go map
  • mapping implementation is Go Map
  • ordering structure is B-tree
  • B-tree offers:
  • — avoiding vector-copy of large sorted slices which is slow and
  • — avoiding linear traversal of linked-lists which is slow and
  • — is a more efficient structure than binary tree

func NewKeyOrderedMap added in v0.4.32

func NewKeyOrderedMap[K btree.Ordered, V any]() (orderedMap *KeyOrderedMap[K, V])

NewKeyOrderedMap returns a mapping whose keys are provided in order.

func NewKeyOrderedMapOrdered added in v0.4.117

func NewKeyOrderedMapOrdered[K constraints.Ordered, V any]() (orderedMap *KeyOrderedMap[K, V])

NewKeyOrderedMap returns a mapping whose keys are provided in order.

func (*KeyOrderedMap[K, V]) Clear added in v0.4.117

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

func (*KeyOrderedMap[K, V]) Clone added in v0.4.32

func (m *KeyOrderedMap[K, V]) Clone() (clone *KeyOrderedMap[K, V])

Clone returns a shallow clone of the map

func (*KeyOrderedMap[K, V]) Delete added in v0.4.32

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

func (*KeyOrderedMap) Get added in v0.4.117

func (m *KeyOrderedMap) Get(key K) (value V, ok bool)

func (*KeyOrderedMap) Length added in v0.4.117

func (m *KeyOrderedMap) Length() (length int)

func (*KeyOrderedMap[K, V]) List added in v0.4.32

func (m *KeyOrderedMap[K, V]) List(n ...int) (list []K)

List provides mapped values in order

  • n zero or missing means all items
  • n non-zero means this many items capped by length

func (*KeyOrderedMap[K, V]) Put added in v0.4.32

func (m *KeyOrderedMap[K, V]) Put(key K, value V)

func (*KeyOrderedMap) Range added in v0.4.117

func (m *KeyOrderedMap) Range(rangeFunc func(key K, value V) (keepGoing bool))

type Map added in v0.4.32

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

Map is a Go Map as a reusable promotable field

  • native Go Map functions: Get Put Delete Length Range
  • convenience functions: Clear Clone
  • — those methods are implemented because they require access to the underlying Go map

func NewMap added in v0.4.32

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

NewMap returns a resusable Go Map object

func (*Map[K, V]) Clear added in v0.4.32

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

Clear empties the map

  • clears by re-initializing the map
  • when instead ranging and deleting all keys, the unused size of the map is retained

func (*Map[K, V]) Clone added in v0.4.32

func (m *Map[K, V]) Clone(mp ...*Map[K, V]) (clone *Map[K, V])

Clone returns a shallow clone of the map

  • mp is an optional pointer to an already allocated map instance to be used
  • clone is done by ranging all keys

func (*Map[K, V]) Delete added in v0.4.32

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

Delete removes mapping for key

  • if key is not mapped, the map is unchanged.
  • O(log n)

func (*Map[K, V]) Get added in v0.4.32

func (m *Map[K, V]) Get(key K) (value V, ok bool)

Get returns the value mapped by key or the V zero-value otherwise

  • ok: true if a mapping was found
  • O(1)

func (*Map[K, V]) Length added in v0.4.32

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

Length returns the number of mappings

func (*Map[K, V]) Put added in v0.4.32

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

Put create or replaces a mapping

func (*Map[K, V]) Range added in v0.4.94

func (m *Map[K, V]) Range(rangeFunc func(key K, value V) (keepGoing bool))

Range traverses map bindings

  • iterates over map until rangeFunc returns false
  • order is undefined
  • similar to: func (*sync.Map).Range(f func(key any, value any) bool)

type OrderedMap added in v0.4.32

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

OrderedMap is a mapping whose values are provided in order

  • mapping implementation is Go Map
  • ordering structure is B-tree
  • constraints.Ordered: integer float string
  • B-tree offers:
  • — avoiding vector-copy of large sorted slices which is slow and
  • — avoiding linear traversal of linked-lists which is slow and
  • — is a more efficient structure than binary tree

func NewOrderedMap added in v0.4.32

func NewOrderedMap[K comparable, V btree.Ordered]() (orderedMap *OrderedMap[K, V])

NewOrderedMap returns a map for btree.Ordered, ie. not ~uintptr

func NewOrderedMapUintptr added in v0.4.117

func NewOrderedMapUintptr[K comparable, V ~uintptr]() (orderedMap *OrderedMap[K, V])

NewOrderedMapUintptr returns a map for ~uintptr

func (*OrderedMap[K, V]) Put added in v0.4.32

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

Put creates or replaces a mapping

type OrderedMapFunc added in v0.4.60

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

OrderedMapFunc is a mapping whose values are provided in custom order

  • less(a, b) implements sort order and returns:
  • — true if a sorts before b
  • — false if a is of equal rank to b, or a is after b
  • — a equals b must not return true
  • mapping implementation is Go Map
  • ordering structure is B-tree
  • B-tree offers:
  • — avoiding vector-copy of large sorted slices which is slow and
  • — avoiding linear traversal of linked-lists which is slow and
  • — is a more efficient structure than binary tree

func NewOrderedMapFunc added in v0.4.60

func NewOrderedMapFunc[K comparable, V any](
	less func(a, b V) (aBeforeB bool),
) (orderedMap *OrderedMapFunc[K, V])

NewOrderedMapFunc returns a mapping whose values are provided in custom order.

  • less(a, b) implements sort order and returns:
  • — true if a sorts before b
  • — false if a is of equal rank to b, or a is after b
  • — a equals b must not return true
  • btree.Ordered does not include ~uintptr

func (*OrderedMapFunc[K, V]) Clone added in v0.4.60

func (m *OrderedMapFunc[K, V]) Clone() (clone *OrderedMapFunc[K, V])

Clone returns a shallow clone of the map

  • clone is done by ranging all keys

func (*OrderedMapFunc[K, V]) Put added in v0.4.60

func (m *OrderedMapFunc[K, V]) Put(key K, value V)

Put creates or replaces a mapping

type RWMap

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

RWMap is a one-liner thread-safe mapping. RWMap implements parli.ThreadSafeMap[K comparable, V any].

  • GetOrCreate method is an atomic, thread-safe operation as opposed to Get-then-Put
  • PutIf is atomic, thread-safe operation
  • native Go map functions: Get Put Delete Length Range
  • convenience methods: Clone Clone2 Clear
  • order functions: List Keys
  • V is copied so if size of V is large or V contains locks, use pointer
  • RWMap uses reader/writer mutual exclusion lock for slightly higher performance.
  • Get methods are O(1)

func NewRWMap2 added in v0.4.34

func NewRWMap2[K comparable, V any]() (rwMap *RWMap[K, V])

NewRWMap2 returns a thread-safe map implementation

func (*RWMap) Clear

func (m *RWMap) Clear(useRange ...bool)

Clear empties the map

func (*RWMap[K, V]) Clone

func (m *RWMap[K, V]) Clone() (clone parli.ThreadSafeMap[K, V])

Clone returns a shallow clone of the map

func (*RWMap[K, V]) Clone2 added in v0.4.47

func (m *RWMap[K, V]) Clone2() (clone *RWMap[K, V])

Clone returns a shallow clone of the map

func (*RWMap) Delete

func (m *RWMap) Delete(key K, useZeroValue ...bool)

func (*RWMap) Get

func (m *RWMap) Get(key K) (value V, ok bool)

func (*RWMap) GetOrCreate

func (m *RWMap) GetOrCreate(
	key K,
	newV func() (value *V),
	makeV func() (value V),
) (value V, ok bool)

GetOrCreate returns an item from the map if it exists otherwise creates it.

  • newV or makeV are invoked in the critical section, ie. these functions may not access the map or deadlock
  • if a key is mapped, its value is returned
  • otherwise, if newV and makeV are both nil, nil is returned.
  • otherwise, if newV is present, it is invoked to return a pointer ot a value. A nil return value from newV causes panic. A new mapping is created using the value pointed to by the newV return value.
  • otherwise, a mapping is created using whatever makeV returns
  • newV and makeV may not access the map. The map’s write lock is held during their execution
  • GetOrCreate is an atomic, thread-safe operation
  • value insert is O(log n)

func (*RWMap[K, V]) Keys added in v0.4.47

func (m *RWMap[K, V]) Keys(n ...int) (list []K)

Keys provides the mapping keys, undefined ordering

  • O(n)
  • invoked while holding RLock or Lock

func (*RWMap) Length

func (m *RWMap) Length() (length int)

func (*RWMap) List

func (m *RWMap) List(n ...int) (list []V)

List provides the mapped values, undefined ordering

  • O(n)

func (*RWMap) Put

func (m *RWMap) Put(key K, value V)

func (*RWMap[K, V]) PutIf added in v0.4.39

func (m *RWMap[K, V]) PutIf(key K, value V, putIf func(value V) (doPut bool)) (wasNewKey bool)

Putif is conditional Put depending on the return value from the putIf function.

  • if key does not exist in the map, the put is carried out and wasNewKey is true
  • if key exists and putIf is nil or returns true, the put is carried out and wasNewKey is false
  • if key exists and putIf returns false, the put is not carried out and wasNewKey is false
  • during PutIf, the map cannot be accessed and the map’s write-lock is held
  • PutIf is an atomic, thread-safe operation

func (*RWMap) Range added in v0.4.93

func (m *RWMap) Range(rangeFunc func(key K, value V) (keepGoing bool))

type SameFunc added in v0.4.117

type SameFunc[V any] func(a, b V) (isSameRank bool)

type ThreadSafeInsOrderedMap added in v0.4.83

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

ThreadSafeInsOrderedMap is a mapping whose values are provided in insertion order. Thread-safe.

func NewThreadSafeInsOrderedMap added in v0.4.83

func NewThreadSafeInsOrderedMap[K comparable, V any]() (orderedMap *ThreadSafeInsOrderedMap[K, V])

func (*ThreadSafeInsOrderedMap[K, V]) Clear added in v0.4.83

func (mp *ThreadSafeInsOrderedMap[K, V]) Clear()

Clear empties the map

func (*ThreadSafeInsOrderedMap[K, V]) Clone added in v0.4.83

func (mp *ThreadSafeInsOrderedMap[K, V]) Clone() (clone *ThreadSafeInsOrderedMap[K, V])

Clone returns a shallow clone of the map

func (*ThreadSafeInsOrderedMap[K, V]) Delete added in v0.4.83

func (mp *ThreadSafeInsOrderedMap[K, V]) Delete(key K)

Delete removes mapping using key K.

  • if key K is not mapped, the map is unchanged.
  • O(log n)

func (*ThreadSafeInsOrderedMap[K, V]) Get added in v0.4.83

func (mp *ThreadSafeInsOrderedMap[K, V]) Get(key K) (value V, ok bool)

func (*ThreadSafeInsOrderedMap[K, V]) Length added in v0.4.83

func (mp *ThreadSafeInsOrderedMap[K, V]) Length() (length int)

func (*ThreadSafeInsOrderedMap[K, V]) List added in v0.4.83

func (mp *ThreadSafeInsOrderedMap[K, V]) List(n ...int) (list []V)

List provides the mapped values in order

  • O(n)

func (*ThreadSafeInsOrderedMap[K, V]) Put added in v0.4.83

func (mp *ThreadSafeInsOrderedMap[K, V]) Put(key K, value V)

Put saves or replaces a mapping

func (*ThreadSafeInsOrderedMap[K, V]) PutIf added in v0.4.83

func (mp *ThreadSafeInsOrderedMap[K, V]) PutIf(key K, value V, putIf func(value V) (doPut bool)) (wasNewKey bool)

Put saves or replaces a mapping

type ThreadSafeMap added in v0.4.94

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

ThreadSafeMap is a thread-safe reusable promotable Go map

  • native Go map functions: Get Put Delete Length Range
  • convenience functions: Clear Clone
  • — those methods need access to the Go map
  • lock control: Lock RLock
  • ThreadSafeMap uses reader/writer mutual exclusion lock for thread-safety

func NewThreadSafeMap added in v0.4.94

func NewThreadSafeMap[K comparable, V any]() (m *ThreadSafeMap[K, V])

NewThreadSafeMap returns a thread-safe Go map

func (*ThreadSafeMap[K, V]) Clear added in v0.4.94

func (m *ThreadSafeMap[K, V]) Clear(useRange ...bool)

Clear empties the map

  • if useRange is RangeDelete, the map is cleared by iterating and deleteing all keys
  • invoked while holding Lock

func (*ThreadSafeMap[K, V]) Clone added in v0.4.94

func (m *ThreadSafeMap[K, V]) Clone() (clone *ThreadSafeMap[K, V])

Clone returns a shallow clone of the map

  • clone is done by ranging all keys
  • invoked while holding RLock or Lock

func (*ThreadSafeMap[K, V]) Delete added in v0.4.94

func (m *ThreadSafeMap[K, V]) Delete(key K, useZeroValue ...bool)

Delete removes mapping for key

  • if key is not mapped, the map is unchanged
  • if useZeroValue is pmaps.SetZeroValue, the mapping value is first set to the zero-value. This prevents temporary memory leaks when V contains pointers to large objects
  • O(log n)
  • invoked while holding Lock

func (*ThreadSafeMap[K, V]) Get added in v0.4.94

func (m *ThreadSafeMap[K, V]) Get(key K) (value V, ok bool)

Get returns the value mapped by key or the V zero-value otherwise.

  • the ok return value is true if a mapping was found.
  • invoked while holding Lock or RLock
  • O(1)

func (*ThreadSafeMap[K, V]) Length added in v0.4.94

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

Length returns the number of mappings

  • invoked while holding RLock or Lock

func (*ThreadSafeMap[K, V]) List added in v0.4.117

func (m *ThreadSafeMap[K, V]) List(n int) (list []V)

List provides the mapped values, undefined ordering

  • O(n)
  • invoked while holding RLock or Lock

func (*ThreadSafeMap[K, V]) Lock added in v0.4.117

func (m *ThreadSafeMap[K, V]) Lock() (unlock func())

allows consumers to obtain the write lock

  • returns a function releasing the lock

func (*ThreadSafeMap[K, V]) Put added in v0.4.96

func (m *ThreadSafeMap[K, V]) Put(key K, value V)

Put creates or replaces a mapping

  • invoked while holding Lock

func (*ThreadSafeMap[K, V]) RLock added in v0.4.117

func (m *ThreadSafeMap[K, V]) RLock() (runlock func())

allows consumers to obtain the read lock

  • returns a function releasing the lock

func (*ThreadSafeMap[K, V]) Range added in v0.4.94

func (m *ThreadSafeMap[K, V]) Range(rangeFunc func(key K, value V) (keepGoing bool))

Range traverses map bindings

  • iterates over map until rangeFunc returns false
  • similar to: func (*sync.Map).Range(f func(key any, value any) bool)
  • invoked while holding RLock or Lock

type ThreadSafeOrderedMapFunc added in v0.4.60

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

ThreadSafeOrderedMapFunc is a mapping whose values are provided in custom order. Thread-safe.

  • mapping implementation is Go Map
  • native Go map functions: Get Put Delete Length Range
  • convenience methods: Clone Clear
  • order methods: List
  • ordering structure is B-tree
  • B-tree offers:
  • — avoiding vector-copy of large sorted slices which is slow and
  • — avoiding linear traversal of linked-lists which is slow and
  • — is a more efficient structure than binary tree

func NewThreadSafeOrderedMapFunc added in v0.4.60

func NewThreadSafeOrderedMapFunc[K comparable, V any](
	less func(a, b V) (aBeforeB bool),
) (orderedMap *ThreadSafeOrderedMapFunc[K, V])

func (*ThreadSafeOrderedMapFunc[K, V]) Clear added in v0.4.60

func (m *ThreadSafeOrderedMapFunc[K, V]) Clear(useRange ...bool)

func (*ThreadSafeOrderedMapFunc[K, V]) Clone added in v0.4.60

func (m *ThreadSafeOrderedMapFunc[K, V]) Clone() (clone *ThreadSafeOrderedMapFunc[K, V])

func (*ThreadSafeOrderedMapFunc[K, V]) Delete added in v0.4.60

func (m *ThreadSafeOrderedMapFunc[K, V]) Delete(key K, useZeroValue ...bool)

func (*ThreadSafeOrderedMapFunc) Get added in v0.4.60

func (m *ThreadSafeOrderedMapFunc) Get(key K) (value V, ok bool)

func (*ThreadSafeOrderedMapFunc) GetOrCreate added in v0.4.60

func (m *ThreadSafeOrderedMapFunc) GetOrCreate(
	key K,
	newV func() (value *V),
	makeV func() (value V),
) (value V, ok bool)

GetOrCreate returns an item from the map if it exists otherwise creates it.

  • newV or makeV are invoked in the critical section, ie. these functions may not access the map or deadlock
  • if a key is mapped, its value is returned
  • otherwise, if newV and makeV are both nil, nil is returned.
  • otherwise, if newV is present, it is invoked to return a pointer ot a value. A nil return value from newV causes panic. A new mapping is created using the value pointed to by the newV return value.
  • otherwise, a mapping is created using whatever makeV returns
  • newV and makeV may not access the map. The map’s write lock is held during their execution
  • GetOrCreate is an atomic, thread-safe operation
  • value insert is O(log n)

func (*ThreadSafeOrderedMapFunc) Length added in v0.4.60

func (m *ThreadSafeOrderedMapFunc) Length() (length int)

func (*ThreadSafeOrderedMapFunc[K, V]) List added in v0.4.60

func (m *ThreadSafeOrderedMapFunc[K, V]) List(n ...int) (list []V)

List provides mapped values in order

  • n zero or missing means all items
  • n non-zero means this many items capped by length

func (*ThreadSafeOrderedMapFunc[K, V]) Put added in v0.4.60

func (m *ThreadSafeOrderedMapFunc[K, V]) Put(key K, value V)

func (*ThreadSafeOrderedMapFunc) Range added in v0.4.117

func (m *ThreadSafeOrderedMapFunc) Range(rangeFunc func(key K, value V) (keepGoing bool))

Jump to

Keyboard shortcuts

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