shardmap

package module
v0.0.0-...-d55978b Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2025 License: MIT Imports: 4 Imported by: 0

README

Shardmap

This is here simply to allow redonation back to the upstream. I will likely delete this, so don't use it.

This is a copy of Josh Baker's shardmap that is updated for generics, using maphash instead of xxhash and a new hashmap package he has.

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 a hashmap. Like map[string]interface{}, but sharded and thread-safe.

func New

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

New returns a new hashmap with the specified capacity. This function is only needed when you must define a minimum capacity, otherwise just use:

var m shardmap.Map

func (*Map[K, V]) Clear

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

Clear out all values from map

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]) DeleteAccept

func (m *Map[K, V]) DeleteAccept(key K, accept func(prev V, replaced bool) bool) (prev V, deleted bool)

DeleteAccept deletes a value for a key. The "accept" function can be used to inspect the previous value, if any, and accept or reject the change. It's also provides a safe way to block other others from writing to the same shard while inspecting. 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]) Len

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

Len returns the number of values in map.

func (*Map[K, V]) Range

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

Range iterates overall all key/values. It's not safe to call or Set or Delete while ranging.

func (*Map[K, V]) Set

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

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

func (*Map[K, V]) SetAccept

func (m *Map[K, V]) SetAccept(key K, value V, accept func(prev V, replaced bool) bool) (prev V, replaced bool)

SetAccept assigns a value to a key. The "accept" function can be used to inspect the previous value, if any, and accept or reject the change. It's also provides a safe way to block other others from writing to the same shard while inspecting. Returns the previous value, or false when no value was assigned.

Directories

Path Synopsis
v2
Package shardmap is a re-done version of Josh Baker's shardmap package.
Package shardmap is a re-done version of Josh Baker's shardmap package.

Jump to

Keyboard shortcuts

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