hashmaps

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2025 License: MIT Imports: 5 Imported by: 0

README

Golang hashmaps

GoDoc MIT License

This package collects several hashmap implementations:

  • Unordered hashmap is a classic hashmap with separate chaining in a single linked list per bucket to handle collisions.
  • Robin Hood hashmap is an open addressing hashmap with robin hood hashing and back shifting.
  • Hopscotch hashmap is an open addressing hashmap with worst case constant runtime for lookup and delete operations.
  • Flat hashmap is an open addressing hashmap with linear probing.

Getting started

go get -u github.com/EinfachAndy/hashmaps

Example usage

package main

import (
	"fmt"

	"github.com/EinfachAndy/hashmaps/hopscotch"
)

func main() {
	m := hopscotch.New[int, int]()
	m.Reserve(100)
	m.Put(1, 1)
	fmt.Println(m.Get(1))
	m.Remove(1)
	fmt.Println(m.Get(1))

	// Output:
	// 1 true
	// 0 false
}

Benchmarks

The benchmarks are implemented and maintained here.

Contributing

If you would like to contribute a new feature or hashmap. please let me know first what you would like to add (via email or issue tracker).

Documentation

Overview

Package hashmaps collects several types of hashmaps.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config added in v0.5.0

type Config[K comparable, V any] struct {
	Type Type
	// Size grows the hashmap to the desired size.
	// If unset `DefaultSize` is used.
	Size uintptr
	// MaxLoad changes the load factor of the hashmap.
	// This value is a trade-off between performance and memory consumption.
	// If unset `DefaultMaxLoad` is used.
	MaxLoad float32
	// Hasher that is used. Must be configured for complex data types or slices.
	// If unset a default hasher is used for golang basic types.
	Hasher shared.HashFn[K]
	// Empty is used by some hash hashmap implementations e.g.: flat hashmap
	// to track empty buckets.
	Empty K
}

Config is used by the factory to create and configure a hashmap instance.

type HashMap added in v0.5.0

type HashMap[K comparable, V any] struct {
	Get     func(key K) (V, bool)
	Put     func(key K, val V) bool
	Remove  func(key K) bool
	Reserve func(n uintptr)
	Load    func() float32
	Clear   func()
	Size    func() int
	Each    func(fn func(key K, val V) bool)
	MaxLoad func(lf float32) error
}

HashMap is the basic hashmap interface as a set of function points.

func MustNewHashMap added in v0.5.0

func MustNewHashMap[K comparable, V any](cfg Config[K, V]) *HashMap[K, V]

MustNewHashMap same as 'NewHashMap' but panics if and only if an error occurs.

func NewHashMap added in v0.5.0

func NewHashMap[K comparable, V any](cfg Config[K, V]) (*HashMap[K, V], error)

NewHashMap is a factory function to instantiate different kind of generic hashmap implementations. A struct with function pointers is used as interface. In most cases the usage of the dedicate hashmap type is recommended.

type Type added in v0.5.0

type Type int

Type specified the type of the hashmap.

const (
	Hopscotch Type = 0
	Robin     Type = 1
	Unordered Type = 2
	Flat      Type = 3
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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