hash: modernc.org/hash Index | Files | Directories

package hash

import "modernc.org/hash"

Package hash implements a hash map.


Maps provided by this package can be useful when using a key type that is not comparable at the language level, like for example a slice or types containing slices etc.

Such types are forbidden as keys of the builtin Go maps for good reasons. Care must be taken to not modify keys inserted into a Map.

Generic types

Keys and their associated values are interface{} typed, similar to all of the containers in the standard library.

Semiautomatic production of a type specific variant of this package is supported via

$ make generic

This command will write to stdout a version of the hash.go file where every key type occurrence is replaced by the word 'KEY' and every value type occurrence is replaced by the word 'VALUE'. Then you have to replace these tokens with your desired type(s), using any technique you're comfortable with.

This is how, for example, 'example/int.go' was created:

$ mkdir example
$ make generic | sed -e 's/KEY/*big.Int/g' -e 's/VALUE/*big.Int/g' > example/int.go

After adding import "math/big", no other changes to int.go are necessary, it compiles just fine.


Package Files

doc.go hash.go

type Cursor Uses

type Cursor struct {
    K   interface{} /*K*/
    V   interface{} /*V*/
    // contains filtered or unexported fields

Cursor provides enumerating of Map items.

func (*Cursor) Next Uses

func (c *Cursor) Next() bool

Next moves the cursor to the next item in the map and sets the K and V fields accordingly. It returns true on success, or false if there is no next item.

Every use of the K/V fields, even the first one, must be preceded by a call to Next, for example

for c := m.Cursor(); c.Next(); {
	... c.K, c.V valid here

The iteration order is not specified and is not guaranteed to be the same from one iteration to the next. If a map entry that has not yet been reached is removed during iteration, the corresponding iteration value will not be produced. If a map entry is created during iteration, that entry may be produced during the iteration or may be skipped. The choice may vary for each entry created and from one iteration to the next.

type Map Uses

type Map struct {
    // contains filtered or unexported fields

Map is a hash table.

func New Uses

func New(hash func(interface{}) int64, eq func(a, b interface{}) bool, initialCapacity int) *Map

New returns a newly created Map. The hash function takes a key and returns its hash. The eq function takes two keys and returns whether they are equal.

func (*Map) Cursor Uses

func (m *Map) Cursor() *Cursor

Cursor returns a new map Cursor.

func (*Map) Delete Uses

func (m *Map) Delete(k interface{})

Delete removes the element with key k from the map.

func (*Map) Get Uses

func (m *Map) Get(k interface{}) (r interface{}, ok bool)

Get returns the value associated with k and a boolean value indicating whether the key is in the map.

func (*Map) Insert Uses

func (m *Map) Insert(k interface{}, v interface{})

Insert inserts v into the map associating it with k.

func (*Map) Len Uses

func (m *Map) Len() int

Len returns the number of items in the map.

func (*Map) RandDelete Uses

func (m *Map) RandDelete(n int)

RandDelete randomly deletes up to n items from m when n is greater than zero.

func (*Map) Vacuum Uses

func (m *Map) Vacuum()

Vacuum rebuilds m, repacking it into a possibly smaller amount of memory.



Package hash imports 2 packages (graph) and is imported by 1 packages. Updated 2020-10-21. Refresh now. Tools for package owners.