hashmap

package
v0.1.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

type Builder[K, V any] []*Entry[K, V]

Builder is a helper to build a Map.

func (*Builder[K, V]) Build

func (ht *Builder[K, V]) Build(threshold float32) *Map[K, V]

Build returns the Map with all the entries.

type Entry

type Entry[K, V any] struct {
	Key   K
	Value V
}

Entry mocks an entry in the Map.

type Map

type Map[K, V any] struct {

	// Threshold is the maximum load factor before resizing the hash table.
	// Must be a value between zero and one.
	// Usually set to 0.75.
	Threshold float32
	// contains filtered or unexported fields
}

Map represents a Map. Use the hash collision resolution technique of separate chaining. https://en.wikipedia.org/wiki/Hash_table#Separate_chaining

func NewMap

func NewMap[K, V any](size uint32, threshold float32) *Map[K, V]

NewMap returns a new Map with the given size and threshold.

func (*Map[K, V]) Clear

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

Clear removes all items from the Map.

func (*Map[K, V]) Delete

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

Delete removes an item from the Map.

func (*Map[K, V]) Get

func (ht *Map[K, V]) Get(key K) (value V, ok bool)

Get returns the value associated with the key.

func (*Map[K, V]) Index

func (ht *Map[K, V]) Index(value K) (index uint32, err error)

Index returns the index of the slot in the hash table where the value should be stored.

It uses Marshal to convert the value to a byte slice, then hashes the byte slice using FNV-1a. The hash is then modded by the size of the hash table to get the index.

func (*Map[K, V]) Iter

func (ht *Map[K, V]) Iter() <-chan entry[K, V]

Iter returns a channel that iterates over all items in the Map.

func (*Map[K, V]) Keys

func (ht *Map[K, V]) Keys() []K

Keys return a slice of all keys in the Map.

func (*Map[K, V]) Len

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

Len returns the number of items in the Map.

func (*Map[K, V]) LoadFactor

func (ht *Map[K, V]) LoadFactor() float32

LoadFactor returns the load factor of the Map.

The load factor is: number of items / *number of slots*

func (*Map[K, V]) Resize

func (ht *Map[K, V]) Resize()

Resize changes the size of the Map.

The new size is calculated by doubling the current size and finding the next prime number. https://planetmath.org/goodhashtableprimes suggests using prime numbers for the size of the hash table. This helps reduce collisions and distribute the items more evenly.

func (*Map[K, V]) Set

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

Set adds an item to the Map.

func (*Map[K, V]) Size

func (ht *Map[K, V]) Size() uint32

Size returns the size of the Map.

func (*Map[K, V]) String

func (ht *Map[K, V]) String() string

String returns a string representation of the Map.

func (*Map[K, V]) Values

func (ht *Map[K, V]) Values() []V

Values return a slice of all values in the Map.

Jump to

Keyboard shortcuts

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