Documentation ¶
Overview ¶
Package hashmap provides an implementation of a hashmap. The map uses linear probing and automatically resizes. The map can also be efficiently copied, and will perform copies lazily, using copy-on-write. However, the copy-on-write will copy the entire map after the first write. One can imagine a more efficient implementation that would split the map into chunks and use copy-on-write selectively for each chunk.
Example ¶
package main import ( "fmt" g "github.com/fufuok/utils/generic" "github.com/fufuok/utils/generic/hashmap" ) func main() { m := hashmap.New[string, int](1, g.Equals[string], g.HashString) m.Put("foo", 42) m.Put("bar", 13) fmt.Println(m.Get("foo")) fmt.Println(m.Get("baz")) m.Remove("foo") fmt.Println(m.Get("foo")) }
Output: 42 true 0 false 0 false
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Map ¶
type Map[K, V any] struct { // contains filtered or unexported fields }
A Map is a hashmap that supports copying via copy-on-write.
func (*Map[K, V]) Copy ¶
Copy returns a copy of this map. The copy will not allocate any memory until the first write, so any number of read-only copies can be made without any additional allocations.
func (*Map[K, V]) Each ¶
func (m *Map[K, V]) Each(fn func(key K, val V))
Each calls 'fn' on every key-value pair in the hashmap in no particular order.
func (*Map[K, V]) Get ¶
Get returns the value stored for this key, or false if there is no such value.
func (*Map[K, V]) Put ¶
func (m *Map[K, V]) Put(key K, val V)
Put maps the given key to the given value. If the key already exists its value will be overwritten with the new value.