-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfstlib.h
1818 lines (1530 loc) · 53.9 KB
/
fstlib.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// fstlib.h
//
// Copyright (c) 2018 Yuji Hirose. All rights reserved.
// MIT License
//
#ifndef INDEXLIB_FSTLIB_H_
#define INDEXLIB_FSTLIB_H_
#include <algorithm>
#include <cassert>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
//-----------------------------------------------------------------------------
// state machine
//-----------------------------------------------------------------------------
inline uint64_t MurmurHash64B(const void *key, size_t len, uint64_t seed) {
const uint32_t m = 0x5bd1e995;
const size_t r = 24;
uint32_t h1 = uint32_t(seed) ^ len;
uint32_t h2 = uint32_t(seed >> 32);
const uint32_t *data = (const uint32_t *)key;
while (len >= 8) {
uint32_t k1 = *data++;
k1 *= m;
k1 ^= k1 >> r;
k1 *= m;
h1 *= m;
h1 ^= k1;
len -= 4;
uint32_t k2 = *data++;
k2 *= m;
k2 ^= k2 >> r;
k2 *= m;
h2 *= m;
h2 ^= k2;
len -= 4;
}
if (len >= 4) {
uint32_t k1 = *data++;
k1 *= m;
k1 ^= k1 >> r;
k1 *= m;
h1 *= m;
h1 ^= k1;
len -= 4;
}
switch (len) {
case 3: h2 ^= ((unsigned char *)data)[2] << 16;
case 2: h2 ^= ((unsigned char *)data)[1] << 8;
case 1: h2 ^= ((unsigned char *)data)[0]; h2 *= m;
};
h1 ^= h2 >> 18;
h1 *= m;
h2 ^= h1 >> 22;
h2 *= m;
h1 ^= h2 >> 17;
h1 *= m;
h2 ^= h1 >> 19;
h2 *= m;
uint64_t h = h1;
h = (h << 32) | h2;
return h;
}
inline size_t get_prefix_length(const std::string &s1, const std::string &s2) {
size_t i = 0;
while (i < s1.length() && i < s2.length() && s1[i] == s2[i]) {
i++;
}
return i;
}
template <typename output_t> struct OutputTraits {};
template <> struct OutputTraits<uint32_t> {
typedef uint32_t value_type;
static value_type initial_value() { return 0; }
static bool empty(value_type val) { return val == 0; }
static std::string to_string(value_type val) { return std::to_string(val); }
static void prepend_value(value_type &base, value_type val) { base += val; }
static value_type get_suffix(value_type a, value_type b) { return a - b; }
static value_type get_common_previx(value_type a, value_type b) {
return std::min(a, b);
}
static void write_value(char *buff, size_t &buff_len, value_type val) {
memcpy(&buff[buff_len], &val, sizeof(val));
buff_len += sizeof(val);
}
};
template <> struct OutputTraits<size_t> {
typedef size_t value_type;
static value_type initial_value() { return 0; }
static bool empty(value_type val) { return val == 0; }
static std::string to_string(value_type val) { return std::to_string(val); }
static void prepend_value(value_type &base, value_type val) { base += val; }
static value_type get_suffix(value_type a, value_type b) { return a - b; }
static value_type get_common_previx(value_type a, value_type b) {
return std::min(a, b);
}
static void write_value(char *buff, size_t &buff_len, value_type val) {
memcpy(&buff[buff_len], &val, sizeof(val));
buff_len += sizeof(val);
}
};
template <> struct OutputTraits<std::string> {
typedef std::string value_type;
static value_type initial_value() { return value_type(); }
static bool empty(const value_type &val) { return val.empty(); }
static value_type to_string(const value_type &val) { return val; }
static void prepend_value(value_type &base, const value_type &val) {
base.insert(0, val);
}
static value_type get_suffix(const value_type &a, const value_type &b) {
return a.substr(b.size());
}
static value_type get_common_previx(const value_type &a,
const value_type &b) {
return a.substr(0, get_prefix_length(a, b));
}
static void write_value(char *buff, size_t &buff_len, const value_type &val) {
memcpy(&buff[buff_len], val.data(), val.size());
buff_len += val.size();
}
};
template <typename output_t> class State {
public:
typedef State *pointer;
struct Transition {
pointer state;
output_t output;
bool operator==(const Transition &rhs) const {
if (this != &rhs) { return state == rhs.state && output == rhs.output; }
return true;
}
};
class Transitions {
public:
// TODO:
std::vector<char> arcs;
std::vector<Transition> states_and_outputs;
std::vector<std::string> text;
bool operator==(const Transitions &rhs) const {
if (this != &rhs) {
return arcs == rhs.arcs && states_and_outputs == rhs.states_and_outputs;
}
return true;
}
size_t size() const { return arcs.size(); }
int get_index(char arc) const {
for (size_t i = 0; i < arcs.size(); i++) {
if (arcs[i] == arc) { return i; }
}
return -1;
}
pointer next_state(const char *start, const char *end,
size_t &read_bytes) const {
auto p = start;
auto arc = *p++;
auto idx = get_index(arc);
if (idx != -1) {
const auto &s = text[idx];
auto size = end - p;
if (s.empty() || (s.size() <= size && s == std::string(p, s.size()))) {
read_bytes = s.size() + 1;
return states_and_outputs[idx].state;
}
}
return nullptr;
}
const output_t &output(char arc) const {
auto idx = get_index(arc);
assert(idx != -1);
return states_and_outputs[idx].output;
}
template <typename Functor> void for_each(Functor fn) const {
for (auto i = 0u; i < arcs.size(); i++) {
fn(arcs[i], states_and_outputs[i], i);
}
}
template <typename Functor> void for_each_with_text(Functor fn) const {
for (auto i = 0u; i < arcs.size(); i++) {
fn(arcs[i], states_and_outputs[i], i, text[i]);
}
}
template <typename Functor> void for_each_reverse(Functor fn) const {
for (auto i = arcs.size(); i > 0; i--) {
auto idx = i - 1;
fn(arcs[idx], states_and_outputs[idx], idx);
}
}
template <typename Functor> void for_each_reverse_text(Functor fn) const {
for (auto i = arcs.size(); i > 0; i--) {
auto idx = i - 1;
fn(arcs[idx], states_and_outputs[idx], idx, text[idx]);
}
}
template <typename Functor> void for_each_arc(Functor fn) const {
for (auto arc : arcs) {
fn(arc);
}
}
private:
void clear() {
arcs.clear();
states_and_outputs.clear();
text.clear();
}
void set_transition(char arc, pointer state) {
auto idx = get_index(arc);
if (idx == -1) {
idx = arcs.size();
arcs.push_back(arc);
states_and_outputs.emplace_back(Transition());
text.push_back(std::string());
}
states_and_outputs[idx].state = state;
}
void set_output(char arc, const output_t &val) {
auto idx = get_index(arc);
states_and_outputs[idx].output = val;
}
void insert_output(char arc, const output_t &val) {
auto idx = get_index(arc);
auto &output = states_and_outputs[idx].output;
OutputTraits<output_t>::prepend_value(output, val);
}
friend class State;
};
size_t id;
bool in_dictionary;
bool final;
Transitions transitions;
std::vector<output_t> state_outputs;
size_t parent_count;
State(size_t id)
: id(id), in_dictionary(false), final(false), parent_count(0) {}
pointer next_state(const char *p, const char *end, size_t &read_bytes) const {
return transitions.next_state(p, end, read_bytes);
}
const output_t &output(char arc) const { return transitions.output(arc); }
bool operator==(const State &rhs) const {
if (this != &rhs) {
return final == rhs.final && transitions == rhs.transitions &&
state_outputs == rhs.state_outputs;
}
return true;
}
uint64_t hash() const;
void set_final(bool final) { this->final = final; }
void set_transition(char arc, pointer state) {
transitions.set_transition(arc, state);
}
void set_output(char arc, const output_t &output) {
transitions.set_output(arc, output);
}
void prepend_suffix_to_output(char arc, const output_t &suffix) {
transitions.insert_output(arc, suffix);
}
void push_to_state_outputs(const output_t &output) {
if (state_outputs.empty()) {
// NOTE: The following code makes good performance...
state_outputs.push_back(0);
}
state_outputs.push_back(output);
}
void prepend_suffix_to_state_outputs(const output_t &suffix) {
if (state_outputs.empty()) {
// NOTE: The following code makes good performance...
state_outputs.push_back(suffix);
} else {
for (auto &state_output : state_outputs) {
OutputTraits<output_t>::prepend_value(state_output, suffix);
}
}
}
void reuse(size_t state_id) {
id = state_id;
set_final(false);
transitions.clear();
state_outputs.clear();
}
static pointer New(std::vector<pointer> &objects, size_t state_id = -1) {
auto p = new State(state_id);
objects.push_back(p);
return p;
}
private:
State(const State &) = delete;
State(State &&) = delete;
};
template <typename output_t> inline uint64_t State<output_t>::hash() const {
char buff[2048]; // TOOD: large enough?
size_t buff_len = 0;
if (final) {
for (const auto &state_output : state_outputs) {
OutputTraits<output_t>::write_value(buff, buff_len, state_output);
buff[buff_len++] = '\t';
}
}
buff[buff_len++] = '\n';
transitions.for_each([&](char arc, const State::Transition &t, size_t i) {
buff[buff_len++] = arc;
uint32_t val = t.state->id;
memcpy(&buff[buff_len], &val, sizeof(val));
buff_len += sizeof(val);
OutputTraits<output_t>::write_value(buff, buff_len, t.output);
buff[buff_len++] = '\t';
});
return MurmurHash64B(buff, buff_len, 0);
}
template <typename output_t> class StateMachine {
public:
size_t last_id;
size_t count;
typename State<output_t>::pointer root;
StateMachine(std::vector<typename State<output_t>::pointer> &&objects,
size_t id, typename State<output_t>::pointer root)
: last_id(id), count(id), root(root),objects_(objects) {}
~StateMachine() {
for (auto p : objects_) {
delete p;
}
objects_.clear();
}
private:
StateMachine(const StateMachine &) = delete;
StateMachine(StateMachine &&) = delete;
std::vector<typename State<output_t>::pointer> objects_;
};
// NOTE: unordered_set is not used here, because it uses size_t as hash value
// which becomes 32-bit on 32-bit platforms. But we want to use 64-bit hash
// value.
template <typename output_t>
using Dictionary =
std::unordered_map<uint64_t, typename State<output_t>::pointer>;
template <typename output_t>
inline typename State<output_t>::pointer
find_minimized(typename State<output_t>::pointer state,
Dictionary<output_t> &dictionary, size_t &state_id,
bool &found) {
auto h = state->hash();
auto it = dictionary.find(h);
if (it != dictionary.end()) {
if (*it->second == *state) {
state_id--;
found = true;
return it->second;
}
}
// NOTE: COPY_STATE is very expensive...
state->in_dictionary = true;
dictionary[h] = state;
found = false;
return state;
};
template <typename output_t>
inline void get_common_prefix_and_word_suffix(const output_t ¤t_output,
const output_t &output,
output_t &common_prefix,
output_t &word_suffix) {
common_prefix =
OutputTraits<output_t>::get_common_previx(output, current_output);
word_suffix = OutputTraits<output_t>::get_suffix(output, common_prefix);
}
template <typename output_t, typename Input>
inline std::shared_ptr<StateMachine<output_t>> make_state_machine(Input input) {
Dictionary<output_t> dictionary;
std::vector<typename State<output_t>::pointer> objects;
size_t state_id = 0;
// Main algorithm ported from the technical paper
std::vector<typename State<output_t>::pointer> temp_states;
std::string previous_word;
temp_states.push_back(State<output_t>::New(objects, state_id++));
input([&](const std::string ¤t_word, output_t current_output) {
// The following loop caluculates the length of the longest common
// prefix of 'current_word' and 'previous_word'
auto prefix_length = get_prefix_length(previous_word, current_word);
// We minimize the states from the suffix of the previous word
typename State<output_t>::pointer prev_state = nullptr;
for (auto i = previous_word.length(); i > prefix_length; i--) {
auto arc = previous_word[i - 1];
bool found;
auto state =
find_minimized<output_t>(temp_states[i], dictionary, state_id, found);
if (found) {
if (prev_state) { prev_state->parent_count--; }
} else {
// Ownership of the object in temp_states[i] has been moved to the
// dictionary...
temp_states[i] = State<output_t>::New(objects);
}
state->parent_count++;
temp_states[i - 1]->set_transition(arc, state);
prev_state = state;
}
// This loop initializes the tail states for the current word
for (auto i = prefix_length + 1; i <= current_word.length(); i++) {
assert(i <= temp_states.size());
if (i == temp_states.size()) {
temp_states.push_back(State<output_t>::New(objects, state_id++));
} else {
temp_states[i]->reuse(state_id++);
}
auto arc = current_word[i - 1];
temp_states[i - 1]->set_transition(arc, temp_states[i]);
}
if (current_word != previous_word) {
auto state = temp_states[current_word.length()];
state->set_final(true);
// NOTE: The following code causes bad performance...
// state->push_to_state_outputs("");
}
for (auto j = 1u; j <= prefix_length; j++) {
auto prev_state = temp_states[j - 1];
auto arc = current_word[j - 1];
const auto &output = prev_state->output(arc);
auto common_prefix = OutputTraits<output_t>::initial_value();
auto word_suffix = OutputTraits<output_t>::initial_value();
get_common_prefix_and_word_suffix(current_output, output, common_prefix,
word_suffix);
prev_state->set_output(arc, common_prefix);
if (!OutputTraits<output_t>::empty(word_suffix)) {
auto state = temp_states[j];
state->transitions.for_each_arc([&](char arc) {
state->prepend_suffix_to_output(arc, word_suffix);
});
if (state->final) {
state->prepend_suffix_to_state_outputs(word_suffix);
}
}
current_output =
OutputTraits<output_t>::get_suffix(current_output, common_prefix);
}
if (current_word == previous_word) {
auto state = temp_states[current_word.length()];
state->push_to_state_outputs(current_output);
} else {
auto state = temp_states[prefix_length];
auto arc = current_word[prefix_length];
state->set_output(arc, current_output);
}
previous_word = current_word;
});
// Here we are minimizing the states of the last word
typename State<output_t>::pointer prev_state = nullptr;
for (auto i = previous_word.length(); i > 0; i--) {
auto arc = previous_word[i - 1];
bool found;
auto state =
find_minimized<output_t>(temp_states[i], dictionary, state_id, found);
if (found) {
if (prev_state) { prev_state->parent_count--; }
}
state->parent_count++;
temp_states[i - 1]->set_transition(arc, state);
prev_state = state;
}
bool found;
auto root =
find_minimized<output_t>(temp_states[0], dictionary, state_id, found);
return std::make_shared<StateMachine<output_t>>(std::move(objects), state_id,
root);
}
template <typename output_t>
inline std::shared_ptr<StateMachine<output_t>>
make_state_machine(const std::vector<std::pair<std::string, output_t>> &input) {
return make_state_machine<output_t>([&](const auto &add_entry) {
for (const auto &item : input) {
add_entry(item.first, item.second);
}
});
}
template <class output_t>
inline bool connectable(typename State<output_t>::pointer state) {
if (state->final) { return false; }
if (state->transitions.size() > 1) { return false; }
if (state->parent_count > 1) { return false; }
return true;
}
template <class output_t>
inline void optimize_core(StateMachine<output_t> &sm,
typename State<output_t>::pointer state,
std::set<size_t> &check) {
auto id = state->id;
if (check.find(id) != check.end()) { return; }
check.insert(id);
for (auto i = 0u; i < state->transitions.arcs.size(); i++) {
auto &t = state->transitions.states_and_outputs[i];
while (connectable<output_t>(t.state)) {
state->transitions.text[i] += t.state->transitions.arcs[0];
t.state = t.state->transitions.states_and_outputs[0].state;
sm.count--;
}
optimize_core<output_t>(sm, t.state, check);
}
}
template <typename output_t> inline void optimize(StateMachine<output_t> &sm) {
std::set<size_t> check;
optimize_core<output_t>(sm, sm.root, check);
}
//-----------------------------------------------------------------------------
// virtual machine
//-----------------------------------------------------------------------------
const size_t DEFAULT_MIN_ARCS_FOR_JUMP_TABLE = 6;
template <typename Val> inline size_t vb_encode_value_length(Val n) {
size_t len = 0;
while (n >= 128) {
len++;
n >>= 7;
}
len++;
return len;
}
template <typename Val> inline size_t vb_encode_value(Val n, char *out) {
size_t len = 0;
while (n >= 128) {
out[len] = (char)(n & 0x7f);
len++;
n >>= 7;
}
out[len] = (char)(n + 128);
len++;
return len;
}
template <typename Val>
inline size_t vb_decode_value(const char *data, Val &n) {
auto p = (const uint8_t *)data;
size_t len = 0;
n = 0;
size_t cnt = 0;
while (p[len] < 128) {
n += (p[len++] << (7 * cnt++));
}
n += (p[len++] - 128) << (7 * cnt);
return len;
}
union Ope {
enum OpeType { ArcNotFinal = 0, ArcFinal, ArcFinalWithStateOutput, Jmp };
enum JumpOffsetType { JumpOffsetNone = 0, JumpOffsetZero, JumpOffsetCurrent };
enum OutputLengthType {
OutputLengthNone = 0,
OutputLengthOne,
OutputLengthTwo,
OutputLength
};
struct {
unsigned type : 2;
unsigned has_text : 1;
unsigned last_transition : 1;
unsigned output_length_type : 2;
unsigned jump_offset_type : 2;
} arc;
struct {
unsigned type : 2;
unsigned need_2byte : 1;
unsigned reserve : 5;
} jmp;
uint8_t byte;
};
inline bool is_ope_arc(Ope ope) { return ope.jmp.type != Ope::Jmp; }
inline bool is_ope_jmp(Ope ope) { return ope.jmp.type == Ope::Jmp; }
inline bool is_ope_final(Ope ope) {
assert(ope.arc.type != Ope::Jmp);
return ope.arc.type != Ope::ArcNotFinal;
}
inline bool has_ope_state_outputs(Ope ope) {
return ope.arc.type == Ope::ArcFinalWithStateOutput;
}
template <typename output_t> struct Command {
// General
size_t id = -1;
size_t next_id = -1;
bool is_arc;
// Arc
bool final;
bool last_transition;
char arc;
std::string text;
Ope::OutputLengthType output_length_type;
output_t output;
std::vector<output_t> state_outputs;
Ope::JumpOffsetType jump_offset_type;
size_t jump_offset;
bool use_jump_table;
// Jmp
std::vector<int16_t> arc_jump_offsets;
size_t output_length(uint32_t output) const {
if (output > 0xffff) {
return sizeof(output_t);
} else if (output > 0xff) {
return sizeof(uint16_t);
} else if (output > 0) {
return sizeof(uint8_t);
}
return 0;
}
size_t output_length(size_t output) const {
//if (output > 0xffffffff) {
if (output > 0xffff) {
return sizeof(output_t);
//} else if (output > 0xffff) {
//return sizeof(uint32_t);
} else if (output > 0xff) {
return sizeof(uint16_t);
} else if (output > 0) {
return sizeof(uint8_t);
}
return 0;
}
size_t output_length(const std::string &output) const {
size_t size = 0;
if (output.length() > 2) { size += sizeof(uint8_t); }
size += output.length();
return size;
}
size_t state_output_length(uint32_t state_output) const {
return sizeof(state_output);
}
size_t state_output_length(size_t state_output) const {
return sizeof(state_output);
}
size_t state_output_length(const std::string &state_output) const {
return sizeof(uint8_t) + state_output.length();
}
size_t byte_code_size() const {
// ope
auto size = sizeof(uint8_t);
if (is_arc) {
// arc
if (!use_jump_table) { size += sizeof(uint8_t); }
// text
if (!text.empty()) {
size += sizeof(uint8_t);
size += text.size();
}
// output
size += output_length(output);
// state_outputs
if (has_state_outputs()) {
size += sizeof(uint8_t);
for (const auto &state_output : state_outputs) {
size += state_output_length(state_output);
}
}
// jump_offset
if (jump_offset_type == Ope::JumpOffsetCurrent) {
size += vb_encode_value_length(jump_offset);
}
} else { // type == Jmp
// arc_jump_offsets
bool need_2byte;
size_t start;
size_t end;
scan_arc_jump_offsets(need_2byte, start, end);
if (need_2byte) {
size += 2 + sizeof(int16_t) * (end - start);
} else {
size += 2 + sizeof(uint8_t) * (end - start);
}
}
return size;
}
void write_output(std::vector<char> &byte_code, uint32_t output) const {
if (output > 0xffff) {
byte_code.insert(byte_code.end(), reinterpret_cast<const char *>(&output),
reinterpret_cast<const char *>(&output) +
sizeof(output));
} else if (output > 0xff) {
uint16_t val = output;
byte_code.insert(byte_code.end(), reinterpret_cast<const char *>(&val),
reinterpret_cast<const char *>(&val) + sizeof(val));
} else if (output > 0) {
uint8_t val = output;
byte_code.insert(byte_code.end(), reinterpret_cast<const char *>(&val),
reinterpret_cast<const char *>(&val) + sizeof(val));
}
}
void write_output(std::vector<char> &byte_code, size_t output) const {
//if (output > 0xffffffff) {
if (output > 0xffff) {
byte_code.insert(byte_code.end(), reinterpret_cast<const char *>(&output),
reinterpret_cast<const char *>(&output) +
sizeof(output));
//} else if (output > 0xffff) {
// uint32_t val = output;
// byte_code.insert(byte_code.end(), reinterpret_cast<const char *>(&output),
// reinterpret_cast<const char *>(&output) +
// sizeof(val));
} else if (output > 0xff) {
uint16_t val = output;
byte_code.insert(byte_code.end(), reinterpret_cast<const char *>(&val),
reinterpret_cast<const char *>(&val) + sizeof(val));
} else if (output > 0) {
uint8_t val = output;
byte_code.insert(byte_code.end(), reinterpret_cast<const char *>(&val),
reinterpret_cast<const char *>(&val) + sizeof(val));
}
}
void write_output(std::vector<char> &byte_code,
const std::string &output) const {
if (output.length() > 2) { byte_code.push_back((char)output.length()); }
byte_code.insert(byte_code.end(), output.begin(), output.end());
}
void write_state_output(std::vector<char> &byte_code,
uint32_t state_output) const {
byte_code.insert(
byte_code.end(), reinterpret_cast<const char *>(&state_output),
reinterpret_cast<const char *>(&state_output) + sizeof(state_output));
}
void write_state_output(std::vector<char> &byte_code,
size_t state_output) const {
byte_code.insert(
byte_code.end(), reinterpret_cast<const char *>(&state_output),
reinterpret_cast<const char *>(&state_output) + sizeof(state_output));
}
void write_state_output(std::vector<char> &byte_code,
const std::string &state_output) const {
byte_code.push_back((char)state_output.length());
byte_code.insert(byte_code.end(), state_output.begin(), state_output.end());
}
void write_byte_code(std::vector<char> &byte_code) const {
auto save_size = byte_code.size();
if (is_arc) {
// ope
assert(final || (!final && !has_state_outputs()));
auto t = Ope::ArcNotFinal;
if (final) {
if (has_state_outputs()) {
t = Ope::ArcFinalWithStateOutput;
} else {
t = Ope::ArcFinal;
}
}
Ope ope = {t, !text.empty(), last_transition,
(unsigned int)output_length_type,
(unsigned int)jump_offset_type};
byte_code.push_back(ope.byte);
// arc
if (!use_jump_table) { byte_code.push_back(arc); }
// text
if (!text.empty()) {
byte_code.push_back((char)text.size());
byte_code.insert(byte_code.end(), text.begin(), text.end());
}
// output
write_output(byte_code, output);
// state_outputs
if (has_state_outputs()) {
byte_code.push_back((char)state_outputs.size());
for (const auto &state_output : state_outputs) {
write_state_output(byte_code, state_output);
}
}
// jump_offset
if (jump_offset_type == Ope::JumpOffsetCurrent) {
char vb[9]; // To hold 64 bits value
auto vb_len = vb_encode_value(jump_offset, vb);
byte_code.insert(byte_code.end(), vb, vb + vb_len);
}
} else { // type == Jmp
bool need_2byte;
size_t start;
size_t end;
scan_arc_jump_offsets(need_2byte, start, end);
// ope
Ope ope = {Ope::Jmp, need_2byte, 0};
byte_code.push_back(ope.byte);
// arc_jump_offsets
auto count = end - start;
byte_code.push_back((unsigned char)start);
byte_code.push_back(
(unsigned char)(count - 1)); // count is stored from 0 to 255
if (need_2byte) {
auto p =
(const char *)arc_jump_offsets.data() + (sizeof(int16_t) * start);
auto table_size = sizeof(int16_t) * count;
byte_code.insert(byte_code.end(), p, p + table_size);
} else {
for (auto i = start; i < end; i++) {
auto offset = arc_jump_offsets[i];
byte_code.push_back((unsigned char)offset);
}
}
}
assert(byte_code.size() - save_size == byte_code_size());
}
private:
bool has_state_outputs() const { return !state_outputs.empty(); }
void scan_arc_jump_offsets(bool &need_2byte, size_t &start,
size_t &end) const {
need_2byte = false;
start = -1;
end = -1;
for (auto i = 0; i < 256; i++) {
auto offset = arc_jump_offsets[i];
if (offset != -1) {
if (offset >= 0xff) { need_2byte = true; }
//if (start == -1) { start = i; }
if ((size_t)-1 == start) { start = i; }
end = i + 1;
}
}
}
};
template <class output_t> using Commands = std::vector<Command<output_t>>;
template <class output_t>
inline size_t compile_core(typename State<output_t>::pointer state,
Commands<output_t> &commands,
std::vector<size_t> &state_positions,
size_t position, size_t min_arcs_for_jump_table) {
assert(state->transitions.arcs.size() > 0);
if (state_positions[state->id]) { return position; }
struct Helper {
static size_t output_length(uint32_t output) {
auto output_len = 0;
if (output > 0xffff) {
output_len = sizeof(output_t);
} else if (output > 0xff) {
output_len = sizeof(uint16_t);
} else if (output > 0) {
output_len = sizeof(uint8_t);