btree

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

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

func New[K, V any](a memory.Allocator, cmp func(a, b K) int) (*Tree[K, V], error)

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

func (t *Tree[K, V]) All() iter.Seq2[K, V]

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

func (t *Tree[K, V]) Backward() iter.Seq2[K, V]

Backward yields every entry in descending key order, under the same rules as All.

func (*Tree[K, V]) Delete

func (t *Tree[K, V]) Delete(key K) bool

Delete removes key and reports whether it was present.

func (*Tree[K, V]) Free

func (t *Tree[K, V]) Free() error

Free releases every entry. t must not be used afterwards.

func (*Tree[K, V]) From

func (t *Tree[K, V]) From(from K) iter.Seq2[K, V]

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]) Get

func (t *Tree[K, V]) Get(key K) (V, bool)

Get returns the value for key, or reports false if key is absent.

func (*Tree[K, V]) Keys added in v0.5.0

func (t *Tree[K, V]) Keys() iter.Seq[K]

Keys yields every key in ascending order, under the same rules as All.

func (*Tree[K, V]) Len

func (t *Tree[K, V]) Len() int

Len returns the number of entries.

func (*Tree[K, V]) Max

func (t *Tree[K, V]) Max() (K, V, bool)

Max returns the largest key and its value, or reports false if t is empty.

func (*Tree[K, V]) Min

func (t *Tree[K, V]) Min() (K, V, bool)

Min returns the smallest key and its value, or reports false if t is empty.

func (*Tree[K, V]) Ptr

func (t *Tree[K, V]) Ptr(key K) (*V, bool)

Ptr returns a pointer to the value for key, or reports false if key is absent.

func (*Tree[K, V]) Range added in v0.5.0

func (t *Tree[K, V]) Range(from, to K) iter.Seq2[K, V]

Range yields the entries with keys in [from, to), in ascending key order, under the same rules as From.

func (*Tree[K, V]) Set

func (t *Tree[K, V]) Set(key K, value V) error

Set stores value under key.

func (*Tree[K, V]) Values added in v0.5.0

func (t *Tree[K, V]) Values() iter.Seq[V]

Values yields every value in ascending key order, under the same rules as All.

Jump to

Keyboard shortcuts

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