/
unpacked_state.h
329 lines (277 loc) · 8.98 KB
/
unpacked_state.h
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* * keyvi - A key value store.
*
* Copyright 2015 Hendrik Muhs<hendrik.muhs@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* unpacked_state.h
*
* Created on: May 2, 2014
* Author: hendrik
*/
#ifndef UNPACKED_STATE_H_
#define UNPACKED_STATE_H_
#include "dictionary/fsa/internal/constants.h"
#include "dictionary/fsa/internal/packed_state.h"
#include "dictionary/fsa/internal/bit_vector.h"
#include "dictionary/fsa/internal/sparse_array_persistence.h"
#include "dictionary/util/vint.h"
//#define ENABLE_TRACING
#include "dictionary/util/trace.h"
namespace keyvi {
namespace dictionary {
namespace fsa {
namespace internal {
template<class PersistenceT>
class UnpackedState
final {
public:
struct Transition
final {
public:
void set(int label, uint64_t value) {
this->label = label;
this->value = value;
}
//private:
int label;
uint64_t value;
};
UnpackedState(PersistenceT* persistence)
: persistence_(persistence) {
}
UnpackedState(UnpackedState&& other):
outgoing_(std::move(other.outgoing_)),
bitvector_(std::move(other.bitvector_)),
persistence_(other.persistence_),
used_(other.used_),
hashcode_(other.hashcode_),
no_minimization_counter_(other.no_minimization_counter_),
weight_(other.weight_),
zerobyte_state_ (other.zerobyte_state_),
zerobyte_label_ (other.zerobyte_label_),
final_(other.final_)
{
other.persistence_ = 0;
other.used_ = 0;
other.hashcode_ = 0;
other.no_minimization_counter_ = 0;
other.weight_ = 0;
other.zerobyte_state_ = 0;
other.zerobyte_label_ = 0xff;
other.final_ = false;
}
UnpackedState& operator=(UnpackedState&& other) {
outgoing_(std::move(other.outgoing_)),
bitvector_ = std::move(other.bitvector_);
persistence_ = other.persistence_;
used_ = other.used_;
hashcode_ = other.hashcode_;
no_minimization_counter_ = other.no_minimization_counter_;
weight_ = other.weight_;
zerobyte_state_ = other.zerobyte_state_;
zerobyte_label_ = other.zerobyte_label_;
final_ = other.final_;
other.persistence_ = 0;
other.used_ = 0;
other.hashcode_ = 0;
other.no_minimization_counter_ = 0;
other.weight_ = 0;
other.zerobyte_state_ = 0;
other.zerobyte_label_ = 0xff;
other.final_ = false;
return *this;
}
~UnpackedState() {
}
bool IsFinal() const {
return final_;
}
void Add(int transition_label, uint64_t transition_value) {
TRACE("UnpackedState: Adding label %d to %d", transition_label, transition_value);
outgoing_[used_++].set(transition_label, transition_value);
bitvector_.Set(transition_label);
}
bool HasLabel(int transition_label) const {
return bitvector_.Get(transition_label);
}
void AddFinalState(uint64_t transition_value);
void SetTransitionValue(uint64_t transition_value){
outgoing_[used_ - 1].value = transition_value;
}
void Clear() {
used_ = 0;
hashcode_ = -1;
bitvector_.Clear();
no_minimization_counter_ = 0;
weight_ = 0;
zerobyte_state_ = 0;
zerobyte_label_ = 0xff;
final_ = false;
}
const BitVector<MAX_TRANSITIONS_OF_A_STATE>& get_BitVector() const {
return bitvector_;
}
size_t size() const {
return used_;
}
void IncrementNoMinimizationCounter(int counter = 1) {
no_minimization_counter_ += counter;
}
int GetNoMinimizationCounter() const {
return no_minimization_counter_;
}
void UpdateWeightIfHigher(uint32_t weight);
uint32_t GetWeight() const{
return weight_;
}
void SetZerobyteState(size_t position) {
zerobyte_state_ = position;
}
size_t GetZerobyteState() const {
return zerobyte_state_;
}
void SetZerobyteLabel(unsigned char label) {
zerobyte_label_ = label;
}
unsigned char GetZerobyteLabel() const {
return zerobyte_label_;
}
inline int64_t GetHashcode() {
if (hashcode_ == -1) {
int64_t b;
int64_t a = b = 0x9e3779b9;
int64_t c = weight_ > 0 ? 1 : 0;
int sz = used_;
for (int i = 0; i < sz; ++i) {
Transition t = outgoing_[i];
a += t.label;
b += t.value;
if (i < sz - 1) {
++i;
t = outgoing_[i];
a += t.label << 16;
b += t.value << 16;
}
// good old Bob Jenkins Hash
a -= b;
a -= c;
a ^= c >> 13;
b -= c;
b -= a;
b ^= a << 8;
c -= a;
c -= b;
c ^= b >> 13;
a -= b;
a -= c;
a ^= c >> 12;
b -= c;
b -= a;
b ^= a << 16;
c -= a;
c -= b;
c ^= b >> 5;
a -= b;
a -= c;
a ^= c >> 3;
b -= c;
b -= a;
b ^= a << 10;
c -= a;
c -= b;
c ^= b >> 15;
}
hashcode_ = c;
}
return hashcode_;
}
template<class OffsetTypeT, class HashCodeTypeT>
bool operator==(const PackedState<OffsetTypeT, HashCodeTypeT>& l) {
// First filter - check if hash code and the number of transitions is the same
if (l.GetHashcode() != static_cast<HashCodeTypeT>(GetHashcode())
|| l.GetNumberOfOutgoingTransitions() != used_) {
return false;
}
// The number of transitions is the same. Verify that they also look the same.
for (int i = 0; i < used_; ++i) {
Transition t = outgoing_[i];
int label = t.label;
if (label < FINAL_OFFSET_TRANSITION) {
// Is there a transition of this kind?
if (persistence_->ReadTransitionLabel(l.GetOffset() + label) != label) {
return false;
}
// Does this transition lead to the same target state?
uint64_t target = persistence_->ReadTransitionValue(l.GetOffset() + label);
target = persistence_->ResolveTransitionValue(l.GetOffset() + label, target);
if (t.value != target) {
return false;
}
} else // (label == FINAL_OFFSET_TRANSITION)
{
if (persistence_->ReadTransitionLabel(l.GetOffset() + label) != FINAL_OFFSET_CODE) {
return false;
}
// check if l has final info
const uint64_t value = persistence_->ReadFinalValue(l.GetOffset());
if (t.value != value) {
return false;
}
}
//// This transition is ok.
}
// note: we do not compare the weight entry because if one state has weight and the other not the hashes differ
// all checks succeeded, states must be equal.
return true;
}
const Transition& operator[](int position) const {
return outgoing_[position];
}
private:
std::array<Transition, MAX_TRANSITIONS_OF_A_STATE> outgoing_;
BitVector<MAX_TRANSITIONS_OF_A_STATE> bitvector_;
PersistenceT* persistence_;
int used_ = 0;
int64_t hashcode_ = -1;
int no_minimization_counter_ = 0;
uint32_t weight_ = 0;
size_t zerobyte_state_ = 0;
unsigned char zerobyte_label_ = 0xff;
bool final_ = false;
};
template<>
inline void UnpackedState<SparseArrayPersistence<uint16_t>>::AddFinalState(uint64_t transition_value)
{
TRACE("UnpackedState: Adding final state %d", transition_value);
outgoing_[used_++].set(FINAL_OFFSET_TRANSITION, transition_value);
size_t vshort_size = util::getVarshortLength(transition_value);
for (size_t i=0; i < vshort_size; ++i) {
bitvector_.Set(FINAL_OFFSET_TRANSITION + i);
}
final_ = true;
}
template<>
inline void UnpackedState<SparseArrayPersistence<uint16_t>>::UpdateWeightIfHigher(uint32_t weight)
{
if (weight > weight_){
weight_ = weight;
bitvector_.Set(INNER_WEIGHT_TRANSITION_COMPACT);
}
}
} /* namespace internal */
} /* namespace fsa */
} /* namespace dictionary */
} /* namespace keyvi */
#endif /* UNPACKED_STATE_H_ */