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 ¶
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 ¶
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) Delete ¶
func (m *Map) Delete(k interface{})
Delete removes the element with key k from the map.
func (*Map) Get ¶
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) RandDelete ¶
RandDelete randomly deletes up to n items from m when n is greater than zero.