Documentation
¶
Index ¶
- type TreeMap
- func (tm *TreeMap[K, V]) Ceiling(key K) *TreeMapNode[K, V]
- func (tm *TreeMap[K, V]) Clear()
- func (tm *TreeMap[K, V]) Contains(k K) bool
- func (tm *TreeMap[K, V]) ContainsAll(ks ...K) bool
- func (tm *TreeMap[K, V]) Copy(am cog.Map[K, V])
- func (tm *TreeMap[K, V]) Each(f func(K, V) bool)
- func (tm *TreeMap[K, V]) Entries() []cog.P[K, V]
- func (tm *TreeMap[K, V]) Floor(key K) *TreeMapNode[K, V]
- func (tm *TreeMap[K, V]) Get(key K) (V, bool)
- func (tm *TreeMap[K, V]) Graph(value bool) string
- func (tm *TreeMap[K, V]) Head() *TreeMapNode[K, V]
- func (tm *TreeMap[K, V]) IsEmpty() bool
- func (tm *TreeMap[K, V]) Items() []*TreeMapNode[K, V]
- func (tm *TreeMap[K, V]) Iterator() cog.Iterator2[K, V]
- func (tm *TreeMap[K, V]) Keys() []K
- func (tm *TreeMap[K, V]) Len() int
- func (tm *TreeMap[K, V]) MarshalJSON() ([]byte, error)
- func (tm *TreeMap[K, V]) MustGet(key K) V
- func (tm *TreeMap[K, V]) PollHead() *TreeMapNode[K, V]
- func (tm *TreeMap[K, V]) PollTail() *TreeMapNode[K, V]
- func (tm *TreeMap[K, V]) Remove(k K) (ov V, ok bool)
- func (tm *TreeMap[K, V]) RemoveAll(ks ...K)
- func (tm *TreeMap[K, V]) ReverseEach(f func(K, V) bool)
- func (tm *TreeMap[K, V]) SafeGet(key K, defaults ...V) V
- func (tm *TreeMap[K, V]) Seq() iter.Seq2[K, V]
- func (tm *TreeMap[K, V]) Set(key K, value V) (ov V, ok bool)
- func (tm *TreeMap[K, V]) SetEntries(pairs ...cog.P[K, V])
- func (tm *TreeMap[K, V]) SetIfAbsent(key K, value V) (ov V, ok bool)
- func (tm *TreeMap[K, V]) String() string
- func (tm *TreeMap[K, V]) Tail() *TreeMapNode[K, V]
- func (tm *TreeMap[K, V]) UnmarshalJSON(data []byte) error
- func (tm *TreeMap[K, V]) Values() []V
- type TreeMapNode
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type TreeMap ¶
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 ¶
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]) ContainsAll ¶ added in v1.0.27
ContainsAll Test to see if the map contains all keys of ks
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 ¶
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]) Head ¶
func (tm *TreeMap[K, V]) Head() *TreeMapNode[K, V]
Head returns a pointer to the minimum item.
func (*TreeMap[K, V]) Items ¶
func (tm *TreeMap[K, V]) Items() []*TreeMapNode[K, V]
Items returns the map item slice
func (*TreeMap[K, V]) MarshalJSON ¶
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 ¶
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 ¶
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]) Set ¶
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 ¶
SetEntries set items from key-value items array, override the existing items
func (*TreeMap[K, V]) SetIfAbsent ¶
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]) Tail ¶
func (tm *TreeMap[K, V]) Tail() *TreeMapNode[K, V]
Tail returns a pointer to the maximum item.
func (*TreeMap[K, V]) UnmarshalJSON ¶
UnmarshalJSON implements type json.Unmarshaler interface, so can be called in json.Unmarshal(data, tm)
type TreeMapNode ¶
TreeMapNode is a node of red-black tree
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