trie: github.com/dghubble/trie Index | Files

package trie

import "github.com/dghubble/trie"

Package trie implements several types of performant Tries (e.g. rune-wise, path-wise).

The implementations are optimized for Get performance and to allocate 0 bytes of heap memory (i.e. garbage) per Get.

The Tries do not synchronize access (not thread-safe). A typical use case is to perform Puts and Deletes upfront to populate the Trie, then perform Gets very quickly.

Index

Package Files

common.go doc.go path_trie.go rune_trie.go trie.go

func PathSegmenter Uses

func PathSegmenter(path string, start int) (segment string, next int)

PathSegmenter segments string key paths by slash separators. For example, "/a/b/c" -> ("/a", 2), ("/b", 4), ("/c", -1) in successive calls. It does not allocate any heap memory.

type PathTrie Uses

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

PathTrie is a trie of string keys and interface{} values. Internal nodes have nil values so stored nil values cannot be distinguished and are excluded from walks. By default, PathTrie will segment keys by forward slashes with PathSegmenter (e.g. "/a/b/c" -> "/a", "/b", "/c"). A custom StringSegmenter may be used to customize how strings are segmented into nodes. A classic trie might segment keys by rune (i.e. unicode points).

func NewPathTrie Uses

func NewPathTrie() *PathTrie

NewPathTrie allocates and returns a new *PathTrie.

func (*PathTrie) Delete Uses

func (trie *PathTrie) Delete(key string) bool

Delete removes the value associated with the given key. Returns true if a node was found for the given key. If the node or any of its ancestors becomes childless as a result, it is removed from the trie.

func (*PathTrie) Get Uses

func (trie *PathTrie) Get(key string) interface{}

Get returns the value stored at the given key. Returns nil for internal nodes or for nodes with a value of nil.

func (*PathTrie) Put Uses

func (trie *PathTrie) Put(key string, value interface{}) bool

Put inserts the value into the trie at the given key, replacing any existing items. It returns true if the put adds a new value, false if it replaces an existing value. Note that internal nodes have nil values so a stored nil value will not be distinguishable and will not be included in Walks.

func (*PathTrie) Walk Uses

func (trie *PathTrie) Walk(walker WalkFunc) error

Walk iterates over each key/value stored in the trie and calls the given walker function with the key and value. If the walker function returns an error, the walk is aborted. The traversal is depth first with no guaranteed order.

type RuneTrie Uses

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

RuneTrie is a trie of runes with string keys and interface{} values. Note that internal nodes have nil values so a stored nil value will not be distinguishable and will not be included in Walks.

func NewRuneTrie Uses

func NewRuneTrie() *RuneTrie

NewRuneTrie allocates and returns a new *RuneTrie.

func (*RuneTrie) Delete Uses

func (trie *RuneTrie) Delete(key string) bool

Delete removes the value associated with the given key. Returns true if a node was found for the given key. If the node or any of its ancestors becomes childless as a result, it is removed from the trie.

func (*RuneTrie) Get Uses

func (trie *RuneTrie) Get(key string) interface{}

Get returns the value stored at the given key. Returns nil for internal nodes or for nodes with a value of nil.

func (*RuneTrie) Put Uses

func (trie *RuneTrie) Put(key string, value interface{}) bool

Put inserts the value into the trie at the given key, replacing any existing items. It returns true if the put adds a new value, false if it replaces an existing value. Note that internal nodes have nil values so a stored nil value will not be distinguishable and will not be included in Walks.

func (*RuneTrie) Walk Uses

func (trie *RuneTrie) Walk(walker WalkFunc) error

Walk iterates over each key/value stored in the trie and calls the given walker function with the key and value. If the walker function returns an error, the walk is aborted. The traversal is depth first with no guaranteed order.

type StringSegmenter Uses

type StringSegmenter func(key string, start int) (segment string, nextIndex int)

StringSegmenter takes a string key with a starting index and returns the first segment after the start and the ending index. When the end is reached, the returned nextIndex should be -1. Implementations should NOT allocate heap memory as Trie Segmenters are called upon Gets. See PathSegmenter.

type Trier Uses

type Trier interface {
    Get(key string) interface{}
    Put(key string, value interface{}) bool
    Delete(key string) bool
    Walk(walker WalkFunc) error
}

Trier exposes the Trie structure capabilities.

type WalkFunc Uses

type WalkFunc func(key string, value interface{}) error

WalkFunc defines some action to take on the given key and value during a Trie Walk. Returning a non-nil error will terminate the Walk.

Package trie imports 1 packages (graph) and is imported by 1 packages. Updated 2019-07-09. Refresh now. Tools for package owners.