trie

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type KeysValue

type KeysValue[K comparable, V any] struct {
	Keys  []K
	Value V
}

type Node

type Node[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Node represents a node in the trie. K is the key type(typically string or rune). V is the value type associated with the key.

func (*Node[K, V]) Children added in v0.7.2

func (n *Node[K, V]) Children() map[K]*Node[K, V]

func (*Node[K, V]) Value added in v0.7.2

func (n *Node[K, V]) Value() V

type Option

type Option[K comparable, V any] func(*Trie[K, V]) error

Option is a function option type for configuring a trie.

func WithKeyFormatter

func WithKeyFormatter[K comparable, V any](fn func(K, V, int, bool) string) Option[K, V]

WithKeyFormatter creates a option that sets the key formatter when call trie.String(). Example usage:

trie.WithKeyFormatter(func(k string, n *Node[string, int]) string {
    return fmt.Sprintf("[%s]", k)
})

func WithNodeFormatter

func WithNodeFormatter[K comparable, V any](fn func(V, int, bool) string) Option[K, V]

WithNodeFormatter creates a option that sets the node formatter when call trie.String(). Example usage:

trie.WithNodeFormatter(func(v int, c count) string {
    return fmt.Sprintf("value: %v, count: %d", v, c)
})

func WithSafe

func WithSafe[K comparable, V any]() Option[K, V]

WithSafe returns a option that makes the trie safe for concurrent use.

type Trie

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

func New

func New[K comparable, V any](ops ...Option[K, V]) (*Trie[K, V], error)

func (*Trie[K, V]) Clear

func (t *Trie[K, V]) Clear()

Clear removes all keys-value pairs from the trie.

func (*Trie[K, V]) Clone

func (t *Trie[K, V]) Clone() *Trie[K, V]

Clone returns a deep copy of the trie.

func (*Trie[K, V]) Delete

func (t *Trie[K, V]) Delete(key []K) (v V, ok bool)

Delete removes the key and its associated value from the trie. Returns the deleted value and true if it exists, zero value and false otherwise.

func (*Trie[K, V]) DeletePrefix

func (t *Trie[K, V]) DeletePrefix(prefix []K) int

DeletePrefix removes all keys-value pairs that start with the given prefix. Returns the number of keys-value pairs removed.

func (*Trie[K, V]) Get

func (t *Trie[K, V]) Get(keys []K) (v V, ok bool)

Get retrives the value associated with the given keys. Returns the value and true if found, zero value and false if not foud.

func (*Trie[K, V]) IsEmpty

func (t *Trie[K, V]) IsEmpty() bool

IsEmpty reports whether the trie is empty.

func (*Trie[K, V]) Keys

func (t *Trie[K, V]) Keys() [][]K

Keys returns all keys in the trie. Returns empty slice(not nil) if the trie is empty. The order of keys is not guaranteed.

func (*Trie[K, V]) KeysValues

func (t *Trie[K, V]) KeysValues() []KeysValue[K, V]

KeysValues returns all keys-value pairs in the trie. Returns empty slice(not nil) if the trie is empty The order of key-value pairs is not guaranteed.

func (*Trie[K, V]) LongestPrefix

func (t *Trie[K, V]) LongestPrefix(keys []K) ([]K, V, bool)

LongestPrefix returns the longest prefix of the given keys that exists in the trie, along with its associated value and a boolean indicating if such a prefix was found.

For example, if the trie contains the following key-value pairs:

  • "internal" -> "A"
  • "inter" -> "B"
  • "in" -> "C"

LongestPrefix("internally") would return ("internal", "A", true) LongestPrefix("inter") would return ("inter", "B", true) LongestPrefix("int") would return ("in", "C", true) LongestPrefix("foo") would return (nil, zero_value, false)

This is particularly useful in scenarios like:

  1. Router matching: finding the most specific route that matches a URL
  2. IP routing: finding the most specific network prefix for an IP
  3. Word segmentation: finding the longest dictionary word in a string

func (*Trie[K, V]) MarshalJSON

func (t *Trie[K, V]) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*Trie[K, V]) PathAncestors added in v0.7.2

func (t *Trie[K, V]) PathAncestors(keys []K) []KeysValue[K, V]

PathAncestors returns all ancestor nodes (including the target node) from root to the given path. This is useful for collecting parameters from all levels in a hierarchical structure. Returns a slice of KeysValue where each entry represents an ancestor node with its path and value.

func (*Trie[K, V]) PrefixCount

func (t *Trie[K, V]) PrefixCount(prefix []K) int

PrefixCount returns the number of keys that start with the given prefix.

func (*Trie[K, V]) PrefixKeys

func (t *Trie[K, V]) PrefixKeys(prefix []K) [][]K

PrefixKeys returns all keys that start with the given prefix. It will returns empty slice(not nil) if the trie is empty or no keys start with the given prefix.

func (*Trie[K, V]) PrefixKeysValues

func (t *Trie[K, V]) PrefixKeysValues(prefix []K) []KeysValue[K, V]

PrefixKeysValues returns the keys and values that start with the given prefix. It will returns empty slice(not nil) if the trie is empty or no keys-value pairs start with the given prefix.

func (*Trie[K, V]) PrefixValues

func (t *Trie[K, V]) PrefixValues(prefix []K) []V

PrefixValues returns all values that start with the given prefix. It will returns empty slice(not nil) if the trie is empty or no values start with the given prefix.

func (*Trie[K, V]) Put

func (t *Trie[K, V]) Put(keys []K, val V) bool

Put inserts or updates a keys-value pair into the trie. Returns true if the key was inserted, false if the key already exists and value wa updated.

func (*Trie[K, V]) Range

func (t *Trie[K, V]) Range(fn func([]K, V) bool)

Range call "fn" for each keys-value pair in the trie. If "fn" returns false, stops the iteration.

func (*Trie[K, V]) Root added in v0.7.2

func (t *Trie[K, V]) Root() *Node[K, V]

func (*Trie[K, V]) Size

func (t *Trie[K, V]) Size() int

Size returns the number of keys-value pairs in the trie.

func (*Trie[K, V]) String

func (t *Trie[K, V]) String() string

String returns a string representation of the trie.

func (*Trie[K, V]) UnmarshalJSON

func (t *Trie[K, V]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (*Trie[K, V]) Values

func (t *Trie[K, V]) Values() []V

Values returns all values in the trie. Returns empty slice(not nil) if the trie is empty. The order of values is not guaranteed.

Jump to

Keyboard shortcuts

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