art

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2015 License: MIT Imports: 4 Imported by: 2

README

                                        __      
                                       /\ \__   
   __     ___               __     _ __\ \ ,_\  
 /'_ `\  / __`\  _______  /'__`\  /\`'__\ \ \/  
/\ \L\ \/\ \L\ \/\______\/\ \L\.\_\ \ \/ \ \ \_ 
\ \____ \ \____/\/______/\ \__/.\_\\ \_\  \ \__\
 \/___L\ \/___/           \/__/\/_/ \/_/   \/__/
   /\____/                                      
   \_/__/                                       

an adaptive radix tree implementation in go

Build Status

what

An Adaptive Radix Tree is an indexing data structure similar to traditional radix trees, but uses internal nodes that grow and shrink intelligently with consecutive inserts and removals.

Adaptive Radix Trees have many interesting attributes that could be seen as improvements on other indexing data structures like Hash Maps or other Prefix Trees, such as:

  • Worst-case search complexity of O(k), where k is the length of the key.
  • They don't have to be rebuilt due to excessive inserts.
  • The structure of their inner nodes is space-efficent when compared to traditional Prefix Trees.
  • They provide prefix compression, a technique where each inner node specifies how far to 'fast-forward' in the search key before traversing to the next child.

usage

Include go-art in your go pacakages with:

import( "github.com/kellydunn/go-art" )

Go nuts:

// Make an ART Tree
tree := art.NewTree()

// Insert some stuff
tree.Insert([]byte("art trees"), []byte("are rad"))

// Search for a key, and get the resultant value
res := tree.Search([]byte("art trees"))

// Inspect your result!
fmt.Printf("%s\n", res) // "are rad"

documentation

Check out the documentation on godoc.org: http://godoc.org/github.com/kellydunn/go-art

implementation details

  • It's currently unclear if golang supports SIMD instructions, so Node16s make use of Binary Search for lookups instead of the originally specified manner.
  • Search is currently implemented in the pessimistic variation as described in the specification linked below.

performance

Worst-case scenarios for basic operations are:

Search Insert Removal
O(k) O(k)+c O(k)+c
  • k is the length of the key that we wish to insert. With prefix compression, this can be faster than Hashing functions, since hashing functions are O(k) operations.
  • c is the number of children at the parent node of insertion or removal. This accounts for the growing or shrinking of the inner node. At the worst case, this is number is 48; the maximum number of children to move when transitioning between the biggest types of inner nodes.

releated works

Documentation

Overview

Pacakge art provides a golang implementation of Adaptive Radix Trees

Pacakge art provides a golang implementation of Adaptive Radix Trees

Index

Constants

View Source
const (
	// From the specification: Radix trees consist of two types of nodes:
	// Inner nodes, which map partial keys to other nodes,
	// and leaf nodes, which store the values corresponding to the keys.
	NODE4 = iota
	NODE16
	NODE48
	NODE256
	LEAF

	// Inner nodes of type Node4 must have between 2 and 4 children.
	NODE4MIN = 2
	NODE4MAX = 4

	// Inner nodes of type Node16 must have between 5 and 16 children.
	NODE16MIN = 5
	NODE16MAX = 16

	// Inner nodes of type Node48 must have between 17 and 48 children.
	NODE48MIN = 17
	NODE48MAX = 48

	// Inner nodes of type Node256 must have between 49 and 256 children.
	NODE256MIN = 49
	NODE256MAX = 256

	MAX_PREFIX_LEN = 10
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ArtNode

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

Defines a single ArtNode and its attributes.

func NewLeafNode

func NewLeafNode(key []byte, value interface{}) *ArtNode

func NewNode16

func NewNode16() *ArtNode

From the specification: This node type is used for storing between 5 and 16 child pointers. Like the Node4, the keys and pointers are stored in separate arrays at corresponding positions, but both arrays have space for 16 entries. A key can be found efficiently with binary search or, on modern hardware, with parallel comparisons using SIMD instructions.

func NewNode256

func NewNode256() *ArtNode

From the specification: The largest node type is simply an array of 256 pointers and is used for storing between 49 and 256 entries. With this representation, the next node can be found very efficiently using a single lookup of the key byte in that array. No additional indirection is necessary. If most entries are not null, this representation is also very space efficient because only pointers need to be stored.

func NewNode4

func NewNode4() *ArtNode

From the specification: The smallest node type can store up to 4 child pointers and uses an array of length 4 for keys and another array of the same length for pointers. The keys and pointers are stored at corresponding positions and the keys are sorted.

func NewNode48

func NewNode48() *ArtNode

From the specification: As the number of entries in a node increases, searching the key array becomes expensive. Therefore, nodes with more than 16 pointers do not store the keys explicitly. Instead, a 256-element array is used, which can be indexed with key bytes directly. If a node has between 17 and 48 child pointers, this array stores indexes into a second array which contains up to 48 pointers.

func (*ArtNode) AddChild

func (n *ArtNode) AddChild(key byte, node *ArtNode)

Adds the passed in node to the current ArtNode's children at the specified key. The current node will grow if necessary in order for the insertion to take place.

func (*ArtNode) FindChild

func (n *ArtNode) FindChild(key byte) **ArtNode

Returns a pointer to the child that matches the passed in key, or nil if not present.

func (*ArtNode) Index

func (n *ArtNode) Index(key byte) int

func (*ArtNode) IsFull

func (n *ArtNode) IsFull() bool

Returns whether or not this particular art node is full.

func (*ArtNode) IsLeaf

func (n *ArtNode) IsLeaf() bool

Returns whether or not this particular art node is a leaf node.

func (*ArtNode) IsMatch

func (n *ArtNode) IsMatch(key []byte) bool

Returns whether or not the key stored in the leaf matches the passed in key.

func (*ArtNode) LongestCommonPrefix

func (n *ArtNode) LongestCommonPrefix(other *ArtNode, depth int) int

Returns the longest number of bytes that match between the current node's prefix and the passed in node at the specified depth.

func (*ArtNode) MaxSize

func (n *ArtNode) MaxSize() int

Returns the maximum number of children for the current node.

func (*ArtNode) Maximum

func (n *ArtNode) Maximum() *ArtNode

Returns the Maximum child at the current node. The maximum child is determined by recursively traversing down the tree by selecting the biggest possible byte in each child until a leaf has been reached.

func (*ArtNode) MinSize

func (n *ArtNode) MinSize() int

Returns the minimum number of children for the current node.

func (*ArtNode) Minimum

func (n *ArtNode) Minimum() *ArtNode

Returns the Minimum child at the current node. The minimum child is determined by recursively traversing down the tree by selecting the smallest possible byte in each child until a leaf has been reached.

func (*ArtNode) PrefixMismatch

func (n *ArtNode) PrefixMismatch(key []byte, depth int) int

Returns the number of bytes that differ between the passed in key and the compressed path of the current node at the specified depth.

func (*ArtNode) RemoveChild

func (n *ArtNode) RemoveChild(key byte)

The child indexed by the passed in key is removed if found and the current ArtNode is shrunk if it falls below its minimum size.

func (*ArtNode) Value

func (n *ArtNode) Value() interface{}

Returns the value of the given node, or nil if it is not a leaf.

type ArtTree

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

func NewArtTree

func NewArtTree() *ArtTree

Creates and returns a new Art Tree with a nil root and a size of 0.

func (*ArtTree) Each

func (t *ArtTree) Each(callback func(*ArtNode))

Convenience method for EachPreorder

func (*ArtTree) Insert

func (t *ArtTree) Insert(key []byte, value interface{})

Inserts the passed in value that is indexed by the passed in key into the ArtTree.

func (*ArtTree) Remove

func (t *ArtTree) Remove(key []byte)

Removes the child that is accessed by the passed in key.

func (*ArtTree) Search

func (t *ArtTree) Search(key []byte) interface{}

Returns the node that contains the passed in key, or nil if not found.

Jump to

Keyboard shortcuts

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