Movatterモバイル変換


[0]ホーム

URL:


btree

package
v0.123.0Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2025 License:Apache-2.0Imports:5Imported by:0

Details

Repository

github.com/googleapis/google-cloud-go

Links

README

This package is a fork of github.com/jba/btree at commitd4edd57f39b8425fc2c631047ff4dc6024d82a4f, which itself was a fork ofgithub.com/google/btree at 316fb6d3f031ae8f4d457c6c5186b9e3ded70435.

This directory makes the following modifications:

  • Updated copyright notice.
  • removed LICENSE (it is the same as the repo-wide license, Apache 2.0)
  • Removed examples_test.go and .travis.yml.
  • Added this file.

Documentation

Overview

Package btree implements in-memory B-Trees of arbitrary degree.

This implementation is based on google/btree (http://github.com/google/btree), andmuch of the code is taken from there. But the API has been changed significantly,particularly around iteration, and support for indexing by position has beenadded.

btree implements an in-memory B-Tree for use as an ordered data structure.It is not meant for persistent storage solutions.

It has a flatter structure than an equivalent red-black or other binary tree,which in some cases yields better memory usage and/or performance.See some discussion on the matter here:

http://google-opensource.blogspot.com/2013/01/c-containers-that-save-memory-and-time.html

Note, though, that this project is in no way related to the C++ B-Treeimplementation written about there.

Within this tree, each node contains a slice of items and a (possibly nil)slice of children. For basic numeric values or raw structs, this can causeefficiency differences when compared to equivalent C++ template code thatstores values in arrays within the node:

  • Due to the overhead of storing values as interfaces (eachvalue needs to be stored as the value itself, then 2 words for theinterface pointing to that value and its type), resulting in highermemory use.
  • Since interfaces can point to values anywhere in memory, values aremost likely not stored in contiguous blocks, resulting in a highernumber of cache misses.

These issues don't tend to matter, though, when working with strings or otherheap-allocated structures, since C++-equivalent structures also must storepointers and also distribute their values across the heap.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

typeBTree

type BTree struct {// contains filtered or unexported fields}

BTree is an implementation of a B-Tree.

BTree stores item instances in an ordered structure, allowing easy insertion,removal, and iteration.

Write operations are not safe for concurrent mutation by multiplegoroutines, but Read operations are.

funcNew

func New(degreeint, less func(interface{}, interface{})bool) *BTree

New creates a new B-Tree with the given degree and comparison function.

New(2, less), for example, will create a 2-3-4 tree (each node contains 1-3 itemsand 2-4 children).

The less function tests whether the current item is less than the given argument.It must provide a strict weak ordering.If !less(a, b) && !less(b, a), we treat this to mean a == b (i.e. the treecan hold only one of a or b).

func (*BTree)After

func (t *BTree) After(kKey) *Iterator

After returns an iterator positioned just after k. After the first call to Next,the Iterator will be at k, or at the key just less than k if k is not in the tree.Subsequent calls to Next will traverse the tree's items in descending order.

func (*BTree)AfterIndex

func (t *BTree) AfterIndex(iint) *Iterator

AfterIndex returns an iterator positioned just after the item with the given index.The iterator will traverse the tree's items in descending order.If i is not in the range [0, tr.Len()], AfterIndex panics.Note that it is not an error to provide an index of tr.Len().

func (*BTree)At

func (t *BTree) At(iint) (Key,Value)

At returns the key and value at index i. The minimum item has index 0.If i is outside the range [0, t.Len()), At panics.

func (*BTree)Before

func (t *BTree) Before(kKey) *Iterator

Before returns an iterator positioned just before k. After the first call to Next,the Iterator will be at k, or at the key just greater than k if k is not in the tree.Subsequent calls to Next will traverse the tree's items in ascending order.

func (*BTree)BeforeIndex

func (t *BTree) BeforeIndex(iint) *Iterator

BeforeIndex returns an iterator positioned just before the item with the given index.The iterator will traverse the tree's items in ascending order.If i is not in the range [0, tr.Len()], BeforeIndex panics.Note that it is not an error to provide an index of tr.Len().

func (*BTree)Clone

func (t *BTree) Clone() *BTree

Clone clones the btree, lazily. Clone should not be called concurrently,but the original tree (t) and the new tree (t2) can be used concurrentlyonce the Clone call completes.

The internal tree structure of b is marked read-only and shared between t andt2. Writes to both t and t2 use copy-on-write logic, creating new nodeswhenever one of b's original nodes would have been modified. Read operationsshould have no performance degredation. Write operations for both t and t2will initially experience minor slow-downs caused by additional allocs andcopies due to the aforementioned copy-on-write logic, but should converge tothe original performance characteristics of the original tree.

func (*BTree)Delete

func (t *BTree) Delete(kKey) (Value,bool)

Delete removes the item with the given key, returning its value. The second return valuereports whether the key was found.

func (*BTree)DeleteMax

func (t *BTree) DeleteMax() (Key,Value)

DeleteMax removes the largest item in the tree and returns its key and value.If the tree is empty, it returns zero values.

func (*BTree)DeleteMin

func (t *BTree) DeleteMin() (Key,Value)

DeleteMin removes the smallest item in the tree and returns its key and value.If the tree is empty, it returns zero values.

func (*BTree)Get

func (t *BTree) Get(kKey)Value

Get returns the value for the given key in the tree, or the zero value if thekey is not in the tree.

To distinguish a zero value from a key that is not present, use GetWithIndex.

func (*BTree)GetWithIndex

func (t *BTree) GetWithIndex(kKey) (Value,int)

GetWithIndex returns the value and index for the given key in the tree, or thezero value and -1 if the key is not in the tree.

func (*BTree)Has

func (t *BTree) Has(kKey)bool

Has reports whether the given key is in the tree.

func (*BTree)Len

func (t *BTree) Len()int

Len returns the number of items currently in the tree.

func (*BTree)Max

func (t *BTree) Max() (Key,Value)

Max returns the largest key in the tree and its value. If the tree is empty, bothreturn values are zero values.

func (*BTree)Min

func (t *BTree) Min() (Key,Value)

Min returns the smallest key in the tree and its value. If the tree is empty, itreturns zero values.

func (*BTree)Set

func (t *BTree) Set(kKey, vValue) (oldValue, presentbool)

Set sets the given key to the given value in the tree. If the key is present inthe tree, its value is changed and the old value is returned along with a secondreturn value of true. If the key is not in the tree, it is added, and the secondreturn value is false.

func (*BTree)SetWithIndexadded inv0.21.0

func (t *BTree) SetWithIndex(kKey, vValue) (oldValue, presentbool, indexint)

SetWithIndex sets the given key to the given value in the tree, and returns theindex at which it was inserted.

typeIterator

type Iterator struct {KeyKeyValueValue// Index is the position of the item in the tree viewed as a sequence.// The minimum item has index zero.Indexint// contains filtered or unexported fields}

An Iterator supports traversing the items in the tree.

func (*Iterator)Next

func (it *Iterator) Next()bool

Next advances the Iterator to the next item in the tree. If Next returns true,the Iterator's Key, Value and Index fields refer to the next item. If Next returnsfalse, there are no more items and the values of Key, Value and Index are undefined.

If the tree is modified during iteration, the behavior is undefined.

typeKey

type Key interface{}

Key represents a key into the tree.

typeValue

type Value interface{}

Value represents a value in the tree.

Source Files

View all Source files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp