intmap

package module
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2024 License: BSD-2-Clause Imports: 2 Imported by: 3

README

Fast hashmap with integer keys for Golang

GoDoc Go Report Card

intmap

import "github.com/kamstrup/intmap"

Package intmap is a fast hashmap implementation for Golang, specialized for maps with integer type keys. The values can be of any type.

It is a full port of https://github.com/brentp/intintmap to use type parameters (aka generics).

It interleaves keys and values in the same underlying array to improve locality. This is also known as open addressing with linear probing.

It is up to 3X faster than the builtin map:

name                             time/op
Map64Fill-8                       201ms ± 5%
IntIntMapFill-8                   207ms ±31%
StdMapFill-8                      371ms ±11%
Map64Get10PercentHitRate-8        148µs ±40%
IntIntMapGet10PercentHitRate-8    171µs ±50%
StdMapGet10PercentHitRate-8       171µs ±33%
Map64Get100PercentHitRate-8      4.50ms ± 5%
IntIntMapGet100PercentHitRate-8  4.82ms ± 6%
StdMapGet100PercentHitRate-8     15.5ms ±32%

Usage

m := intmap.New[int64,int64](32768)
m.Put(int64(1234), int64(-222))
m.Put(int64(123), int64(33))

v, ok := m.Get(int64(222))
v, ok := m.Get(int64(333))

m.Del(int64(222))
m.Del(int64(333))

fmt.Println(m.Len())

m.ForEach(func(k int64, v int64) {
    fmt.Printf("key: %d, value: %d\n", k, v)
})

m.Clear() // all gone, but buffers kept

Documentation

Overview

Package intmap contains a fast hashmap implementation for maps with keys of any integer type

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IntKey

type IntKey interface {
	~int | ~uint | ~int64 | ~uint64 | ~int32 | ~uint32 | ~int16 | ~uint16 | ~int8 | ~uint8 | ~uintptr
}

IntKey is a type constraint for values that can be used as keys in Map

type Map

type Map[K IntKey, V any] struct {
	// contains filtered or unexported fields
}

Map is a hashmap where the keys are some any integer type. It is valid to call methods that read a nil map, similar to a standard Go map. Methods valid on a nil map are Has, Get, Len, and ForEach.

func New

func New[K IntKey, V any](capacity int) *Map[K, V]

New creates a new map with keys being any integer subtype. The map can store up to the given capacity before reallocation and rehashing occurs.

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

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

All returns an iterator over key-value pairs from m. The iterator returns immediately if invoked on a nil map.

The iteration order of a Map is not defined, so please avoid relying on it.

func (*Map[K, V]) Clear

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

Clear removes all items from the map, but keeps the internal buffers for reuse.

func (*Map[K, V]) Del

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

Del deletes a key and its value, returning true iff the key was found

func (*Map[K, V]) ForEach

func (m *Map[K, V]) ForEach(f func(K, V) bool)

ForEach iterates through key-value pairs in the map while the function f returns true. This method returns immediately if invoked on a nil map.

The iteration order of a Map is not defined, so please avoid relying on it.

func (*Map[K, V]) Get

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

Get returns the value if the key is found. If you just need to check for existence it is easier to use Has. Calling this method on a nil map will return the zero value for V and false.

func (*Map[K, V]) Has added in v0.1.2

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

Has checks if the given key exists in the map. Calling this method on a nil map will return false.

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

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

Keys returns an iterator over keys in m. The iterator returns immediately if invoked on a nil map.

The iteration order of a Map is not defined, so please avoid relying on it.

func (*Map[K, V]) Len

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

Len returns the number of elements in the map. The length of a nil map is defined to be zero.

func (*Map[K, V]) Put

func (m *Map[K, V]) Put(key K, val V)

Put adds or updates key with value val.

func (*Map[K, V]) PutIfNotExists added in v0.1.2

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

PutIfNotExists adds the key-value pair only if the key does not already exist in the map, and returns the current value associated with the key and a boolean indicating whether the value was newly added or not.

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

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

Values returns an iterator over values in m. The iterator returns immediately if invoked on a nil map.

The iteration order of a Map is not defined, so please avoid relying on it.

type Set added in v0.3.0

type Set[K IntKey] Map[K, struct{}]

Set is a specialization of Map modelling a set of integers. Like Map, methods that read from the set are valid on the nil Set. This include Has, Len, and ForEach.

func NewSet added in v0.3.0

func NewSet[K IntKey](capacity int) *Set[K]

NewSet creates a new Set with a given initial capacity.

func (*Set[K]) Add added in v0.3.0

func (s *Set[K]) Add(k K) bool

Add an element to the set. Returns true if the element was not already present.

func (*Set[K]) All added in v0.5.0

func (s *Set[K]) All() iter.Seq[K]

All returns an iterator over keys from the set. The iterator returns immediately if the set is nil.

The iteration order of a Set is not defined, so please avoid relying on it.

func (*Set[K]) Clear added in v0.4.0

func (s *Set[K]) Clear()

Clear removes all items from the Set, but keeps the internal buffers for reuse.

func (*Set[K]) Del added in v0.4.0

func (s *Set[K]) Del(k K) bool

Del deletes a key, returning true iff the key was found

func (*Set[K]) ForEach added in v0.3.0

func (s *Set[K]) ForEach(visit func(k K) bool)

ForEach iterates over the elements in the set while the visit function returns true. This method returns immediately if the set is nil.

The iteration order of a Set is not defined, so please avoid relying on it.

func (*Set[K]) Has added in v0.3.0

func (s *Set[K]) Has(k K) bool

Has returns true if the key is in the set. If the set is nil this method always return false.

func (*Set[K]) Len added in v0.3.0

func (s *Set[K]) Len() int

Len returns the number of elements in the set. If the set is nil this method return 0.

Jump to

Keyboard shortcuts

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