ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ParallelText.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File ParallelText.h --------------------------*/
3/*--------------------------------------------------------------------------*/
52/*--------------------------------------------------------------------------*/
53/*----------------------------- DEFINITIONS --------------------------------*/
54/*--------------------------------------------------------------------------*/
55
56#ifndef __ADS_ParallelText
57 #define __ADS_ParallelText
58/* self-identification: #endif constant */
59
60/*--------------------------------------------------------------------------*/
61/*------------------------------ INCLUDES ----------------------------------*/
62/*--------------------------------------------------------------------------*/
63
64#include "ADS/Contract.h"
65#include "ADS/memory/Prefetch.h"
69
70#include <algorithm>
71#include <bit>
72#include <cstddef>
73#include <cstdint>
74#include <cstring>
75#include <memory>
76#include <string_view>
77#include <vector>
78
79/*--------------------------------------------------------------------------*/
80/*--------------------------- NAMESPACE ------------------------------------*/
81/*--------------------------------------------------------------------------*/
82
83namespace ADS
84{
85
122template< Executor E , class S >
123[[nodiscard]] std::vector< std::size_t >
124lcpArray( E & ex , const S & T , const std::vector< std::size_t > & SA );
125
154template< Executor E , class I >
155[[nodiscard]] std::vector< I > lcpArrayPhi( E & ex , std::string_view T ,
156 const std::vector< I > & SA );
157
158/*--------------------------------------------------------------------------*/
159
160template< Executor E , class S >
161std::vector< std::size_t > lcpArray( E & ex , const S & T ,
162 const std::vector< std::size_t > & SA )
163{
164 const std::size_t n = SA.size();
165 ADS_PRE( std::size_t( std::size( T ) ) == n );
166 if( ( ex.concurrency() == 1 ) || ( n < ( std::size_t( 1 ) << 15 ) ) )
167 return( lcpArray( T , SA ) );
168 // Phi, then PLCP in its place
169 std::vector< std::size_t > P( n );
171 ex , 0 , n ,
172 [ & ]( std::size_t r ) { P[ SA[ r ] ] = r == 0 ? n : SA[ r - 1 ]; } ,
173 4096 );
174 const std::size_t B = std::min( n , 4 * ex.concurrency() );
176 ex , 0 , B ,
177 [ & ]( std::size_t b ) {
178 const std::size_t i0 = n * b / B , i1 = n * ( b + 1 ) / B;
179 std::size_t h = 0;
180 for( std::size_t i = i0 ; i < i1 ; ++i ) {
181 const std::size_t j = P[ i ];
182 if( j == n ) { // the least suffix
183 h = 0;
184 P[ i ] = 0;
185 continue;
186 }
187 while( ( i + h < n ) && ( j + h < n ) && ( T[ i + h ] == T[ j + h ] ) )
188 ++h;
189 P[ i ] = h;
190 if( h > 0 )
191 --h;
192 }
193 } ,
194 1 );
195 std::vector< std::size_t > LCP( n - 1 );
197 ex , 0 , n - 1 , [ & ]( std::size_t r ) { LCP[ r ] = P[ SA[ r + 1 ] ]; } ,
198 4096 );
199 return( LCP );
200 }
201
202/*--------------------------------------------------------------------------*/
203
204template< Executor E , class I >
205std::vector< I > lcpArrayPhi( E & ex , std::string_view T ,
206 const std::vector< I > & SA )
207{
208 const std::size_t n = SA.size();
209 ADS_PRE( T.size() == n );
210 if( ( ex.concurrency() == 1 ) || ( n < ( std::size_t( 1 ) << 15 ) ) )
211 return( lcpArrayPhi( T , SA ) );
212 const auto * t = reinterpret_cast< const unsigned char * >( T.data() );
213 const auto P = std::make_unique_for_overwrite< I[] >( n );
215 ex , 0 , n ,
216 [ & ]( std::size_t r ) { P[ SA[ r ] ] = r == 0 ? I( n ) : SA[ r - 1 ]; } ,
217 4096 );
218 // the lcp of the suffixes i and Phi[ i ] from h on: 8 symbols at a time
219 // after the first 4, which are compared one by one (most lcp are short)
220 auto lcp = [ & ]( std::size_t i , std::size_t j , std::size_t h ) {
221 const std::size_t far = n - std::max( i , j ) ,
222 near = std::min( far , h + 4 );
223 while( ( h < near ) && ( t[ i + h ] == t[ j + h ] ) )
224 ++h;
225 if( h == near )
226 for( ; ; ) {
227 if( h + 8 > far ) {
228 while( ( h < far ) && ( t[ i + h ] == t[ j + h ] ) )
229 ++h;
230 break;
231 }
232 std::uint64_t a , b;
233 std::memcpy( &a , t + i + h , 8 );
234 std::memcpy( &b , t + j + h , 8 );
235 if( a != b ) {
236 if constexpr( std::endian::native == std::endian::little )
237 h += std::size_t( std::countr_zero( a ^ b ) ) / 8;
238 else
239 h += std::size_t( std::countl_zero( a ^ b ) ) / 8;
240 break;
241 }
242 h += 8;
243 }
244 return( h );
245 };
246 const std::size_t B = std::min( n , 4 * ex.concurrency() );
248 ex , 0 , B ,
249 [ & ]( std::size_t b ) {
250 const std::size_t i0 = n * b / B , i1 = n * ( b + 1 ) / B;
251 constexpr std::size_t D = 16; // the text at Phi, ahead
252 std::size_t h = 0;
253 for( std::size_t i = i0 ; i < i1 ; ++i ) {
254 if( i + D < i1 )
255 ADS::detail::prefetchLine(
256 t + std::min< std::size_t >( std::size_t( P[ i + D ] ) + h , n - 1 ) );
257 const std::size_t j = std::size_t( P[ i ] );
258 h = j == n ? 0 : lcp( i , j , h );
259 P[ i ] = I( h );
260 h -= ( h != 0 );
261 }
262 } ,
263 1 );
264 std::vector< I > LCP( n - 1 );
266 ex , 0 , n - 1 , [ & ]( std::size_t r ) { LCP[ r ] = P[ SA[ r + 1 ] ]; } ,
267 4096 );
268 return( LCP );
269 }
270
271/*--------------------------------------------------------------------------*/
272
273} // end( namespace ADS )
274
275/*--------------------------------------------------------------------------*/
276
277#endif /* ParallelText.h included */
278
279/*--------------------------------------------------------------------------*/
280/*------------------------- End File ParallelText.h ------------------------*/
281/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The layer of execution of the parallel algorithms of ADS, beyond the book: an executor runs the calls...
The prefetch of a line of the cache, beyond the book: ADS::prefetch( p ) asks the processor to bring ...
The suffix array of a text of symbols, beyond the book: is the starting place of the -th smallest ...
The engineered construction of the suffix array and of its LCP array, beyond the book: the ones that ...
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
void parallelFor(E &ex, std::size_t first, std::size_t last, F &&f, std::size_t grain=kGrain, std::size_t blocks=kBlocks)
the calls f( i ) for each , in parallel
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< I > lcpArrayPhi(E &ex, std::string_view T, const std::vector< I > &SA)
the LCP array of the text of bytes and of its suffix array of indices of the type I,...
Definition ParallelText.h:205
Definition ADT.h:96