treemap

package
v0.0.0-...-1b0cd73 Latest Latest
Warning

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

Go to latest
Published: May 17, 2024 License: MIT Imports: 4 Imported by: 0

README

treemap

This implements a generic-type-supported sorted map with rbtree.

Refer:

Usage

package main

import "github.com/tableauio/loader/pkg/treemap"

func main() {
	m := treemap.New[int, string]() // empty
	m.Put(1, "x")                   // 1->x
	m.Put(2, "b")                   // 1->x, 2->b (in order)
	m.Put(1, "a")                   // 1->a, 2->b (in order)
	_, _ = m.Get(2)                 // b, true
	_, _ = m.Get(3)                 // "", false
	_ = m.Values()                  // [a b] (in order)
	_ = m.Keys()                    // [1 2] (in order)
	m.Remove(1)                     // 2->b
	m.Clear()                       // empty
	m.Empty()                       // true
	m.Size()                        // 0

	// Other:
	m.Min() // Returns the minimum key and its value from map.
	m.Max() // Returns the maximum key and its value from map.
}

Iterate

package main

import (
	"github.com/tableauio/loader/pkg/treemap"
)

func main() {
	m := treemap.New[int, string]()
	m.Put(2, "b")
	m.Put(1, "a")
	m.Put(3, "c")

	// iterate
	iter := m.Iterator()
	for iter.Begin(); iter.Next(); {
		_, _ = iter.Key(), iter.Value() // 1->a, 2->b, 3->c
	}

	// iterate in reverse order
	for iter.End(); iter.Prev(); {
		_, _ = iter.Key(), iter.Value() // 3->c, 2->b, 1->a
	}

	iter = m.LowerBound(0)
	if !iter.IsEnd() {
		_, _ = iter.Key(), iter.Value() // 1->a
	}
	iter = m.LowerBound(2)
	if !iter.IsEnd() {
		_, _ = iter.Key(), iter.Value() // 2->b
	}
	iter = m.LowerBound(4)
	if !iter.IsEnd() {
		_, _ = iter.Key(), iter.Value() // panic if code reaches here
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Iterator

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

Iterator holding the iterator's state

func (*Iterator[K, V]) Begin

func (iterator *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 (iterator *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 (iterator *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 (iterator *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 (iterator *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 (iterator *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]) NextTo

func (iterator *Iterator[K, V]) NextTo(f func(key K, value V) bool) bool

NextTo moves the iterator to the next element from current position that satisfies the condition given by the passed function, and returns true if there was a next element in the container. If NextTo() returns true, then next element's key and value can be retrieved by Key() and Value(). Modifies the state of the iterator.

func (*Iterator[K, V]) Node

func (iterator *Iterator[K, V]) Node() *Node[K, V]

Node returns the current element's node. Does not modify the state of the iterator.

func (*Iterator[K, V]) Prev

func (iterator *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]) PrevTo

func (iterator *Iterator[K, V]) PrevTo(f func(key K, value V) bool) bool

PrevTo moves the iterator to the previous element from current position that satisfies the condition given by the passed function, and returns true if there was a next element in the container. If PrevTo() returns true, then next element's key and value can be retrieved by Key() and Value(). Modifies the state of the iterator.

func (*Iterator[K, V]) Value

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

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

type Node

type Node[K constraints.Ordered, 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 tree

func (*Node[K, V]) Size

func (node *Node[K, V]) Size() int

Size returns the number of elements stored in the subtree. Computed dynamically on each call, i.e. the subtree is traversed to count the number of the nodes.

func (*Node[K, V]) String

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

type Tree

type Tree[K constraints.Ordered, V any] struct {
	Root *Node[K, V]
	// contains filtered or unexported fields
}

Tree holds elements of the red-black tree

func (*Tree[K, V]) Ceiling

func (tree *Tree[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 (*Tree[K, V]) Clear

func (tree *Tree[K, V]) Clear()

Clear removes all nodes from the tree.

func (*Tree[K, V]) Empty

func (tree *Tree[K, V]) Empty() bool

Empty returns true if tree does not contain any nodes

func (*Tree[K, V]) Floor

func (tree *Tree[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 (*Tree[K, V]) FromJSON

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

FromJSON populates the tree from the input JSON representation.

func (*Tree[K, V]) Get

func (tree *Tree[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 (*Tree[K, V]) GetNode

func (tree *Tree[K, V]) GetNode(key K) *Node[K, V]

GetNode searches the node in the tree by key and returns its node or nil if key is not found in tree. Key should adhere to the comparator's type assertion, otherwise method panics.

func (*Tree[K, V]) Iterator

func (tree *Tree[K, V]) Iterator() Iterator[K, V]

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

func (*Tree[K, V]) IteratorAt

func (tree *Tree[K, V]) IteratorAt(node *Node[K, V]) Iterator[K, V]

IteratorAt returns a stateful iterator whose elements are key/value pairs that is initialised at a particular node.

func (*Tree[K, V]) Keys

func (tree *Tree[K, V]) Keys() []K

Keys returns all keys in-order

func (*Tree[K, V]) Left

func (tree *Tree[K, V]) Left() *Node[K, V]

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

func (*Tree[K, V]) MarshalJSON

func (tree *Tree[K, V]) MarshalJSON() ([]byte, error)

MarshalJSON @implements json.Marshaler

func (*Tree[K, V]) Put

func (tree *Tree[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 (*Tree[K, V]) Remove

func (tree *Tree[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 (*Tree[K, V]) Right

func (tree *Tree[K, V]) Right() *Node[K, V]

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

func (*Tree[K, V]) Size

func (tree *Tree[K, V]) Size() int

Size returns number of nodes in the tree.

func (*Tree[K, V]) String

func (tree *Tree[K, V]) String() string

String returns a string representation of container

func (*Tree[K, V]) ToJSON

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

ToJSON outputs the JSON representation of the tree.

func (*Tree[K, V]) UnmarshalJSON

func (tree *Tree[K, V]) UnmarshalJSON(bytes []byte) error

UnmarshalJSON @implements json.Unmarshaler

func (*Tree[K, V]) Values

func (tree *Tree[K, V]) Values() []V

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

type TreeMap

type TreeMap[K constraints.Ordered, V any] struct {
	// contains filtered or unexported fields
}

func New

func New[K constraints.Ordered, V any]() *TreeMap[K, V]

func (*TreeMap[K, V]) All

func (m *TreeMap[K, V]) All(f func(key K, value V) bool) bool

All passes each element of the container to the given function and returns true if the function returns true for all elements.

func (*TreeMap[K, V]) Any

func (m *TreeMap[K, V]) Any(f func(key K, value V) bool) bool

Any passes each element of the container to the given function and returns true if the function ever returns true for any element.

func (*TreeMap[K, V]) Clear

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

Clear removes all elements from the map.

func (*TreeMap[K, V]) Each

func (m *TreeMap[K, V]) Each(f func(key K, value V))

Each calls the given function once for each element, passing that element's key and value.

func (*TreeMap[K, V]) Empty

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

Empty returns true if map does not contain any elements

func (*TreeMap[K, V]) Find

func (m *TreeMap[K, V]) Find(f func(key K, value V) bool) TreeMapIterator[K, V]

Find passes each element of the container to the given function and returns iterator to the first element for which the function is true. If no element matches the criteria, this returns the iterator past the last element (one-past-the-end).

func (*TreeMap[K, V]) FromJSON

func (m *TreeMap[K, V]) FromJSON(data []byte) error

FromJSON populates the map from the input JSON representation.

func (*TreeMap[K, V]) Get

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

Get searches the element in the map 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 (*TreeMap[K, V]) Iterator

func (m *TreeMap[K, V]) Iterator() TreeMapIterator[K, V]

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

func (*TreeMap[K, V]) Keys

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

Keys returns all keys in-order

func (*TreeMap[K, V]) LowerBound

func (m *TreeMap[K, V]) LowerBound(key K) TreeMapIterator[K, V]

LowerBound returns an iterator pointing to the first element that is not less than key. If no such element is found, a past-the-end iterator is returned.

func (*TreeMap[K, V]) Map

func (m *TreeMap[K, V]) Map(f func(key1 K, value1 V) (K, V)) *TreeMap[K, V]

Map invokes the given function once for each element and returns a container containing the values returned by the given function as key/value pairs.

func (*TreeMap[K, V]) MarshalJSON

func (m *TreeMap[K, V]) MarshalJSON() ([]byte, error)

MarshalJSON @implements json.Marshaler

func (*TreeMap[K, V]) Max

func (m *TreeMap[K, V]) Max() (key K, value V, ok bool)

Max returns the maximum key and its value from the tree map. If the map is empty, the third return parameter will be false.

func (*TreeMap[K, V]) Min

func (m *TreeMap[K, V]) Min() (key K, value V, ok bool)

Min returns the minimum key and its value from the tree map. If the map is empty, the third return parameter will be false.

func (*TreeMap[K, V]) Put

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

Put inserts key-value pair into the map. Key should adhere to the comparator's type assertion, otherwise method panics.

func (*TreeMap[K, V]) Remove

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

Remove removes the element from the map by key. Key should adhere to the comparator's type assertion, otherwise method panics.

func (*TreeMap[K, V]) Select

func (m *TreeMap[K, V]) Select(f func(key K, value V) bool) *TreeMap[K, V]

Select returns a new container containing all elements for which the given function returns a true value.

func (*TreeMap[K, V]) Size

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

Size returns number of elements in the map.

func (*TreeMap[K, V]) String

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

String returns a string representation of container

func (*TreeMap[K, V]) ToJSON

func (m *TreeMap[K, V]) ToJSON() ([]byte, error)

ToJSON outputs the JSON representation of the map.

func (*TreeMap[K, V]) UnmarshalJSON

func (m *TreeMap[K, V]) UnmarshalJSON(bytes []byte) error

UnmarshalJSON @implements json.Unmarshaler

func (*TreeMap[K, V]) UpperBound

func (m *TreeMap[K, V]) UpperBound(key K) TreeMapIterator[K, V]

UpperBound returns an iterator pointing to the first element that is greater than key. If no such element is found, a past-the-end iterator is returned.

func (*TreeMap[K, V]) Values

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

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

type TreeMapIterator

type TreeMapIterator[K constraints.Ordered, V any] struct {
	// contains filtered or unexported fields
}

func (*TreeMapIterator[K, V]) Begin

func (iterator *TreeMapIterator[K, V]) Begin()

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

func (*TreeMapIterator[K, V]) End

func (iterator *TreeMapIterator[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 (*TreeMapIterator[K, V]) First

func (iterator *TreeMapIterator[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 (*TreeMapIterator[K, V]) IsBegin

func (iterator *TreeMapIterator[K, V]) IsBegin() bool

IsBegin returns true if the iterator is in initial state (one-before-first)

func (*TreeMapIterator[K, V]) IsEnd

func (iterator *TreeMapIterator[K, V]) IsEnd() bool

IsEnd returns true if the iterator is past the last element (one-past-the-end).

func (*TreeMapIterator[K, V]) Key

func (iterator *TreeMapIterator[K, V]) Key() K

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

func (*TreeMapIterator[K, V]) Last

func (iterator *TreeMapIterator[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 (*TreeMapIterator[K, V]) Next

func (iterator *TreeMapIterator[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 (*TreeMapIterator[K, V]) NextTo

func (iterator *TreeMapIterator[K, V]) NextTo(f func(key K, value V) bool) bool

NextTo moves the iterator to the next element from current position that satisfies the condition given by the passed function, and returns true if there was a next element in the container. If NextTo() returns true, then next element's key and value can be retrieved by Key() and Value(). Modifies the state of the iterator.

func (*TreeMapIterator[K, V]) Prev

func (iterator *TreeMapIterator[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 (*TreeMapIterator[K, V]) PrevTo

func (iterator *TreeMapIterator[K, V]) PrevTo(f func(key K, value V) bool) bool

PrevTo moves the iterator to the previous element from current position that satisfies the condition given by the passed function, and returns true if there was a next element in the container. If PrevTo() returns true, then next element's key and value can be retrieved by Key() and Value(). Modifies the state of the iterator.

func (*TreeMapIterator[K, V]) Value

func (iterator *TreeMapIterator[K, V]) Value() V

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

Jump to

Keyboard shortcuts

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