ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SuffixTree.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File SuffixTree.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
113/*--------------------------------------------------------------------------*/
114/*----------------------------- DEFINITIONS --------------------------------*/
115/*--------------------------------------------------------------------------*/
116
117#ifndef __ADS_SuffixTree
118 #define __ADS_SuffixTree
119/* self-identification: #endif constant */
120
121/*--------------------------------------------------------------------------*/
122/*------------------------------ INCLUDES ----------------------------------*/
123/*--------------------------------------------------------------------------*/
124
125#include "ADS/Contract.h"
128
129#include <cstddef>
130#include <limits>
131#include <map>
132#include <string_view>
133#include <utility>
134#include <vector>
135
136/*--------------------------------------------------------------------------*/
137/*--------------------------- NAMESPACE ------------------------------------*/
138/*--------------------------------------------------------------------------*/
139
140namespace ADS
141{
142
143/*--------------------------------------------------------------------------*/
144/*---------------------------- CLASS SuffixTree ----------------------------*/
145/*--------------------------------------------------------------------------*/
146
149{
150
151/*--------------------------------------------------------------------------*/
152 public:
154 static constexpr std::size_t npos = std::numeric_limits< std::size_t >::max();
155
162
166 struct Node
167 {
168 std::size_t pos = 0;
169 std::size_t depth = 0;
170 std::size_t parent = npos;
171 std::size_t suffix = npos;
172 std::size_t leaves = 0;
173 std::map< std::size_t , std::size_t > child;
174 };
175
177 SuffixTree( void ) = default;
178
219 explicit SuffixTree( std::string_view T , Build b = Build::mcCreight );
220
223 SuffixTree( const Symbols & T , std::size_t K , Build b = Build::mcCreight );
224
226 [[nodiscard]] std::size_t textSize( void ) const;
227
229 [[nodiscard]] std::size_t root( void ) const { return( 0 ); }
230
233 [[nodiscard]] const Node & node( std::size_t u ) const;
234
238 [[nodiscard]] std::size_t leaf( std::size_t i ) const;
239
243 [[nodiscard]] std::size_t symbol( std::size_t i ) const;
244
266 [[nodiscard]] std::size_t locus( std::string_view P ) const;
267
269 [[nodiscard]] std::size_t locus( const Symbols & P ) const;
270
273 [[nodiscard]] std::size_t count( std::string_view P ) const;
274
290 [[nodiscard]] std::vector< std::size_t > locate( std::string_view P ) const;
291
307 [[nodiscard]] std::size_t lca( std::size_t u , std::size_t v ) const;
308
325 [[nodiscard]] std::size_t lce( std::size_t i , std::size_t j ) const;
326
350 [[nodiscard]] std::pair< std::vector< std::size_t > ,
351 std::vector< std::size_t > >
352 toSuffixArray( void ) const;
353
355 [[nodiscard]] std::vector< std::size_t > preorder( void ) const;
356
358 [[nodiscard]] std::size_t size( void ) const { return( f_N.size() ); }
359
360/*------------------------- THE STANDARD INTERFACE -------------------------*/
361
362 // a constant view of the nodes, random access, the root first
363
364 using value_type = Node;
365 using size_type = std::size_t;
366 using difference_type = std::ptrdiff_t;
367 using const_reference = const Node &;
370 using const_iterator = std::vector< Node >::const_iterator;
372
374 [[nodiscard]] const_iterator begin( void ) const { return( f_N.begin() ); }
376 [[nodiscard]] const_iterator end( void ) const { return( f_N.end() ); }
378 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
380 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
381
382/*--------------------------------------------------------------------------*/
383 private:
386 void construct( const Symbols & T , std::size_t K , Build b );
387 void buildNaive( void );
388 void buildMcCreight( void );
389 void buildFromSuffixArray( const Symbols & T , std::size_t K );
390 void finish( void );
391
393 std::size_t newNode( std::size_t pos , std::size_t depth ,
394 std::size_t parent );
395
397 std::size_t attachLeaf( std::size_t u , std::size_t i );
398
401 std::size_t split( std::size_t u , std::size_t c , std::size_t d );
402
404 [[nodiscard]] bool valid( void ) const;
405
406 Symbols f_T;
407 std::vector< Node > f_N;
408 std::vector< std::size_t > f_leaf;
409 std::vector< std::size_t > f_E;
410 std::vector< std::size_t > f_first;
412
413 }; // end( class SuffixTree )
414
415/*--------------------------------------------------------------------------*/
416
417} // end( namespace ADS )
418
419/*--------------------------------------------------------------------------*/
420/*-------------------------- IMPLEMENTATION --------------------------------*/
421/*--------------------------------------------------------------------------*/
422
423#include "ADS/string/impl/SuffixTree.tpp"
424
425/*--------------------------------------------------------------------------*/
426
427#endif /* SuffixTree.h included */
428
429/*--------------------------------------------------------------------------*/
430/*------------------------- End File SuffixTree.h --------------------------*/
431/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The range minimum query, beyond the book: given an array of items of an ordered universe,...
The suffix array of a text of symbols, beyond the book: is the starting place of the -th smallest ...
the range minima of an array whose adjacent items differ by 1
Definition RangeMinimum.h:238
const Node & const_reference
a node
Definition SuffixTree.h:367
std::ptrdiff_t difference_type
a difference of names
Definition SuffixTree.h:366
std::size_t locus(const Symbols &P) const
the locus of locus() above, of an integer pattern
std::size_t size(void) const
the number of nodes
Definition SuffixTree.h:358
const_iterator cend(void) const
one past the last node
Definition SuffixTree.h:380
std::vector< std::size_t > preorder(void) const
the nodes in preorder, the children by increasing first symbol
std::pair< std::vector< std::size_t >, std::vector< std::size_t > > toSuffixArray(void) const
and of , by a visit in order
SuffixTree(std::string_view T, Build b=Build::mcCreight)
the suffix tree of the bytes of , built as b says
SuffixTree(void)=default
the tree of no text, with no node
const_iterator begin(void) const
the root
Definition SuffixTree.h:374
std::size_t symbol(std::size_t i) const
the symbol at the place of as stored: the symbols of plus 1, and 0 for the terminator at
std::size_t size_type
the type of the names
Definition SuffixTree.h:365
std::size_t root(void) const
the root, the node 0
Definition SuffixTree.h:229
std::size_t leaf(std::size_t i) const
the leaf of the suffix , the terminator being the suffix
std::size_t locus(std::string_view P) const
the extended locus of , or npos if does not occur
const_iterator iterator
on the nodes
Definition SuffixTree.h:371
SuffixTree(const Symbols &T, std::size_t K, Build b=Build::mcCreight)
the suffix tree of an integer text, built as the one of the bytes above
const_iterator cbegin(void) const
the root
Definition SuffixTree.h:378
const Node & node(std::size_t u) const
the node
const_iterator end(void) const
one past the last node
Definition SuffixTree.h:376
std::size_t textSize(void) const
the length of , without the terminator
std::size_t lce(std::size_t i, std::size_t j) const
the length of the longest common prefix of the suffixes and of
std::vector< Node >::const_iterator const_iterator
a constant iterator on the nodes
Definition SuffixTree.h:370
std::vector< std::size_t > locate(std::string_view P) const
the places of the occurrences of , in lexicographic order of their suffixes; the empty pattern at the...
Node value_type
a node
Definition SuffixTree.h:364
const_reference reference
a node
Definition SuffixTree.h:368
Build
the construction
Definition SuffixTree.h:157
@ mcCreight
the suffix links of McCreight
Definition SuffixTree.h:159
@ naive
each suffix by a walk down from the root
Definition SuffixTree.h:158
static constexpr std::size_t npos
no node, no suffix
Definition SuffixTree.h:154
std::size_t lca(std::size_t u, std::size_t v) const
the lowest common ancestor of and ,
std::size_t count(std::string_view P) const
the number of occurrences of , the leaves below its locus (the terminator left out)
std::vector< std::size_t > Symbols
a text on an integer alphabet
Definition SuffixArray.h:186
std::vector< I > suffixArray(std::string_view T)
the suffix array of the bytes of , by SA-IS on integers I (32 or 64 bits, unsigned)
Definition ADT.h:96
a node ; the edge into it is labeled by the symbols of from pos plus the depth of the parent to pos ...
Definition SuffixTree.h:167
std::size_t suffix
the suffix of a leaf, npos inside
Definition SuffixTree.h:171
std::size_t parent
npos at the root
Definition SuffixTree.h:170
std::size_t depth
the string depth
Definition SuffixTree.h:169
std::map< std::size_t, std::size_t > child
by first symbol
Definition SuffixTree.h:173
std::size_t leaves
the leaves below
Definition SuffixTree.h:172
std::size_t pos
where occurs once
Definition SuffixTree.h:168