Help us shape the future of Go! Take the 2026 Go Developer Survey

prefixtrie

package
v1.22.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package prefixtrie implements a compressed (radix) trie that finds the keys a string starts with in one O(len(s)) walk, however many keys there are. A trie of reversed keys (see Reverse) finds the keys a string ends with.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Reverse

func Reverse(s string) string

Reverse returns s with its bytes, not runes, reversed: the form of a key in a trie queried with SuffixesOf or HasSuffixOf.

Types

type Entry

type Entry[V any] struct {
	Key   string
	Value *V
}

Entry is a key in the trie and its value.

type Trie

type Trie[V any] struct {
	// contains filtered or unexported fields
}

Trie maps string keys to values of type V. The zero value is an empty trie.

func (*Trie[V]) Compact

func (p *Trie[V]) Compact()

Compact releases the edge slices' spare capacity, often more than half of it. Call it once nothing more will be inserted.

func (*Trie[V]) Entries

func (p *Trie[V]) Entries() []Entry[V]

Entries returns the trie's keys in lexicographic order. It allocates a string per key, so is meant for debugging and tests.

func (*Trie[V]) HasPrefixOf

func (p *Trie[V]) HasPrefixOf(s string) bool

HasPrefixOf reports whether s starts with any key in the trie.

func (*Trie[V]) HasSuffixOf

func (p *Trie[V]) HasSuffixOf(s string) bool

HasSuffixOf reports whether s ends with any key, in a trie of reversed keys.

func (*Trie[V]) Insert

func (p *Trie[V]) Insert(key string) *V

Insert returns the value for key, creating it with new(V) if key is new.

func (*Trie[V]) PrefixesOf

func (p *Trie[V]) PrefixesOf(s string, visit func(*V) error) error

PrefixesOf calls visit with the value of every key s starts with, shortest first, stopping at the first error.

func (*Trie[V]) SuffixesOf

func (p *Trie[V]) SuffixesOf(s string, visit func(*V) error) error

SuffixesOf is PrefixesOf for the end of s, in a trie of reversed keys.

func (*Trie[V]) Values

func (p *Trie[V]) Values(visit func(*V) error) error

Values calls visit with every value in the trie, stopping at the first error.

Jump to

Keyboard shortcuts

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