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.
Example ¶
package main import ( "fmt" "github.com/gbrlsnchs/radix" ) func main() { // Example from https://upload.wikimedia.org/wikipedia/commons/a/ae/Patricia_trie.svg. t := radix.New("Example") t.Add("romane", 1) t.Add("romanus", 2) t.Add("romulus", 3) t.Add("rubens", 4) t.Add("ruber", 5) t.Add("rubicon", 6) t.Add("rubicundus", 7) t.Sort(radix.AscLabelSort) err := t.Debug() if err != nil { // ... } t.Sort(radix.DescLabelSort) err = t.Debug() if err != nil { // ... } t.Sort(radix.PrioritySort) err = t.Debug() if err != nil { // ... } n := t.Get("romanus") fmt.Println(n.Value) }
Output: 2
Example (Named) ¶
package main import ( "fmt" "github.com/gbrlsnchs/radix" ) func main() { t := radix.New("Named Edge Example") t.Add("foo@bar!@baz", "foobar") err := t.Debug() if err != nil { // ... } _, params := t.GetByRune("foo123!456", '@', '!') fmt.Println(params["bar"]) fmt.Println(params["baz"]) }
Output: 123 456
Index ¶
- type Node
- type SortingTechnique
- type Tree
- func (t *Tree) Add(s string, v interface{})
- func (t *Tree) Debug() error
- func (t *Tree) Del(s string)
- func (t *Tree) Get(s string) *Node
- func (t *Tree) GetByRune(s string, ph, delim rune) (*Node, map[string]string)
- func (t *Tree) Print() error
- func (t *Tree) Size() uint
- func (t *Tree) Sort(st SortingTechnique)
- func (t *Tree) String(debug bool) (string, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Node ¶
type Node struct { // Value is a value of any type held by a node. Value interface{} // contains filtered or unexported fields }
Node is a node of a radix tree.
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 { // Safe tells whether the tree's operations // should be thread-safe. By default, the tree's // not thread-safe. Safe bool // contains filtered or unexported fields }
Tree is a radix tree.
func (*Tree) Del ¶
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) GetByRune ¶
GetByRune dynamically retrieves a node based on a placeholder and a delimiter. It also returns a map of "named parameters".
func (*Tree) Sort ¶ added in v0.2.0
func (t *Tree) Sort(st SortingTechnique)
Sort sorts the tree nodes and its children recursively according to their priority counter.