TrieHard (TrieHard v0.2.7)
View SourceA blazing fast, memory-efficient Trie (prefix tree) implementation for Elixir with autocomplete support, powered by the trie_hard_rs Rust library.
Features
- Fast operations: Sub-microsecond autocomplete performance
- Memory efficient: Shared prefix storage
- Unicode support: Full UTF-8 character support
- Generic values: Store any Erlang term as values
- Batch operations: Efficient bulk insertions
- Thread-safe: Concurrent access support
Example
iex> trie = TrieHard.new()
iex> TrieHard.insert(trie, "cat", :feline)
:ok
iex> TrieHard.insert(trie, "car", %{type: "vehicle", wheels: 4})
:ok
iex> TrieHard.insert(trie, "card", {"payment", "plastic"})
:ok
iex> TrieHard.get(trie, "cat")
{:ok, :feline}
iex> {:ok, results} = TrieHard.auto_complete(trie, "ca", 10)
iex> Enum.sort(results)
["car", "card", "cat"]
Summary
Functions
Efficiently inserts multiple words at once.
Returns words that start with the given prefix, up to max_results.
Returns the number of words with a given prefix.
Removes a key and its associated value from the trie.
Retrieves a value by exact key match.
Inserts a key-value pair into the trie.
Creates a new empty Trie.
Checks if any words in the trie start with the given prefix.
Types
Functions
Efficiently inserts multiple words at once.
Each word will be inserted with itself as the value.
Parameters
trie- The trie referencewords- List of words to insert
Examples
iex> trie = TrieHard.new()
iex> TrieHard.add_word_list(trie, ["cat", "car", "card"])
:ok
iex> TrieHard.get(trie, "cat")
{:ok, "cat"}
@spec auto_complete(trie(), String.t(), non_neg_integer()) :: {:ok, [String.t()]} | {:error, []}
Returns words that start with the given prefix, up to max_results.
Parameters
trie- The trie referenceprefix- The prefix to search formax_results- Maximum number of results to return (default: 10)
Returns
{:ok, [String.t()]}- List of matching words{:error, []}on internal error
Examples
iex> trie = TrieHard.new()
iex> TrieHard.insert(trie, "cat", "1")
:ok
iex> TrieHard.insert(trie, "car", "2")
:ok
iex> TrieHard.insert(trie, "card", "3")
:ok
iex> {:ok, results} = TrieHard.auto_complete(trie, "ca", 2)
iex> length(results) <= 2
true
@spec count_prefix(trie(), String.t()) :: {:ok, non_neg_integer()} | {:error, 0}
Returns the number of words with a given prefix.
This is a convenience function that uses auto_complete with unlimited results.
Examples
iex> trie = TrieHard.new()
iex> TrieHard.add_word_list(trie, ["cat", "car", "card", "dog"])
:ok
iex> TrieHard.count_prefix(trie, "ca")
{:ok, 3}
iex> TrieHard.count_prefix(trie, "do")
{:ok, 1}
Removes a key and its associated value from the trie.
Parameters
trie- The trie referencekey- The key to remove
Examples
iex> trie = TrieHard.new()
iex> TrieHard.insert(trie, "hello", "world")
:ok
iex> TrieHard.delete(trie, "hello")
:ok
iex> TrieHard.get(trie, "hello")
{:not_found, nil}
Retrieves a value by exact key match.
Parameters
trie- The trie referencekey- The key to look up
Returns
{:ok, value}if the key exists{:not_found, nil}if the key doesn't exist{:error, nil}on internal error
Examples
iex> trie = TrieHard.new()
iex> TrieHard.insert(trie, "hello", "world")
:ok
iex> TrieHard.get(trie, "hello")
{:ok, "world"}
iex> TrieHard.insert(trie, "data", {:ok, %{id: 123}})
:ok
iex> TrieHard.get(trie, "data")
{:ok, {:ok, %{id: 123}}}
iex> TrieHard.get(trie, "missing")
{:not_found, nil}
Inserts a key-value pair into the trie.
Parameters
trie- The trie referencekey- The key to insert (string)value- The value to associate with the key (any Erlang term)
Examples
iex> trie = TrieHard.new()
iex> TrieHard.insert(trie, "hello", "world")
:ok
iex> TrieHard.insert(trie, "config", %{timeout: 5000, retry: true})
:ok
iex> TrieHard.insert(trie, "data", {:ok, [1, 2, 3]})
:ok
@spec new() :: trie()
Creates a new empty Trie.
Examples
iex> trie = TrieHard.new()
iex> is_reference(trie)
true
Checks if any words in the trie start with the given prefix.
Parameters
trie- The trie referenceprefix- The prefix to search for
Returns
{:ok, true}if words with the prefix exist{:ok, false}if no words with the prefix exist{:error, false}on internal error
Examples
iex> trie = TrieHard.new()
iex> TrieHard.insert(trie, "cat", "feline")
:ok
iex> TrieHard.prefix_search(trie, "ca")
{:ok, true}
iex> TrieHard.prefix_search(trie, "dog")
{:ok, false}