ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::Trie< V > Class Template Reference

a DICTIONARY of strings on a trie More...

#include <ADS/dictionary/Trie.h>

Public types

using Key = std::string
 the type of the keys
using Value = V
 the type of the values
using value_type = std::pair< std::string , V >
 the type of the items, for the standard library
using key_type = std::string
 the type of the keys
using mapped_type = V
 the type of the values
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 = PairRef< std::string , const V & >
 a reference to an item
using const_reference = reference
 a constant reference to an item
using const_iterator = detail::TrieIterator< Node , V , false >
 the iterator on the items, constant
using iterator = const_iterator
 the iterator on the items

Public member functions

std::optional< V > lookup (std::string_view k) const
 the value of the key \( k \), or nothing, in \( O( |k| \log \sigma ) \) time
void insert (std::string_view k, const V &v)
 associates the value \( v \) to the key \( k \), the path of \( k \) made by the nodes missing, in \( O( |k| \log \sigma ) \) time
void remove (std::string_view k)
 removes the key \( k \), if it is there, and the nodes of its path left with no value and no child, bottom up
bool contains (std::string_view k) const
 true if the key \( k \) is there
std::size_t size (void) const
 the number of keys
std::vector< std::string > keysWithPrefix (std::string_view p) const
 the keys that begin with \( p \), in increasing order: those below the node of \( p \)
std::size_t nodes (void) const
 the number of nodes, the root included
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

template<class V>
class ADS::Trie< V >

a DICTIONARY of strings on a trie

Member typedef documentation

◆ const_iterator

template<class V>
using ADS::Trie< V >::const_iterator = detail::TrieIterator< Node , V , false >

the iterator on the items, constant

◆ const_reference

template<class V>
using ADS::Trie< V >::const_reference = reference

a constant reference to an item

◆ difference_type

template<class V>
using ADS::Trie< V >::difference_type = std::ptrdiff_t

the type of the distances between two iterators

◆ iterator

template<class V>
using ADS::Trie< V >::iterator = const_iterator

the iterator on the items

◆ Key

template<class V>
using ADS::Trie< V >::Key = std::string

the type of the keys

◆ key_type

template<class V>
using ADS::Trie< V >::key_type = std::string

the type of the keys

◆ mapped_type

template<class V>
using ADS::Trie< V >::mapped_type = V

the type of the values

◆ reference

template<class V>
using ADS::Trie< V >::reference = PairRef< std::string , const V & >

a reference to an item

◆ size_type

template<class V>
using ADS::Trie< V >::size_type = std::size_t

the type of the sizes

◆ Value

template<class V>
using ADS::Trie< V >::Value = V

the type of the values

◆ value_type

template<class V>
using ADS::Trie< V >::value_type = std::pair< std::string , V >

the type of the items, for the standard library

Member function documentation

◆ begin()

template<class V>
const_iterator ADS::Trie< V >::begin ( void ) const
inlinenodiscard

the iterator on the first item

◆ cbegin()

template<class V>
const_iterator ADS::Trie< V >::cbegin ( void ) const
inlinenodiscard

the constant iterator on the first item

◆ cend()

template<class V>
const_iterator ADS::Trie< V >::cend ( void ) const
inlinenodiscard

the constant iterator past the last item

◆ contains()

template<class V>
bool ADS::Trie< V >::contains ( std::string_view k) const
nodiscard

true if the key \( k \) is there

◆ end()

template<class V>
const_iterator ADS::Trie< V >::end ( void ) const
inlinenodiscard

the iterator past the last item

◆ insert()

template<class V>
void ADS::Trie< V >::insert ( std::string_view k,
const V & v )

associates the value \( v \) to the key \( k \), the path of \( k \) made by the nodes missing, in \( O( |k| \log \sigma ) \) time

Postcondition
\( k \) has the value \( v \)

◆ keysWithPrefix()

template<class V>
std::vector< std::string > ADS::Trie< V >::keysWithPrefix ( std::string_view p) const
nodiscard

the keys that begin with \( p \), in increasing order: those below the node of \( p \)

◆ lookup()

template<class V>
std::optional< V > ADS::Trie< V >::lookup ( std::string_view k) const
nodiscard

the value of the key \( k \), or nothing, in \( O( |k| \log \sigma ) \) time

In pseudocode (Fredkin, section 6.3 of TAOCP, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lookup}( k ) \cr 2 & \quad x \gets \mathit{root} \cr 3 & \quad \textbf{foreach}\ \text{character } c \text{ of } k \ \textbf{do} \cr 4 & \qquad x \gets \text{the child of } x \text{ by } c;\ \textbf{if}\ x = \textbf{nil}\ \textbf{then return nil} \cr 5 & \quad \textbf{return}\ x.\mathit{value} \end{array}} \]

◆ nodes()

template<class V>
std::size_t ADS::Trie< V >::nodes ( void ) const
nodiscard

the number of nodes, the root included

◆ remove()

template<class V>
void ADS::Trie< V >::remove ( std::string_view k)

removes the key \( k \), if it is there, and the nodes of its path left with no value and no child, bottom up

Postcondition
\( k \) is not there

◆ size()

template<class V>
std::size_t ADS::Trie< V >::size ( void ) const
nodiscard

the number of keys


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