pairtree

package module
v0.0.0-...-a29be3d Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2017 License: Apache-2.0 Imports: 6 Imported by: 0

README

PairTree

BTree implementation for Go

GoDoc

This package provides an in-memory B-Tree implementation for Go, useful as an ordered, mutable data structure.

This is a fork of the google/btree package which uses tidwall/pair as items.

Documentation

Overview

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

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-Tree implementation 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 cause efficiency differences when compared to equivalent C++ template code that stores values in arrays within the node:

  • Due to the overhead of storing values as interfaces (each value needs to be stored as the value itself, then 2 words for the interface pointing to that value and its type), resulting in higher memory use.
  • Since interfaces can point to values anywhere in memory, values are most likely not stored in contiguous blocks, resulting in a higher number of cache misses.

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

This implementation is designed to be a drop-in replacement to gollrb.LLRB trees, (http://github.com/petar/gollrb), an excellent and probably the most widely used ordered tree implementation in the Go ecosystem currently. Its functions, therefore, exactly mirror those of llrb.LLRB where possible. Unlike gollrb, though, we currently don't support storing multiple equivalent values.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cursor

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

Cursor represents an iterator that can traverse over all items in the tree in sorted order.

Changing data while traversing a cursor may result in unexpected items to be returned. You must reposition your cursor after mutating data.

func (*Cursor) First

func (c *Cursor) First() pair.Pair

First moves the cursor to the first item in the tree and returns that item.

func (*Cursor) Last

func (c *Cursor) Last() pair.Pair

Last moves the cursor to the last item in the tree and returns that item.

func (*Cursor) Next

func (c *Cursor) Next() pair.Pair

Next moves the cursor to the next item and returns that item.

func (*Cursor) Prev

func (c *Cursor) Prev() pair.Pair

Prev moves the cursor to the previous item and returns that item.

func (*Cursor) Seek

func (c *Cursor) Seek(pivot pair.Pair) pair.Pair

Seek moves the cursor to provided item and returns that item. If the item does not exist then the next item is returned.

type PairTree

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

PairTree is an implementation of a B-Tree.

PairTree stores Pair instances in an ordered structure, allowing easy insertion, removal, and iteration.

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

func New

func New(less func(a, b pair.Pair) bool) *PairTree

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

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

func (*PairTree) Ascend

func (t *PairTree) Ascend(iterator func(item pair.Pair) bool)

Ascend calls the iterator for every value in the tree within the range [first, last], until iterator returns false.

func (*PairTree) AscendGreaterOrEqual

func (t *PairTree) AscendGreaterOrEqual(pivot pair.Pair, iterator func(item pair.Pair) bool)

AscendGreaterOrEqual calls the iterator for every value in the tree within the range [pivot, last], until iterator returns false.

func (*PairTree) AscendLessThan

func (t *PairTree) AscendLessThan(pivot pair.Pair, iterator func(item pair.Pair) bool)

AscendLessThan calls the iterator for every value in the tree within the range [first, pivot), until iterator returns false.

func (*PairTree) AscendRange

func (t *PairTree) AscendRange(greaterOrEqual, lessThan pair.Pair, iterator func(item pair.Pair) bool)

AscendRange calls the iterator for every value in the tree within the range [greaterOrEqual, lessThan), until iterator returns false.

func (*PairTree) Clone

func (t *PairTree) Clone() (t2 *PairTree)

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

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

func (*PairTree) Cursor

func (t *PairTree) Cursor() *Cursor

Cursor returns a new cursor used to traverse over items in the tree.

func (*PairTree) Delete

func (t *PairTree) Delete(item pair.Pair) pair.Pair

Delete removes an item equal to the passed in item from the tree, returning it. If no such item exists, returns nil.

func (*PairTree) DeleteMax

func (t *PairTree) DeleteMax() pair.Pair

DeleteMax removes the largest item in the tree and returns it. If no such item exists, returns nil.

func (*PairTree) DeleteMin

func (t *PairTree) DeleteMin() pair.Pair

DeleteMin removes the smallest item in the tree and returns it. If no such item exists, returns nil.

func (*PairTree) Descend

func (t *PairTree) Descend(iterator func(item pair.Pair) bool)

Descend calls the iterator for every value in the tree within the range [last, first], until iterator returns false.

func (*PairTree) DescendGreaterThan

func (t *PairTree) DescendGreaterThan(pivot pair.Pair, iterator func(item pair.Pair) bool)

DescendGreaterThan calls the iterator for every value in the tree within the range (pivot, last], until iterator returns false.

func (*PairTree) DescendLessOrEqual

func (t *PairTree) DescendLessOrEqual(pivot pair.Pair, iterator func(item pair.Pair) bool)

DescendLessOrEqual calls the iterator for every value in the tree within the range [pivot, first], until iterator returns false.

func (*PairTree) DescendRange

func (t *PairTree) DescendRange(lessOrEqual, greaterThan pair.Pair, iterator func(item pair.Pair) bool)

DescendRange calls the iterator for every value in the tree within the range [lessOrEqual, greaterThan), until iterator returns false.

func (*PairTree) Get

func (t *PairTree) Get(key pair.Pair) pair.Pair

Get looks for the key item in the tree, returning it. It returns nil if unable to find that item.

func (*PairTree) Has

func (t *PairTree) Has(key pair.Pair) bool

Has returns true if the given key is in the tree.

func (*PairTree) Len

func (t *PairTree) Len() int

Len returns the number of items currently in the tree.

func (*PairTree) Max

func (t *PairTree) Max() pair.Pair

Max returns the largest item in the tree, or nil if the tree is empty.

func (*PairTree) Min

func (t *PairTree) Min() pair.Pair

Min returns the smallest item in the tree, or nil if the tree is empty.

func (*PairTree) ReplaceOrInsert

func (t *PairTree) ReplaceOrInsert(item pair.Pair) pair.Pair

ReplaceOrInsert adds the given item to the tree. If an item in the tree already equals the given one, it is removed from the tree and returned. Otherwise, nil is returned.

nil cannot be added to the tree (will panic).

Jump to

Keyboard shortcuts

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