trie

package
v0.0.0-...-b92a033 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: MIT, MIT Imports: 0 Imported by: 4

README

trie

A minimal implementation of a trie data structure for Go. Differs from most implementations in that it uses string slices ([]string) as keys, rather than just strings.

This makes it suitable for efficiently storing information about hierarchical systems in general, rather than being specifically geared towards string lookup.

See also the Godoc documentation for trie.

Documentation

Overview

Package trie implements a simple trie data structure that maps "paths" (which are slices of strings) to values of some type T.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Trie

type Trie[T interface{}] struct {
	// contains filtered or unexported fields
}

func NewTrie

func NewTrie[T interface{}]() *Trie[T]

NewTrie makes a new, empty Trie.

func (*Trie[T]) Del

func (t *Trie[T]) Del(path []string) bool

Del removes an entry from the Trie, returning true if it deleted an entry.

func (*Trie[T]) Get

func (t *Trie[T]) Get(path []string) (entry T, ok bool)

Get retrieves an entry from the Trie. If there is no fully-matching entry, Get returns `(nil, false)`. `path` can be empty, to denote the root node.

Example:

if res, ok := trie.Get([]string{"foo", "bar"}); ok {
  fmt.Println("Value at /foo/bar was", res)
}

func (*Trie[T]) GetLongestPrefix

func (t *Trie[T]) GetLongestPrefix(path []string) (entry T, ok bool)

GetLongestPrefix retrieves the longest matching entry from the Trie.

GetLongestPrefix returns a full match if there is one, or the entry with the longest matching prefix. If there is no match at all, GetLongestPrefix returns `(nil, false)`. `path` can be empty, to denote the root node.

Example:

if res, ok := trie.GetLongestPrefix([]string{"foo", "bar"}); ok {
  fmt.Println("Value at /foo/bar was", res)
}

func (*Trie[T]) Set

func (t *Trie[T]) Set(path []string, value T)

Set adds an entry to the Trie. `path` can be empty, to denote the root node.

Jump to

Keyboard shortcuts

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