Documentation
¶
Overview ¶
Package hashmap provides a hash map whose entries live off the Go heap.
It uses open addressing with linear probing and backward-shift deletion, hashing keys with hash/maphash. Each slot stores its key's hash, so probes compare hashes before keys and resizing never rehashes.
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 ¶
- 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]) 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]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
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 (*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.