radix

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2019 License: MIT Imports: 5 Imported by: 4

README

radix (Radix tree implementation in Go)

Build Status Sourcegraph GoDoc Minimal Version

About

This package is an implementation of a radix tree in Go (or Golang).

Searching for static values in the tree doesn't allocate memory on the heap, what makes it pretty fast.
It can also sort nodes by priority, therefore traversing nodes that hold more non-nil values first.

Usage

Full documentation here.

Installing
Go 1.10

vgo get -u github.com/gbrlsnchs/radix

Go 1.11

go get -u github.com/gbrlsnchs/radix

Importing
import (
	// ...

	"github.com/gbrlsnchs/radix"
)
Building this example from Wikipedia
tr := radix.New(radix.Tdebug)
tr.Add("romane", 1)
tr.Add("romanus", 2)
tr.Add("romulus", 3)
tr.Add("rubens", 4)
tr.Add("ruber", 5)
tr.Add("rubicon", 6)
tr.Add("rubicundus", 7)
tr.Sort(radix.PrioritySort) // optional step
fmt.Println(tr)
The code above will print this
. (14 nodes)
└── 7↑ r → <nil>
    ├── 4↑ ub → <nil>
    │   ├── 2↑ ic → <nil>
    │   │   ├── 1↑ undus 🍂 → 7
    │   │   └── 1↑ on 🍂 → 6
    │   └── 2↑ e → <nil>
    │       ├── 1↑ r 🍂 → 5
    │       └── 1↑ ns 🍂 → 4
    └── 3↑ om → <nil>
        ├── 2↑ an → <nil>
        │   ├── 1↑ us 🍂 → 2
        │   └── 1↑ e 🍂 → 1
        └── 1↑ ulus 🍂 → 3
Retrieving a value from the tree
n, _ := tr.Get("rubicon") // zero-allocation search
fmt.Println(n.Value)      // prints "6"
Building a dynamic tree

A dynamic tree is a tree that can match labels based on a placeholder and a demiliter (e.g. an HTTP router that accepts dynamic routes).
Note that this only works with prefix trees, not binary ones.

tr := radix.New(0) // passing 0 means passing no flags
tr.Add("/dynamic/path/@id", 1)
tr.Add("/dynamic/path/@id/subpath/@name", 2)
tr.Add("/static/path", 3)
tr.SetBoundaries('@', '/')

var (
	n *radix.Node
	p map[string]string
)
n, p = tr.Get("/dynamic/path/123")
fmt.Println(n.Value) // prints "1"
fmt.Println(p["id"]) // prints "123"

n, p = tr.Get("/dynamic/path/456/subpath/foobar")
fmt.Println(n.Value)   // prints "2"
fmt.Println(p["id"])   // prints "456"
fmt.Println(p["name"]) // prints "foobar"

n, _ = tr.Get("/static/path") // p would be nil
fmt.Println(n.Value)          // prints "3"
Building a binary tree
tr := radix.New(radix.Tdebug | radix.Tbinary)
tr.Add("deck", 1)
tr.Add("did", 2)
tr.Add("doe", 3)
tr.Add("dog", 4)
tr.Add("doge", 5)
tr.Add("dogs", 6)
The code above will print this
. (71 nodes)
01100100011001010110001101101011 🍂 → 1
011001000110100101100100 🍂 → 2
011001000110111101100101 🍂 → 3
011001000110111101100111 → 4
01100100011011110110011101100101 🍂 → 5
01100100011011110110011101110011 🍂 → 6

Contributing

How to help

Documentation

Overview

Package radix is an implementation of a radix tree.

It has the three basic operations (insertion, lookup and deletion) plus some additional methods, notably one that allows a dynamic lookup based on delimiters, which works similar to a named parameter functionality in HTTP routers.

Index

Examples

Constants

View Source
const (
	// Tsafe activates thread safety.
	Tsafe = 1 << iota
	// Tdebug adds more information to the tree's string representation.
	Tdebug
	// Tbinary uses a binary PATRICIA tree instead of a prefix tree.
	Tbinary
	// Tnocolor disables colorful output.
	Tnocolor
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Node

type Node struct {
	Value interface{}
	// contains filtered or unexported fields
}

Node is a node of a radix tree.

func (*Node) Depth

func (n *Node) Depth() int

Depth returns the node's depth.

func (*Node) IsLeaf

func (n *Node) IsLeaf() bool

IsLeaf returns whether the node is a leaf.

func (*Node) Priority

func (n *Node) Priority() int

Priority returns the node's priority.

type SortingTechnique added in v0.3.0

type SortingTechnique uint8

SortingTechnique is the technique used to sort the tree.

const (
	// AscLabelSort is the value for sorting
	// the tree's edges by label in ascending order.
	AscLabelSort SortingTechnique = iota
	// DescLabelSort is the value for sorting
	// the tree's edges by label in descending order.
	DescLabelSort
	// PrioritySort is the value for sorting
	// the tree's edges by the priority of their nodes.
	PrioritySort
)

type Tree

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

Tree is a radix tree.

Example
package main

import (
	"fmt"

	"github.com/gbrlsnchs/radix"
)

func main() {
	tr := radix.New(radix.Tdebug)
	tr.Add("romane", 1)
	tr.Add("romanus", 2)
	tr.Add("romulus", 3)
	tr.Add("rubens", 4)
	tr.Add("ruber", 5)
	tr.Add("rubicon", 6)
	tr.Add("rubicundus", 7)
	fmt.Printf("%v\n", tr)
}
Output:

func New

func New(flags int) *Tree

New creates a named radix tree with a single node (its root).

func (*Tree) Add

func (tr *Tree) Add(label string, v interface{})

Add adds a new node to the tree.

func (*Tree) Del

func (tr *Tree) Del(label string)

Del deletes a node.

If a parent node that holds no value ends up holding only one edge after a deletion of one of its edges, it gets merged with the remaining edge.

func (*Tree) Get

func (tr *Tree) Get(label string) (*Node, map[string]string)

Get retrieves a node.

func (*Tree) Len added in v1.0.0

func (tr *Tree) Len() int

Len returns the total numbers of nodes, including the tree's root.

func (*Tree) SetBoundaries added in v1.0.0

func (tr *Tree) SetBoundaries(placeholder, delim byte)

SetBoundaries sets a placeholder and a delimiter for the tree to be able to search for named labels.

func (*Tree) Size

func (tr *Tree) Size() int

Size returns the total byte size stored in the tree.

func (*Tree) Sort added in v0.2.0

func (tr *Tree) Sort(st SortingTechnique)

Sort sorts the tree nodes and its children recursively according to their priority lengther.

func (*Tree) String

func (tr *Tree) String() string

String returns a string representation of the tree structure.

Jump to

Keyboard shortcuts

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