Documentation
¶
Overview ¶
Package hashmap provides hash maps whose entries live off the Go heap.
Map is keyed by any comparable pointer-free type, and Strings by text. Both use a Swiss table: groups of eight slots with one control byte each, so a u64-to-u64 map costs 17 bytes per slot at up to 7/8 load. Keys hash with hash/maphash unless a Map is given a faster Hasher.
Example ¶
package main
import (
"fmt"
memory "github.com/stubbedev/memory-go"
"github.com/stubbedev/memory-go/hashmap"
)
func main() {
var a memory.Local
defer a.Close()
type pos struct{ X, Y int32 }
m, err := hashmap.New[pos, uint8](&a, 16)
if err != nil {
panic(err)
}
defer m.Free()
if err := m.Set(pos{1, 2}, 7); err != nil {
panic(err)
}
if p, ok := m.Ptr(pos{1, 2}); ok {
*p++
}
v, ok := m.Get(pos{1, 2})
_, missing := m.Get(pos{2, 1})
fmt.Println(v, ok, missing, m.Len())
}
Output: 8 true false 1
Index ¶
- func Int[K Integer](seed uint64, key K) uint64
- type Hasher
- type Integer
- type Map
- func (m *Map[K, V]) All() iter.Seq2[K, V]
- func (m *Map[K, V]) Clip() error
- func (m *Map[K, V]) Delete(key K) bool
- func (m *Map[K, V]) Free() error
- func (m *Map[K, V]) Get(key K) (V, bool)
- func (m *Map[K, V]) GetMany(keys []K, values []V, found []bool)
- func (m *Map[K, V]) Keys() iter.Seq[K]
- func (m *Map[K, V]) Len() int
- func (m *Map[K, V]) Ptr(key K) (*V, bool)
- func (m *Map[K, V]) Set(key K, value V) error
- func (m *Map[K, V]) Values() iter.Seq[V]
- type Strings
- func (m *Strings[V]) All() iter.Seq2[string, V]
- func (m *Strings[V]) Clip() error
- func (m *Strings[V]) Delete(key string) (bool, error)
- func (m *Strings[V]) Free() error
- func (m *Strings[V]) Get(key string) (V, bool)
- func (m *Strings[V]) GetBytes(key []byte) (V, bool)
- func (m *Strings[V]) Len() int
- func (m *Strings[V]) Ptr(key string) (*V, bool)
- func (m *Strings[V]) Set(key string, value V) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Hasher ¶ added in v0.4.0
Hasher hashes a key under a seed chosen per Map. Equal keys must hash equal.
type Integer ¶ added in v0.4.0
type Integer interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}
Integer is the set of key types Int hashes.
type Map ¶
type Map[K comparable, V any] struct { // contains filtered or unexported fields }
Map is a hash map with off-heap entries. It is not safe for concurrent use. Iteration order is unspecified. Pointers into it are valid until the next call that may grow or shrink it.
func NewHashed ¶ added in v0.4.0
func NewHashed[K comparable, V any](a memory.Allocator, capacity int, h Hasher[K]) (*Map[K, V], error)
NewHashed returns an empty Map with room for capacity entries, hashing with h under a random seed.
func (*Map[K, V]) All ¶
All yields every key and value. Updating the value of an existing key during the loop is allowed; adding or deleting keys, or Clip, panics.
func (*Map[K, V]) Clip ¶
Clip shrinks the table to the smallest size that holds the current entries.
func (*Map[K, V]) GetMany ¶ added in v0.4.0
GetMany looks up every key, storing each value in values and whether it was found in found, which must be at least as long as keys. On a table larger than the CPU caches it beats calling Get in a loop, because it starts each batch's cache misses together rather than one after another.
type Strings ¶ added in v0.4.0
type Strings[V any] struct { // contains filtered or unexported fields }
Strings is a hash map from text to V with both keys and values off-heap, so it costs the garbage collector nothing however many entries it holds, unlike a map[string]V. Lookups do not allocate. It is not safe for concurrent use. Iteration order is unspecified. Pointers into it are valid until the next call that may grow or shrink it.
func NewStrings ¶ added in v0.4.0
NewStrings returns an empty Strings with room for capacity entries.
func (*Strings[V]) All ¶ added in v0.4.0
All yields every key and value. The keys read off-heap memory and are valid until deleted; clone them with strings.Clone to keep them longer. Updating the value of an existing key during the loop is allowed; adding or deleting keys, or Clip, panics.
func (*Strings[V]) Clip ¶ added in v0.4.0
Clip shrinks the table to the smallest size that holds the current entries.
func (*Strings[V]) Delete ¶ added in v0.4.0
Delete removes key and reports whether it was present, or returns an error if releasing the key failed.
func (*Strings[V]) Free ¶ added in v0.4.0
Free releases the keys and entries. m must not be used afterwards.
func (*Strings[V]) Get ¶ added in v0.4.0
Get returns the value for key, or reports false if key is absent.
func (*Strings[V]) GetBytes ¶ added in v0.4.0
GetBytes is Get for a key held as bytes, without converting it to a string.