hashmap

package
v0.0.0-...-e62b5e0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2021 License: 0BSD Imports: 11 Imported by: 0

Documentation

Overview

Package hashmap implements a persistent HAMT based hashmap. This package is inspired by on Clojure's persistent and transient hashmap implementations. See https://lampwww.epfl.ch/papers/idealhashtrees.pdf for more information on the algoritm.

A note about Key and Value equality. If you would like to override the default go equality operator for keys and values in this map library implement the Equal(other interface{}) bool function for the type. Otherwise '==' will be used with all its restrictions.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Entry

type Entry interface {
	Key() interface{}
	Value() interface{}
}

Entry is a map entry. Each entry consists of a key and value.

func EntryNew

func EntryNew(key, value interface{}) Entry

EntryNew constructs a map entry that may be used with Conj.

type Iterator

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

Iterator is a mutable iterator for a map. It has a fixed size stack, the size of which is computed from the maximum number of nested nodes possible based on the branching factor and the size of the hash type.

func (*Iterator) HasNext

func (i *Iterator) HasNext() bool

HasNext is true when there are more elements to be iterated over.

func (*Iterator) Next

func (i *Iterator) Next() (k, v interface{})

Next provides the next key value pair and increments the cursor.

type Map

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

Map is a persistent immutable map. Operations on map returns a new map that shares much of the structure with the original map.

func Empty

func Empty() *Map

Empty returns a new empty persistent map with a random hashSeed.

Example
// Empty returns a new empty map with a unique hashseed.
m := Empty()
fmt.Println(m)
Output:

{ }

func From

func From(value interface{}) *Map

From will convert many different go types to an immutable map. Converting some types is more efficient than others and the mechanisms are described below.

*Map:

Returned directly as it is already immutable.

*TMap:

AsPersistent is called on it and the result is returned.

map[interface{}]interface{}:

Converted directly by looping over the map and calling Assoc starting with an empty transient map. The transient map is the converted to a persistent one and returned.

[]Entry:

The entries are looped over and Assoc is called on an empty transient map. The transient map is converted to a persistent map and then returned.

[]interface{}:

The elements are passed to New.

map[kT]vT:

Reflection is used to loop over the entries of the map and associate them with an empty transient map. The transient map is converted to a persistent map and then returned.

[]T:

Reflection is used to convert the slice to []interface{} and then passed to New.
Example (Entries)
// From generates a map from several different types.
// One of these types is a slice of entry types.
sl := []Entry{
	&tent{key: "a", value: false},
	&tent{key: "b", value: true},
}
m := From(sl)
fmt.Println(m)
Output:

Example (Map)
// From generates a map from several different types.
// One of these types are go native maps.
m := From(map[string]bool{"a": true, "b": false})
fmt.Println(m)
Output:

Example (Slice)
// From generates a map from several different types.
// One of these types is a slice of arbitray type
// that has an even number of elements.
m := From([]int{1, 2, 3, 4})
fmt.Println(m)
Output:

func New

func New(elems ...interface{}) *Map

New converts a list of elements to a persistent map by associating them pairwise. New will panic if the number of elements is not even.

Example
// New generates pairs from a list of keys and values
m := New("a", true, "b", false)
fmt.Println(m)

// It is equivalent to the following code using go's
// native map type
gm := map[string]bool{"a": true, "b": false}
fmt.Println(gm)
Output:

func (*Map) Apply

func (m *Map) Apply(args ...interface{}) interface{}

Apply takes an arbitrary number of arguments and returns the value At the first argument. Apply allows map to be called as a function by the 'dyn' library.

func (*Map) AsNative

func (m *Map) AsNative() map[interface{}]interface{}

AsNative returns the map converted to a go native map type.

Example
m := New("a", true, "b", false)
gm := m.AsNative()
fmt.Printf("%T\n", gm)
Output:

map[interface {}]interface {}

func (*Map) AsTransient

func (m *Map) AsTransient() *TMap

AsTransient will return a transient map that shares structure with the persistent map.

func (*Map) Assoc

func (m *Map) Assoc(key, value interface{}) *Map

Assoc associates a value with a key in the map. A new persistent map is returned if the key and value are different from one already in the map, if the entry is already in the map the original map is returned.

Example
// Assoc is similar to the go builtin m[k]=v operation, except
// it does not modify the map in place.
gm := map[string]bool{"a": true, "b": false}
m := From(gm)

m = m.Assoc("c", true)
gm["c"] = true

fmt.Println(dyn.Equal(m, From(gm)))
Output:

true

func (*Map) At

func (m *Map) At(key interface{}) interface{}

At returns the value associated with the key. If one is not found, nil is returned.

Example
// At is similar to the go builtin operator m[k].
gm := map[string]bool{"a": true, "b": false}
m := From(gm)
fmt.Println(m.At("a"))
fmt.Println(gm["a"])
Output:

true
true

func (*Map) Conj

func (m *Map) Conj(value interface{}) interface{}

