treemap

package
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2025 License: MIT Imports: 9 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TreeMap

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

TreeMap implements an tree map that keeps the compare order of keys. The zero value for TreeMap is an empty map ready to use.

https://en.wikipedia.org/wiki/Red%E2%80%93black_tree

To iterate over a tree map (where tm is a *TreeMap):

for it := tm.Iterator(); it.Next(); {
	// do something with it.Key(), it.Value()
}
Example
m := NewTreeMap[int, string](cmp.Compare[int])
m.Set(1, "x")   // 1->x
m.Set(2, "b")   // 1->x, 2->b (in order)
m.Set(1, "a")   // 1->a, 2->b (in order)
_, _ = m.Get(2) // b, true
_, _ = m.Get(3) // nil, false
_ = m.Values()  // []interface {}{"a", "b"} (in order)
_ = m.Keys()    // []interface {}{1, 2} (in order)
m.Remove(1)     // 2->b
m.Clear()       // empty
m.IsEmpty()     // true
m.Len()         // 0

// Other:
m.Head() // Returns the minimum key and its value from map.
m.Tail() // Returns the maximum key and its value from map.

func NewTreeMap

func NewTreeMap[K any, V any](compare cog.Compare[K], kvs ...cog.P[K, V]) *TreeMap[K, V]

NewTreeMap creates a new TreeMap. Example: cog.NewTreeMap(cog.CompareString, cog.KV("k1", "v1"), cog.KV("k2", "v2"))

func (*TreeMap[K, V]) Ceiling

func (tm *TreeMap[K, V]) Ceiling(key K) *TreeMapNode[K, V]

Ceiling finds ceiling node of the input key, return the ceiling node or nil if no ceiling is found.

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

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

Clear clears the map

func (*TreeMap[K, V]) Contains

func (tm *TreeMap[K, V]) Contains(k K) bool

Contains Test to see if the map contains the given key

func (*TreeMap[K, V]) ContainsAll added in v1.0.27

func (tm *TreeMap[K, V]) ContainsAll(ks ...K) bool

ContainsAll Test to see if the map contains all keys of ks

func (*TreeMap[K, V]) Copy

func (tm *TreeMap[K, V]) Copy(am cog.Map[K, V])

Copy copy items from another map am, override the existing items

func (*TreeMap[K, V]) Each

func (tm *TreeMap[K, V]) Each(f func(K, V) bool)

Each call f for each item in the map

func (*TreeMap[K, V]) Entries

func (tm *TreeMap[K, V]) Entries() []cog.P[K, V]

Entries returns the key-value pair slice

func (*TreeMap[K, V]) Floor

func (tm *TreeMap[K, V]) Floor(key K) *TreeMapNode[K, V]

Floor Finds floor node of the input key, return the floor node or nil if no floor is found.

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 (*TreeMap[K, V]) Get

func (tm *TreeMap[K, V]) Get(key K) (V, bool)

Get looks for the given key, and returns the value associated with it, or nil if not found. The boolean it returns says whether the key is ok in the map.

func (*TreeMap[K, V]) Graph

func (tm *TreeMap[K, V]) Graph(value bool) string

Graph return the map's graph

func (*TreeMap[K, V]) Head

func (tm *TreeMap[K, V]) Head() *TreeMapNode[K, V]

Head returns a pointer to the minimum item.

func (*TreeMap[K, V]) IsEmpty

func (tm *TreeMap[K, V]) IsEmpty() bool

IsEmpty returns true if the map has no items

func (*TreeMap[K, V]) Items

func (tm *TreeMap[K, V]) Items() []*TreeMapNode[K, V]

Items returns the map item slice

func (*TreeMap[K, V]) Iterator

func (tm *TreeMap[K, V]) Iterator() cog.Iterator2[K, V]

Iterator returns a iterator for the map

func (*TreeMap[K, V]) Keys

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

Keys returns the key slice

func (*TreeMap[K, V]) Len

func (tm *TreeMap[K, V]) Len() int

Len returns the length of the tree map.

func (*TreeMap[K, V]) MarshalJSON

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

MarshalJSON implements type json.Marshaler interface, so can be called in json.Marshal(tm)

func (*TreeMap[K, V]) MustGet

func (tm *TreeMap[K, V]) MustGet(key K) V

MustGet looks for the given key, and returns the value associated with it. Panic if not found.

func (*TreeMap[K, V]) PollHead

func (tm *TreeMap[K, V]) PollHead() *TreeMapNode[K, V]

PollHead remove the first item of map.

func (*TreeMap[K, V]) PollTail

func (tm *TreeMap[K, V]) PollTail() *TreeMapNode[K, V]

PollTail remove the last item of map.

func (*TreeMap[K, V]) Remove

func (tm *TreeMap[K, V]) Remove(k K) (ov V, ok bool)

Remove remove the item with key k, and returns what `Get` would have returned on that key prior to the call to `Set`.

func (*TreeMap[K, V]) RemoveAll added in v1.0.27

func (tm *TreeMap[K, V]) RemoveAll(ks ...K)

RemoveAll remove all items with key of ks.

func (*TreeMap[K, V]) ReverseEach

func (tm *TreeMap[K, V]) ReverseEach(f func(K, V) bool)

ReverseEach call f for each item in the map with reverse order

func (*TreeMap[K, V]) SafeGet added in v1.0.27

func (tm *TreeMap[K, V]) SafeGet(key K, defaults ...V) V

SafeGet looks for the given key, and returns the value associated with it. If not found, return defaults[0] or zero V.

func (*TreeMap[K, V]) Seq added in v1.0.27

func (tm *TreeMap[K, V]) Seq() iter.Seq2[K, V]

Seq returns a iter.Seq[K, V] for range

func (*TreeMap[K, V]) Set

func (tm *TreeMap[K, V]) Set(key K, value V) (ov V, ok bool)

Set sets the paired key-value item, and returns what `Get` would have returned on that key prior to the call to `Set`. key should adhere to the comparator's type assertion, otherwise method panics.

func (*TreeMap[K, V]) SetEntries

func (tm *TreeMap[K, V]) SetEntries(pairs ...cog.P[K, V])

SetEntries set items from key-value items array, override the existing items

func (*TreeMap[K, V]) SetIfAbsent

func (tm *TreeMap[K, V]) SetIfAbsent(key K, value V) (ov V, ok bool)

SetIfAbsent sets the key-value item if the key does not exists in the map, and returns true if the tree is changed.

func (*TreeMap[K, V]) String

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

String print map to string

func (*TreeMap[K, V]) Tail

func (tm *TreeMap[K, V]) Tail() *TreeMapNode[K, V]

Tail returns a pointer to the maximum item.

func (*TreeMap[K, V]) UnmarshalJSON

func (tm *TreeMap[K, V]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements type json.Unmarshaler interface, so can be called in json.Unmarshal(data, tm)

func (*TreeMap[K, V]) Values

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

Values returns the value slice

type TreeMapNode

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

TreeMapNode is a node of red-black tree

func (*TreeMapNode[K, V]) Key

func (tn *TreeMapNode[K, V]) Key() K

Key returns the key

func (*TreeMapNode[K, V]) SetValue

func (tn *TreeMapNode[K, V]) SetValue(v V)

SetValue sets the value

func (*TreeMapNode[K, V]) String

func (tn *TreeMapNode[K, V]) String() string

String print the key/value node to string

func (*TreeMapNode[K, V]) Value

func (tn *TreeMapNode[K, V]) Value() V

Value returns the key

Jump to

Keyboard shortcuts

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