critbit

package module
v0.0.0-...-487ef94 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2018 License: MIT Imports: 1 Imported by: 0

README

Crit-bit tree library written in Go

GoDoc Build Status Go Report Card

This is a Crit-bit tree implementation written in Go by referring the C implementation and document.

Installation

Install and update by go get -u github.com/tatsushid/go-critbit.

Usage

package main

import (
	"github.com/tatsushid/go-critbit"
)

func main() {
	// create a tree
	tr := critbit.New()

	tr.Insert([]byte("foo"), 1)
	tr.Insert([]byte("bar"), 2)
	tr.Insert([]byte("foobar"), 3)

	// search exact key
	v, ok := tr.Get([]byte("bar"))
	if !ok {
		panic("should be ok")
	}
	if v.(int) != 2 {
		panic("should be 2")
	}

	// find the longest prefix of a given key
	k, _, _ := tr.LongestPrefix([]byte("foozip"))
	if string(k) != "foo" {
		panic("should be foo")
	}

	var sum int
	// walk the tree with a callback function
	tr.Walk(func(k []byte, v interface{}) bool {
		sum += v.(int)
		return false
	})
	if sum != 6 {
		panic("should be 6 = 1 + 2 + 3")
	}
}

Please see GoDoc for more APIs and details.

This also has Graphviz exporter sample. It is implemented as a part of tests. To use it, please compile a test command by

go test -c

and run the generated command like

./critbit.test -random 10 -printdot > critbit.dot

You can see Graphviz image by opening the created file.

The command has following options

-add key
	add key to critbit tree. this can be used multiple times
-del key
	delete key from critbit tree. this can be used multiple times
-printdot
	print graphviz dot of critbit tree and exit
-random times
	insert keys chosen at random up to specified times

Notes

  • go-radix is widly used tree library and its API is well organized I think. I wrote this library to have a similar API to that and used some test data patterns to make sure it returns same results as that.

  • To compare performance, I took the performance test data from An Adaptive Radix Tree Implementation in Go. If you are interested in it, you can see it by running

    go test -bench . -benchmem
    

    in both cloned repositories. In my environment, this library is a bit slower but a little more memory efficient than that.

  • This can handle a byte sequence with null bytes in it as same as critbitgo, the other Crit-bit library written in Go.

Thanks for all these libraries!

License

This program is under MIT license. Please see the LICENSE file for details.

Documentation

Overview

Package critbit implements Crit-Bit tree for byte sequences.

Crit-Bit tree [1] is fast, memory efficient and a variant of PATRICIA trie. This implementation can be used for byte sequences if it includes a null byte or not. This is based on [2] and extends it to support a null byte in a byte sequence.

[1]: http://cr.yp.to/critbit.html (definition)
[2]: https://github.com/agl/critbit (C implementation and document)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Tree

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

Tree represents a critbit tree.

func New

func New() *Tree

New returns an empty tree.

func (*Tree) Clear

func (t *Tree) Clear() bool

Clear removes all elements in the tree. If it removes something, it returns true while the tree is empty and there is nothing to remove, it returns false.

func (*Tree) Delete

func (t *Tree) Delete(k []byte) (interface{}, bool)

Delete removes a given key and its value from the tree. If it succeeded, it returns the key's previous value and true while if not, it returns nil and false. On an empty tree, it always fails.

func (*Tree) Get

func (t *Tree) Get(k []byte) (interface{}, bool)

Get searches a given key from the tree. If the key exists in the tree, it returns its value and true. If not, it returns nil and false.

func (*Tree) Insert

func (t *Tree) Insert(k []byte, v interface{}) (interface{}, bool)

Insert adds or updates a given key to the tree and returns its previous value and if anything was set or not. If there is the key in the tree, it adds the key and the value to the tree and returns nil and true when it succeeded while if not, it updates the key's value and returns its previous value and true when it succeeded.

func (*Tree) Len

func (t *Tree) Len() int

Len returns a number of elements in the tree.

func (*Tree) LongestPrefix

func (t *Tree) LongestPrefix(prefix []byte) ([]byte, interface{}, bool)

LongestPrefix searches the longest key which is included in a given key and returns the found key and its value. For example, if there are "f", "fo", "foobar" in the tree and "foo" is given, it returns "fo". If it found such a key, it returns true as the bool value while if not, it returns false as it.

func (*Tree) Maximum

func (t *Tree) Maximum() ([]byte, interface{}, bool)

Maximum searches a key from the tree in lexicographic order and returns the last one and its value. If it found such a key, it also returns true as the bool value while if not, it returns false as it.

func (*Tree) Minimum

func (t *Tree) Minimum() ([]byte, interface{}, bool)

Minimum searches a key from the tree in lexicographic order and returns the first one and its value. If it found such a key, it also returns true as the bool value while if not, it returns false as it.

func (*Tree) Walk

func (t *Tree) Walk(fn WalkFn)

Walk walks whole the tree and call a given function with each element's key and value. If the function returns true, the walk is terminated at there.

func (*Tree) WalkPath

func (t *Tree) WalkPath(path []byte, fn WalkFn)

WalkPath walks the tree from the root up to a given key and call a given function with each element's key and value. For example, the tree has "f", "fo", "foob", "foobar" and "foo" is given, it visits "f" and "fo" elements. If the function returns true, the walk is terminated at there.

func (*Tree) WalkPrefix

func (t *Tree) WalkPrefix(prefix []byte, fn WalkFn)

WalkPrefix walks the tree under a given prefix and call a given function with each element's key and value. For example, the tree has "f", "fo", "foob", "foobar" and "foo" is given, it visits "foob" and "foobar" elements. If the function returns true, the walk is terminated at there.

type WalkFn

type WalkFn func(k []byte, v interface{}) bool

WalkFn is used at walking a tree. It receives a key and its value of each elements which a walk function gives. If it returns true, a walk function should be terminated at there.

Jump to

Keyboard shortcuts

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