treemap

package
v0.0.0-...-7241100 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2019 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package treemap implements a persistent Red/Black tree. This tree is based on Okasaki's persistent Red/Black tree with Germane and Might's deletion extension. See: http://www.eecs.usma.edu/webs/people/okasaki/jfp99.ps and http://matt.might.net/papers/germane2014deletion.pdf for details.

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. Additionally, Key's must be comparable. One may implement Compare(other interface{}) int to override the default comparable restrcitions.

Index

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 returns an Entry

type Map

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

Map is a persistent immutable map based on Red/Black trees. Operations on map returns a new map that shares much of the structure with the original map.

func Empty

func Empty(options ...Option) *Map

Empty returns a new empty persistent map, one may supply options for the map by using one of the option generating functions and providing that to Empty.

func From

func From(value interface{}, options ...Option) *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.

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.

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.

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.

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.

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.

func (*Map) Conj

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

Conj associates a value with a key in the map.

func (*Map) Contains

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

Contains will test if the key exists in the map.

func (*Map) Delete

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

Delete removes a key and associated value from the map.

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.

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.

func (*Map) Length

func (m *Map) Length() int

Length returns the number of entries in the map.

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.

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.

type Option

type Option func(*mapOptions)

Option is a type that allows changes to pluggable parts of the Map implementation.

func Compare

func Compare(cmp func(k1, k2 interface{}) int) Option

Compare is an option to the Empty function that will allow one to specify a different comparison operator instead of the default which is from the dyn library.

Jump to

Keyboard shortcuts

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