Documentation
¶
Index ¶
- type KeysValue
- type Node
- type Option
- type Trie
- func (t *Trie[K, V]) Clear()
- func (t *Trie[K, V]) Clone() *Trie[K, V]
- func (t *Trie[K, V]) Delete(key []K) (v V, ok bool)
- func (t *Trie[K, V]) DeletePrefix(prefix []K) int
- func (t *Trie[K, V]) Get(keys []K) (v V, ok bool)
- func (t *Trie[K, V]) IsEmpty() bool
- func (t *Trie[K, V]) Keys() [][]K
- func (t *Trie[K, V]) KeysValues() []KeysValue[K, V]
- func (t *Trie[K, V]) LongestPrefix(keys []K) ([]K, V, bool)
- func (t *Trie[K, V]) MarshalJSON() ([]byte, error)
- func (t *Trie[K, V]) PathAncestors(keys []K) []KeysValue[K, V]
- func (t *Trie[K, V]) PrefixCount(prefix []K) int
- func (t *Trie[K, V]) PrefixKeys(prefix []K) [][]K
- func (t *Trie[K, V]) PrefixKeysValues(prefix []K) []KeysValue[K, V]
- func (t *Trie[K, V]) PrefixValues(prefix []K) []V
- func (t *Trie[K, V]) Put(keys []K, val V) bool
- func (t *Trie[K, V]) Range(fn func([]K, V) bool)
- func (t *Trie[K, V]) Root() *Node[K, V]
- func (t *Trie[K, V]) Size() int
- func (t *Trie[K, V]) String() string
- func (t *Trie[K, V]) UnmarshalJSON(data []byte) error
- func (t *Trie[K, V]) Values() []V
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.
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 ¶
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 ¶
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 (*Trie[K, V]) Clear ¶
func (t *Trie[K, V]) Clear()
Clear removes all keys-value pairs from the trie.
func (*Trie[K, V]) Delete ¶
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 ¶
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 ¶
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]) 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 ¶
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 ¶
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:
- Router matching: finding the most specific route that matches a URL
- IP routing: finding the most specific network prefix for an IP
- Word segmentation: finding the longest dictionary word in a string
func (*Trie[K, V]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (*Trie[K, V]) PathAncestors ¶ added in v0.7.2
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 ¶
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 ¶
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 ¶
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 ¶
Range call "fn" for each keys-value pair in the trie. If "fn" returns false, stops the iteration.
func (*Trie[K, V]) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface.