hashmap

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

README

hashmap

Hash-based map implementations.

Types

For Comparable Keys

Use these when your key type supports == comparison (strings, integers, etc.):

// Mutable map
m := hashmap.NewComparable[string, int]()
m.Put("a", 1)
m.Put("b", 2)

// From entries
m := hashmap.NewComparable(
    ds.MapEntry[string, int]{Key: "a", Value: 1},
    ds.MapEntry[string, int]{Key: "b", Value: 2},
)

// From parallel slices
m, err := hashmap.CollectToComparable([]string{"a", "b"}, []int{1, 2})

// Immutable map
frozen := hashmap.NewImmutableComparable[string, int]()
For Hashable Keys

Use these when your key type implements ds.Hashable[T]:

// Mutable map
m := hashmap.NewHashable[*MyKey, int]()

// From parallel slices
m, err := hashmap.CollectToHashable(keys, values)

// Immutable map
frozen := hashmap.NewImmutableHashable[*MyKey, int]()
Thread-Safe Maps

Wrap any mutable map for concurrent access:

inner := hashmap.NewComparable[string, int]()
m := hashmap.NewConcurrentMap(inner)

// Atomic operations
m.Compute("key", func(k string, old int, exists bool) (int, bool) {
    return old + 1, true
})

m.ComputeIfAbsent("key", func(k string) (int, bool) {
    return 42, true
})

m.ComputeIfPresent("key", func(k string, old int) (int, bool) {
    return old * 2, true
})

Common Operations

// Basic operations
value, exists := m.Get("key")
m.Put("key", value)
m.Remove("key")
m.Clear()

// Query
m.Size()
m.IsEmpty()
m.ContainsKey("key")
m.Keys()
m.Values()

// Transform
filtered := m.Filter(func(k string) bool { return len(k) > 2 })
retained := m.Retain("a", "b", "c")

// Iterate
for k, v := range m.Iter() {
    // ...
}

// Freeze/Unfreeze
frozen := m.Freeze()
mutable := frozen.Unfreeze()

Documentation

Overview

Package hashmap provides hash-based map implementations for the datastructures interfaces.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CollectToComparable

func CollectToComparable[K comparable, V any](xs []K, ys []V) (ds.MutableMap[K, V], error)

CollectToComparable creates a new mutable map from parallel slices of keys and values. Returns an error if the slices have different lengths.

func CollectToHashable

func CollectToHashable[K ds.Hashable[K], V any](xs []K, ys []V) (ds.MutableMap[K, V], error)

CollectToHashable creates a new mutable map from parallel slices of keys and values. Returns an error if the slices have different lengths.

func CollectToImmutableComparable

func CollectToImmutableComparable[K comparable, V any](xs []K, ys []V) (ds.Map[K, V], error)

CollectToImmutableComparable creates a new immutable map from parallel slices of keys and values. Returns an error if the slices have different lengths.

func CollectToImmutableHashable

func CollectToImmutableHashable[K ds.Hashable[K], V any](xs []K, ys []V) (ds.Map[K, V], error)

CollectToImmutableHashable creates a new immutable map from parallel slices of keys and values. Returns an error if the slices have different lengths.

func NewComparableFromNativeLike

func NewComparableFromNativeLike[K comparable, V any, T ~map[K]V](arg T) ds.MutableMap[K, V]

NewComparableFromNativeLike creates a new mutable map by copying from a native Go map.

func NewConcurrentMap

func NewConcurrentMap[K any, V any](innerMap ds.MutableMap[K, V]) ds.ConcurrentMap[K, V]

NewConcurrentMap creates a new thread-safe map wrapping the given mutable map.

func NewHashable

func NewHashable[K ds.Hashable[K], V any]() ds.MutableMap[K, V]

NewHashable creates a new empty mutable map for hashable key types.

func NewImmutableComparable

func NewImmutableComparable[K comparable, V any](xs ...ds.MapEntry[K, V]) ds.Map[K, V]

NewImmutableComparable creates a new immutable map from the given entries.

func NewImmutableComparableFromNativeLike

