Documentation
¶
Overview ¶
Package tree implements in-memory ordered maps. OMap[K, V] is suitable for ordered types K, while Map[K, V] supports arbitrary keys and comparison functions.
Index ¶
- type Map
- func (m *Map[K, V]) Above(lo K) Range[K, V]
- func (m *Map[K, V]) All() iter.Seq2[K, V]
- func (m *Map[K, V]) At(key K) V
- func (m *Map[K, V]) Backward() iter.Seq2[K, V]
- func (m *Map[K, V]) Below(hi K) Range[K, V]
- func (m *Map[K, V]) Clear()
- func (m *Map[K, V]) Clone() *Map[K, V]
- func (m *Map[K, V]) Delete(key K) bool
- func (m *Map[K, V]) From(lo K) Range[K, V]
- func (m *Map[K, V]) Get(key K) (V, bool)
- func (m *Map[K, V]) Index(key K) int
- func (m *Map[K, V]) Len() int
- func (m *Map[K, V]) Max() (K, V, bool)
- func (m *Map[K, V]) Min() (K, V, bool)
- func (m *Map[K, V]) Nth(i int) (K, V)
- func (m *Map[K, V]) Set(key K, val V) (old V, added bool)
- func (m *Map[K, V]) To(hi K) Range[K, V]
- type OMap
- func (m *OMap[K, V]) Above(lo K) ORange[K, V]
- func (m *OMap[K, V]) All() iter.Seq2[K, V]
- func (m *OMap[K, V]) At(key K) V
- func (m *OMap[K, V]) Backward() iter.Seq2[K, V]
- func (m *OMap[K, V]) Below(hi K) ORange[K, V]
- func (m *OMap[K, V]) Clear()
- func (m *OMap[K, V]) Clone() *OMap[K, V]
- func (m *OMap[K, V]) Delete(key K) bool
- func (m *OMap[K, V]) From(lo K) ORange[K, V]
- func (m *OMap[K, V]) Get(key K) (V, bool)
- func (m *OMap[K, V]) Index(key K) int
- func (m *OMap[K, V]) Len() int
- func (m *OMap[K, V]) Max() (K, V, bool)
- func (m *OMap[K, V]) Min() (K, V, bool)
- func (m *OMap[K, V]) Nth(i int) (K, V)
- func (m *OMap[K, V]) Set(key K, val V) (old V, added bool)
- func (m *OMap[K, V]) To(hi K) ORange[K, V]
- type ORange
- func (r ORange[K, V]) All() iter.Seq2[K, V]
- func (r ORange[K, V]) Backward() iter.Seq2[K, V]
- func (r ORange[K, V]) Below(hi K) ORange[K, V]
- func (r ORange[K, V]) Clear()
- func (r ORange[K, V]) Clone() *OMap[K, V]
- func (r ORange[K, V]) Index(key K) int
- func (r ORange[K, V]) Len() int
- func (r ORange[K, V]) Max() (K, V, bool)
- func (r ORange[K, V]) Min() (K, V, bool)
- func (r ORange[K, V]) Nth(i int) (K, V)
- func (r ORange[K, V]) To(hi K) ORange[K, V]
- type Range
- func (r Range[K, V]) All() iter.Seq2[K, V]
- func (r Range[K, V]) Backward() iter.Seq2[K, V]
- func (r Range[K, V]) Below(hi K) Range[K, V]
- func (r Range[K, V]) Clear()
- func (r Range[K, V]) Clone() *Map[K, V]
- func (r Range[K, V]) Index(key K) int
- func (r Range[K, V]) Len() int
- func (r Range[K, V]) Max() (K, V, bool)
- func (r Range[K, V]) Min() (K, V, bool)
- func (r Range[K, V]) Nth(i int) (K, V)
- func (r Range[K, V]) To(hi K) Range[K, V]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Map ¶
type Map[K, V any] struct { // contains filtered or unexported fields }
A Map is a map[K]V ordered according to an arbitrary comparison function. The zero value of a Map is not meaningful since it has no comparison function. Use NewMap to create a Map. A nil *Map, like a nil Go map, can be read but not written and contains no entries.
func (*Map[K, V]) Above ¶
Above returns an Range with lower bound lo, exclusive, and no upper bound.
func (*Map[K, V]) All ¶
All returns an iterator over the map m from smallest to largest key. See OMap.All for the guarantee provided if m is modified during the iteration.
Example ¶
package main
import (
"fmt"
"github.com/jba/omap/tree"
)
func main() {
m := &tree.OMap[int, string]{}
m.Set(1, "one")
m.Set(2, "two")
m.Set(3, "three")
for k, v := range m.All() {
fmt.Println(k, v)
}
}
Output: 1 one 2 two 3 three
func (*Map[K, V]) At ¶
func (m *Map[K, V]) At(key K) V
At returns the value of m[key], or the zero value if key is not present.
func (*Map[K, V]) Backward ¶
Backward returns an iterator over the map m from largest to smallest key. See OMap.All for the guarantee provided if m is modified during the iteration.
func (*Map[K, V]) Below ¶
Below returns an Range with upper bound hi, exclusive, and no lower bound.
Example ¶
package main
import (
"fmt"
"github.com/jba/omap/tree"
)
func main() {
m := &tree.OMap[int, string]{}
m.Set(1, "one")
m.Set(2, "two")
m.Set(3, "three")
for k, v := range m.Above(1).Below(3).All() {
fmt.Println(k, v)
}
}
Output: 2 two
func (*Map[K, V]) From ¶
From returns an Range with lower bound lo, inclusive, and no upper bound.
Example ¶
package main
import (
"fmt"
"github.com/jba/omap/tree"
)
func main() {
m := &tree.OMap[int, string]{}
m.Set(1, "one")
m.Set(2, "two")
m.Set(3, "three")
for k, v := range m.From(2).All() {
fmt.Println(k, v)
}
}
Output: 2 two 3 three
func (*Map[K, V]) Max ¶
Max returns the maximum key in m, is value, and true. If m is empty, the third return value is false.
func (*Map[K, V]) Min ¶
Min returns the minimum key in m, its value, and true. If m is empty, the third return value is false.
func (*Map[K, V]) Nth ¶
Nth returns the key and value at index i. It panics if i < 0 or i >= m.Len().
type OMap ¶ added in v0.3.0
A OMap is a map[K]V ordered according to K's standard Go ordering. The zero value of a OMap is an empty OMap ready to use.
func (*OMap[K, V]) Above ¶ added in v0.3.0
Above returns an ORange with lower bound lo, exclusive, and no upper bound.
func (*OMap[K, V]) All ¶ added in v0.3.0
All returns an iterator over the map m from smallest to largest key. If m is modified during the iteration, All makes this guarantee: when a key is yielded, it is the successor of the key that was previously yielded (or the minimum key in the map, if it is the first key).
For example, if the map contains keys 10 and 20, the iterator has yielded 10, and then 15 is inserted, then the next yielded key will be 15.
Another example: if the map contains keys 10, 20 and 30, the iterator has yielded 10, and then 20 is deleted, then the next yielded key will be 30.
func (*OMap[K, V]) At ¶ added in v0.3.0
func (m *OMap[K, V]) At(key K) V
At returns the value of m[key], or the zero value if key is not present.
func (*OMap[K, V]) Backward ¶ added in v0.3.0
Backward returns an iterator over the map m from largest to smallest key. See OMap.All for the guarantee provided if m is modified during the iteration.
func (*OMap[K, V]) Below ¶ added in v0.3.0
Below returns an ORange with upper bound hi, exclusive, and no lower bound.
func (*OMap[K, V]) Clear ¶ added in v0.3.0
func (m *OMap[K, V]) Clear()
Clear deletes m[k] for all keys in m.
func (*OMap[K, V]) From ¶ added in v0.3.0
From returns an ORange with lower bound lo, inclusive, and no upper bound.
func (*OMap[K, V]) Get ¶ added in v0.3.0
Get returns the value of m[key] and reports whether it exists.
func (*OMap[K, V]) Index ¶ added in v0.3.0
Index returns the index of key in m, or -1 if key is not present.
func (*OMap[K, V]) Max ¶ added in v0.3.0
Max returns the maximum key in m, its value, and true. If m is empty, the third return value is false.
func (*OMap[K, V]) Min ¶ added in v0.3.0
Min returns the minimum key in m, its value, and true. If m is empty, the third return value is false.
func (*OMap[K, V]) Nth ¶ added in v0.3.0
Nth returns the key and value at index i. It panics if i < 0 or i >= m.Len().
type ORange ¶ added in v0.3.0
A ORange is a subsequence of keys in an OMap.
func (ORange[K, V]) All ¶ added in v0.3.0
All returns an iterator over r's underlying map from smallest to largest key in r. See OMap.All for the guarantee provided if m is modified during the iteration.
func (ORange[K, V]) Backward ¶ added in v0.3.0
Backward returns an iterator over r's underlying map from largest to smallest key in r. See OMap.All for the guarantee provided if m is modified during the iteration.
func (ORange[K, V]) Below ¶ added in v0.3.0
Below returns an ORange with upper bound hi, exclusive and the same lower bound as r. It panics if r already has an upper bound.
func (ORange[K, V]) Clear ¶ added in v0.3.0
func (r ORange[K, V]) Clear()
Clear deletes all the entries in r from r's underlying map.
func (ORange[K, V]) Index ¶ added in v0.3.0
Index returns the index of key within r, or -1 if key is not present or not in bounds.
func (ORange[K, V]) Max ¶ added in v0.3.0
Max returns the maximum key from r's underlying map that is in r, its value, and true. If m is empty, the third return value is false.
func (ORange[K, V]) Min ¶ added in v0.3.0
Min returns the minimum key from r's underlying map that is in r, its value, and true. If m is empty, the third return value is false.
type Range ¶
type Range[K, V any] struct { // contains filtered or unexported fields }
A Range is a subsequence of keys in a Map.
func (Range[K, V]) All ¶
All returns an iterator over r's underlying map from smallest to largest key in r. See OMap.All for the guarantee provided if m is modified during the iteration.
func (Range[K, V]) Backward ¶
Backward returns an iterator over r's underlying map from largest to smallest key in r. See OMap.All for the guarantee provided if m is modified during the iteration.
func (Range[K, V]) Below ¶
Below returns a Range with upper bound hi, exclusive and the same lower bound as r. It panics if r already has an upper bound.
func (Range[K, V]) Clear ¶
func (r Range[K, V]) Clear()
Clear deletes all the entries in r from r's underlying map.
func (Range[K, V]) Index ¶
Index returns the index of key within r, or -1 if key is not present or not in bounds.
func (Range[K, V]) Max ¶
Max returns the maximum key from r's underlying map that is in r, its value, and true. If m is empty, the third return value is false.
func (Range[K, V]) Min ¶
Min returns the minimum key from r's underlying map that is in r, its value, and true. If m is empty, the third return value is false.