ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::PatriciaTrie Class Reference

a sorted dictionary of strings on a Patricia trie, with the blind search More...

#include <ADS/dictionary/PatriciaTrie.h>

Public types

using value_type = std::string
 the type of the items, for the standard library
using size_type = std::size_t
 the type of the sizes
using difference_type = std::ptrdiff_t
 the type of the distances between two iterators
using reference = const std::string &
 a reference to an item
using const_reference = const std::string &
 a constant reference to an item
using const_iterator = std::vector< std::string >::const_iterator
 the iterator on the items, constant
using iterator = const_iterator
 the iterator on the items

Public member functions

 PatriciaTrie (std::vector< std::string > D)
 the trie of \( D \), built in \( O( N ) \) time
std::size_t lexicographicPosition (std::string_view P) const
 the number of strings smaller than \( P \), by the blind search
std::size_t prefixEnd (std::string_view P) const
 the number of strings smaller than \( P\# \), i.e., smaller than \( P \) or prefixed by \( P \), by the blind search of blind() with \( P \) followed by a character larger than all the others
std::pair< std::size_t, std::size_t > prefixRange (std::string_view P) const
 the range \( [ l , r ) \) of the strings prefixed by \( P \), i.e., lexicographicPosition( P ) and prefixEnd( P )
std::size_t blindLeaf (std::string_view P) const
 the leaf \( l \) of the first stage of the blind search of \( P \), the only string that the search reads
bool contains (std::string_view P) const
 true if \( P \) is one of the strings
std::size_t size (void) const
 the number of strings
std::size_t nodes (void) const
 the number of nodes, the root and the leaves included
const std::string & string (std::size_t i) const
 the string \( i \)
std::vector< std::pair< std::size_t, int > > shape (void) const
 the length of the prefix of each node and its branching character, in preorder (-1 for the end of a string, and for the root)
const_iterator begin (void) const
 the iterator on the first item
const_iterator end (void) const
 the iterator past the last item
const_iterator cbegin (void) const
 the constant iterator on the first item
const_iterator cend (void) const
 the constant iterator past the last item

Detailed description

a sorted dictionary of strings on a Patricia trie, with the blind search

Member typedef documentation

◆ const_iterator

using ADS::PatriciaTrie::const_iterator = std::vector< std::string >::const_iterator

the iterator on the items, constant

◆ const_reference

using ADS::PatriciaTrie::const_reference = const std::string &

a constant reference to an item

◆ difference_type

using ADS::PatriciaTrie::difference_type = std::ptrdiff_t

the type of the distances between two iterators

◆ iterator

the iterator on the items

◆ reference

using ADS::PatriciaTrie::reference = const std::string &

a reference to an item

◆ size_type

using ADS::PatriciaTrie::size_type = std::size_t

the type of the sizes

◆ value_type

using ADS::PatriciaTrie::value_type = std::string

the type of the items, for the standard library

Constructor and destructor documentation

◆ PatriciaTrie()

ADS::PatriciaTrie::PatriciaTrie ( std::vector< std::string > D)
explicit

the trie of \( D \), built in \( O( N ) \) time

In pseudocode (chapter 9 of Ferragina), the nodes of the rightmost path on a stack, \( \ell_i \) the longest common prefix of \( D[ i - 1 ] \) and \( D[ i ] \) (0 for \( i = 0 \)), the depth of a node the length of its prefix:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{build}( D ) \cr 2 & \quad \mathit{st} \gets ( \mathit{root} ) \cr 3 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ n - 1\ \textbf{do} \cr 4 & \qquad \textbf{while}\ \text{the top of } \mathit{st} \text{ is } \text{deeper than } \ell_i\ \textbf{do}\ \mathit{last} \gets \mathrm{pop}( \mathit{st} ) \cr 5 & \qquad \textbf{if}\ \text{the top is shallower than } \ell_i \ \textbf{then} \cr 6 & \qquad\quad \text{split its last edge by a node } u \text{ of } \text{depth } \ell_i \text{, parent of } \mathit{last}; \ \mathrm{push}( \mathit{st} , u ) \cr 7 & \qquad \text{add the leaf of } D[ i ] \text{ as the last child } \text{of the top};\ \mathrm{push}( \mathit{st} , \text{the leaf} ) \cr 8 & \quad \text{give each node the range of the strings below it, bottom } \text{up} \end{array}} \]