func NewImmutableComparableFromNativeLike[K comparable, V any, T ~map[K]V](arg T) ds.Map[K, V]

NewImmutableComparableFromNativeLike creates a new immutable map by copying from a native Go map.

func NewImmutableHashable

func NewImmutableHashable[K ds.Hashable[K], V any]() ds.Map[K, V]

NewImmutableHashable creates a new empty immutable map for hashable key types.

Types

type ConcurrentMap

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

ConcurrentMap is a thread-safe wrapper around a MutableMap. All operations are protected by a read-write mutex.

func (*ConcurrentMap[K, V]) Clear

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

Clear removes all entries from the map.

func (*ConcurrentMap[K, V]) Clone

func (m *ConcurrentMap[K, V]) Clone() ds.ConcurrentMap[K, V]

Clone returns a new concurrent map with a copy of the data.

func (*ConcurrentMap[K, V]) Compute

func (m *ConcurrentMap[K, V]) Compute(key K, remappingFunction func(key K, oldVal V, ifExist bool) (V, bool)) V

Compute atomically computes a new value based on the key's current mapping. The remappingFunction receives the key, current value (if any), and existence flag, returning the new value and whether to store it (false removes the key).

func (*ConcurrentMap[K, V]) ComputeIfAbsent

func (m *ConcurrentMap[K, V]) ComputeIfAbsent(key K, mappingFunction func(key K) (V, bool)) V

ComputeIfAbsent atomically computes a value only if the key is not present. The mappingFunction returns the value to store and whether to store it. If the key exists, returns the current value without calling mappingFunction.

func (*ConcurrentMap[K, V]) ComputeIfPresent

func (m *ConcurrentMap[K, V]) ComputeIfPresent(key K, remappingFunction func(key K, oldVal V) (V, bool)) V

ComputeIfPresent atomically computes a new value only if the key is present. The remappingFunction returns the new value and whether to keep it (false removes the key). If the key is absent, returns the zero value without calling remappingFunction.

func (*ConcurrentMap[K, V]) ContainsKey

func (m *ConcurrentMap[K, V]) ContainsKey(key K) bool

ContainsKey returns true if the key exists in the map.

func (*ConcurrentMap[K, V]) Enumerate

func (m *ConcurrentMap[K, V]) Enumerate() iter.Seq2[int, ds.MapEntry[K, V]]

Enumerate returns an iterator with index and MapEntry pairs.

func (*ConcurrentMap[K, V]) Filter

func (m *ConcurrentMap[K, V]) Filter(predicate func(key K) bool) ds.ConcurrentMap[K, V]

Filter returns a new concurrent map containing only entries where the predicate returns true.

func (*ConcurrentMap[K, V]) Get

func (m *ConcurrentMap[K, V]) Get(key K) (value V, exists bool)

Get returns the value associated with the key and whether it exists.

func (*ConcurrentMap[K, V]) IsEmpty

func (m *ConcurrentMap[K, V]) IsEmpty() bool

IsEmpty returns true if the map contains no entries.

func (*ConcurrentMap[K, V]) Iter

func (m *ConcurrentMap[K, V]) Iter() iter.Seq2[K, V]

Iter returns an iterator over all key-value pairs.

func (*ConcurrentMap[K, V]) Keys

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

Keys returns a slice of all keys in the map.

func (*ConcurrentMap[K, V]) Put

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

Put adds or updates a key-value pair in the map.

func (*ConcurrentMap[K, V]) Remove

func (m *ConcurrentMap[K, V]) Remove(key K)

Remove deletes the entry with the given key from the map.

func (*ConcurrentMap[K, V]) Retain

func (m *ConcurrentMap[K, V]) Retain(keys ...K) ds.ConcurrentMap[K, V]

Retain returns a new concurrent map containing only entries with the specified keys.

func (*ConcurrentMap[K, V]) Size

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

Size returns the number of entries in the map.

func (*ConcurrentMap[K, V]) TryPut

func (m *ConcurrentMap[K, V]) TryPut(key K, newValue V) (replaced bool, oldValue V)

TryPut adds or updates a key-value pair, returning whether a value was replaced and the old value.

