Documentation
¶
Overview ¶
Package trie implements a generic trie (prefix tree) data structure.
Package trie implements a generic trie (prefix tree) data structure.
Package trie implements a generic trie (prefix tree) data structure.
Index ¶
- type Entry
- func (e Entry[K, V]) AndModify(modifyFn func(*V)) Entry[K, V]
- func (e Entry[K, V]) Delete() bool
- func (e Entry[K, V]) Get() (V, bool)
- func (e Entry[K, V]) Insert(value V) (V, bool)
- func (e Entry[K, V]) OrInsert(value V) *V
- func (e Entry[K, V]) OrInsertWith(f func() V) *V
- func (e Entry[K, V]) OrInsertWithKey(f func([]K) V) *V
- type TrieMap
- func (m *TrieMap[K, V]) Clone() *TrieMap[K, V]
- func (m *TrieMap[K, V]) ContainsKey(key []K) bool
- func (m *TrieMap[K, V]) Entry(key []K) Entry[K, V]
- func (m *TrieMap[K, V]) Extend(it iter.Seq2[[]K, V])
- func (m *TrieMap[K, V]) Get(key []K) (V, bool)
- func (m *TrieMap[K, V]) GetMut(key []K) *V
- func (m *TrieMap[K, V]) Insert(key []K, value V) bool
- func (m *TrieMap[K, V]) IsEmpty() bool
- func (m *TrieMap[K, V]) Iter() iter.Seq2[[]K, V]
- func (m *TrieMap[K, V]) IterByPrefix(prefix []K) iter.Seq2[[]K, V]
- func (m *TrieMap[K, V]) IterMut() iter.Seq2[[]K, *V]
- func (m *TrieMap[K, V]) IterMutByPrefix(prefix []K) iter.Seq2[[]K, *V]
- func (m *TrieMap[K, V]) Keys() iter.Seq[[]K]
- func (m *TrieMap[K, V]) KeysByPrefix(prefix []K) iter.Seq[[]K]
- func (m *TrieMap[K, V]) Len() int
- func (m *TrieMap[K, V]) Remove(key []K) (V, bool)
- func (m *TrieMap[K, V]) Values() iter.Seq[V]
- func (m *TrieMap[K, V]) ValuesByPrefix(prefix []K) iter.Seq[V]
- func (m *TrieMap[K, V]) ValuesMut() iter.Seq[*V]
- func (m *TrieMap[K, V]) ValuesMutByPrefix(prefix []K) iter.Seq[*V]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Entry ¶
Entry represents a potential entry in the TrieMap. It can be used to check if a key exists and to conditionally modify, insert, or delete values.
func (Entry[K, V]) AndModify ¶
AndModify applies the provided function to the value if the key exists, and returns the Entry itself to support chaining.
Parameters:
- modifyFn: A function that takes a pointer to the value and modifies it
Returns:
- The same Entry, allowing for chained calls to other methods
func (Entry[K, V]) Delete ¶
Delete removes the key from the map and returns whether the key was present.
Returns:
- A boolean indicating whether the key was present and removed
func (Entry[K, V]) Get ¶
Get returns the current value for the key and a boolean indicating whether it exists.
Returns:
- value: The value associated with the key (undefined if not found)
- found: A boolean indicating whether the key exists
func (Entry[K, V]) Insert ¶
Insert unconditionally inserts or updates the key-value pair and returns the previous value (if any) and a boolean indicating if the key existed.
Parameters:
- value: The value to insert or update
Returns:
- If the key already existed, returns the old value and true
- If the key did not exist, returns the zero value and false
func (Entry[K, V]) OrInsert ¶
func (e Entry[K, V]) OrInsert(value V) *V
OrInsert inserts the value into the map if the key is not already present, and returns a reference to the existing or inserted value.
Parameters:
- value: The value to insert if the key is not present
Returns:
- A pointer to the existing value if the key was present, or the new value if it was inserted
func (Entry[K, V]) OrInsertWith ¶
func (e Entry[K, V]) OrInsertWith(f func() V) *V
OrInsertWith inserts a value using the provided function if the key is not already present, and returns a reference to the existing or inserted value.
Parameters:
- f: A function that returns the value to insert
Returns:
- A pointer to the existing value if the key was present, or the new value if it was inserted
func (Entry[K, V]) OrInsertWithKey ¶
func (e Entry[K, V]) OrInsertWithKey(f func([]K) V) *V
OrInsertWithKey inserts a value using the provided function with the key as an argument if the key is not already present, and returns a reference to the existing or inserted value.
Parameters:
- f: A function that takes the key and returns the value to insert
Returns:
- A pointer to the existing value if the key was present, or the new value if it was inserted
type TrieMap ¶
type TrieMap[K, V any] struct { // contains filtered or unexported fields }
TrieMap represents a trie (prefix tree) data structure that maps sequences of keys to values. It supports efficient prefix-based operations.
- root: The root node of the trie
- size: The number of key-value pairs in the trie
- comparator: Function used to compare keys
Type parameters:
- K: The type of individual key elements
- V: The type of values stored in the trie
func New ¶
New creates a new TrieMap with the specified key comparator. Parameters:
- comparator: Function used to compare individual key elements
Return value:
- A new empty TrieMap instance
Time complexity: O(1)
func NewOrdered ¶
NewOrdered creates a new TrieMap with a default comparator for ordered types. Return value:
- A new empty TrieMap instance with a default comparator
func (*TrieMap[K, V]) Clone ¶
Clone creates a deep copy of the trie. Return value:
- A new TrieMap that is a deep copy of the original
Time complexity: O(N), where N is the number of nodes in the trie
func (*TrieMap[K, V]) ContainsKey ¶
ContainsKey checks if the trie contains the given key. Parameters:
- key: The key to check for
Return value:
- A boolean indicating whether the key exists in the trie
Time complexity: O(L), where L is the length of the key
func (*TrieMap[K, V]) Entry ¶
Entry creates a new Entry for the given key. This allows for convenient operations on a specific key.
Parameters:
- key: The key to create an Entry for
Returns:
- A new Entry instance for the given key
func (*TrieMap[K, V]) Extend ¶
Extend adds all key-value pairs from the provided iterator to the trie. For each key-value pair in the iterator:
- If the key already exists, its value is updated
- Otherwise, a new key-value pair is added
Parameters:
- it: An iterator over key-value pairs to add to the trie
Time complexity: O(N*L), where N is the number of key-value pairs in the iterator and L is the average length of the keys
func (*TrieMap[K, V]) Get ¶
Get retrieves the value associated with the given key. Parameters:
- key: The key to search for
Return values:
- The value associated with the key, or the zero value of V if not found
- A boolean indicating whether the key was found
Time complexity: O(L), where L is the length of the key
func (*TrieMap[K, V]) GetMut ¶
func (m *TrieMap[K, V]) GetMut(key []K) *V
GetMut retrieves a pointer to the value associated with the given key. Parameters:
- key: The key to search for
Return values:
- A pointer to the value associated with the key, or nil if not found
Time complexity: O(L), where L is the length of the key
func (*TrieMap[K, V]) Insert ¶
Insert adds a key-value pair to the trie. If the key already exists, its value is updated. Parameters:
- key: The key to insert
- value: The value to associate with the key
Return value:
- A boolean indicating whether the key was newly added (true) or updated (false)
Time complexity: O(L), where L is the length of the key
func (*TrieMap[K, V]) IsEmpty ¶
IsEmpty checks if the trie is empty. Return value:
- A boolean indicating whether the trie is empty
Time complexity: O(1)
func (*TrieMap[K, V]) Iter ¶
Iter returns an iterator over all key-value pairs in the trie. Return value:
- Iterator over key-value pairs, of type iter.Seq2[[]K, V]
func (*TrieMap[K, V]) IterByPrefix ¶
IterByPrefix returns an iterator over all key-value pairs in the trie with the given prefix. Parameters:
- prefix: The prefix to filter key-value pairs by
Return value:
- Iterator over key-value pairs with the given prefix, of type iter.Seq2[[]K, V]
func (*TrieMap[K, V]) IterMut ¶
IterMut returns a mutable iterator over all key-value pairs in the trie. Return value:
- Mutable iterator over key-value pairs, of type iter.Seq2[[]K, *V]
func (*TrieMap[K, V]) IterMutByPrefix ¶
IterMutByPrefix returns a mutable iterator over all key-value pairs in the trie with the given prefix. Parameters:
- prefix: The prefix to filter key-value pairs by
Return value:
- Mutable iterator over key-value pairs with the given prefix, of type iter.Seq2[[]K, *V]
func (*TrieMap[K, V]) Keys ¶
Keys returns an iterator over all keys in the trie. Return value:
- Iterator over keys, of type iter.Seq[[]K]
func (*TrieMap[K, V]) KeysByPrefix ¶
KeysByPrefix returns all keys in the trie with the given prefix as an iterator. Parameters:
- prefix: The prefix to filter keys by
Return value:
- Iterator over keys with the given prefix, of type iter.Seq[[]K]
func (*TrieMap[K, V]) Len ¶
Len returns the number of key-value pairs in the trie. Return value:
- The number of key-value pairs
Time complexity: O(1)
func (*TrieMap[K, V]) Remove ¶
Remove removes a key-value pair from the trie. Parameters:
- key: The key to remove
Return values:
- If the key exists, returns the deleted value and true
- If the key does not exist, returns the zero value and false
Time complexity: O(L), where L is the length of the key
func (*TrieMap[K, V]) Values ¶
Values returns an iterator over all values in the trie. Return value:
- Iterator over values, of type iter.Seq[V]
func (*TrieMap[K, V]) ValuesByPrefix ¶
ValuesByPrefix returns all values in the trie with the given prefix as an iterator. Parameters:
- prefix: The prefix to filter values by
Return value:
- Iterator over values with the given prefix, of type iter.Seq[V]
func (*TrieMap[K, V]) ValuesMut ¶
ValuesMut returns a mutable iterator over all values in the trie. Return value:
- Mutable iterator over values, of type iter.Seq[*V]
ValuesMut returns an iterator over all values in the trie, allowing mutation. Return value:
- Iterator over value pointers, of type iter.Seq[*V]
func (*TrieMap[K, V]) ValuesMutByPrefix ¶
ValuesMutByPrefix returns a mutable iterator over all values in the trie with the given prefix. Parameters:
- prefix: The prefix to filter values by
Return value:
- Mutable iterator over values with the given prefix, of type iter.Seq[*V]