hashmap

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 6 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 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

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 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.

func (*Map[K, V]) All

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

All yields every key and value.

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]) 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]) 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]) Values

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

Values yields every value.

Jump to

Keyboard shortcuts

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