btree

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Copyright 2025 Stoolap Contributors

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Package btree provides optimized B-tree implementations

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BTree

type BTree[K Comparer[K], V any] struct {
	// contains filtered or unexported fields
}

BTree is a generic B-tree implementation for any key type that implements Comparer

func NewBTree

func NewBTree[K Comparer[K], V any]() *BTree[K, V]

NewBTree creates a new BTree for keys of type K that implement Comparer

func (*BTree[K, V]) Delete

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

Delete removes a key-value pair from the tree

func (*BTree[K, V]) ForEach

func (t *BTree[K, V]) ForEach(callback func(key K, value V) bool)

ForEach visits all keys in the tree in order

func (*BTree[K, V]) Insert

func (t *BTree[K, V]) Insert(key K, value V)

Insert adds or updates a key-value pair in the tree

func (*BTree[K, V]) Iterate

func (t *BTree[K, V]) Iterate() *Iterator[K, V]

Iterate creates an iterator for the BTree

func (*BTree[K, V]) Search

func (t *BTree[K, V]) Search(key K) (V, bool)

Search looks up a key in the tree Returns the value and true if found, or zero value and false if not found

func (*BTree[K, V]) SeekGE

func (t *BTree[K, V]) SeekGE(target K) *Iterator[K, V]

SeekGE positions the iterator at the first key greater than or equal to the target

func (*BTree[K, V]) Size

func (t *BTree[K, V]) Size() int

Size returns the number of key-value pairs in the tree

func (*BTree[K, V]) String

func (t *BTree[K, V]) String() string

String returns a string representation of the tree (for debugging)

type Comparer

type Comparer[K any] interface {
	// Compare compares this key with another key
	// Returns negative if this < other, 0 if equal, positive if this > other
	Compare(other K) int
}

Comparer defines the interface for objects that can be used as keys in a BTree

type Int64BTree

type Int64BTree[V any] struct {
	// contains filtered or unexported fields
}

Int64BTree is a highly optimized B-tree for int64 keys Key design points: 1. Fixed size arrays for keys/values/children to avoid resizing 2. Cache-friendly memory layout 3. Binary search for fast key lookup 4. Block transfer for bulk operations

func NewInt64BTree

func NewInt64BTree[V any]() *Int64BTree[V]

NewInt64BTree creates a new BTree optimized for int64 keys

func (*Int64BTree[V]) BatchInsert

func (t *Int64BTree[V]) BatchInsert(keys []int64, values []V)

BatchInsert efficiently inserts multiple key-value pairs

func (*Int64BTree[V]) Delete

func (t *Int64BTree[V]) Delete(key int64) bool

Delete removes a key-value pair from the tree Returns true if the key was found and deleted, false otherwise

func (*Int64BTree[V]) ForEach

func (t *Int64BTree[V]) ForEach(callback func(key int64, value V) bool)

ForEach visits all keys in the tree in order

func (*Int64BTree[V]) GetAll

func (t *Int64BTree[V]) GetAll() iter.Seq2[int64, V]

GetAll returns all values in the tree

func (*Int64BTree[V]) Insert

func (t *Int64BTree[V]) Insert(key int64, value V)

Insert adds or updates a key-value pair in the tree Returns true if a new key was inserted, false if an existing key was updated

func (*Int64BTree[V]) RangeSearch

func (t *Int64BTree[V]) RangeSearch(start, end int64) []V

RangeSearch finds all values with keys in range [start, end] inclusive

func (*Int64BTree[V]) Search

func (t *Int64BTree[V]) Search(key int64) (V, bool)

Search looks up a key in the tree Returns the value and true if found, or zero value and false if not found

func (*Int64BTree[V]) Size

func (t *Int64BTree[V]) Size() int

Size returns the number of key-value pairs in the tree

type Iterator

type Iterator[K Comparer[K], V any] struct {
	// contains filtered or unexported fields
}

Iterator provides a way to iterate through all key-value pairs in the BTree

func (*Iterator[K, V]) Get

func (iter *Iterator[K, V]) Get() (K, V)

Get returns the current key and value

func (*Iterator[K, V]) Next

func (iter *Iterator[K, V]) Next() bool

Next advances the iterator to the next key-value pair

func (*Iterator[K, V]) Valid

func (iter *Iterator[K, V]) Valid() bool

Valid returns whether the iterator is positioned at a valid element

Jump to

Keyboard shortcuts

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