Conj takes a value that must be an Entry. Conj implements a generic mechanism for building collections.

func (*Map) Contains

func (m *Map) Contains(key interface{}) bool

Contains will test if the key exists in the map.

Example
gm := map[string]bool{"a": true, "b": false}
m := From(gm)

fmt.Println(m.Contains("a"))

_, contains := gm["a"]
fmt.Println(contains)
Output:

true
true

func (*Map) Delete

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

Delete removes a key and associated value from the map.

Example
// Delete is similar to the builtin delete function in go,
// except it does not modify the map in place.
gm := map[string]bool{"a": true, "b": false}
m := From(gm)

m = m.Delete("b")
delete(gm, "b")

fmt.Println(dyn.Equal(m, From(gm)))
Output:

true

func (*Map) EntryAt

func (m *Map) EntryAt(key interface{}) Entry

EntryAt returns the entry (key, value pair) of the key. If one is not found, nil is returned.

Example
m := New("a", true, "b", false)
fmt.Println(m.EntryAt("a"))
Output:

[a true]

func (*Map) Equal

func (m *Map) Equal(o interface{}) bool

Equal tests if two maps are Equal by comparing the entries of each. Equal implements the Equaler which allows for deep comparisons when there are maps of maps

func (*Map) Find

func (m *Map) Find(key interface{}) (value interface{}, exists bool)

Find will return the value for a key if it exists in the map and whether the key exists in the map. For non-nil values, exists will always be true.

Example
gm := map[string]bool{"a": true, "b": false}
m := From(gm)

val, gotIt := m.Find("a")
fmt.Println(val, gotIt)

val, contains := gm["a"]
fmt.Println(val, contains)
Output:

true true
true true

func (*Map) Iterator

func (m *Map) Iterator() Iterator

Iterator provides a mutable iterator over the map. This allows efficient, heap allocation-less access to the contents. Iterators are not safe for concurrent access so they may not be shared between goroutines.

Example
// Iterator returns a mutable iterator over the map contents
m := New("a", true, "b", false, "c", true, "d", false)
iter := m.Iterator()
for iter.HasNext() {
	key, value := iter.Next()
	if value.(bool) {
		break
	}
	fmt.Println("key", key, "value", value)
}
Output:

func (*Map) Length

func (m *Map) Length() int

Length returns the number of entries in the map.

Example
gm := map[string]bool{"a": true, "b": false}
m := From(gm)

fmt.Println(m.Length(), len(gm))
Output:

2 2

func (*Map) MakeTransient

func (m *Map) MakeTransient() interface{}

MakeTransient is a generic version of AsTransient.

func (*Map) Range

func (m *Map) Range(do interface{})

Range will loop over the entries in the Map and call 'do' on each entry. The 'do' function may be of many types:

func(key, value interface{}) bool:

Takes empty interfaces and returns if the loop should continue.
Useful to avoid reflection or for hetrogenous maps.

func(key, value interface{}):

Takes empty interfaces.
Useful to avoid reflection or for hetrogenous maps.

func(entry Entry) bool:

Takes the Entry type and returns if the loop should continue
Is called directly and avoids entry unpacking if not necessary.

func(entry Entry):

Takes the Entry type.
Is called directly and avoids entry unpacking if not necessary.

func(k kT, v vT) bool

Takes a key of key type and a value of value type and returns if the loop should contiune.
Is called with reflection and will panic if the kT and vT types are incorrect.

func(k kT, v vT)

Takes a key of key type and a value of value type.
Is called with reflection and will panic if the kT and vT types are incorrect.

Range will panic if passed anything not matching these signatures.

Example (Entry)
// Range is a replacement for go's range builtin
// it takes several function forms this version
// will always process all elements.
m := New("a", true, "b", false, "c", true, "d", false)
m.Range(func(e Entry) {
	fmt.Println("key", e.Key(), "value", e.Value())
})
Output:

Example (Entry_continue)
// Range is a replacement for go's range builtin
// it takes several function forms this version
// allows one to stop processing by returning false
m := New("a", true, "b", false, "c", true, "d", false)
m.Range(func(e Entry) bool {
	if e.Value().(bool) {
		return false
	}
	fmt.Println("key", e.Key(), "value", e.Value())
	return true
})
Output:

Example (Kv)
// Range is a replacement for go's range builtin
// it takes several function forms this version
// will always process all elements.
m := New("a", true, "b", false, "c", true, "d", false)
m.Range(func(key string, value bool) {
	fmt.Println("key", key, "value", value)
})
Output:

Example (Kv_continue)
// Range is a replacement for go's range builtin
// it takes several function forms this version
// allows one to stop processing by returning false
m := New("a", true, "b", false, "c", true, "d", false)
m.Range(func(key string, value bool) bool {
	if value {
		return false
	}
	fmt.Println("key", key, "value", value)
	return true
})
Output:

func (*Map) Reduce

