ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
PatriciaTrie.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File PatriciaTrie.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
68/*--------------------------------------------------------------------------*/
69/*----------------------------- DEFINITIONS --------------------------------*/
70/*--------------------------------------------------------------------------*/
71
72#ifndef __ADS_PatriciaTrie
73 #define __ADS_PatriciaTrie
74/* self-identification: #endif constant */
75
76/*--------------------------------------------------------------------------*/
77/*------------------------------ INCLUDES ----------------------------------*/
78/*--------------------------------------------------------------------------*/
79
80#include "ADS/Contract.h"
82
83#include <algorithm>
84#include <cstddef>
85#include <functional>
86#include <string>
87#include <string_view>
88#include <utility>
89#include <vector>
90
91/*--------------------------------------------------------------------------*/
92/*--------------------------- NAMESPACE ------------------------------------*/
93/*--------------------------------------------------------------------------*/
94
95namespace ADS
96{
97
98/*--------------------------------------------------------------------------*/
99/*--------------------------- CLASS PatriciaTrie ---------------------------*/
100/*--------------------------------------------------------------------------*/
101
104{
105
106/*--------------------------------------------------------------------------*/
107 public:
134 explicit PatriciaTrie( std::vector< std::string > D );
135
162 [[nodiscard]] std::size_t lexicographicPosition( std::string_view P ) const;
163
167 [[nodiscard]] std::size_t prefixEnd( std::string_view P ) const;
168
171 [[nodiscard]] std::pair< std::size_t , std::size_t >
172 prefixRange( std::string_view P ) const;
173
177 [[nodiscard]] std::size_t blindLeaf( std::string_view P ) const;
178
180 [[nodiscard]] bool contains( std::string_view P ) const;
181
183 [[nodiscard]] std::size_t size( void ) const;
184
186 [[nodiscard]] std::size_t nodes( void ) const;
187
190 [[nodiscard]] const std::string & string( std::size_t i ) const;
191
194 [[nodiscard]] std::vector< std::pair< std::size_t , int > >
195 shape( void ) const;
196
197/*------------------------- THE STANDARD INTERFACE -------------------------*/
198
199 // a constant view of the strings in their order, contiguous
200
202 using value_type = std::string;
204 using size_type = std::size_t;
206 using difference_type = std::ptrdiff_t;
208 using reference = const std::string &;
210 using const_reference = const std::string &;
212 using const_iterator = std::vector< std::string >::const_iterator;
215
217 [[nodiscard]] const_iterator begin( void ) const { return( f_D.begin() ); }
219 [[nodiscard]] const_iterator end( void ) const { return( f_D.end() ); }
221 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
223 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
224
225/*--------------------------------------------------------------------------*/
226 private:
227 struct Node
228 {
229 std::size_t depth;
230 std::size_t lo ,
231 hi;
232 bool leaf;
233 std::vector< std::pair< int , std::size_t > > child;
234 };
235
237 [[nodiscard]] static int at( std::string_view s , std::size_t i );
238
240 [[nodiscard]] std::size_t childOf( std::size_t x , int c ) const;
241
244 void descend( std::string_view P , int term ,
245 std::vector< std::size_t > & path ) const;
246
249 [[nodiscard]] std::size_t blind( std::string_view P , int term ,
250 std::vector< std::size_t > & path ) const;
251
254 [[nodiscard]] bool wellFormed( void ) const;
255
256 static constexpr std::size_t kNone = ~std::size_t( 0 );
257
258 std::vector< std::string > f_D;
259 std::vector< Node > f_N;
260
261 }; // end( class PatriciaTrie )
262
263/*--------------------------------------------------------------------------*/
264
265} // end( namespace ADS )
266
267/*--------------------------------------------------------------------------*/
268/*-------------------------- IMPLEMENTATION --------------------------------*/
269/*--------------------------------------------------------------------------*/
270
271#include "ADS/dictionary/impl/PatriciaTrie.tpp"
272
273/*--------------------------------------------------------------------------*/
274
275#endif /* PatriciaTrie.h included */
276
277/*--------------------------------------------------------------------------*/
278/*------------------------ End File PatriciaTrie.h -------------------------*/
279/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The search by prefix in a sorted dictionary of strings, beyond the book: the strings prefixed by ar...
const_iterator cend(void) const
the constant iterator past the last item
Definition PatriciaTrie.h:223
std::size_t size_type
the type of the sizes
Definition PatriciaTrie.h:204
std::ptrdiff_t difference_type
the type of the distances between two iterators
Definition PatriciaTrie.h:206
const std::string & const_reference
a constant reference to an item
Definition PatriciaTrie.h:210
bool contains(std::string_view P) const
true if is one of the strings
PatriciaTrie(std::vector< std::string > D)
the trie of , built in time
std::size_t lexicographicPosition(std::string_view P) const
the number of strings smaller than , by the blind search
const_iterator cbegin(void) const
the constant iterator on the first item
Definition PatriciaTrie.h:221
std::pair< std::size_t, std::size_t > prefixRange(std::string_view P) const
the range of the strings prefixed by , i.e., lexicographicPosition( P ) and prefixEnd( P )
std::size_t prefixEnd(std::string_view P) const
the number of strings smaller than , i.e., smaller than or prefixed by , by the blind search of blin...
std::size_t blindLeaf(std::string_view P) const
the leaf of the first stage of the blind search of , the only string that the search reads
std::vector< std::string >::const_iterator const_iterator
the iterator on the items, constant
Definition PatriciaTrie.h:212
const_iterator end(void) const
the iterator past the last item
Definition PatriciaTrie.h:219
std::size_t nodes(void) const
the number of nodes, the root and the leaves included
const_iterator begin(void) const
the iterator on the first item
Definition PatriciaTrie.h:217
const std::string & string(std::size_t i) const
the string
const_iterator iterator
the iterator on the items
Definition PatriciaTrie.h:214
std::size_t size(void) const
the number of strings
const std::string & reference
a reference to an item
Definition PatriciaTrie.h:208
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 s...
std::string value_type
the type of the items, for the standard library
Definition PatriciaTrie.h:202
std::vector< int > path(int r, int s, const std::vector< int > &p)
the path from to in the tree of the parents , empty if there is none (the procedure printPath of th...
Definition ADT.h:96