Documentation ¶
Overview ¶
Package trie provides generic Trie implementations
Index ¶
- Constants
- func ForEachKey[P, K any, KY KeyIter[P, K]](keyer KY, searchPath P, visit func(k K) bool) error
- type DeletionMode
- type ErrBadSearchPath
- type KeyIter
- type Keyer
- type MapNode
- func (n *MapNode[P, K, V, KY]) Children(keyer KY) (ret []*MapNode[P, K, V, KY])
- func (n *MapNode[P, K, V, KY]) GetChild(keyer KY, key K) (child *MapNode[P, K, V, KY])
- func (n *MapNode[P, K, V, KY]) Key() K
- func (n *MapNode[P, K, V, KY]) New(parent *MapNode[P, K, V, KY], key K) *MapNode[P, K, V, KY]
- func (n *MapNode[P, K, V, KY]) SearchPath(gen KY) P
- type MapTrie
- func (t *MapTrie[P, K, V, KY]) Delete(searchPath P, mode DeletionMode) (nodeDeleted *MapNode[P, K, V, KY], err error)
- func (t *MapTrie[P, K, V, KY]) Get(searchPath P) (_ *MapNode[P, K, V, KY], exact bool, err error)
- func (t *MapTrie[P, K, V, KY]) InOrder(visit func(*MapNode[P, K, V, KY]) bool)
- func (t *MapTrie[P, K, V, KY]) Init()
- func (t *MapTrie[P, K, V, KY]) LevelOrder(visit func(*MapNode[P, K, V, KY]) bool)
- func (t *MapTrie[P, K, V, KY]) PostOrder(visit func(*MapNode[P, K, V, KY]) bool)
- func (t *MapTrie[P, K, V, KY]) PreOrder(visit func(*MapNode[P, K, V, KY]) bool)
- func (t *MapTrie[P, K, V, KY]) Root() *MapNode[P, K, V, KY]
- func (t *MapTrie[P, K, V, KY]) Set(searchPath P, value V) (*MapNode[P, K, V, KY], error)
- func (t *MapTrie[P, K, V, KY]) Walk(searchPath P, fn MapTrieWalkFunc[P, K, V, KY]) error
- type MapTrieWalkFunc
- type Node
- type PathKeyer
- type PathTrieMapStore
- type PathTrieSliceStore
- type RuneKeyer
- type RuneTrieMapStore
- type RuneTrieSliceStore
- type SectionKeyer
- type SliceNode
- func (n *SliceNode[P, K, V, KY]) Children(KY) []*SliceNode[P, K, V, KY]
- func (n *SliceNode[P, K, V, KY]) GetChild(keyer KY, key K) (child *SliceNode[P, K, V, KY])
- func (n *SliceNode[P, K, V, KY]) Key() K
- func (n *SliceNode[P, K, V, KY]) New(parent *SliceNode[P, K, V, KY], key K) *SliceNode[P, K, V, KY]
- func (n *SliceNode[P, K, V, KY]) SearchPath(gen KY) P
- type SliceTrie
- func (t *SliceTrie[P, K, V, KY]) Delete(searchPath P, mode DeletionMode) (nodeDeleted *SliceNode[P, K, V, KY], err error)
- func (t *SliceTrie[P, K, V, KY]) Get(searchPath P) (_ *SliceNode[P, K, V, KY], exact bool, err error)
- func (t *SliceTrie[P, K, V, KY]) InOrder(visit func(*SliceNode[P, K, V, KY]) bool)
- func (t *SliceTrie[P, K, V, KY]) Init()
- func (t *SliceTrie[P, K, V, KY]) LevelOrder(visit func(*SliceNode[P, K, V, KY]) bool)
- func (t *SliceTrie[P, K, V, KY]) PostOrder(visit func(*SliceNode[P, K, V, KY]) bool)
- func (t *SliceTrie[P, K, V, KY]) PreOrder(visit func(*SliceNode[P, K, V, KY]) bool)
- func (t *SliceTrie[P, K, V, KY]) Root() *SliceNode[P, K, V, KY]
- func (t *SliceTrie[P, K, V, KY]) Set(searchPath P, value V) (*SliceNode[P, K, V, KY], error)
- func (t *SliceTrie[P, K, V, KY]) Walk(searchPath P, fn SliceTrieWalkFunc[P, K, V, KY]) error
- type SliceTrieWalkFunc
Constants ¶
const ( NextPosTerminate = -1 NextPosErrBadKey = -2 )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type DeletionMode ¶
type DeletionMode uint8
const ( // DeletionMode_TargetNode deletes the target node entirely, and all its children will be gone DeletionMode_TargetNode DeletionMode = iota // DeletionMode_TargetIfLeafNode deletes the target node only when it's a leaf node (no child). // // for non-leaf nodes, return that node and do nothing DeletionMode_TargetIfLeafNode )
type ErrBadSearchPath ¶
func (*ErrBadSearchPath) Error ¶
func (e *ErrBadSearchPath) Error() string
type KeyIter ¶
type KeyIter[SearchType, KeyType any] interface { // FormatSearchPath formats a search path to string FormatSearchPath(s SearchType) string // Next generates next key in a trie from the full search path // // return value of nextPos has special meanings when it's less than zero: // // -1: end of search path, no key will be generated // -2: bad search path at pos Next(s SearchType, pos int) (k KeyType, nextPos int) }
type Keyer ¶
type Keyer[SearchType, KeyType any] interface { // ZeroSearchPath returns the zero value of the search path for the key generator ZeroSearchPath() SearchType // JoinSearchPath appends k to s JoinSearchPath(s SearchType, k KeyType) SearchType // CompareKey returns true if k1 < k2 CompareKey(k1, k2 KeyType) int KeyIter[SearchType, KeyType] }
type MapNode ¶
type MapNode[P any, K comparable, V any, KY Keyer[P, K]] struct { Value V // contains filtered or unexported fields }
func (*MapNode[P, K, V, KY]) Key ¶
func (n *MapNode[P, K, V, KY]) Key() K
Key implements Node[*MapNode, P, K, V, KY]
func (*MapNode[P, K, V, KY]) SearchPath ¶
func (n *MapNode[P, K, V, KY]) SearchPath(gen KY) P
SearchPath rimplements Node[*MapNode, P, K, V, KY]
type MapTrie ¶
type MapTrie[P any, K comparable, V any, KY Keyer[P, K]] struct { Keyer KY // contains filtered or unexported fields }
MapTrie is a trie with MapNode
type param P for searchPath, K for node key, V for node value, KY for keyer
func NewMapTrie ¶
func NewMapTrie[P any, K comparable, V any, KY Keyer[P, K]](keyer KY) *MapTrie[P, K, V, KY]
func (*MapTrie[P, K, V, KY]) Delete ¶
func (t *MapTrie[P, K, V, KY]) Delete( searchPath P, mode DeletionMode, ) (nodeDeleted *MapNode[P, K, V, KY], err error)
Delete the exact matching Node of the searchPath
nodeDeleted is nil if there is no exact match
func (*MapTrie[P, K, V, KY]) Get ¶
Get returns the exact or the nearest matching Node for searchPath
exact is set to ture when the returned Node is a exact match
func (*MapTrie[P, K, V, KY]) LevelOrder ¶
LevelOrder calls visit on every nodes of the trie in level order
func (*MapTrie[P, K, V, KY]) Root ¶
Root returns the root node of the trie
MUST NOT assign custom value to the returned node
func (*MapTrie[P, K, V, KY]) Walk ¶
func (t *MapTrie[P, K, V, KY]) Walk(searchPath P, fn MapTrieWalkFunc[P, K, V, KY]) error
Walk visits all matched trie nodes in searchPath
type MapTrieWalkFunc ¶
type MapTrieWalkFunc[P any, K comparable, V any, KY Keyer[P, K]] func(key K, n *MapNode[P, K, V, KY]) bool
MapTrieWalkFunc is the function type called in SliceTrie.Walk
type Node ¶
type Node[Self, P, K, V any, KY Keyer[P, K]] interface { // New creates a new node of the same type New(parent Self, key K) Self // Key returns the key stored in the current node Key() K // SearchPath returns the search path leading to this node from the root node SearchPath(KY) P // Children returns all direct children of this node in ascending order Children(KY) []Self // GetChild returns the child Node with the expected key of n, when not found, return (nil, false) GetChild(keyer KY, key K) Self }
type PathKeyer ¶
type PathKeyer struct{}
PathKeyer generates keys from a slash separated path
for absolute paths (paths prefixed with a slash): the first key is always the root path (`/`), otherwise the first path element in the path becomes the first key.
func (PathKeyer) CompareKey ¶
CompareKey implements Keyer[string, string]
func (PathKeyer) FormatSearchPath ¶
FormatSearchPath implements Keyer[string, string]
func (PathKeyer) JoinSearchPath ¶
JoinSearchPath implements Keyer[string, string]
func (PathKeyer) ZeroSearchPath ¶
ZeroSearchPath implements Keyer[string, string]
type PathTrieMapStore ¶
PathTrieMapStore
type PathTrieSliceStore ¶
PathTrieSliceStore is a trie for slash (`/`) paths using sorted slice to store node childrens
zero value of PathTrieSliceStore is valid
NOTE: posix path elements `..` and `.` are treated as normal path elements
type RuneKeyer ¶
type RuneKeyer struct{}
func (RuneKeyer) CompareKey ¶
CompareKey implements Keyer[string, rune]
func (RuneKeyer) FormatSearchPath ¶
FormatSearchPath implements Keyer[string, rune]
func (RuneKeyer) JoinSearchPath ¶
JoinSearchPath implements Keyer[string, rune]
func (RuneKeyer) ZeroSearchPath ¶
ZeroSearchPath implements Keyer[string, rune]
type RuneTrieMapStore ¶
RuneTrieMapStore
type RuneTrieSliceStore ¶
RuneTrieSliceStore is a trie for utf-8 runes, each rune in string becomes a node key
zero value of RuneTrieSliceStore is valid
type SectionKeyer ¶
type SectionKeyer string
SectionKeyer generates keys from a multi-sectioned string
sections are defined using common separator, leading sep does produce a empty section, but tail sep doesn't. for example:
let sep = "#", s = "#foo#bar#" will produce a sequence of key: ["", "foo", "bar"]
func (SectionKeyer) CompareKey ¶
func (SectionKeyer) CompareKey(k1, k2 string) int
CompareKey implements Keyer[string, string]
func (SectionKeyer) FormatSearchPath ¶
func (SectionKeyer) FormatSearchPath(s string) string
FormatSearchPath implements Keyer[string, rune]
func (SectionKeyer) JoinSearchPath ¶
func (SectionKeyer) JoinSearchPath(s, k string) string
JoinSearchPath implements Keyer[string, string]
func (SectionKeyer) Next ¶
func (sep SectionKeyer) Next(s string, pos int) (k string, nextPos int)
Next implements Keyer[string, string]
pos is the byte index into key
func (SectionKeyer) ZeroSearchPath ¶
func (SectionKeyer) ZeroSearchPath() (zero string)
ZeroSearchPath implements Keyer[string, string]
type SliceNode ¶
type SliceNode[P, K, V any, KY Keyer[P, K]] struct { Value V // contains filtered or unexported fields }
func (*SliceNode[P, K, V, KY]) Key ¶
func (n *SliceNode[P, K, V, KY]) Key() K
Key implements Node[*SliceNode, P, K, V, KY]
func (*SliceNode[P, K, V, KY]) SearchPath ¶
func (n *SliceNode[P, K, V, KY]) SearchPath(gen KY) P
SearchPath rimplements Node[*SliceNode, P, K, V, KY]
type SliceTrie ¶
type SliceTrie[P, K, V any, KY Keyer[P, K]] struct { Keyer KY // contains filtered or unexported fields }
SliceTrie is a trie with SliceNode
type param P for searchPath, K for node key, V for node value, KY for keyer
func NewSliceTrie ¶
NewSliceTrie creates a new trie tree with custom keyer
func (*SliceTrie[P, K, V, KY]) Delete ¶
func (t *SliceTrie[P, K, V, KY]) Delete( searchPath P, mode DeletionMode, ) (nodeDeleted *SliceNode[P, K, V, KY], err error)
Delete the exact matching Node of the searchPath
deleted will be false if there is no exact match, in that case, nodeDeleted is nil
func (*SliceTrie[P, K, V, KY]) Get ¶
func (t *SliceTrie[P, K, V, KY]) Get(searchPath P) (_ *SliceNode[P, K, V, KY], exact bool, err error)
Get returns the exact or the nearest matching Node for searchPath
exact is set to ture when the returned Node is a exact match
func (*SliceTrie[P, K, V, KY]) LevelOrder ¶
LevelOrder calls visit on every nodes of the trie in level order
func (*SliceTrie[P, K, V, KY]) Root ¶
Root returns the root node of the trie
MUST NOT assign custom value to the returned node
func (*SliceTrie[P, K, V, KY]) Walk ¶
func (t *SliceTrie[P, K, V, KY]) Walk(searchPath P, fn SliceTrieWalkFunc[P, K, V, KY]) error
Walk visits all matched trie nodes in searchPath