ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
StringSearch.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File StringSearch.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
132/*--------------------------------------------------------------------------*/
133/*----------------------------- DEFINITIONS --------------------------------*/
134/*--------------------------------------------------------------------------*/
135
136#ifndef __ADS_StringSearch
137 #define __ADS_StringSearch
138/* self-identification: #endif constant */
139
140/*--------------------------------------------------------------------------*/
141/*------------------------------ INCLUDES ----------------------------------*/
142/*--------------------------------------------------------------------------*/
143
144#include "ADS/Contract.h"
145#include "ADS/SimdInline.h"
146#include "ADS/memory/Prefetch.h"
148
149#include <cstddef>
150#include <array>
151#include <cstdint>
152#include <iterator>
153#include <string>
154#include <string_view>
155#include <utility>
156#include <vector>
157
162#ifndef ADS_SEARCH_SIMD
163 #define ADS_SEARCH_SIMD ADS_SIMD
164#endif
165#if ADS_SEARCH_SIMD && ! ADS_SIMD
166 #error "ADS_SEARCH_SIMD = 1 needs ADS_WITH_HIGHWAY and no ADS_SIMD_SCALAR"
167#endif
168
169/*--------------------------------------------------------------------------*/
170/*--------------------------- NAMESPACE ------------------------------------*/
171/*--------------------------------------------------------------------------*/
172
173namespace ADS
174{
175
176/*--------------------------------------------------------------------------*/
177/*------------------------- CLASS StringSearcher ---------------------------*/
178/*--------------------------------------------------------------------------*/
179
182{
183
184/*--------------------------------------------------------------------------*/
185 public:
187 static constexpr std::size_t npos = std::string_view::npos;
188
191 static constexpr std::size_t kSkip = 64;
192
194 StringSearcher( void ) : StringSearcher( std::string_view() ) { }
195
227 explicit StringSearcher( std::string_view P );
228
263 [[nodiscard]] std::size_t find( std::string_view T ,
264 std::size_t from = 0 ) const;
265
269 [[nodiscard]] std::vector< std::size_t > findAll( std::string_view T ) const;
270
273 [[nodiscard]] std::size_t count( std::string_view T ) const;
274
279 template< std::contiguous_iterator I >
280 requires( sizeof( std::iter_value_t< I > ) == 1 )
281 [[nodiscard]] std::pair< I , I > operator()( I first , I last ) const {
282 const std::string_view T(
283 reinterpret_cast< const char * >( std::to_address( first ) ) ,
284 std::size_t( last - first ) );
285 const std::size_t s = find( T );
286 if( s == npos )
287 return( std::pair( last , last ) );
288 return( std::pair( first + std::ptrdiff_t( s ) ,
289 first + std::ptrdiff_t( s + f_P.size() ) ) );
290 }
291
293 [[nodiscard]] std::string_view pattern( void ) const { return( f_P ); }
294
295/*--------------------------------------------------------------------------*/
296 private:
299 template< class F >
300 std::size_t scan( std::string_view T , std::size_t from , F && report ) const;
301
303 template< int k , int W , class F >
304 std::size_t filter( const unsigned char * T , std::size_t n ,
305 std::size_t from ,
306 F && report ) const;
307
309 template< class F >
310 std::size_t filterBytes( const unsigned char * T , std::size_t n ,
311 std::size_t from , F && report ) const;
312
315 template< class F >
316 std::size_t skip( const unsigned char * T , std::size_t n , std::size_t from ,
317 F && report ) const;
318
322 template< class F >
323 std::size_t skipPair( const unsigned char * T , std::size_t n ,
324 std::size_t from , F && report ) const;
325
327 [[nodiscard]] std::size_t gram( const unsigned char * p ) const;
328
330 template< class F >
331 std::size_t twoWay( const unsigned char * T , std::size_t n ,
332 std::size_t from ,
333 F && report ) const;
334
335 std::string f_P;
336 std::size_t f_off[ 4 ] = { 0 , 0 , 0 , 0 };
337 unsigned char f_byte[ 4 ] = { 0 , 0 , 0 , 0 };
338 int f_probes = 0;
339 std::size_t f_q = 0;
340 std::uint64_t f_qmask = 0;
341 std::array< std::uint8_t , 4096 > f_shift;
342 std::size_t f_shift0 = 1;
344
345 }; // end( class StringSearcher )
346
347/*--------------------------------------------------------------------------*/
348
351[[nodiscard]] inline std::size_t
352findFirst( std::string_view T , std::string_view P , std::size_t from = 0 );
353
356[[nodiscard]] inline std::vector< std::size_t > findAll( std::string_view T ,
357 std::string_view P );
358
359/*--------------------------------------------------------------------------*/
360
361} // end( namespace ADS )
362
363/*--------------------------------------------------------------------------*/
364/*-------------------------- IMPLEMENTATION --------------------------------*/
365/*--------------------------------------------------------------------------*/
366
367#include "ADS/string/impl/StringSearch.tpp"
368
369/*--------------------------------------------------------------------------*/
370
371#endif /* StringSearch.h included */
372
373/*--------------------------------------------------------------------------*/
374/*------------------------ End File StringSearch.h -------------------------*/
375/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The prefetch of a line of the cache, beyond the book: ADS::prefetch( p ) asks the processor to bring ...
The vector kernels of ADS that are inlined in the middle of a search, on Google Highway,...
The exact string matching, beyond the book: the occurrences of a pattern of characters in a text o...
std::vector< std::size_t > findAll(std::string_view T) const
all the occurrences of in , in increasing order: the search of find(), which goes on after each occu...
std::size_t find(std::string_view T, std::size_t from=0) const
the first occurrence of at or after from in , npos if none
std::string_view pattern(void) const
the pattern
Definition StringSearch.h:293
StringSearcher(void)
the empty pattern
Definition StringSearch.h:194
static constexpr std::size_t npos
no place: the result of a search that finds no occurrence
Definition StringSearch.h:187
static constexpr std::size_t kSkip
the length from which a pattern is searched by the shifts of its q-grams instead of the filter of the...
Definition StringSearch.h:191
StringSearcher(std::string_view P)
the pattern prepared, its probes and the shifts of its q-grams, in
std::size_t count(std::string_view T) const
the number of occurrences of in , by the search of findAll()
std::vector< std::size_t > twoWay(std::string_view T, std::string_view P)
the places of all the occurrences of in , by the Two-Way algorithm of Crochemore and Perrin
std::size_t findFirst(std::string_view T, std::string_view P, std::size_t from=0)
the first occurrence of in at or after from, npos if none, by a StringSearcher used once
std::vector< std::size_t > findAll(std::string_view T, std::string_view P)
all the occurrences of in , overlapping ones included, by a StringSearcher used once
Definition ADT.h:96