hash

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2023 License: BSD-3-Clause Imports: 2 Imported by: 0

README

hash

Package hash implements a hash map.

Installation:

$ go get modernc.org/hash

Documentation: godoc.org/modernc.org/hash

Purpose

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.

Documentation

Overview

Package hash implements a hash map.

Purpose

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.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cursor

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

Cursor provides enumerating of Map items.

func (*Cursor) Next

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

type Map struct {
	// contains filtered or unexported fields
}

Map is a hash table.

func New

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

func (m *Map) Cursor() *Cursor

Cursor returns a new map Cursor.

func (*Map) Delete

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

Delete removes the element with key k from the map.

func (*Map) Get

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

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

Insert inserts v into the map associating it with k.

func (*Map) Len

func (m *Map) Len() int

Len returns the number of items in the map.

func (*Map) RandDelete

func (m *Map) RandDelete(n int)

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

func (*Map) Vacuum

func (m *Map) Vacuum()

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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