Documentation ¶
Index ¶
- type Builder
- type Entry
- type Map
- func (ht *Map[K, V]) Clear()
- func (ht *Map[K, V]) Delete(key K)
- func (ht *Map[K, V]) Get(key K) (value V, ok bool)
- func (ht *Map[K, V]) Index(value K) (index uint32, err error)
- func (ht *Map[K, V]) Iter() <-chan entry[K, V]
- func (ht *Map[K, V]) Keys() []K
- func (ht *Map[K, V]) Len() int
- func (ht *Map[K, V]) LoadFactor() float32
- func (ht *Map[K, V]) Resize()
- func (ht *Map[K, V]) Set(key K, value V)
- func (ht *Map[K, V]) Size() uint32
- func (ht *Map[K, V]) String() string
- func (ht *Map[K, V]) Values() []V
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
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 (*Map[K, V]) Index ¶
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]) LoadFactor ¶
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.