treemap

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2025 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.
	m.Range(func(key int, value string) bool{
		if key == 2 {
			return false
		}
		return true
	})
}

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 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]) Ceiling added in v0.1.1

func (m *TreeMap[K, V]) Ceiling(key K) (foundKey K, foundValue V, ok bool)

Ceiling finds the ceiling key-value pair for the input key. In case that no ceiling is found, then both returned values will be corresponding type's empty value.

Ceiling key is defined as the least key that is greater than or equal to the given key. A ceiling key may not be found, either because the map is empty, or because all keys in the map are less than the given key.

func (*TreeMap[K, V]) CeilingOrMax added in v0.1.1

func (m *TreeMap[K, V]) CeilingOrMax(key K) (foundKey K, foundValue V, ok bool)

Ceiling finds the ceiling key-value pair for the input key. If no ceiling is found, returns the key-value pair for the greatest key. In case that map is empty, then both returned values will be corresponding type's empty value.

Ceiling key is defined as the least key that is greater than or equal to the given key. A ceiling key may not be found, either because the map is empty, or because all keys in the map are less than the given key.

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) (k K, v V)

Find passes each element of the container to the given function and returns the first (key,value) for which the function is true or nil,nil otherwise if no element matches the criteria.

func (*TreeMap[K, V]) FindIter added in v0.1.1

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

FindIter returns an iterator pointing to the element with specified key. If no such element is found, a past-the-end iterator is returned. See: https://en.cppreference.com/w/cpp/container/map/find

func (*TreeMap[K, V]) Floor added in v0.1.1

func (m *TreeMap[K, V]) Floor(key K) (foundKey K, foundValue V, ok bool)

Floor finds the floor key-value pair for the input key. In case that no floor is found, then both returned values will be corresponding type's empty value.

Floor key is defined as the greatest key that is less than or equal to the given key. A floor key may not be found, either because the map is empty, or because all keys in the map are greater than the given key.

func (*TreeMap[K, V]) FloorOrMin added in v0.3.0

func (m *TreeMap[K, V]) FloorOrMin(key K) (foundKey K, foundValue V, ok bool)

FloorOrMin finds the floor key-value pair for the input key. If no floor is found, returns the key-value pair for the least key. In case that map is empty, then both returned values will be corresponding type's empty value.

Floor key is defined as the greatest key that is less than or equal to the given key. A floor key may not be found, either because the map is empty, or because all keys in the map are greater than the given key.

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 empty value if key is not found in tree. Second return parameter is true if key was found, otherwise false.

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. See: https://en.cppreference.com/w/cpp/container/map/lower_bound

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.

func (*TreeMap[K, V]) Range added in v0.1.1

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

Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.

func (*TreeMap[K, V]) Remove

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

Remove removes the element from the map by key.

func (*TreeMap[K, V]) ReverseRange added in v0.4.0

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

ReverseRange calls f sequentially in reverse order for each key and value present in the map. If f returns false, range stops the iteration.

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. See: https://en.cppreference.com/w/cpp/container/map/upper_bound

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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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