Precondition
\( D \) is sorted and its strings distinct (an audit)

Member function documentation

◆ begin()

const_iterator ADS::PatriciaTrie::begin ( void ) const
inlinenodiscard

the iterator on the first item

◆ blindLeaf()

std::size_t ADS::PatriciaTrie::blindLeaf ( std::string_view P) const
nodiscard

the leaf \( l \) of the first stage of the blind search of \( P \), the only string that the search reads

Precondition
size() \( \ge 1 \)

◆ cbegin()

const_iterator ADS::PatriciaTrie::cbegin ( void ) const
inlinenodiscard

the constant iterator on the first item

◆ cend()

const_iterator ADS::PatriciaTrie::cend ( void ) const
inlinenodiscard

the constant iterator past the last item

◆ contains()

bool ADS::PatriciaTrie::contains ( std::string_view P) const
nodiscard

true if \( P \) is one of the strings

◆ end()

const_iterator ADS::PatriciaTrie::end ( void ) const
inlinenodiscard

the iterator past the last item

◆ lexicographicPosition()

std::size_t ADS::PatriciaTrie::lexicographicPosition ( std::string_view P) const
nodiscard

the number of strings smaller than \( P \), by the blind search

In pseudocode (the blind search, chapter 9 of Ferragina), \( |x| \) the depth of the node \( x \), \( x.\mathit{lo} \) and \( x.\mathit{hi} \) the range of its strings, \( P \) followed by a character smaller than all the others:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{blind}( P ) \cr 2 & \quad \pi \gets \text{the path from the root down by the characters } P[ |x| ] \text{ of the nodes} \cr 3 & \quad s \gets D[ ( \text{the last node of } \pi ).\mathit{lo} ]\quad \triangleright\ \text{the one string read} \cr 4 & \quad \ell \gets \text{the longest common prefix of } P \text{ and } s \cr 5 & \quad \textbf{if}\ \ell = |P| + 1\ \textbf{then return}\ \text{the } \text{index of } s \cr 6 & \quad ( u , v ) \gets \text{the edge of } \pi \text{ with } |u| \le \ell < |v| \cr 7 & \quad \textbf{if}\ |u| = \ell\ \textbf{then return}\ \text{the first } \text{string of the first child of } u \text{ after } P[ \ell ] \cr 8 & \quad \textbf{if}\ P[ \ell ] > s[ \ell ]\ \textbf{then return} \ v.\mathit{hi}\ \textbf{else return}\ v.\mathit{lo} \end{array}} \]

Postcondition
\( D[ r - 1 ] < P \le D[ r ] \)

◆ nodes()

std::size_t ADS::PatriciaTrie::nodes ( void ) const
nodiscard

the number of nodes, the root and the leaves included

◆ prefixEnd()

std::size_t ADS::PatriciaTrie::prefixEnd ( std::string_view P) const
nodiscard

the number of strings smaller than \( P\# \), i.e., smaller than \( P \) or prefixed by \( P \), by the blind search of blind() with \( P \) followed by a character larger than all the others

◆ prefixRange()

std::pair< std::size_t, std::size_t > ADS::PatriciaTrie::prefixRange ( std::string_view P) const
nodiscard

the range \( [ l , r ) \) of the strings prefixed by \( P \), i.e., lexicographicPosition( P ) and prefixEnd( P )

◆ shape()

std::vector< std::pair< std::size_t, int > > ADS::PatriciaTrie::shape ( void ) const
nodiscard

the length of the prefix of each node and its branching character, in preorder (-1 for the end of a string, and for the root)

◆ size()

std::size_t ADS::PatriciaTrie::size ( void ) const
nodiscard

the number of strings

◆ string()

const std::string & ADS::PatriciaTrie::string ( std::size_t i) const
nodiscard

the string \( i \)

Precondition
\( i \) is less than size()

The documentation for this class was generated from the following file: