hashmap

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package hashmap implements a map backed by a hash table.

Elements are unordered in the map.

Structure is not thread safe.

Reference: http://en.wikipedia.org/wiki/Associative_array

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Map

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

func New

func New[K cmp.Ordered, V any]() *Map[K, V]

New instantiates a hash map with no key comparator.

func NewWith added in v0.4.0

func NewWith[K comparable, V any](cmp cmp.Comparator[K], size int) *Map[K, V]

NewWith instantiates a hash map with the specified size and key comparator. If cmp is nil, SortedKeys will panic or produce undefined behavior.

func (*Map[K, V]) Clear

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

Clear removes all key-value pairs from the map.

func (*Map[K, V]) Clone added in v0.4.0

func (m *Map[K, V]) Clone() container.Map[K, V]

Clone returns a new Map containing all key-value pairs from the current map, with the same comparator.

func (*Map[K, V]) Delete added in v0.4.0

func (m *Map[K, V]) Delete(key K) (value V, found bool)

Delete removes the key-value pair associated with the specified key. Returns the value and true if the key was found and removed, false if the key was not present.

func (*Map[K, V]) Entries added in v0.4.0

func (m *Map[K, V]) Entries() (keys []K, values []V)

SortedEntries returns two slices containing all keys and values in the map, sorted by keys using m.cmp.

func (*Map[K, V]) Get

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

Get retrieves the value associated with the specified key. Returns the value and true if the key was found, or the zero value of V and false if not.

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

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

Has returns true if the specified key is present in the map, false otherwise.

func (*Map[K, V]) IsEmpty added in v0.4.0

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

IsEmpty returns true if the map contains no key-value pairs.

func (*Map[K, V]) Iter added in v0.4.0

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

Iter returns an iterator over key-value pairs from m. The iteration order is not specified and is not guaranteed to be the same from one call to the next.

func (*Map[K, V]) Keys

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

Keys returns a slice containing all keys in the map, sorted using the comparator.

func (*Map[K, V]) Len added in v0.4.0

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

Len returns the number of key-value pairs in the map.

func (*Map[K, V]) MarshalJSON

func (m *Map[K, V]) MarshalJSON() ([]byte, error)

MarshalJSON outputs the JSON representation of the map.

func (*Map[K, V]) Put

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

Put associates the specified value with the given key in the map. If the key already exists, its value is updated with the new value.

func (*Map[K, V]) String

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

String returns a string representation of the map.

func (*Map[K, V]) ToSlice added in v0.4.0

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

ToSlice returns a slice containing all values in the map (in random order).

func (*Map[K, V]) UnmarshalJSON

func (m *Map[K, V]) UnmarshalJSON(data []byte) error

UnmarshalJSON populates the map from the input JSON representation. The comparator is preserved.

func (*Map[K, V]) UnsortedEntries added in v0.8.0

func (m *Map[K, V]) UnsortedEntries() (keys []K, values []V)

UnsortedEntries returns two slices containing all keys and values in the map (in random order).

func (*Map[K, V]) UnsortedKeys added in v0.8.0

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

UnsortedKeys returns a slice containing all keys in the map (in random order).

func (*Map[K, V]) UnsortedValues added in v0.8.0

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

UnsortedValues returns a slice containing all values in the map (in random order).

func (*Map[K, V]) Values

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

Values returns a slice containing all values in the map, sorted by their corresponding keys using m.cmp.

Jump to

Keyboard shortcuts

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