func (m *Map) Reduce(fn interface{}, init interface{}) interface{}

Reduce is a fast mechanism for reducing a Map. Reduce can take the following types as the fn:

func(init interface{}, entry Entry) interface{} func(init interface{}, key interface{}, value interface{}) interface{} func(init iT, e Entry) oT func(init iT, k kT, v vT) oT Reduce will panic if given any other function type.

func (*Map) Seq

func (m *Map) Seq() seq.Sequence

Seq returns a seralized sequence of Entry corresponding to the maps entries.

func (*Map) String

func (m *Map) String() string

String returns a string representation of the map.

func (*Map) Transform

func (m *Map) Transform(actions ...func(*TMap) *TMap) *Map

Transform takes a set of actions and performs them on the persistent map. It does this by making a transient map and calling each action on it, then converting it back to a persistent map.

Example
// Transform allows one to transactionally change a
// map by going through a transient to make changes
// this allows for faster large changes in a scoped
// way.
m := New("a", true, "b", false, "c", true, "d", false)
m = m.Transform(func(t *TMap) *TMap {
	return t.Assoc("e", true).Assoc("f", false)
})
fmt.Println(m)
Output:

type TMap

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

TMap is a transient version of a map. Changes made to a transient map will not effect the original persistent structure. Changes to a transient map occur as mutations. These mutations are then made persistent when the transient is transformed into a persistent structure. These are useful when appling multiple transforms to a persistent map where the intermediate results will not be seen or stored anywhere.

func (*TMap) Apply

func (m *TMap) Apply(args ...interface{}) interface{}

Apply takes an arbitrary number of arguments and returns the value At the first argument. Apply allows map to be called as a function by the 'dyn' library.

func (*TMap) AsPersistent

func (m *TMap) AsPersistent() *Map

AsPersistent will transform this transient map into a persistent map. Once this occurs any additional actions on the transient map will fail.

func (*TMap) Assoc

func (m *TMap) Assoc(key, value interface{}) *TMap

Assoc associates a value with a key in the map. The transient map is modified and then returned.

func (*TMap) At

func (m *TMap) At(key interface{}) interface{}

At returns the value associated with the key. If one is not found, nil is returned.

func (*TMap) Conj

func (m *TMap) Conj(value interface{}) interface{}

Conj takes a value that must be an Entry. Conj implements a generic mechanism for building collections.

func (*TMap) Contains

func (m *TMap) Contains(key interface{}) bool

Contains will test if the key exists in the map.

func (*TMap) Delete

func (m *TMap) Delete(key interface{}) *TMap

Delete removes a key and associated value from the map.

func (*TMap) EntryAt

func (m *TMap) EntryAt(key interface{}) Entry

EntryAt returns the entry (key, value pair) of the key. If one is not found, nil is returned.

func (*TMap) Equal

func (m *TMap) Equal(o interface{}) bool

Equal tests if two maps are Equal by comparing the entries of each. Equal implements the Equaler which allows for deep comparisons when there are maps of maps

func (*TMap) Find

func (m *TMap) Find(key interface{}) (value interface{}, exists bool)

Find will return the value for a key if it exists in the map and whether the key exists in the map. For non-nil values, exists will always be true.

func (*TMap) Length

func (m *TMap) Length() int

Length returns the number of entries in the map.

func (*TMap) MakePersistent

func (m *TMap) MakePersistent() interface{}

MakePersistent is a generic version of AsPersistent.

func (*TMap) Range

func (m *TMap) Range(do interface{})

Range will loop over the entries in the Map and call 'do' on each entry. The 'do' function may be of many types:

func(key, value interface{}) bool:

Takes empty interfaces and returns if the loop should continue.
Useful to avoid reflection or for hetrogenous maps.

func(key, value interface{}):

Takes empty interfaces.
Useful to avoid reflection or for hetrogenous maps.

func(entry Entry) bool:

Takes the Entry type and returns if the loop should continue
Is called directly and avoids entry unpacking if not necessary.

func(entry Entry):

Takes the Entry type.
Is called directly and avoids entry unpacking if not necessary.

func(k kT, v vT) bool

Takes a key of key type and a value of value type and returns if the loop should contiune.
Is called with reflection and will panic if the kT and vT types are incorrect.

func(k kT, v vT)

Takes a key of key type and a value of value type.
Is called with reflection and will panic if the kT and vT types are incorrect.

Range will panic if passed anything not matching these signatures.

func (*TMap) Reduce

func (m *TMap) Reduce(fn interface{}, init interface{}) interface{}

Reduce is a fast mechanism for reducing a Map. Reduce can take the following types as the fn:

func(init interface{}, entry Entry) interface{} func(init interface{}, key interface{}, value interface{}) interface{} func(init iT, e Entry) oT func(init iT, k kT, v vT) oT Reduce will panic if given any other function type.

func (*TMap) String

func (m *TMap) String() string

String returns a string representation of the map.

Jump to

Keyboard shortcuts

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