safemap

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2025 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package safemap provides a concurrency-safe generic map.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToSlice

func ToSlice[K comparable, V any, T any](sm *SafeMap[K, V], mapFn func(K, V) T) []T

Types

type SafeMap

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

SafeMap is a threadsafe map[K]V.

func New

func New[K comparable, V any]() *SafeMap[K, V]

New returns an empty SafeMap.

func NewFrom

func NewFrom[K comparable, V any](src map[K]V) *SafeMap[K, V]

NewFrom initializes the map from an existing map (copied).

func (*SafeMap[K, V]) Clear

func (s *SafeMap[K, V]) Clear()

func (*SafeMap[K, V]) Compute

func (s *SafeMap[K, V]) Compute(key K, fn func(prev V, ok bool) (next V, delete bool)) (V, bool)

Compute atomically updates the value for key using fn. If fn returns delete=true, the entry is removed.

func (*SafeMap[K, V]) Delete

func (s *SafeMap[K, V]) Delete(key K)

func (*SafeMap[K, V]) Get

func (s *SafeMap[K, V]) Get(key K) (V, bool)

func (*SafeMap[K, V]) Has

func (s *SafeMap[K, V]) Has(key K) bool

func (*SafeMap[K, V]) Keys

func (s *SafeMap[K, V]) Keys() []K

Keys returns a snapshot of keys.

func (*SafeMap[K, V]) Len

func (s *SafeMap[K, V]) Len() int

func (*SafeMap[K, V]) LoadOrStore

func (s *SafeMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)

LoadOrStore returns the existing value if present; otherwise stores and returns the given value.

func (*SafeMap[K, V]) Range

func (s *SafeMap[K, V]) Range(fn func(K, V) bool)

Range iterates over entries; stop early by returning false.

func (*SafeMap[K, V]) Set

func (s *SafeMap[K, V]) Set(key K, value V)

func (*SafeMap[K, V]) ToMap

func (s *SafeMap[K, V]) ToMap() map[K]V

ToMap returns a copy of the underlying map.

func (*SafeMap[K, V]) Values

func (s *SafeMap[K, V]) Values() []V

Values returns a snapshot of values.

Jump to

Keyboard shortcuts

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