maps

package
v1.9.16 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Comparator added in v1.9.9

func Comparator[K OrderKey](a, b K) int

IntComparator provides a basic comparison on int

func ToString

func ToString(value interface{}) string

ToString converts a value to string.

Types

type IMap

type IMap[K OrderKey, V any] interface {
	Empty() bool
	Size() int
	Clear()
	Values() []V
}

Map interface that all Maps implement

type Iterator

type Iterator[K OrderKey, V any] struct {
	// contains filtered or unexported fields
}

Iterator holding the iterator's state

func (*Iterator[K, V]) Begin

func (it *Iterator[K, V]) Begin()

Begin resets the iterator to its initial state (one-before-first) Call Next() to fetch the first element if any.

func (*Iterator[K, V]) End

func (it *Iterator[K, V]) End()

End moves the iterator past the last element (one-past-the-end). Call Prev() to fetch the last element if any.

func (*Iterator[K, V]) First

func (it *Iterator[K, V]) First() bool

First moves the iterator to the first element and returns true if there was a first element in the container. If First() returns true, then first element's key and value can be retrieved by Key() and Value(). Modifies the state of the iterator

func (*Iterator[K, V]) Key

func (it *Iterator[K, V]) Key() K

Key returns the current element's key. Does not modify the state of the iterator.

func (*Iterator[K, V]) Last

func (it *Iterator[K, V]) Last() bool

Last moves the iterator to the last element and returns true if there was a last element in the container. If Last() returns true, then last element's key and value can be retrieved by Key() and Value(). Modifies the state of the iterator.

func (*Iterator[K, V]) Next

func (it *Iterator[K, V]) Next() bool

Next moves the iterator to the next element and returns true if there was a next element in the container. If Next() returns true, then next element's key and value can be retrieved by Key() and Value(). If Next() was called for the first time, then it will point the iterator to the first element if it exists. Modifies the state of the iterator.

func (*Iterator[K, V]) Prev

func (it *Iterator[K, V]) Prev() bool

Prev moves the iterator to the previous element and returns true if there was a previous element in the container. If Prev() returns true, then previous element's key and value can be retrieved by Key() and Value(). Modifies the state of the iterator.

func (*Iterator[K, V]) Value

func (it *Iterator[K, V]) Value() V

Value returns the current element's value. Does not modify the state of the iterator.

type Map

type Map[K OrderKey, V any] struct {
	Root *Node[K, V]

	Nil V
	// contains filtered or unexported fields
}

Map holds elements of the red-black tree

func (*Map[K, V]) Ceiling

func (m *Map[K, V]) Ceiling(key K) (ceiling *Node[K, V], found bool)

Ceiling finds ceiling node of the input key, return the ceiling node or nil if no ceiling is found. Second return parameter is true if ceiling was found, otherwise false.

Ceiling node is defined as the smallest node that is larger than or equal to the given node. A ceiling node may not be found, either because the tree is empty, or because all nodes in the tree are smaller than the given node.

Key should adhere to the comparator's type assertion, otherwise method panics.

func (*Map[K, V]) Clear

func (m *Map[K, V]) Clear()

Clear removes all nodes from the tree.

func (*Map[K, V]) Empty

func (m *Map[K, V]) Empty() bool

Empty returns true if tree does not contain any nodes

func (*Map[K, V]) Floor

func (m *Map[K, V]) Floor(key K) (floor *Node[K, V], found bool)

Floor Finds floor node of the input key, return the floor node or nil if no floor is found. Second return parameter is true if floor was found, otherwise false.

Floor node is defined as the largest node that is smaller than or equal to the given node. A floor node may not be found, either because the tree is empty, or because all nodes in the tree are larger than the given node.

Key should adhere to the comparator's type assertion, otherwise method panics.

func (*Map[K, V]) FromJSON

func (tree *Map[K, V]) FromJSON(data []byte) error

FromJSON populates the tree from the input JSON representation.

func (*Map[K, V]) Get

func (m *Map[K, V]) Get(key K) (value V, found bool)

Get searches the node in the tree by key and returns its value or nil if key is not found in tree. Second return parameter is true if key was found, otherwise false. Key should adhere to the comparator's type assertion, otherwise method panics.

func (*Map[K, V]) Iterator

func (it *Map[K, V]) Iterator() Iterator[K, V]

Iterator returns a stateful iterator whose elements are key/value pairs.

func (*Map[K, V]) Keys

func (m *Map[K, V]) Keys() []K

Keys returns all keys in-order

func (*Map[K, V]) Left

func (m *Map[K, V]) Left() *Node[K, V]

Left returns the left-most (min) node or nil if tree is empty.

func (*Map[K, V]) Put

func (m *Map[K, V]) Put(key K, value V)

Put inserts node into the tree. Key should adhere to the comparator's type assertion, otherwise method panics.

func (*Map[K, V]) Remove

func (m *Map[K, V]) Remove(key K)

Remove remove the node from the tree by key. Key should adhere to the comparator's type assertion, otherwise method panics.

func (*Map[K, V]) Right

func (m *Map[K, V]) Right() *Node[K, V]

Right returns the right-most (max) node or nil if tree is empty.

func (*Map[K, V]) Size

func (m *Map[K, V]) Size() int

Size returns number of nodes in the tree.

func (*Map[K, V]) String

func (m *Map[K, V]) String() string

String returns a string representation of container

func (*Map[K, V]) ToJSON

func (tree *Map[K, V]) ToJSON() ([]byte, error)

ToJSON outputs the JSON representation of the tree.

func (*Map[K, V]) Values

func (m *Map[K, V]) Values() []V

Values returns all values in-order based on the key.

type Node

type Node[K OrderKey, V any] struct {
	Key   K
	Value V

	Left   *Node[K, V]
	Right  *Node[K, V]
	Parent *Node[K, V]
	// contains filtered or unexported fields
}

Node is a single element within the Map

func (*Node[K, V]) String

func (node *Node[K, V]) String() string

type OrderKey added in v1.9.9

type OrderKey interface {
	int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 |
		uint32 | uint64 | uintptr | float32 | float64 | string
}

Jump to

Keyboard shortcuts

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