trie

package
v0.0.0-...-bd0c551 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2025 License: Apache-2.0 Imports: 2 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Entry

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

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

func (e Entry[K, V]) AndModify(modifyFn func(*V)) Entry[K, V]

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

func (e Entry[K, V]) Delete() bool

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

func (e Entry[K, V]) Get() (V, bool)

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

func (e Entry[K, V]) Insert(value V) (V, bool)

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

func New[K, V any](comparator func(K, K) int) *TrieMap[K, V]

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

func NewOrdered[K cmp.Ordered, V any]() *TrieMap[K, V]

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

func (m *TrieMap[K, V]) Clone() *TrieMap[K, V]

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

func (m *TrieMap[K, V]) ContainsKey(key []K) bool

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

func (m *TrieMap[K, V]) Entry(key []K) Entry[K, V]

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

func (m *TrieMap[K, V]) Extend(it iter.Seq2[[]K, V])

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

func (m *TrieMap[K, V]) Get(key []K) (V, bool)

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

func (m *TrieMap[K, V]) Insert(key []K, value V) bool

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

func (m *TrieMap[K, V]) IsEmpty() bool

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

func (m *TrieMap[K, V]) Iter() iter.Seq2[[]K, V]

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

func (m *TrieMap[K, V]) IterByPrefix(prefix []K) iter.Seq2[[]K, V]

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

func (m *TrieMap[K, V]) IterMut() iter.Seq2[[]K, *V]

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

func (m *TrieMap[K, V]) IterMutByPrefix(prefix []K) iter.Seq2[[]K, *V]

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

func (m *TrieMap[K, V]) Keys() iter.Seq[[]K]

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

func (m *TrieMap[K, V]) KeysByPrefix(prefix []K) iter.Seq[[]K]

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

func (m *TrieMap[K, V]) Len() int

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

func (m *TrieMap[K, V]) Remove(key []K) (V, bool)

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

func (m *TrieMap[K, V]) Values() iter.Seq[V]

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

func (m *TrieMap[K, V]) ValuesByPrefix(prefix []K) iter.Seq[V]

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

func (m *TrieMap[K, V]) ValuesMut() iter.Seq[*V]

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

func (m *TrieMap[K, V]) ValuesMutByPrefix(prefix []K) iter.Seq[*V]

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]

Jump to

Keyboard shortcuts

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