func (*ConcurrentMap[K, V]) TryRemove

func (m *ConcurrentMap[K, V]) TryRemove(key K) (removed bool, removedValue V)

TryRemove deletes the entry with the given key, returning whether it existed and its value.

func (*ConcurrentMap[K, V]) Values

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

Values returns a slice of all values in the map.

type HashableEntry

type HashableEntry[K ds.Hashable[K], V any] ds.MapEntry[K, V]

HashableEntry represents a key-value pair where the key implements Hashable.

type HashableMapping

type HashableMapping[K ds.Hashable[K], V any] map[base.HashCode][]*HashableEntry[K, V]

HashableMapping is the internal storage for hashable maps, using hash codes as bucket keys.

func (HashableMapping[K, V]) TryPut

func (m HashableMapping[K, V]) TryPut(key K, newValue V) (replaced bool, oldValue V)

TryPut adds or updates a key-value pair, returning whether a value was replaced and the old value.

func (HashableMapping[K, V]) TryRemove

func (m HashableMapping[K, V]) TryRemove(key K) (removed bool, removedValue V)

TryRemove deletes the entry with the given key, returning whether it existed and its value.

type HashableTrait

type HashableTrait[K ds.Hashable[K], V any] struct {
	// contains filtered or unexported fields
}

HashableTrait provides common functionality for hashable map implementations.

func (HashableTrait[K, V]) ContainsKey

func (m HashableTrait[K, V]) ContainsKey(key K) bool

ContainsKey returns true if the key exists in the map.

func (HashableTrait[K, V]) Enumerate

func (m HashableTrait[K, V]) Enumerate() iter.Seq2[int, ds.MapEntry[K, V]]

Enumerate returns an iterator with index and MapEntry pairs.

func (HashableTrait[K, V]) Get

func (m HashableTrait[K, V]) Get(key K) (value V, exists bool)

Get returns the value associated with the key and whether it exists.

func (HashableTrait[K, V]) IsEmpty

func (m HashableTrait[K, V]) IsEmpty() bool

IsEmpty returns true if the map contains no entries.

func (HashableTrait[K, V]) Iter

func (m HashableTrait[K, V]) Iter() iter.Seq2[K, V]

Iter returns an iterator over all key-value pairs.

func (HashableTrait[K, V]) Keys

func (m HashableTrait[K, V]) Keys() []K

Keys returns a slice of all keys in the map.

func (HashableTrait[K, V]) Size

func (m HashableTrait[K, V]) Size() int

Size returns the number of entries in the map.

func (HashableTrait[K, V]) Values

func (m HashableTrait[K, V]) Values() []V

Values returns a slice of all values in the map.

type ImmutableComparableMap

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

ImmutableComparableMap is an immutable hash map for comparable key types.

func (ImmutableComparableMap[K, V]) Clone

func (m ImmutableComparableMap[K, V]) Clone() ds.Map[K, V]

Clone returns a copy of this map.

func (ImmutableComparableMap[K, V]) ContainsKey

func (m ImmutableComparableMap[K, V]) ContainsKey(key K) bool

ContainsKey returns true if the key exists in the map.

func (ImmutableComparableMap[K, V]) Enumerate

func (m ImmutableComparableMap[K, V]) Enumerate() iter.Seq2[int, ds.MapEntry[K, V]]

Enumerate returns an iterator with index and MapEntry pairs.

func (ImmutableComparableMap[K, V]) Filter

func (m ImmutableComparableMap[K, V]) Filter(predicate func(key K) bool) ds.Map[K, V]

Filter returns a new map containing only entries where the predicate returns true.

func (ImmutableComparableMap[K, V]) Get

func (m ImmutableComparableMap[K, V]) Get(key K) (value V, exists bool)

Get returns the value associated with the key and whether it exists.

func (ImmutableComparableMap[K, V]) IsEmpty

func (m ImmutableComparableMap[K, V]) IsEmpty() bool

IsEmpty returns true if the map contains no entries.

func (ImmutableComparableMap[K, V]) IsImmutable

func (ImmutableComparableMap[K, V]) IsImmutable() bool

