trie

package module
v0.0.0-...-a8dc277 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2014 License: GPL-3.0 Imports: 2 Imported by: 2 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

quicktrie

A pure go trie library

  • A quickly made string keyed trie implimentation.
  • Which was refined into a black/white radix trie.
  • Which was then refactored to allow both plain BW tries and KV BW tries

Documentation: http://godoc.org/gopkg.in/apokalyptik/quicktrie.v1

Documentation

Overview

Package trie presents a simple, clean, black and white radix trie interface. For more information on what kind of data structure a radix trie is, please see http://en.wikipedia.org/wiki/Radix_tree.

Two types of data structures are available. A black and white only tree (default) which merely records the existence of keys added to the data structure, and key-value black and white trie which stores an arbitrary value in the trie attached to the trie. Because the value is stored seperately from the existence of the key you can store nil values in the trie and be able to tell that apart from a key that does not exist.

The data structures inside this package are NOT synchronized, you'll want to add a sync.Mutex or sync.RWMutex to your code if it needs to be thread safe. At this point in time I do consider the structures to be *mostly* safe to use concurrently without locking as long as you're willing to end up in a part of the trie that existed when you started making your call but not by the time the call has ended. For more read operations this is "OK" (please know the gaurantees required by your application) but it is absolutely not OK for write operations and you should absolutely synchronize those or risk fairly massive corruption.

Example BW Trie usage

trie := trie.NewBWTrie
trie.Add("apple")
trie.Add("apply")
trie.Add("actually")
trie.Add("actively")
trie.Add("Alaska")
trie.Get("actively") // true
trie.Get("ancillary") // false
trie.Log()
	// 2014/06/16 12:06:29   ++
	// 2014/06/16 12:06:29   ↪-a
	// 2014/06/16 12:06:29   | ↪-ppl
	// 2014/06/16 12:06:29 * |  ↪-e
	// 2014/06/16 12:06:29 * |  ↪-y
	// 2014/06/16 12:06:29   | ↪-ct
	// 2014/06/16 12:06:29 * |  ↪-ually
	// 2014/06/16 12:06:29 * |  ↪-ively
	// 2014/06/16 12:06:29 * ↪-Alaska
trie.Drop("ap")
trie.Log()
	// 2014/06/16 12:08:07   ++
	// 2014/06/16 12:08:07   ↪-a
	// 2014/06/16 12:08:07   | ↪-ct
	// 2014/06/16 12:08:07 * |  ↪-ually
	// 2014/06/16 12:08:07 * |  ↪-ively
	// 2014/06/16 12:08:07 * ↪-Alaska

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IterFunc

type IterFunc func([]byte, interface{})

IterFunc describes the function signature that it required for the callback portion of the Iterate function

type Trie

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

Trie is the itnerface to your requested trie. This is the interface you'll use whether you requested a BW trie or a KV trie.

func NewBWTrie

func NewBWTrie() *Trie

NewBWTrie returns a new "black and white" radix trie. A black and white radix trie records the existence of a string, but there is no data associated with it. The existence is the data. This kind of trie is useful for simple "exists" stype lookups.

func NewKVTrie

func NewKVTrie() *Trie

NewKVTrie returns a new Key/Value trie. This is essentially a black and white radix trie where each node has arbitrary data associated with it.

func NewTrie

func NewTrie() *Trie

NewTrie is a convenience function, it merely calls NewBWTrie. Please see the documentation for NewBWTrie

func (*Trie) Add

func (t *Trie) Add(key interface{}, data ...interface{})

Add allows you to add a key to your trie. For BW tries the data argument is ignored and you may ommit it. For KV tries you may pass any value that you wish, and it will be stored along with your key. If you choose to ommit the data value for a KV trie then the stored data will be nil. Only the first data argument is recognized, so if you wish to store an array or slice with your key you should pass that, not depend on the variadic

func (*Trie) Count

func (t *Trie) Count() int

Count returns the number of keys in the trie. Internally it uses the Iterate function to do this

func (*Trie) Del

func (t *Trie) Del(key interface{})

Del allows you to remove a single key from the trie. Del will only delete the exactly matching key, unlike drop, and is therefor considerably safer unless you know why you would want to drop an entire prefix from your trie

func (*Trie) Drop

func (t *Trie) Drop(key interface{})

Drop allows you to cut an enitre branch off of your trie. This means that every existing key of which the passed key is a prefix string (inclusive) will be removed from the trie.

func (*Trie) Exists

func (t *Trie) Exists(key interface{}) bool

Exists allows you to check the existence of a key within the trie. As it only returns asingle boolean value it's convenient to use inside of if statements

func (*Trie) Get

func (t *Trie) Get(key interface{}) (bool, interface{})

Get allows you to fetch a key from the trie. If the trie is a BW trie then get returns true, or false depending on whether the key has been added to the trie and nil for the data. KV tries will return the data passed to add as the second return value

func (*Trie) GetBranch

func (t *Trie) GetBranch(prefix interface{}) [][]byte

GetBranch returns all of the keys which have a prefix of the prefix argument (inclusive.) Under the hood this simply uses IterateFrom

func (*Trie) Iterate

func (t *Trie) Iterate(callback IterFunc)

Iterate allows you to run a function against every key inserted into the trie.

func (*Trie) IterateFrom

func (t *Trie) IterateFrom(prefix interface{}, callback IterFunc)

IterateFrom works the same as Iterate except that it only iterates on keys for which the prefix parameter is a prefix (inclusive.) This function is more efficient than using Iterate when you're looking to act only on a specific subset of keys because it traverses the trie before starting the callback iteration

func (*Trie) Log

func (t *Trie) Log()

Log prints a "pretty" representation of the trie. This is mainly useful for debugging, and it'll print arbitrary binary data if you've used something other than text strings for your keys

func (*Trie) Set

func (t *Trie) Set(key interface{}, data ...interface{})

Set allows you to set a key in your trie. For BW tries the data argument is ignored and you may ommit it. For KV tries you mat pass any value that you wish, and it will be stored along with your Key. If you choose to ommit the data value for a KV trie then the stored data will be nil. Only the first data argument is recognized, so if you wish to store an array or slice with your key then you should pass that, not depend on the variadic. The difference between Set and Add is that Set will overwrite the existing value in the trie. This is not useful for BW tries, but helps reduce boilerplate code when using KV tries

Jump to

Keyboard shortcuts

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