Documentation
¶
Overview ¶
Package hashmap provides hash-based map implementations for the datastructures interfaces.
Index ¶
- func CollectToComparable[K comparable, V any](xs []K, ys []V) (ds.MutableMap[K, V], error)
- func CollectToHashable[K ds.Hashable[K], V any](xs []K, ys []V) (ds.MutableMap[K, V], error)
- func CollectToImmutableComparable[K comparable, V any](xs []K, ys []V) (ds.Map[K, V], error)
- func CollectToImmutableHashable[K ds.Hashable[K], V any](xs []K, ys []V) (ds.Map[K, V], error)
- func NewComparableFromNativeLike[K comparable, V any, T ~map[K]V](arg T) ds.MutableMap[K, V]
- func NewConcurrentMap[K any, V any](innerMap ds.MutableMap[K, V]) ds.ConcurrentMap[K, V]
- func NewHashable[K ds.Hashable[K], V any]() ds.MutableMap[K, V]
- func NewImmutableComparable[K comparable, V any](xs ...ds.MapEntry[K, V]) ds.Map[K, V]
- func NewImmutableComparableFromNativeLike[K comparable, V any, T ~map[K]V](arg T) ds.Map[K, V]
- func NewImmutableHashable[K ds.Hashable[K], V any]() ds.Map[K, V]
- type ConcurrentMap
- func (m *ConcurrentMap[K, V]) Clear()
- func (m *ConcurrentMap[K, V]) Clone() ds.ConcurrentMap[K, V]
- func (m *ConcurrentMap[K, V]) Compute(key K, remappingFunction func(key K, oldVal V, ifExist bool) (V, bool)) V
- func (m *ConcurrentMap[K, V]) ComputeIfAbsent(key K, mappingFunction func(key K) (V, bool)) V
- func (m *ConcurrentMap[K, V]) ComputeIfPresent(key K, remappingFunction func(key K, oldVal V) (V, bool)) V
- func (m *ConcurrentMap[K, V]) ContainsKey(key K) bool
- func (m *ConcurrentMap[K, V]) Enumerate() iter.Seq2[int, ds.MapEntry[K, V]]
- func (m *ConcurrentMap[K, V]) Filter(predicate func(key K) bool) ds.ConcurrentMap[K, V]
- func (m *ConcurrentMap[K, V]) Get(key K) (value V, exists bool)
- func (m *ConcurrentMap[K, V]) IsEmpty() bool
- func (m *ConcurrentMap[K, V]) Iter() iter.Seq2[K, V]
- func (m *ConcurrentMap[K, V]) Keys() []K
- func (m *ConcurrentMap[K, V]) Put(key K, value V)
- func (m *ConcurrentMap[K, V]) Remove(key K)
- func (m *ConcurrentMap[K, V]) Retain(keys ...K) ds.ConcurrentMap[K, V]
- func (m *ConcurrentMap[K, V]) Size() int
- func (m *ConcurrentMap[K, V]) TryPut(key K, newValue V) (replaced bool, oldValue V)
- func (m *ConcurrentMap[K, V]) TryRemove(key K) (removed bool, removedValue V)
- func (m *ConcurrentMap[K, V]) Values() []V
- type HashableEntry
- type HashableMapping
- type HashableTrait
- func (m HashableTrait[K, V]) ContainsKey(key K) bool
- func (m HashableTrait[K, V]) Enumerate() iter.Seq2[int, ds.MapEntry[K, V]]
- func (m HashableTrait[K, V]) Get(key K) (value V, exists bool)
- func (m HashableTrait[K, V]) IsEmpty() bool
- func (m HashableTrait[K, V]) Iter() iter.Seq2[K, V]
- func (m HashableTrait[K, V]) Keys() []K
- func (m HashableTrait[K, V]) Size() int
- func (m HashableTrait[K, V]) Values() []V
- type ImmutableComparableMap
- func (m ImmutableComparableMap[K, V]) Clone() ds.Map[K, V]
- func (m ImmutableComparableMap[K, V]) ContainsKey(key K) bool
- func (m ImmutableComparableMap[K, V]) Enumerate() iter.Seq2[int, ds.MapEntry[K, V]]
- func (m ImmutableComparableMap[K, V]) Filter(predicate func(key K) bool) ds.Map[K, V]
- func (m ImmutableComparableMap[K, V]) Get(key K) (value V, exists bool)
- func (m ImmutableComparableMap[K, V]) IsEmpty() bool
- func (ImmutableComparableMap[K, V]) IsImmutable() bool
- func (m ImmutableComparableMap[K, V]) Iter() iter.Seq2[K, V]
- func (m ImmutableComparableMap[K, V]) Keys() []K
- func (m ImmutableComparableMap[K, V]) Retain(keys ...K) ds.Map[K, V]
- func (m ImmutableComparableMap[K, V]) Size() int
- func (m ImmutableComparableMap[K, V]) Unfreeze() ds.MutableMap[K, V]
- func (m ImmutableComparableMap[K, V]) Values() []V
- type ImmutableHashableMap
- func (m ImmutableHashableMap[K, V]) Clone() ds.Map[K, V]
- func (m ImmutableHashableMap[K, V]) Filter(predicate func(key K) bool) ds.Map[K, V]
- func (ImmutableHashableMap[K, V]) IsImmutable() bool
- func (m ImmutableHashableMap[K, V]) Retain(keys ...K) ds.Map[K, V]
- func (m ImmutableHashableMap[K, V]) Unfreeze() ds.MutableMap[K, V]
- type MutableComparableMap
- func (m MutableComparableMap[K, V]) Clone() ds.MutableMap[K, V]
- func (m MutableComparableMap[K, V]) Filter(predicate func(key K) bool) ds.MutableMap[K, V]
- func (m MutableComparableMap[K, V]) Freeze() ds.Map[K, V]
- func (MutableComparableMap[K, V]) IsImmutable() bool
- func (m MutableComparableMap[K, V]) Retain(keys ...K) ds.MutableMap[K, V]
- func (m *MutableComparableMap[K, V]) ToNative() map[K]V
- type MutableHashableMap
- func (m MutableHashableMap[K, V]) Clear()
- func (m MutableHashableMap[K, V]) Clone() ds.MutableMap[K, V]
- func (m MutableHashableMap[K, V]) Filter(predicate func(key K) bool) ds.MutableMap[K, V]
- func (m MutableHashableMap[K, V]) Freeze() ds.Map[K, V]
- func (MutableHashableMap[K, V]) IsImmutable() bool
- func (m MutableHashableMap[K, V]) Put(key K, value V)
- func (m MutableHashableMap[K, V]) Remove(key K)
- func (m MutableHashableMap[K, V]) Retain(keys ...K) ds.MutableMap[K, V]
- func (m MutableHashableMap[K, V]) TryPut(key K, newValue V) (replaced bool, oldValue V)
- func (m MutableHashableMap[K, V]) TryRemove(key K) (removed bool, removedValue V)
- type NativeMap
- func (m NativeMap[K, V]) Clear()
- func (m NativeMap[K, V]) ContainsKey(key K) bool
- func (m NativeMap[K, V]) Enumerate() iter.Seq2[int, ds.MapEntry[K, V]]
- func (m NativeMap[K, V]) Get(key K) (value V, exists bool)
- func (m NativeMap[K, V]) IsEmpty() bool
- func (m NativeMap[K, V]) Iter() iter.Seq2[K, V]
- func (m NativeMap[K, V]) Keys() []K
- func (m NativeMap[K, V]) Put(key K, value V)
- func (m NativeMap[K, V]) Remove(key K)
- func (m NativeMap[K, V]) Size() int
- func (m NativeMap[K, V]) TryPut(key K, newValue V) (replaced bool, oldValue V)
- func (m NativeMap[K, V]) TryRemove(key K) (removed bool, removedValue V)
- func (m NativeMap[K, V]) Values() []V
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 ¶
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 ¶
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 ¶
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.
Types ¶
type ConcurrentMap ¶
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 ¶
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 ¶
HashableEntry represents a key-value pair where the key implements Hashable.
type HashableMapping ¶
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 ¶
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]) 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 ¶
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 ¶
ContainsKey returns true if the key exists in the map.
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]) TryPut ¶
TryPut adds or updates a key-value pair, returning whether a value was replaced and the old value.