hashmap

package
v0.0.0-...-052ef2a Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HasherFunc

type HasherFunc[K any] func(key K) uint64

type Map

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

Map is a hashmap. Like map[string]interface{}

func New

func New[K comparable, V any](cap int, hasher HasherFunc[K]) *Map[K, V]

New returns a new Map. Like map[string]interface{}

func (*Map[K, V]) Copy

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

Copy the smapet. This is a copy-on-write operation and is very fast because it only performs a shadow copy.

func (*Map[K, V]) Delete

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

Delete deletes a value for a key. Returns the deleted value, or false when no value was assigned.

func (*Map[K, V]) Get

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

Get returns a value for a key. Returns false when no value has been assign for key.

func (*Map[K, V]) GetValue

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

func (*Map[K, V]) Keys

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

Keys returns all keys as a slice

func (*Map[K, V]) Len

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

Len returns the number of values in map.

func (*Map[K, V]) Scan

func (m *Map[K, V]) Scan(iter func(key K, value V) bool)

Scan iterates over all key/values. It's not safe to call or Set or Delete while scanning.

func (*Map[K, V]) Set

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

Set assigns a value to a key. Returns the previous value, or false when no value was assigned.

func (*Map[K, V]) Values

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

Values returns all values as a slice

type SyncMap

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

SyncMap is a thread-safe version of Map. It achieves this by sharding into "x" number of shards each with a spinlock and an instance of Map. Shards are determined by key hash. The key is only ever hashed once per operation. Map code is embedded only to reduce the hash operation count.

func NewSyncMap

func NewSyncMap[K comparable, V any](numShards, initialCapacity int, hasher HasherFunc[K]) *SyncMap[K, V]

func (*SyncMap[K, V]) Delete

func (m *SyncMap[K, V]) Delete(key K) (prev V, ok bool)

func (*SyncMap[K, V]) DeleteIf

func (m *SyncMap[K, V]) DeleteIf(key K, condition func(existing V) bool) (prev V, ok bool)

func (*SyncMap[K, V]) Get

func (m *SyncMap[K, V]) Get(key K) (val V, ok bool)

Get is volatile and extremely fast. It's possible to have a small window where it misses. When it does miss calling Load

func (*SyncMap[K, V]) GetOrCreate

func (m *SyncMap[K, V]) GetOrCreate(key K, supplier func(K) V) (value V, created bool)

func (*SyncMap[K, V]) GetOrLoad

func (m *SyncMap[K, V]) GetOrLoad(key K) (val V, ok bool)

func (*SyncMap[K, V]) GetOrLoadCreate

func (m *SyncMap[K, V]) GetOrLoadCreate(key K, supplier func(K) V) (val V, created bool)

func (*SyncMap[K, V]) GetValue

func (m *SyncMap[K, V]) GetValue(key K) V

func (*SyncMap[K, V]) Load

func (m *SyncMap[K, V]) Load(key K) (val V, ok bool)

func (*SyncMap[K, V]) LoadOrCreate

func (m *SyncMap[K, V]) LoadOrCreate(key K, supplier func(K) V) (value V, created bool)

func (*SyncMap[K, V]) Put

func (m *SyncMap[K, V]) Put(key K, value V) (prev V, ok bool)

func (*SyncMap[K, V]) PutIf

func (m *SyncMap[K, V]) PutIf(
	key K,
	value V,
	condition func(prev V, prevExists bool) bool,
) (prev V, prevExists, ok bool)

func (*SyncMap[K, V]) PutIfAbsent

func (m *SyncMap[K, V]) PutIfAbsent(key K, value V) (prev V, ok bool)

func (*SyncMap[K, V]) Range

func (m *SyncMap[K, V]) Range(iter func(key K, value V) bool)

func (*SyncMap[K, V]) Scan

func (m *SyncMap[K, V]) Scan(iter func(key K, value V) bool)

func (*SyncMap[K, V]) ScanUnsafe

func (m *SyncMap[K, V]) ScanUnsafe(iter func(key K, value V) bool)

func (*SyncMap[K, V]) Store

func (m *SyncMap[K, V]) Store(key K, value V) (prev V, ok bool)

Jump to

Keyboard shortcuts

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