ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SuffixArray.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File SuffixArray.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
152/*--------------------------------------------------------------------------*/
153/*----------------------------- DEFINITIONS --------------------------------*/
154/*--------------------------------------------------------------------------*/
155
156#ifndef __ADS_SuffixArray
157 #define __ADS_SuffixArray
158/* self-identification: #endif constant */
159
160/*--------------------------------------------------------------------------*/
161/*------------------------------ INCLUDES ----------------------------------*/
162/*--------------------------------------------------------------------------*/
163
164#include "ADS/Contract.h"
166#include "ADS/sort/RadixSort.h"
167
168#include <algorithm>
169#include <cstddef>
170#include <cstdint>
171#include <limits>
172#include <numeric>
173#include <string_view>
174#include <tuple>
175#include <utility>
176#include <vector>
177
178/*--------------------------------------------------------------------------*/
179/*--------------------------- NAMESPACE ------------------------------------*/
180/*--------------------------------------------------------------------------*/
181
182namespace ADS
183{
184
186using Symbols = std::vector< std::size_t >;
187
189[[nodiscard]] inline Symbols toSymbols( std::string_view T );
190
205[[nodiscard]] inline std::vector< std::size_t >
207
216[[nodiscard]] inline std::vector< std::size_t >
217suffixArraySorting( std::string_view T );
218
247[[nodiscard]] inline std::vector< std::size_t >
249
259[[nodiscard]] inline std::vector< std::size_t >
260suffixArrayDoubling( std::string_view T );
261
289[[nodiscard]] inline std::vector< std::size_t >
291
301[[nodiscard]] inline std::vector< std::size_t >
302suffixArrayLarssonSadakane( std::string_view T );
303
332[[nodiscard]] inline std::vector< std::size_t >
333suffixArraySkew( const Symbols & T , std::size_t K );
334
343[[nodiscard]] inline std::vector< std::size_t >
344suffixArraySkew( std::string_view T );
345
389[[nodiscard]] inline std::vector< std::size_t >
390suffixArraySais( const Symbols & T , std::size_t K );
391
400[[nodiscard]] inline std::vector< std::size_t >
401suffixArraySais( std::string_view T );
402
430[[nodiscard]] inline std::vector< std::size_t >
431suffixArrayScan( const Symbols & T , std::size_t m );
432
442[[nodiscard]] inline std::vector< std::size_t >
443suffixArrayScan( std::string_view T , std::size_t m );
444
446[[nodiscard]] inline std::vector< std::size_t >
447inversePermutation( const std::vector< std::size_t > & SA );
448
474[[nodiscard]] inline std::vector< std::size_t >
475lcpArray( const Symbols & T , const std::vector< std::size_t > & SA );
476
486[[nodiscard]] inline std::vector< std::size_t >
487lcpArray( std::string_view T , const std::vector< std::size_t > & SA );
488
489/*--------------------------------------------------------------------------*/
490/*--------------------------- CLASS SuffixArray ----------------------------*/
491/*--------------------------------------------------------------------------*/
492
497{
498
499/*--------------------------------------------------------------------------*/
500 public:
502 SuffixArray( void ) = default;
503
523 explicit SuffixArray( std::string_view T );
524
527 SuffixArray( Symbols T , std::size_t K );
528
553 [[nodiscard]] std::pair< std::size_t , std::size_t >
554 range( const Symbols & P , long * comparisons = nullptr ) const;
555
563 [[nodiscard]] std::pair< std::size_t , std::size_t >
564 range( std::string_view P , long * comparisons = nullptr ) const;
565
596 [[nodiscard]] std::pair< std::size_t , std::size_t >
597 rangeLcp( const Symbols & P , long * comparisons = nullptr ) const;
598
607 [[nodiscard]] std::pair< std::size_t , std::size_t >
608 rangeLcp( std::string_view P , long * comparisons = nullptr ) const;
609
612 [[nodiscard]] std::size_t count( std::string_view P ) const;
613
616 [[nodiscard]] std::vector< std::size_t > locate( std::string_view P ) const;
617
619 [[nodiscard]] std::vector< std::size_t > locate( const Symbols & P ) const;
620
639 [[nodiscard]] std::size_t lce( std::size_t i , std::size_t j ) const;
640
643 [[nodiscard]] std::size_t rank( std::size_t i ) const;
644
647 [[nodiscard]] std::size_t operator[]( std::size_t r ) const;
648
650 [[nodiscard]] std::size_t size( void ) const { return( f_SA.size() ); }
651
653 [[nodiscard]] const Symbols & text( void ) const { return( f_T ); }
654
656 [[nodiscard]] const std::vector< std::size_t > & lcp( void ) const {
657 return( f_lcp );
658 }
659
660/*------------------------- THE STANDARD INTERFACE -------------------------*/
661
662 // a constant view of SA, random access
663
664 using value_type = std::size_t;
665 using size_type = std::size_t;
666 using difference_type = std::ptrdiff_t;
667 using const_reference = const std::size_t &;
670 using const_iterator = std::vector< std::size_t >::const_iterator;
672
674 [[nodiscard]] const_iterator begin( void ) const { return( f_SA.begin() ); }
676 [[nodiscard]] const_iterator end( void ) const { return( f_SA.end() ); }
678 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
680 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
681
682/*--------------------------------------------------------------------------*/
683 private:
687 void index( void );
688
692 std::size_t build( long L , long R );
693
698 std::pair< std::size_t , bool > compare( const Symbols & P , std::size_t M ,
699 std::size_t h , bool upper ,
700 long * comparisons ) const;
701
705 std::size_t bound( const Symbols & P , bool upper , long * c ) const;
706 std::size_t boundLcp( const Symbols & P , bool upper , long * c ) const;
707
708 Symbols f_T;
709 std::vector< std::size_t > f_SA;
710 std::vector< std::size_t > f_rank;
711 std::vector< std::size_t > f_lcp;
712 std::vector< std::size_t > f_Llcp;
713 std::vector< std::size_t > f_Rlcp;
715
716 }; // end( class SuffixArray )
717
718/*--------------------------------------------------------------------------*/
719
720} // end( namespace ADS )
721
722/*--------------------------------------------------------------------------*/
723/*-------------------------- IMPLEMENTATION --------------------------------*/
724/*--------------------------------------------------------------------------*/
725
726#include "ADS/string/impl/SuffixArray.tpp"
727
728/*--------------------------------------------------------------------------*/
729
730#endif /* SuffixArray.h included */
731
732/*--------------------------------------------------------------------------*/
733/*------------------------- End File SuffixArray.h -------------------------*/
734/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The sorts by the digits of the keys, beyond the book (which has Counting Sort and Pigeonhole Sort,...
The range minimum query, beyond the book: given an array of items of an ordered universe,...
std::size_t lce(std::size_t i, std::size_t j) const
the length of the longest common prefix of the suffixes and ,
SuffixArray(std::string_view T)
the index of the bytes of , built by suffixArraySais() and lcpArray()
std::size_t value_type
a place of the text
Definition SuffixArray.h:664
const std::size_t & const_reference
an entry of SA
Definition SuffixArray.h:667
const_reference reference
an entry of SA
Definition SuffixArray.h:668
std::size_t size(void) const
the length of the text
Definition SuffixArray.h:650
const Symbols & text(void) const
the text
Definition SuffixArray.h:653
std::size_t count(std::string_view P) const
the number of occurrences of , the size of the range of rangeLcp()
std::pair< std::size_t, std::size_t > range(const Symbols &P, long *comparisons=nullptr) const
the range of of the suffixes prefixed by , by two binary searches; the comparisons of symbols go to...
std::ptrdiff_t difference_type
a difference of ranks
Definition SuffixArray.h:666
std::pair< std::size_t, std::size_t > range(std::string_view P, long *comparisons=nullptr) const
the range of range() above, of the bytes of
const std::vector< std::size_t > & lcp(void) const
the array , entries
Definition SuffixArray.h:656
SuffixArray(void)=default
the index of the empty text
SuffixArray(Symbols T, std::size_t K)
the index of an integer text, built as the one of the bytes above
std::vector< std::size_t >::const_iterator const_iterator
a constant iterator on the entries of SA
Definition SuffixArray.h:670
const_iterator cbegin(void) const
the first entry of
Definition SuffixArray.h:678
const_iterator iterator
on the entries of SA
Definition SuffixArray.h:671
const_iterator end(void) const
one past the last entry of
Definition SuffixArray.h:676
const_iterator begin(void) const
the first entry of
Definition SuffixArray.h:674
std::pair< std::size_t, std::size_t > rangeLcp(const Symbols &P, long *comparisons=nullptr) const
the range of range() by the search of Manber and Myers, comparisons of symbols
std::size_t operator[](std::size_t r) const
the place of the suffix of rank
std::vector< std::size_t > locate(std::string_view P) const
the places of the occurrences of , in the order of : the entries of the range of rangeLcp()
std::size_t size_type
the type of the ranks
Definition SuffixArray.h:665
std::vector< std::size_t > locate(const Symbols &P) const
the places of locate() above, of an integer pattern
std::size_t rank(std::size_t i) const
the rank of the suffix in
std::pair< std::size_t, std::size_t > rangeLcp(std::string_view P, long *comparisons=nullptr) const
the range of rangeLcp() above, of the bytes of
const_iterator cend(void) const
one past the last entry of
Definition SuffixArray.h:680
std::vector< std::size_t > lcpArray(E &ex, const S &T, const std::vector< std::size_t > &SA)
the LCP array of and of its suffix array on the threads of ex, entries (none if )
Definition ParallelText.h:161
std::vector< std::size_t > inversePermutation(const std::vector< std::size_t > &SA)
the inverse of a permutation:
std::vector< std::size_t > suffixArrayLarssonSadakane(const Symbols &T)
the suffix array of by the doubling of Larsson and Sadakane
std::vector< std::size_t > Symbols
a text on an integer alphabet
Definition SuffixArray.h:186
std::vector< std::size_t > suffixArraySorting(const Symbols &T)
the suffix array of by a sort of comparisons of its suffixes
Symbols toSymbols(std::string_view T)
the bytes of as symbols in
std::vector< std::size_t > suffixArraySkew(const Symbols &T, std::size_t K)
the suffix array of by the Skew algorithm of Kärkkäinen and Sanders
std::vector< std::size_t > suffixArrayDoubling(const Symbols &T)
the suffix array of by the prefix doubling of Manber and Myers
std::vector< std::size_t > suffixArraySais(const Symbols &T, std::size_t K)
the suffix array of by the induced sorting of Nong, Zhang and Chan
std::vector< std::size_t > suffixArrayScan(const Symbols &T, std::size_t m)
the suffix array of by the scan-based algorithm of Baeza-Yates, Gonnet and Snider,...
Definition ADT.h:96