btree

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2025 License: Apache-2.0 Imports: 1 Imported by: 4

README

btree

GoDoc Beta

A Go generic library providing copy-on-write B-tree data structures including maps, sets, interval trees, and order-statistic trees. All variants share a common augmented B-tree implementation and support O(1) lazy cloning.

Note: This library is still in beta. Please report any issues on the GitHub issue tracker.

Read more about the design in the blog post.

Interval Trees

The interval package provides interval trees for efficiently finding all intervals that overlap a query range. The iterator supports FirstOverlap() and NextOverlap() methods for querying overlapping intervals.

Order-Statistic Trees

The orderstat package provides order-statistic trees that support O(log n) rank queries and nth element selection. The iterator adds Rank() and SeekNth() methods for efficient positional queries.

License

Copyright 2021 Andrew Werner. Licensed under the Apache License, Version 2.0.

Documentation

Index

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 {
	abstract.Map[K, V, struct{}]
}

Map is a ordered map from K to V.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/ajwerner/btree"
)

func main() {
	m := btree.MakeMap[string, int](strings.Compare)
	m.Upsert("foo", 1)
	m.Upsert("bar", 2)
	fmt.Println(m.Get("foo"))
	fmt.Println(m.Get("baz"))
	it := m.Iterator()
	for it.First(); it.Valid(); it.Next() {
		fmt.Println(it.Cur(), it.Value())
	}

}
Output:
1 true
0 false
bar 2
foo 1

func MakeMap

func MakeMap[K, V any](cmp func(K, K) int) Map[K, V]

MakeMap constructs a new Map with the provided comparison function.

func (*Map[K, V]) Clone

func (m *Map[K, V]) Clone() Map[K, V]

Clone clones the Map, lazily. It does so in constant time.

type MapIterator

type MapIterator[K, V any] = abstract.Iterator[K, V, struct{}]

MapIterator is an iterator for a Map.

type Set

type Set[T any] Map[T, struct{}]

Set is an ordered set of items of type T.

func MakeSet

func MakeSet[T any](cmp func(T, T) int) Set[T]

MakeSet constructs a new Set with the provided comparison function.

func (*Set[T]) Clone

func (t *Set[T]) Clone() Set[T]

Clone clones the Set, lazily. It does so in constant time.

func (*Set[K]) Delete

func (t *Set[K]) Delete(item K) (removed bool)

Delete removes the value with the provided key. It returns true if the item existed in the set.

func (*Set[T]) Upsert

func (t *Set[T]) Upsert(item T) (replaced T, overwrote bool)

Upsert inserts or updates the provided item. It returns the overwritten item if a previous value existed for the key.

type SetIterator

type SetIterator[T any] = MapIterator[T, struct{}]

SetIterator is an iterator for a Set.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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