hashmap

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Int added in v0.4.0

func Int[K Integer](seed uint64, key K) uint64

Int is a Hasher for integer keys, about five times faster than the default. It is not collision resistant, so an attacker who controls keys and learns the seed could degrade a Map; use the default for untrusted keys.

Types

type Hasher added in v0.4.0

type Hasher[K any] func(seed uint64, key K) uint64

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 New

func New[K comparable, V any](a memory.Allocator, capacity int) (*Map[K, V], error)

New returns an empty Map with room for capacity entries, hashing with hash/maphash.

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

func (m *Map[K, V]) All() iter.Seq2[K, V]

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]) Clear added in v0.5.0

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

Clear removes every entry, keeping the memory for reuse.

func (*Map[K, V]) Clip

func (m *Map[K, V]) Clip() error

Clip shrinks the table to the smallest size that holds the current entries.

func (*Map[K, V]) Contains added in v0.5.0

func (m *Map[K, V]) Contains(key K) bool

Contains reports whether key is present.

func (*Map[K, V]) Delete

func (m *Map[K, V]) Delete(key K) bool

Delete removes key and reports whether it was present.

func (*Map[K, V]) Free

func (m *Map[K, V]) Free() error

Free releases the entries. m must not be used afterwards.

func (*Map[K, V]) Get

func (m *Map[K, V]) Get(key K) (V, bool)

Get returns the value for key, or reports false if key is absent.

func (*Map[K, V]) GetMany added in v0.4.0

func (m *Map[K, V]) GetMany(keys []K, values []V, found []bool)

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.

func (*Map[K, V]) GetOrSet added in v0.5.0

func (m *Map[K, V]) GetOrSet(key K, value V) (V, bool, error)

GetOrSet returns the value for key and true if key is present. Otherwise it stores value and returns it and false, hashing key only once either way.

func (*Map[K, V]) Keys

func (m *Map[K, V]) Keys() iter.Seq[K]

Keys yields every key.

func (*Map[K, V]) Len

func (m *Map[K, V]) Len() int

Len returns the number of entries.

func (*Map[K, V]) Ptr

func (m *Map[K, V]) Ptr(key K) (*V, bool)

Ptr returns a pointer to the value for key, or reports false if key is absent.

func (*Map[K, V]) Set

func (m *Map[K, V]) Set(key K, value V) error

Set stores value under key.

func (*Map[K, V]) Upsert added in v0.5.0

func (m *Map[K, V]) Upsert(key K, f func(old V, found bool) V) error

Upsert stores f(old, true) under key if key is present, and f(zero, false) otherwise, hashing key only once. f must not modify m.

func (*Map[K, V]) Values

func (m *Map[K, V]) Values() iter.Seq[V]

Values yields every value.

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

func NewStrings[V any](a memory.Allocator, capacity int) (*Strings[V], error)

NewStrings returns an empty Strings with room for capacity entries.

func (*Strings[V]) All added in v0.4.0

func (m *Strings[V]) All() iter.Seq2[string, V]

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]) Clear added in v0.5.0

func (m *Strings[V]) Clear() error

Clear removes every entry, releasing the keys and keeping the table's memory.

func (*Strings[V]) Clip added in v0.4.0

func (m *Strings[V]) Clip() error

Clip shrinks the table to the smallest size that holds the current entries.

func (*Strings[V]) Contains added in v0.5.0

func (m *Strings[V]) Contains(key string) bool

Contains reports whether key is present.

func (*Strings[V]) Delete added in v0.4.0

func (m *Strings[V]) Delete(key string) (bool, error)

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

func (m *Strings[V]) Free() error

Free releases the keys and entries. m must not be used afterwards.

func (*Strings[V]) Get added in v0.4.0

func (m *Strings[V]) Get(key string) (V, bool)

Get returns the value for key, or reports false if key is absent.

func (*Strings[V]) GetBytes added in v0.4.0

func (m *Strings[V]) GetBytes(key []byte) (V, bool)

GetBytes is Get for a key held as bytes, without converting it to a string.

func (*Strings[V]) Keys added in v0.5.0

func (m *Strings[V]) Keys() iter.Seq[string]

Keys yields every key, under the same rules as All.

func (*Strings[V]) Len added in v0.4.0

func (m *Strings[V]) Len() int

Len returns the number of entries.

func (*Strings[V]) Ptr added in v0.4.0

func (m *Strings[V]) Ptr(key string) (*V, bool)

Ptr returns a pointer to the value for key, or reports false if key is absent.

func (*Strings[V]) Set added in v0.4.0

func (m *Strings[V]) Set(key string, value V) error

Set stores value under key, copying key off-heap if it is new.

func (*Strings[V]) Values added in v0.5.0

func (m *Strings[V]) Values() iter.Seq[V]

Values yields every value, under the same rules as All.

Jump to

Keyboard shortcuts

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