hashmap

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2022 License: MIT Imports: 1 Imported by: 0

README

hashmap

import "github.com/zendesk/generic/hashmap"

Package hashmap provides an implementation of a hashmap. The map uses linear probing and automatically resizes. The map can also be efficiently copied, and will perform copies lazily, using copy-on-write. However, the copy-on-write will copy the entire map after the first write. One can imagine a more efficient implementation that would split the map into chunks and use copy-on-write selectively for each chunk.

Example

package main

import (
	"fmt"
	g "github.com/zendesk/generic"
	"github.com/zendesk/generic/hashmap"
)

func main() {
	m := hashmap.New[string, int](1, g.Equals[string], g.HashString)
	m.Put("foo", 42)
	m.Put("bar", 13)

	fmt.Println(m.Get("foo"))
	fmt.Println(m.Get("baz"))

	m.Remove("foo")

	fmt.Println(m.Get("foo"))

}
Output
42 true
0 false
0 false

Index

type Map

A Map is a hashmap that supports copying via copy-on-write.

type Map[K, V any] struct {
    // contains filtered or unexported fields
}
func New
func New[K, V any](capacity uint64, equals g.EqualsFn[K], hash g.HashFn[K]) *Map[K, V]

New constructs a new map with the given capacity.

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

Copy returns a copy of this map. The copy will not allocate any memory until the first write, so any number of read-only copies can be made without any additional allocations.

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

Each calls 'fn' on every key-value pair in the hashmap in no particular order.

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

Get returns the value stored for this key, or false if there is no such value.

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

Put maps the given key to the given value. If the key already exists its value will be overwritten with the new value.

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

Remove removes the specified key-value pair from the map.

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

Size returns the number of items in the map.

Generated by gomarkdoc

Documentation

Overview

Package hashmap provides an implementation of a hashmap. The map uses linear probing and automatically resizes. The map can also be efficiently copied, and will perform copies lazily, using copy-on-write. However, the copy-on-write will copy the entire map after the first write. One can imagine a more efficient implementation that would split the map into chunks and use copy-on-write selectively for each chunk.

Example
package main

import (
	"fmt"

	g "github.com/zendesk/generic"
	"github.com/zendesk/generic/hashmap"
)

func main() {
	m := hashmap.New[string, int](1, g.Equals[string], g.HashString)
	m.Put("foo", 42)
	m.Put("bar", 13)

	fmt.Println(m.Get("foo"))
	fmt.Println(m.Get("baz"))

	m.Remove("foo")

	fmt.Println(m.Get("foo"))

}
Output:

42 true
0 false
0 false

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Map

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

A Map is a hashmap that supports copying via copy-on-write.

func New

func New[K, V any](capacity uint64, equals g.EqualsFn[K], hash g.HashFn[K]) *Map[K, V]

New constructs a new map with the given capacity.

func (*Map[K, V]) Copy

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

Copy returns a copy of this map. The copy will not allocate any memory until the first write, so any number of read-only copies can be made without any additional allocations.

func (*Map[K, V]) Each

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

Each calls 'fn' on every key-value pair in the hashmap in no particular order.

func (*Map[K, V]) Get

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

Get returns the value stored for this key, or false if there is no such value.

func (*Map[K, V]) Put

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

Put maps the given key to the given value. If the key already exists its value will be overwritten with the new value.

func (*Map[K, V]) Remove

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

Remove removes the specified key-value pair from the map.

func (*Map[K, V]) Size

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

Size returns the number of items in the map.

Jump to

Keyboard shortcuts

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