Trie
in package
Implements a trie data structure which can be used to store terms read from a dictionary in a succinct way
Tags
Table of Contents
- $end_marker : string
- The marker used to represent the end of an entry in a trie
- $trie_array : array<string|int, mixed>
- A nested array used to represent the trie
- __construct() : mixed
- Creates and returns an empty trie. Sets the end of term character
- add() : array<string|int, mixed>
- Adds a term to the Trie
- exists() : array<string|int, mixed>
- Returns the sub trie_array under $term in $this->trie_array. If $term does not exist in $trie->trie_array returns false
- getValues() : array<string|int, mixed>
- Returns all the terms in the trie beneath the provided term prefix
- getValuesTrieArray() : array<string|int, mixed>
- Computes the suffixes $count,...$max_results-$count in the trie_array beneath the provided $find_more is true. Prepends $prefix to each and returns the array of the result.
Properties
$end_marker
The marker used to represent the end of an entry in a trie
public
string
$end_marker
$trie_array
A nested array used to represent the trie
public
array<string|int, mixed>
$trie_array
Methods
__construct()
Creates and returns an empty trie. Sets the end of term character
public
__construct([string $end_marker = " " ]) : mixed
Parameters
- $end_marker : string = " "
-
end of term marker
Return values
mixed —add()
Adds a term to the Trie
public
add(string $term[, bool $raw_url_decode = true ]) : array<string|int, mixed>
Parameters
- $term : string
-
the term to be inserted if a space separated term value pair. Then the term is inserted into the trie and the leaf label is the value.
- $raw_url_decode : bool = true
-
decide term should be encoded in utf-8 or url
Return values
array<string|int, mixed> —$trie_array beneath last letter of term inserted
exists()
Returns the sub trie_array under $term in $this->trie_array. If $term does not exist in $trie->trie_array returns false
public
exists(string $term) : array<string|int, mixed>
Parameters
- $term : string
-
term to look up
Return values
array<string|int, mixed> —$trie_array subtrie under term
getValues()
Returns all the terms in the trie beneath the provided term prefix
public
getValues(string $prefix, int $max_results) : array<string|int, mixed>
Parameters
- $prefix : string
-
of term to look up
- $max_results : int
-
maximum number of strings to return
Return values
array<string|int, mixed> —$terms under $prefix
getValuesTrieArray()
Computes the suffixes $count,...$max_results-$count in the trie_array beneath the provided $find_more is true. Prepends $prefix to each and returns the array of the result.
private
getValuesTrieArray(array<string|int, mixed> $trie_array, string $prefix, int $max_results, int &$count[, bool &$find_more = true ]) : array<string|int, mixed>
Parameters
- $trie_array : array<string|int, mixed>
-
a nested array representing a trie to look up suffixes in
- $prefix : string
-
to prepend to each found suffix
- $max_results : int
-
maximum number of strings to return
- $count : int
-
which suffix in trie_array to start with
- $find_more : bool = true
-
whether to try to look up or not (stops recursion)
Return values
array<string|int, mixed> —$terms a list of ($prefix.suffix1, $prefix, $suffix2,...)