#ifndef AC_FAST_H
#define AC_FAST_H
#include <vector>
#include "ac.h"
#include "ac_slow.hpp"
using namespace std;
class ACS_Constructor;
typedef uint32 AC_Ofst;
typedef uint32 State_ID;
typedef struct {
buf_header_t hdr; #ifdef VERIFY
ACS_Constructor* slow_impl;
#endif
uint32 buf_len;
AC_Ofst root_goto_ofst; AC_Ofst states_ofst_ofst; AC_Ofst first_state_ofst; uint16 root_goto_num; uint16 state_num;
} AC_Buffer;
typedef struct {
State_ID first_kid;
AC_Ofst fail_link;
short depth; unsigned short is_term; unsigned char goto_num; InputTy input_vect[1]; } AC_State;
class Buf_Allocator {
public:
Buf_Allocator() : _buf(0) {}
virtual ~Buf_Allocator() { free(); }
virtual AC_Buffer* alloc(int sz) = 0;
virtual void free() {};
protected:
AC_Buffer* _buf;
};
class AC_Converter {
public:
AC_Converter(ACS_Constructor& acs, Buf_Allocator& ba) :
_acs(acs), _buf_alloc(ba) {}
AC_Buffer* Convert();
private:
uint32 Calc_State_Sz(const ACS_State *) const;
State_ID Get_Renumbered_Id(const ACS_State *s) const {
const vector<uint32> &m = _id_map;
return m[s->Get_ID()];
}
AC_Buffer* Alloc_Buffer();
void Populate_Root_Goto_Func(AC_Buffer *, GotoVect&);
#ifdef DEBUG
void dump_buffer(AC_Buffer*, FILE*);
#endif
private:
ACS_Constructor& _acs;
Buf_Allocator& _buf_alloc;
vector<uint32> _id_map;
vector<AC_Ofst> _ofst_map;
};
ac_result_t Match(AC_Buffer* buf, const char* str, uint32 len);
ac_result_t Match_Longest_L(AC_Buffer* buf, const char* str, uint32 len);
#endif