ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
AhoCorasick.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File AhoCorasick.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
56/*--------------------------------------------------------------------------*/
57/*----------------------------- DEFINITIONS --------------------------------*/
58/*--------------------------------------------------------------------------*/
59
60#ifndef __ADS_AhoCorasick
61 #define __ADS_AhoCorasick
62/* self-identification: #endif constant */
63
64/*--------------------------------------------------------------------------*/
65/*------------------------------ INCLUDES ----------------------------------*/
66/*--------------------------------------------------------------------------*/
67
68#include "ADS/Contract.h"
69
70#include <array>
71#include <cstddef>
72#include <cstdint>
73#include <string>
74#include <string_view>
75#include <utility>
76#include <vector>
77
78/*--------------------------------------------------------------------------*/
79/*--------------------------- NAMESPACE ------------------------------------*/
80/*--------------------------------------------------------------------------*/
81
82namespace ADS
83{
84
85/*--------------------------------------------------------------------------*/
86/*--------------------------- CLASS AhoCorasick ----------------------------*/
87/*--------------------------------------------------------------------------*/
88
91{
92
93/*--------------------------------------------------------------------------*/
94 public:
96 AhoCorasick( void );
97
101 explicit AhoCorasick( const std::vector< std::string > & P );
102
122 std::size_t add( std::string_view P );
123
149 void build( void );
150
174 [[nodiscard]] std::vector< std::pair< std::size_t , std::size_t > >
175 findAll( std::string_view T ) const;
176
180 [[nodiscard]] std::size_t count( std::string_view T ) const;
181
186 template< class F >
187 void forEach( std::string_view T , F && report ) const {
188 scan( T , [ & ]( std::size_t j , std::uint32_t id ) {
189 report( j + 1 - f_P[ id ].size() , std::size_t( id ) );
190 } );
191 }
192
194 [[nodiscard]] std::size_t size( void ) const { return( f_P.size() ); }
195
197 [[nodiscard]] std::size_t states( void ) const { return( f_go.size() ); }
198
199/*--------------------------------------------------------------------------*/
200 private:
202 template< class F >
203 void scan( std::string_view T , F && report ) const;
204
205 std::vector< std::string > f_P;
206 std::vector< std::array< std::uint32_t , 256 > > f_go;
207 std::vector< std::uint32_t > f_fail;
208 std::vector< std::uint32_t > f_out;
209 std::vector< std::vector< std::uint32_t > > f_ends;
210 bool f_built = true;
211
212 }; // end( class AhoCorasick )
213
214/*--------------------------------------------------------------------------*/
215
216} // end( namespace ADS )
217
218/*--------------------------------------------------------------------------*/
219/*-------------------------- IMPLEMENTATION --------------------------------*/
220/*--------------------------------------------------------------------------*/
221
222#include "ADS/string/impl/AhoCorasick.tpp"
223
224/*--------------------------------------------------------------------------*/
225
226#endif /* AhoCorasick.h included */
227
228/*--------------------------------------------------------------------------*/
229/*------------------------ End File AhoCorasick.h --------------------------*/
230/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
AhoCorasick(const std::vector< std::string > &P)
the automaton of the patterns , numbered in their order, built
void build(void)
the failures, the transitions and the output links, in
std::vector< std::pair< std::size_t, std::size_t > > findAll(std::string_view T) const
the occurrences of the patterns in , by increasing end, the longer pattern first for one end
void forEach(std::string_view T, F &&report) const
each occurrence of the patterns in given to report(), by increasing end, the longer pattern first f...
Definition AhoCorasick.h:187
std::size_t states(void) const
the number of states, the nodes of the trie
Definition AhoCorasick.h:197
std::size_t add(std::string_view P)
adds the pattern to the trie, the automaton to be built again; its number
std::size_t size(void) const
the number of patterns
Definition AhoCorasick.h:194
std::size_t count(std::string_view T) const
the number of the occurrences of the patterns in , by the scan of findAll()
AhoCorasick(void)
no pattern
Definition ADT.h:96