radix

package module
v0.0.0-...-06e8287 Latest Latest
Warning

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

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

README

go-radix Build Status

Provides the radix package that implements a radix tree. The package only provides a single Tree implementation, optimized for sparse nodes.

As a radix tree, it provides the following:

  • O(k) operations. In many cases, this can be faster than a hash table since the hash function is an O(k) operation, and hash tables have very poor cache locality.
  • Minimum / Maximum value lookups
  • Ordered iteration

For an immutable variant, see go-immutable-radix.

Documentation

The full documentation is available on Godoc.

Example

Below is a simple example of usage

// Create a tree
r := radix.New()
r.Insert("foo", 1)
r.Insert("bar", 2)
r.Insert("foobar", 2)

// Find the longest prefix match
m, _, _ := r.LongestPrefix("foozip")
if m != "foo" {
    panic("should be foo")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Edge

type Edge struct {
	Label byte  `msgp:"l"`
	Node  *Node `msgp:"n"`
}

Edge is used to represent an Edge Node

func (*Edge) DecodeMsg

func (z *Edge) DecodeMsg(dc *msgp.Reader) (err error)

DecodeMsg implements msgp.Decodable

func (*Edge) EncodeMsg

func (z *Edge) EncodeMsg(en *msgp.Writer) (err error)

EncodeMsg implements msgp.Encodable

func (*Edge) MarshalMsg

func (z *Edge) MarshalMsg(b []byte) (o []byte, err error)

MarshalMsg implements msgp.Marshaler

func (*Edge) Msgsize

func (z *Edge) Msgsize() (s int)

Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message

func (*Edge) UnmarshalMsg

func (z *Edge) UnmarshalMsg(bts []byte) (o []byte, err error)

UnmarshalMsg implements msgp.Unmarshaler

type Edges

type Edges []Edge

func (*Edges) DecodeMsg

func (z *Edges) DecodeMsg(dc *msgp.Reader) (err error)

DecodeMsg implements msgp.Decodable

func (Edges) EncodeMsg

func (z Edges) EncodeMsg(en *msgp.Writer) (err error)

EncodeMsg implements msgp.Encodable

func (Edges) Len

func (e Edges) Len() int

func (Edges) Less

func (e Edges) Less(i, j int) bool

func (Edges) MarshalMsg

func (z Edges) MarshalMsg(b []byte) (o []byte, err error)

MarshalMsg implements msgp.Marshaler

func (Edges) Msgsize

func (z Edges) Msgsize() (s int)

Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message

func (Edges) Sort

func (e Edges) Sort()

func (Edges) Swap

func (e Edges) Swap(i, j int)

func (*Edges) UnmarshalMsg

func (z *Edges) UnmarshalMsg(bts []byte) (o []byte, err error)

UnmarshalMsg implements msgp.Unmarshaler

type LeafNode

type LeafNode struct {
	Key string      `msgp:"k"`
	Val interface{} `msgp:"v"`
}

LeafNode is used to represent a value

func (*LeafNode) DecodeMsg

func (z *LeafNode) DecodeMsg(dc *msgp.Reader) (err error)

DecodeMsg implements msgp.Decodable

func (LeafNode) EncodeMsg

func (z LeafNode) EncodeMsg(en *msgp.Writer) (err error)

EncodeMsg implements msgp.Encodable

func (LeafNode) MarshalMsg

func (z LeafNode) MarshalMsg(b []byte) (o []byte, err error)

MarshalMsg implements msgp.Marshaler

func (LeafNode) Msgsize

func (z LeafNode) Msgsize() (s int)

Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message

func (*LeafNode) UnmarshalMsg

func (z *LeafNode) UnmarshalMsg(bts []byte) (o []byte, err error)

UnmarshalMsg implements msgp.Unmarshaler

type Node

type Node struct {
	// Leaf is used to store possible Leaf
	Leaf *LeafNode `msgp:"l"`

	// Prefix is the common Prefix we ignore
	Prefix string `msgp:"p"`

	// Edges should be stored in-order for iteration.
	// We avoid a fully materialized slice to save memory,
	// since in most cases we expect to be sparse
	Edges Edges `msgp:"e"`
}

func (*Node) DecodeMsg

func (z *Node) DecodeMsg(dc *msgp.Reader) (err error)

DecodeMsg implements msgp.Decodable

func (*Node) EncodeMsg

func (z *Node) EncodeMsg(en *msgp.Writer) (err error)

EncodeMsg implements msgp.Encodable

func (*Node) MarshalMsg

func (z *Node) MarshalMsg(b []byte) (o []byte, err error)

MarshalMsg implements msgp.Marshaler

func (*Node) Msgsize

func (z *Node) Msgsize() (s int)

Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message

func (*Node) UnmarshalMsg

func (z *Node) UnmarshalMsg(bts []byte) (o []byte, err error)

UnmarshalMsg implements msgp.Unmarshaler

type Tree

type Tree struct {
	Root *Node `msgp:"r"`
	Size int   `msgp:"s"`
}

Tree implements a radix tree. This can be treated as a Dictionary abstract data type. The main advantage over a standard hash map is Prefix-based lookups and ordered iteration,

func New

func New() *Tree

New returns an empty Tree

func NewFromMap

func NewFromMap(m map[string]interface{}) *Tree

NewFromMap returns a new tree containing the keys from an existing map

func (*Tree) DecodeMsg

func (z *Tree) DecodeMsg(dc *msgp.Reader) (err error)

DecodeMsg implements msgp.Decodable

func (*Tree) Delete

func (t *Tree) Delete(s string) (interface{}, bool)

Delete is used to delete a Key, returning the previous value and if it was deleted

func (*Tree) DeletePrefix

func (t *Tree) DeletePrefix(s string) int

DeletePrefix is used to delete the subtree under a Prefix Returns how many nodes were deleted Use this to delete large subtrees efficiently

func (*Tree) EncodeMsg

func (z *Tree) EncodeMsg(en *msgp.Writer) (err error)

EncodeMsg implements msgp.Encodable

func (*Tree) Get

func (t *Tree) Get(s string) (interface{}, bool)

Get is used to lookup a specific Key, returning the value and if it was found

func (*Tree) Insert

func (t *Tree) Insert(s string, v interface{}) (interface{}, bool)

Insert is used to add a newentry or update an existing entry. Returns if updated.

func (*Tree) Len

func (t *Tree) Len() int

Len is used to return the number of elements in the tree

func (*Tree) LongestPrefix

func (t *Tree) LongestPrefix(s string) (string, interface{}, bool)

LongestPrefix is like Get, but instead of an exact match, it will return the longest Prefix match.

func (*Tree) MarshalMsg

func (z *Tree) MarshalMsg(b []byte) (o []byte, err error)

MarshalMsg implements msgp.Marshaler

func (*Tree) Maximum

func (t *Tree) Maximum() (string, interface{}, bool)

Maximum is used to return the maximum value in the tree

func (*Tree) Minimum

func (t *Tree) Minimum() (string, interface{}, bool)

Minimum is used to return the minimum value in the tree

func (*Tree) Msgsize

func (z *Tree) Msgsize() (s int)

Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message

func (*Tree) ToMap

func (t *Tree) ToMap() map[string]interface{}

ToMap is used to walk the tree and convert it into a map

func (*Tree) UnmarshalMsg

func (z *Tree) UnmarshalMsg(bts []byte) (o []byte, err error)

UnmarshalMsg implements msgp.Unmarshaler

func (*Tree) Walk

func (t *Tree) Walk(fn WalkFn)

Walk is used to walk the tree

func (*Tree) WalkPath

func (t *Tree) WalkPath(path string, fn WalkFn)

WalkPath is used to walk the tree, but only visiting nodes from the Root down to a given Leaf. Where WalkPrefix walks all the entries *under* the given Prefix, this walks the entries *above* the given Prefix.

func (*Tree) WalkPrefix

func (t *Tree) WalkPrefix(prefix string, fn WalkFn)

WalkPrefix is used to walk the tree under a Prefix

type WalkFn

type WalkFn func(s string, v interface{}) bool

WalkFn is used when walking the tree. Takes a Key and value, returning if iteration should be terminated.

Jump to

Keyboard shortcuts

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