cfsa1_impl.hpp
5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* File: cfsa1_impl.hpp
* Author: mlenart
*
* Created on November 4, 2013, 1:08 PM
*/
#ifndef CFSA1_IMPL_HPP
#define CFSA1_IMPL_HPP
#include <vector>
#include "fsa.hpp"
#include "../deserializationUtils.hpp"
using namespace std;
static const unsigned char CFSA1_ACCEPTING_FLAG = 128;
static const unsigned char CFSA1_ARRAY_FLAG = 64;
static const unsigned char CFSA1_TRANSITIONS_NUM_MASK = 63;
static const unsigned char CFSA1_OFFSET_SIZE_MASK = 3;
static const unsigned int CFSA1_INITIAL_ARRAY_STATE_OFFSET = 257;
struct StateData2 {
unsigned int transitionsNum;
bool isArray;
bool isAccepting;
};
struct TransitionData2 {
unsigned int offsetSize;
unsigned int shortLabel;
};
static inline StateData2 readStateData(const unsigned char*& ptr) {
StateData2 res;
unsigned char firstByte = readInt8(ptr);
res.isArray = firstByte & CFSA1_ARRAY_FLAG;
res.isAccepting = firstByte & CFSA1_ACCEPTING_FLAG;
res.transitionsNum = firstByte & CFSA1_TRANSITIONS_NUM_MASK;
if (res.transitionsNum == CFSA1_TRANSITIONS_NUM_MASK) {
res.transitionsNum = readInt8(ptr);
}
return res;
}
static inline TransitionData2 readTransitionFirstByte(const unsigned char*& ptr) {
TransitionData2 res;
unsigned char firstByte = readInt8(ptr);
res.offsetSize = firstByte & CFSA1_OFFSET_SIZE_MASK;
res.shortLabel = firstByte >> 2;
return res;
}
template <class T>
vector<unsigned char> CompressedFSA1<T>::initializeChar2PopularCharIdx(const unsigned char* ptr) {
return vector<unsigned char>(ptr, ptr + CFSA1_INITIAL_ARRAY_STATE_OFFSET);
}
template <class T>
CompressedFSA1<T>::CompressedFSA1(const unsigned char* ptr, const Deserializer<T>& deserializer)
: FSA<T>(ptr + CFSA1_INITIAL_ARRAY_STATE_OFFSET, deserializer),
label2ShortLabel(initializeChar2PopularCharIdx(ptr)) {
}
template <class T>
CompressedFSA1<T>::~CompressedFSA1() {
}
template <class T>
void CompressedFSA1<T>::reallyDoProceed(
const unsigned char* statePtr,
State<T>& state) const {
const unsigned char* currPtr = statePtr;
const StateData2 sd = readStateData(currPtr);
if (sd.isAccepting) {
T object;
long size = this->deserializer.deserialize(currPtr, object);
// long size = this->deserializer.deserialize(statePtr + 1, object);
state.setNext(statePtr - this->initialStatePtr, object, size);
}
else {
state.setNext(statePtr - this->initialStatePtr);
}
}
template <class T>
void CompressedFSA1<T>::doProceedToNextByList(
const char c,
const unsigned char shortLabel,
const unsigned char* ptr,
const unsigned int transitionsNum,
State<T>& state) const {
const unsigned char* currPtr = ptr;
// TransitionData* foundTransition = (TransitionData*) (fromPointer + transitionsTableOffset);
bool found = false;
TransitionData2 td;
for (unsigned int i = 0; i < transitionsNum; i++) {
td = readTransitionFirstByte(currPtr);
if (td.shortLabel == shortLabel) {
if (shortLabel == 0) {
char label = static_cast<char>(readInt8(currPtr));
if (label == c) {
found = true;
break;
}
else {
currPtr += td.offsetSize;
}
}
else {
found = true;
break;
}
}
else {
if (td.shortLabel == 0) {
currPtr++;
}
currPtr += td.offsetSize;
}
}
if (!found) {
state.setNextAsSink();
}
else {
uint32_t offset;
switch (td.offsetSize) {
case 0:
offset = 0;
break;
case 1:
offset = readInt8(currPtr);
break;
case 2:
offset = readInt16(currPtr);
break;
case 3:
offset = readInt16(currPtr);
offset <<= 8;
offset += readInt8(currPtr);
break;
}
currPtr += offset;
reallyDoProceed(currPtr, state);
}
}
template <class T>
void CompressedFSA1<T>::doProceedToNextByArray(
const unsigned char shortLabel,
const uint32_t* ptr,
State<T>& state) const {
uint32_t offset = ntohl(ptr[shortLabel]);
if (offset != 0) {
const unsigned char* currPtr = this->initialStatePtr + offset;
reallyDoProceed(currPtr, state);
}
else {
state.setNextAsSink();
}
}
template <class T>
void CompressedFSA1<T>::proceedToNext(const char c, State<T>& state) const {
const unsigned char* currPtr = this->initialStatePtr + state.getOffset();
unsigned char shortLabel = this->label2ShortLabel[(const unsigned char) c];
// unsigned long transitionsTableOffset = 1;
const StateData2 sd = readStateData(currPtr);
if (state.isAccepting()) {
currPtr += state.getValueSize();
}
if (sd.isArray) {
if (shortLabel > 0) {
this->doProceedToNextByArray(
shortLabel,
reinterpret_cast<const uint32_t*> (currPtr),
state);
}
else {
reallyDoProceed(currPtr + 256, state);
proceedToNext(c, state);
}
}
else {
this->doProceedToNextByList(
c,
shortLabel,
currPtr,
sd.transitionsNum,
state);
}
}
#endif /* CFSA1_IMPL_HPP */