Documentation
¶
Overview ¶
Package btree provides an ordered map whose entries live off the Go heap.
Fifteen entries share a node, so a lookup touches a few cache lines per level and each level multiplies reach sixteenfold. Leaves carry no child array: it is allocated separately and only for inner nodes, and most nodes are leaves.
Example ¶
package main
import (
"cmp"
"fmt"
memory "github.com/stubbedev/memory-go"
"github.com/stubbedev/memory-go/btree"
)
func main() {
var a memory.Local
defer a.Close()
t, err := btree.New[int, string32](&a, cmp.Compare[int])
if err != nil {
panic(err)
}
for _, k := range []int{30, 10, 20} {
if err := t.Set(k, string32{byte(k)}); err != nil {
panic(err)
}
}
for k := range t.From(15) {
fmt.Println(k)
}
}
type string32 [32]byte
Output: 20 30
Index ¶
- type Tree
- func (t *Tree[K, V]) All() iter.Seq2[K, V]
- func (t *Tree[K, V]) Backward() iter.Seq2[K, V]
- func (t *Tree[K, V]) Delete(key K) bool
- func (t *Tree[K, V]) Free() error
- func (t *Tree[K, V]) From(from K) iter.Seq2[K, V]
- func (t *Tree[K, V]) Get(key K) (V, bool)
- func (t *Tree[K, V]) Keys() iter.Seq[K]
- func (t *Tree[K, V]) Len() int
- func (t *Tree[K, V]) Max() (K, V, bool)
- func (t *Tree[K, V]) Min() (K, V, bool)
- func (t *Tree[K, V]) Ptr(key K) (*V, bool)
- func (t *Tree[K, V]) Range(from, to K) iter.Seq2[K, V]
- func (t *Tree[K, V]) Set(key K, value V) error
- func (t *Tree[K, V]) Values() iter.Seq[V]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Tree ¶
type Tree[K, V any] struct { // contains filtered or unexported fields }
Tree is an ordered map with off-heap entries, sorted by the function given to New. It is not safe for concurrent use. Pointers from Ptr are valid until the next insertion or deletion.
func New ¶
New returns an empty Tree ordered by cmp, which returns a negative number when a sorts before b, zero when they are equal and a positive number otherwise.
func (*Tree[K, V]) All ¶
All yields every entry in ascending key order. Updating a value during the loop is allowed; adding or deleting keys panics.
func (*Tree[K, V]) Backward ¶ added in v0.5.0
Backward yields every entry in descending key order, under the same rules as All.
func (*Tree[K, V]) From ¶
From yields the entries with keys at or above from, in ascending key order, seeking directly to the first. Updating a value during the loop is allowed; adding or deleting keys panics.
func (*Tree[K, V]) Keys ¶ added in v0.5.0
Keys yields every key in ascending order, under the same rules as All.
func (*Tree[K, V]) Min ¶
Min returns the smallest key and its value, or reports false if t is empty.
func (*Tree[K, V]) Ptr ¶
Ptr returns a pointer to the value for key, or reports false if key is absent.