hashmap

package
v2.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2025 License: Apache-2.0 Imports: 8 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, N mapNode[K, V]] struct {
	// contains filtered or unexported fields
}

Map is like a Go map[K]V but is safe for concurrent use by multiple goroutines without additional locking or coordination. It follows the interface of sync.Map with a number of valuable extensions like Compute or Size.

A Map must not be copied after first use.

Map uses a modified version of Cache-Line Hash Table (CLHT) data structure: https://github.com/LPD-EPFL/CLHT

CLHT is built around idea to organize the hash table in cache-line-sized buckets, so that on all modern CPUs update operations complete with at most one cache-line transfer. Also, Get operations involve no write to memory, as well as no mutexes or any other sort of locks. Due to this design, in all considered scenarios Map outperforms sync.Map.

Map also borrows ideas from Java's j.u.c.ConcurrentHashMap (immutable K/V pair structs instead of atomic snapshots) and C++'s absl::flat_hash_map (meta memory and SWAR-based lookups).

func New

func New[K comparable, V any, N mapNode[K, V]](nodeManager mapNodeManager[K, V, N]) *Map[K, V, N]

New creates a new Map instance.

func NewWithSize

func NewWithSize[K comparable, V any, N mapNode[K, V]](nodeManager mapNodeManager[K, V, N], size int) *Map[K, V, N]

NewWithSize creates a new Map instance with capacity enough to hold size nodes. If size is zero or negative, the value is ignored.

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

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

Clear deletes all keys and values currently stored in the map.

func (*Map[K, V, N]) Compute

func (m *Map[K, V, N]) Compute(key K, computeFunc func(n N) N) N

Compute either sets the computed new value for the key or deletes the value for the key.

This call locks a hash table bucket while the compute function is executed. It means that modifications on other nodes in the bucket will be blocked until the computeFn executes. Consider this when the function includes long-running operations.

func (*Map[K, V, N]) Get

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

Get returns the node stored in the map for a key, or nil if no value is present.

func (*Map[K, V, N]) Range

func (m *Map[K, V, N]) Range(fn func(n N) 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, Range may reflect any mapping for that key from any point during the Range call.

It is safe to modify the map while iterating it, including entry creation, modification and deletion. However, the concurrent modification rule apply, i.e. the changes may be not reflected in the subsequently iterated nodes.

func (*Map[K, V, N]) Size

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

Size returns current size of the map.

Jump to

Keyboard shortcuts

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