IsImmutable returns true as this is an immutable map.

func (ImmutableComparableMap[K, V]) Iter

func (m ImmutableComparableMap[K, V]) Iter() iter.Seq2[K, V]

Iter returns an iterator over all key-value pairs.

func (ImmutableComparableMap[K, V]) Keys

func (m ImmutableComparableMap[K, V]) Keys() []K

Keys returns a slice of all keys in the map.

func (ImmutableComparableMap[K, V]) Retain

func (m ImmutableComparableMap[K, V]) Retain(keys ...K) ds.Map[K, V]

Retain returns a new map containing only entries with the specified keys.

func (ImmutableComparableMap[K, V]) Size

func (m ImmutableComparableMap[K, V]) Size() int

Size returns the number of entries in the map.

func (ImmutableComparableMap[K, V]) Unfreeze

func (m ImmutableComparableMap[K, V]) Unfreeze() ds.MutableMap[K, V]

Unfreeze returns a mutable copy of this map.

func (ImmutableComparableMap[K, V]) Values

func (m ImmutableComparableMap[K, V]) Values() []V

Values returns a slice of all values in the map.

type ImmutableHashableMap

type ImmutableHashableMap[K ds.Hashable[K], V any] struct {
	HashableTrait[K, V]
}

ImmutableHashableMap is an immutable hash map for hashable key types.

func (ImmutableHashableMap[K, V]) Clone

func (m ImmutableHashableMap[K, V]) Clone() ds.Map[K, V]

Clone returns a copy of this map.

func (ImmutableHashableMap[K, V]) Filter

func (m ImmutableHashableMap[K, V]) Filter(predicate func(key K) bool) ds.Map[K, V]

Filter returns a new map containing only entries where the predicate returns true.

func (ImmutableHashableMap[K, V]) IsImmutable

func (ImmutableHashableMap[K, V]) IsImmutable() bool

IsImmutable returns true as this is an immutable map.

func (ImmutableHashableMap[K, V]) Retain

func (m ImmutableHashableMap[K, V]) Retain(keys ...K) ds.Map[K, V]

Retain returns a new map containing only entries with the specified keys.

func (ImmutableHashableMap[K, V]) Unfreeze

func (m ImmutableHashableMap[K, V]) Unfreeze() ds.MutableMap[K, V]

Unfreeze returns a mutable copy of this map.

type MutableComparableMap

type MutableComparableMap[K comparable, V any] struct {
	NativeMap[K, V]
}

MutableComparableMap is a mutable hash map for comparable key types.

func NewComparable

func NewComparable[K comparable, V any](xs ...ds.MapEntry[K, V]) *MutableComparableMap[K, V]

NewComparable creates a new mutable map from the given entries.

func (MutableComparableMap[K, V]) Clone

func (m MutableComparableMap[K, V]) Clone() ds.MutableMap[K, V]

Clone returns a mutable copy of this map.

func (MutableComparableMap[K, V]) Filter

func (m MutableComparableMap[K, V]) Filter(predicate func(key K) bool) ds.MutableMap[K, V]

Filter returns a new map containing only entries where the predicate returns true.

func (MutableComparableMap[K, V]) Freeze

func (m MutableComparableMap[K, V]) Freeze() ds.Map[K, V]

Freeze returns an immutable snapshot of this map.

func (MutableComparableMap[K, V]) IsImmutable

func (MutableComparableMap[K, V]) IsImmutable() bool

IsImmutable returns false as this is a mutable map.

func (MutableComparableMap[K, V]) Retain

func (m MutableComparableMap[K, V]) Retain(keys ...K) ds.MutableMap[K, V]

Retain returns a new map containing only entries with the specified keys.

func (*MutableComparableMap[K, V]) ToNative

func (m *MutableComparableMap[K, V]) ToNative() map[K]V

ToNative returns a copy of the map data as a native Go map.

type MutableHashableMap

type MutableHashableMap[K ds.Hashable[K], V any] struct {
	HashableTrait[K, V]
}

MutableHashableMap is a mutable hash map for hashable key types.

