btree

package
v0.4.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: 4 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]) 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]) 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]) Set

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

Set stores value under key.

Jump to

Keyboard shortcuts

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