func (MutableHashableMap[K, V]) Clear

func (m MutableHashableMap[K, V]) Clear()

Clear removes all entries from the map.

func (MutableHashableMap[K, V]) Clone

func (m MutableHashableMap[K, V]) Clone() ds.MutableMap[K, V]

Clone returns a mutable copy of this map.

func (MutableHashableMap[K, V]) Filter

func (m MutableHashableMap[K, V]) Filter(predicate func(key K) bool) ds.MutableMap[K, V]

Filter returns a new map containing only entries where the predicate returns true.

func (MutableHashableMap[K, V]) Freeze

func (m MutableHashableMap[K, V]) Freeze() ds.Map[K, V]

Freeze returns an immutable snapshot of this map.

func (MutableHashableMap[K, V]) IsImmutable

func (MutableHashableMap[K, V]) IsImmutable() bool

IsImmutable returns false as this is a mutable map.

func (MutableHashableMap[K, V]) Put

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

Put adds or updates a key-value pair in the map.

func (MutableHashableMap[K, V]) Remove

func (m MutableHashableMap[K, V]) Remove(key K)

Remove deletes the entry with the given key from the map.

func (MutableHashableMap[K, V]) Retain

func (m MutableHashableMap[K, V]) Retain(keys ...K) ds.MutableMap[K, V]

Retain returns a new map containing only entries with the specified keys.

func (MutableHashableMap[K, V]) TryPut

func (m MutableHashableMap[K, V]) TryPut(key K, newValue V) (replaced bool, oldValue V)

TryPut adds or updates a key-value pair, returning whether a value was replaced and the old value.

func (MutableHashableMap[K, V]) TryRemove

func (m MutableHashableMap[K, V]) TryRemove(key K) (removed bool, removedValue V)

TryRemove deletes the entry with the given key, returning whether it existed and its value.

type NativeMap

type NativeMap[K comparable, V any] map[K]V

NativeMap is a type alias for Go's built-in map with additional methods to satisfy common map interface requirements.

func (NativeMap[K, V]) Clear

func (m NativeMap[K, V]) Clear()

Clear removes all entries from the map.

func (NativeMap[K, V]) ContainsKey

func (m NativeMap[K, V]) ContainsKey(key K) bool

ContainsKey returns true if the key exists in the map.

func (NativeMap[K, V]) Enumerate

func (m NativeMap[K, V]) Enumerate() iter.Seq2[int, ds.MapEntry[K, V]]

Enumerate returns an iterator with index and MapEntry pairs.

func (NativeMap[K, V]) Get

func (m NativeMap[K, V]) Get(key K) (value V, exists bool)

Get returns the value associated with the key and whether it exists.

func (NativeMap[K, V]) IsEmpty

func (m NativeMap[K, V]) IsEmpty() bool

IsEmpty returns true if the map contains no entries.

func (NativeMap[K, V]) Iter

func (m NativeMap[K, V]) Iter() iter.Seq2[K, V]

Iter returns an iterator over all key-value pairs.

func (NativeMap[K, V]) Keys

func (m NativeMap[K, V]) Keys() []K

Keys returns a slice of all keys in the map.

func (NativeMap[K, V]) Put

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

Put adds or updates a key-value pair in the map.

func (NativeMap[K, V]) Remove

func (m NativeMap[K, V]) Remove(key K)

Remove deletes the entry with the given key from the map.

func (NativeMap[K, V]) Size

func (m NativeMap[K, V]) Size() int

Size returns the number of entries in the map.

func (NativeMap[K, V]) TryPut

func (m NativeMap[K, V]) TryPut(key K, newValue V) (replaced bool, oldValue V)

TryPut adds or updates a key-value pair, returning whether a value was replaced and the old value.

func (NativeMap[K, V]) TryRemove

func (m NativeMap[K, V]) TryRemove(key K) (removed bool, removedValue V)

TryRemove deletes the entry with the given key, returning whether it existed and its value.

func (NativeMap[K, V]) Values

func (m NativeMap[K, V]) Values() []V

Values returns a slice of all values in the map.

Jump to

Keyboard shortcuts

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