-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathpattern.cpp
More file actions
2176 lines (2014 loc) · 69.2 KB
/
pattern.cpp
File metadata and controls
2176 lines (2014 loc) · 69.2 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
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
#include "pattern.h"
#include "patternstore.h"
#include "SpookyV2.h" //spooky hash
#include <cstring>
#include "algorithms.h"
/*****************************
* Colibri Core
* by Maarten van Gompel
* Centre for Language Studies
* Radboud University Nijmegen
*
*
* http://proycon.github.io/colibri-core
*
* Licensed under GPLv3
*****************************/
using namespace std;
unsigned char mainpatternbuffer[MAINPATTERNBUFFERSIZE + 1];
PatternCategory datacategory(const unsigned char* data, int maxbytes = 0) {
PatternCategory category = NGRAM;
if (data == NULL)
return category;
int i = 0;
unsigned int length;
do {
if ((maxbytes > 0) && (i >= maxbytes))
return category;
const unsigned int cls = bytestoint(data + i, &length);
i += length;
if ((maxbytes == 0) && (cls == ClassDecoder::delimiterclass)) {
//end marker
return category;
} else if (cls == ClassDecoder::skipclass) {
return SKIPGRAM;
} else if (cls == ClassDecoder::flexclass) {
return FLEXGRAM;
}
} while (1);
}
PatternCategory Pattern::category() const {
return datacategory(data);
}
PatternCategory PatternPointer::category() const {
if (mask == 0) {
return datacategory(data, bytes);
} else if (mask > B32 / 2) {
return FLEXGRAM;
} else {
return SKIPGRAM;
}
}
size_t Pattern::bytesize() const {
//return the size of the pattern (in bytes)
if (data == NULL)
return 0;
size_t i = 0;
bool prevhigh = false;
do {
if ((!prevhigh) && (data[i] == ClassDecoder::delimiterclass)) { //end marker
return i;
}
prevhigh = (data[i] >= 128);
++i;
} while (1);
}
size_t datasize(const unsigned char* data, size_t maxbytes = 0) {
//return the size of the pattern (in tokens)
if (data == NULL)
return 0;
size_t i = 0;
size_t n = 0;
bool prevhigh = false;
do {
if ((maxbytes > 0) && (i >= maxbytes)) {
return n;
}
if ((!prevhigh) && (data[i] == ClassDecoder::delimiterclass)) {
if (maxbytes == 0) {
return n;
} else {
++n;
}
} else if (data[i] < 128) {
++n;
}
prevhigh = (data[i] >= 128);
++i;
} while (1);
}
size_t Pattern::n() const {
return datasize(data);
}
size_t PatternPointer::n() const {
return datasize(data, bytes);
}
bool Pattern::isgap(int index) const { //is the word at this position a gap?
if (data == NULL)
return false;
int i = 0;
int _n = 0;
bool prevhigh = false;
do {
const unsigned char c = data[i];
if ((!prevhigh) && (c == ClassDecoder::delimiterclass)) {
//end marker
return false;
} else if ((!prevhigh) && ((c == ClassDecoder::skipclass) || (c == ClassDecoder::flexclass))) {
//FLEXMARKER is counted as 1, the minimum fill
if (_n == index)
return true;
++i;
++_n;
prevhigh = false;
} else if (c < 128) {
//we have a size
if (_n == index)
return false;
++i;
++_n;
prevhigh = false;
} else {
prevhigh = true;
++i;
}
} while (1);
}
bool PatternPointer::isgap(int index) const { //is the word at this position a gap?
if ((mask == 0) || (index > 30))
return false;
return (mask & bitmask[index]);
}
Pattern Pattern::toflexgram() const { //converts a fixed skipgram into a dynamic one, ngrams just come out unchanged
//to be computed in bytes
if (data == NULL)
return *this;
int i = 0;
int j = 0;
int copybytes = 0;
bool skipgap = false;
bool prevhigh = false;
do {
const unsigned char c = data[i++];
if (j >= MAINPATTERNBUFFERSIZE) {
std::cerr << "ERROR: toflexgram(): Patternbuffer size exceeded" << std::endl;
throw InternalError();
}
if (copybytes) {
mainpatternbuffer[j++] = c;
copybytes--;
} else if ((!prevhigh) && (c == ClassDecoder::delimiterclass)) {
mainpatternbuffer[j++] = c;
break;
} else if ((!prevhigh) && (c == ClassDecoder::skipclass)) {
if (!skipgap) {
mainpatternbuffer[j++] = ClassDecoder::flexclass; //store a DYNAMIC GAP instead
skipgap = true; //skip next consecutive gap markers
}
} else {
mainpatternbuffer[j++] = c;
skipgap = false;
}
prevhigh = (c >= 128);
} while (1);
//copy from mainpatternbuffer
return Pattern(mainpatternbuffer, j - 1);
}
PatternPointer PatternPointer::toflexgram() const { //converts a fixed skipgram into a flexgram, ngrams just come out unchanged
PatternPointer copy = *this;
if (mask != 0)
copy.mask = mask | (unsigned(1) << 31);
return copy;
}
uint32_t Pattern::getmask() const {
uint32_t temp_mask = 0;
unsigned char _n = 0;
bool isflex = false;
const unsigned char bytes = (unsigned char)bytesize();
for (unsigned char i = 0; (i < bytes) && (_n < 31); ++i) {
if (data[i] < 128) {
if ((i == 0) || (data[i - 1] < 128)) {
if (data[i] == ClassDecoder::flexclass) {
isflex = true;
temp_mask |= (1 << _n);
} else if (data[i] == ClassDecoder::skipclass) {
temp_mask |= (1 << _n);
}
}
++_n;
}
}
if (isflex)
temp_mask |= (unsigned(1) << 31);
return temp_mask;
}
uint32_t PatternPointer::computemask() const {
uint32_t temp_mask = 0;
unsigned char _n = 0;
bool isflex = false;
for (unsigned char i = 0; (i < (unsigned char)bytes) && (_n < 31); ++i) {
if (data[i] < 128) {
if ((i == 0) || (data[i - 1] < 128)) {
if (data[i] == ClassDecoder::flexclass) {
isflex = true;
temp_mask |= (1 << _n);
} else if (data[i] == ClassDecoder::skipclass) {
temp_mask |= (1 << _n);
}
}
++_n;
}
}
if (isflex)
temp_mask |= (unsigned(1) << 31);
return temp_mask;
}
size_t Pattern::hash() const {
if (data == NULL || data[0] == 0)
return 0;
return SpookyHash::Hash64((const void*)data, bytesize());
}
size_t PatternPointer::hash() const {
if ((data == NULL) || (data[0] == 0))
return 0;
if (mask == 0) {
return SpookyHash::Hash64((const void*)data, bytesize());
} else if (isflexgram()) {
//hashing skipgrams/flexgrams is a bit more expensive cause we need to process the mask before we can compute the hash:
unsigned char datacopy[bytes + 1];
int size = flexcollapse(datacopy);
datacopy[size] = ClassDecoder::delimiterclass;
return SpookyHash::Hash64((const void*)datacopy, size + 1);
} else {
//hashing skipgrams/flexgrams is a bit more expensive cause we need to process the mask before we can compute the hash:
unsigned char datacopy[bytes + 1];
memcpy(datacopy, data, bytes);
datacopy[bytes] = ClassDecoder::delimiterclass;
PPSizeType _n = 0;
for (PPSizeType i = 0; i < bytes; ++i) {
if (datacopy[i] < 128) {
if (isgap(_n))
datacopy[i] = ClassDecoder::skipclass;
++_n;
}
}
return SpookyHash::Hash64((const void*)datacopy, bytes);
}
}
void Pattern::write(ostream& out, const unsigned char*) const {
//corpusstart is not used but does need to be present so we have the same signature as PatternPointer
const size_t s = bytesize();
if (s > 0) {
out.write((char*)data, s + 1); //+1 to include the \0 marker
} else {
const unsigned char null = 0;
out.write((char*)&null, 1); //marker only
}
}
void PatternPointer::write(ostream& out, const unsigned char* corpusstart) const {
if (corpusstart != NULL) {
const unsigned int offset = data - corpusstart;
out.write((char*)&offset, sizeof(unsigned int));
out.write((char*)&bytes, sizeof(PPSizeType));
out.write((char*)&mask, sizeof(uint32_t));
} else {
const size_t s = bytesize();
if (s > 0)
out.write((char*)data, s);
const unsigned char null = 0;
out.write((char*)&null, 1); //marker
}
}
std::string datatostring(const unsigned char* data, const ClassDecoder& classdecoder, int maxbytes = 0) {
std::string result = "";
if (data == NULL)
return result;
int i = 0;
unsigned int length;
do {
if ((maxbytes > 0) && (i >= maxbytes)) {
return result;
}
const unsigned int cls = bytestoint(data + i, &length);
if (length == 0) {
cerr << "ERROR: Class length==0, shouldn't happen" << endl;
throw InternalError();
}
i += length;
if ((maxbytes == 0) && (cls == ClassDecoder::delimiterclass)) {
return result;
} else if (classdecoder.hasclass(cls)) {
if (!result.empty())
result += " ";
result += classdecoder[cls];
} else {
if (!result.empty())
result += " ";
result += "{?}";
}
} while (1);
return result;
}
std::string Pattern::tostring(const ClassDecoder& classdecoder) const {
return datatostring(data, classdecoder);
}
std::string PatternPointer::tostring(const ClassDecoder& classdecoder) const {
std::string result = "";
unsigned int i = 0;
unsigned int _n = 0;
unsigned int length;
bool flex = false;
if (mask != 0)
flex = isflexgram();
do {
if ((bytes > 0) && (i >= bytes)) {
return result;
}
unsigned int cls = bytestoint(data + i, &length);
if ((mask != 0) && (isgap(_n))) {
if (flex) {
cls = ClassDecoder::flexclass;
} else {
cls = ClassDecoder::skipclass;
}
}
++_n;
if (length == 0) {
cerr << "ERROR: Class length==0, shouldn't happen" << endl;
throw InternalError();
}
i += length;
if ((bytes == 0) && (cls == ClassDecoder::delimiterclass)) {
return result;
} else if (classdecoder.hasclass(cls)) {
if (!result.empty())
result += " ";
result += classdecoder[cls];
} else {
if (!result.empty())
result += " ";
result += "{?}";
}
} while (1);
return result;
}
bool dataout(const unsigned char* data, int maxbytes = 0) {
if (data == NULL)
return true;
int i = 0;
unsigned int length;
do {
if ((maxbytes > 0) && (i >= maxbytes)) {
return true;
}
const unsigned int cls = bytestoint(data + i, &length);
i += length;
if ((maxbytes == 0) && (cls == ClassDecoder::delimiterclass)) {
cerr << endl;
return true;
} else {
cerr << cls << " ";
}
} while (1);
return false;
}
bool Pattern::out() const {
return dataout(data);
}
bool PatternPointer::out() const {
return dataout(data, bytesize());
}
bool Pattern::unknown() const {
if (data == NULL)
return false;
int i = 0;
bool prevhigh = false;
do {
if ((!prevhigh) && (data[i] == ClassDecoder::unknownclass)) {
return true;
} else if ((!prevhigh) && (data[i] == ClassDecoder::delimiterclass)) {
return false;
}
prevhigh = (data[i] >= 128);
++i;
} while (1);
}
bool PatternPointer::unknown() const {
if (data == NULL)
return false;
unsigned int i = 0;
bool prevhigh = false;
do {
if ((bytes > 0) && (i >= bytes)) {
return false;
}
if ((!prevhigh) && (data[i] == ClassDecoder::unknownclass)) {
return true;
}
prevhigh = (data[i] >= 128);
++i;
} while (1);
}
vector<unsigned int> Pattern::tovector() const {
vector<unsigned int> v;
if (data == NULL)
return v;
int i = 0;
unsigned int length;
do {
const unsigned int cls = bytestoint(data + i, &length);
i += length;
if (cls == ClassDecoder::delimiterclass) {
return v;
} else {
v.push_back(cls);
}
} while (1);
}
void readanddiscardpattern(std::istream& in, bool pointerformat) {
unsigned char c;
if (pointerformat) {
in.read((char*)&c, sizeof(unsigned int));
in.read((char*)&c, sizeof(uint32_t));
in.read((char*)&c, sizeof(uint32_t));
} else {
bool prevhigh = false;
do {
in.read((char*)&c, sizeof(char));
if ((!prevhigh) && (c == ClassDecoder::delimiterclass)) {
return;
}
prevhigh = (c >= 128);
} while (1);
}
}
void readanddiscardpattern_v1(std::istream& in) {
unsigned char c;
do {
in.read((char*)&c, sizeof(char));
if (c == 0) {
return;
} else if (c < 128) {
//we have a size
const size_t pos = in.tellg();
in.seekg(pos + c);
} else {
//we have a marker
}
} while (1);
}
Pattern::Pattern(std::istream& in, bool ignoreeol, const unsigned char version, const unsigned char*, bool debug) {
if (version == 2) {
//stage 1 -- get length
unsigned char c = 0;
std::streampos beginpos = 0;
bool gotbeginpos = false;
bool prevhigh = false;
//stage 1 -- get length
int length = 0;
do {
if (in.good()) {
if (!gotbeginpos) {
beginpos = in.tellg();
gotbeginpos = true;
}
in.read((char*)&c, sizeof(char));
if (debug)
std::cerr << "DEBUG read1=" << (int)c << endl;
} else {
if (ignoreeol) {
break;
} else {
std::cerr << "WARNING: Unexpected end of file (stage 1, length=" << length << "), no EOS marker found (adding and continuing)" << std::endl;
in.clear(); //clear error bits
break;
}
}
++length;
if ((!prevhigh) && (c == ClassDecoder::delimiterclass)) {
if (!ignoreeol)
break;
}
prevhigh = (c >= 128);
} while (1);
if (length == 0) {
std::cerr << "ERROR: Attempting to read pattern from file, but file is empty?" << std::endl;
throw InternalError();
}
//allocate buffer
if (c == 0) {
data = new unsigned char[length];
} else {
data = new unsigned char[length + 1];
}
//stage 2 -- read buffer
int i = 0;
if (debug)
std::cerr << "STARTING STAGE 2: BEGINPOS=" << beginpos << ", LENGTH=" << length << std::endl;
if (!gotbeginpos) {
std::cerr << "ERROR: Invalid position in input stream whilst Reading pattern" << std::endl;
throw InternalError();
}
in.seekg(beginpos, ios::beg);
std::streampos beginposcheck = in.tellg();
if ((beginposcheck != beginpos) && (beginposcheck >= numeric_limits<std::streampos>::max())) {
std::cerr << "ERROR: Resetting read pointer for stage 2 failed! (" << (unsigned long)beginposcheck << " != " << (unsigned long)beginpos << ")" << std::endl;
throw InternalError();
} else if (!in.good()) {
std::cerr << "ERROR: After resetting readpointer for stage 2, istream is not 'good': eof=" << (int)in.eof() << ", fail=" << (int)in.fail()
<< ", badbit=" << (int)in.bad() << std::endl;
throw InternalError();
}
while (i < length) { //TODO: read multiple bytes in one go
if (in.good()) {
in.read((char*)&c, sizeof(char));
if (debug)
std::cerr << "DEBUG read2=" << (int)c << endl;
} else {
std::cerr << "ERROR: Invalid pattern data, unexpected end of file (stage 2,i=" << i << ",length=" << length << ",beginpos=" << beginpos << ",eof=" << (int)in.eof()
<< ",fail=" << (int)in.fail() << ",badbit=" << (int)in.bad() << ")" << std::endl;
throw InternalError();
}
data[i++] = c;
//if ((c == 0) && (!ignoreeol)) break; //not needed, already computed length
}
if (c != ClassDecoder::delimiterclass) { //add endmarker
data[i++] = ClassDecoder::delimiterclass;
}
if (debug)
std::cerr << "DEBUG: DONE READING PATTERN" << std::endl;
//if this is the end of file, we want the eof bit set already, so we try to
//read one more byte (and wind back if succesful):
if (in.good()) {
if (debug)
std::cerr << "DEBUG: (TESTING EOF)" << std::endl;
in.read((char*)&c, sizeof(char));
if (in.good())
in.unget();
}
} else if (version == 1) {
data = convert_v1_v2(in, ignoreeol, debug);
} else {
std::cerr << "ERROR: Unknown version " << (int)version << std::endl;
throw InternalError();
}
}
PatternPointer::PatternPointer(std::istream& in, bool, const unsigned char, unsigned char* corpusstart, bool debug) {
if (corpusstart == NULL) {
std::cerr << "ERROR: Can not read PatternPointer, no corpusstart passed!" << std::endl;
throw InternalError();
} else {
unsigned int corpusoffset;
in.read((char*)&corpusoffset, sizeof(unsigned int));
data = corpusstart + corpusoffset;
in.read((char*)&bytes, sizeof(PPSizeType));
in.read((char*)&mask, sizeof(uint32_t));
if (debug)
std::cerr << "DEBUG read patternpointer @corpusoffset=" << (size_t)data << " bytes=" << (int)bytes << " mask=" << (int)mask << std::endl;
}
}
/*
Pattern::Pattern(std::istream * in, unsigned char * buffer, int maxbuffersize, bool ignoreeol, const unsigned char version, bool debug) {
//read pattern using a buffer
if (version == 2) {
} else if (version == 1){
int i = 0;
int readingdata = 0;
unsigned char c = 0;
do {
if (in.good()) {
in.read( (char* ) &c, sizeof(char));
if (debug) std::cerr << "DEBUG read=" << (int) c << endl;
} else {
std::cerr << "ERROR: Invalid pattern data, unexpected end of file i=" << i << std::endl;
throw InternalError();
}
if (i >= maxbuffersize) {
std::cerr << "ERROR: Pattern read would exceed supplied buffer size (" << maxbuffersize << ")! Aborting prior to segmentation fault..." << std::endl;
throw InternalError();
}
buffer[i++] = c;
if (readingdata) {
readingdata--;
} else {
if (c == ENDMARKER) {
if (!ignoreeol) break;
} else if (c < 128) {
//we have a size
if (c == 0) {
std::cerr << "ERROR: Pattern length is zero according to input stream.. not possible! (stage 2)" << std::endl;
throw InternalError();
} else {
readingdata = c;
}
}
}
} while (1);
if (c != ENDMARKER) { //add endmarker
buffer[i++] = ENDMARKER;
}
if (debug) std::cerr << "DEBUG: Copying from buffer" << std::endl;
data = new unsigned char[i];
for (int j = 0; j < i; j++) {
data[j] = buffer[j];
}
if (debug) std::cerr << "DEBUG: DONE READING PATTERN" << std::endl;
} else {
std::cerr << "ERROR: Unknown version " << (int) version << std::endl;
throw InternalError();
}
}
*/
Pattern::Pattern(const unsigned char* dataref, const int _size) {
if (dataref != NULL) {
data = new unsigned char[_size + 1];
memcpy(data, dataref, _size);
data[_size] = ClassEncoder::delimiterclass;
}
}
void Pattern::set(const unsigned char* dataref, const int _size) {
if (data != NULL) {
delete[] data;
data = NULL;
}
if (dataref != NULL) {
data = new unsigned char[_size + 1];
memcpy(data, dataref, _size);
data[_size] = ClassEncoder::delimiterclass;
}
}
Pattern::Pattern(const Pattern& ref, size_t begin, size_t length, size_t* byteoffset, bool byteoffset_shiftbyone) { //slice constructor
//to be computed in bytes
size_t begin_b = (byteoffset != NULL) ? *byteoffset : 0;
size_t length_b = 0;
bool prevhigh = false;
if (ref.data == NULL) {
throw logic_error("Pattern( Pattern&, ....) called with NULL data");
}
size_t i = (byteoffset != NULL) ? *byteoffset : 0;
//std::cerr << "DEBUG: starting with offset " << i << ", begin=" << begin << ", length=" << length << std::endl;
size_t _n = 0;
do {
const unsigned char c = ref.data[i];
if (c < 128) {
//we have a token
_n++;
if ((_n == 1) && (byteoffset_shiftbyone) && (byteoffset != NULL))
*byteoffset = i + 1;
if (_n - begin == length) {
length_b = (i + 1) - begin_b;
break;
} else if ((c == ClassDecoder::delimiterclass) && (!prevhigh)) {
length_b = i - begin_b;
break;
}
i++;
if (_n == begin)
begin_b = i;
prevhigh = false;
} else {
prevhigh = true;
i++;
}
} while (1);
if ((byteoffset != NULL) && (!byteoffset_shiftbyone))
*byteoffset = i + 1;
//std::cerr << "DEBUG: slicing: this=" << (size_t) this << ",begin=" << begin << ",length=" << length << ",begin_b=" << begin_b << ",length_b=" << length_b << std::endl; //#TODO:remove
const unsigned char _size = length_b + 1;
data = new unsigned char[_size];
memcpy(data, ref.data + begin_b, length_b);
data[length_b] = ClassDecoder::delimiterclass;
//std::cerr << "DEBUG: slicing done"; //#TODO:remove
}
PatternPointer::PatternPointer(unsigned char* ref, size_t begin, size_t length, size_t* byteoffset, bool byteoffset_shiftbyone) { //slice constructor
//to be computed in bytes
size_t begin_b = (byteoffset != NULL) ? *byteoffset : 0;
size_t length_b = 0;
bool prevhigh = false;
size_t i = (byteoffset != NULL) ? *byteoffset : 0;
size_t _n = 0;
do {
unsigned char c = ref[i];
if (c < 128) {
//we have a token
_n++;
if ((_n == 1) && (byteoffset_shiftbyone) && (byteoffset != NULL))
*byteoffset = i + 1;
if (_n - begin == length) {
length_b = (i + 1) - begin_b;
break;
} else if ((c == ClassDecoder::delimiterclass) && (!prevhigh)) {
length_b = i - begin_b;
break;
}
i++;
if (_n == begin)
begin_b = i;
prevhigh = false;
} else {
prevhigh = true;
i++;
}
} while (1);
if ((byteoffset != NULL) && (!byteoffset_shiftbyone))
*byteoffset = i + 1;
data = ref + begin_b;
bytes = length_b;
mask = computemask();
}
PatternPointer::PatternPointer(const Pattern& ref, size_t begin, size_t length, size_t* byteoffset, bool byteoffset_shiftbyone) { //slice constructor
//to be computed in bytes
size_t begin_b = (byteoffset != NULL) ? *byteoffset : 0;
size_t length_b = 0;
bool prevhigh = false;
size_t i = (byteoffset != NULL) ? *byteoffset : 0;
size_t _n = 0;
do {
unsigned char c = ref.data[i];
if (c < 128) {
//we have a token
_n++;
if ((_n == 1) && (byteoffset_shiftbyone) && (byteoffset != NULL))
*byteoffset = i + 1;
if (_n - begin == length) {
length_b = (i + 1) - begin_b;
break;
} else if ((c == ClassDecoder::delimiterclass) && (!prevhigh)) {
length_b = i - begin_b;
break;
}
i++;
if (_n == begin)
begin_b = i;
prevhigh = false;
} else {
prevhigh = true;
i++;
}
} while (1);
if ((byteoffset != NULL) && (!byteoffset_shiftbyone))
*byteoffset = i + 1;
data = ref.data + begin_b;
/*if (length_b >= B32) {
std::cerr << "ERROR: Pattern too long for pattern pointer [length_b=" << length_b << ",begin=" << begin << ",length=" << length << ", reference_length_b=" << ref.bytesize() << "] (did you set MAXLENGTH (-l)?)" << std::endl;
std::cerr << "Reference=";
ref.out();
std::cerr << std::endl;
throw InternalError();
}*/
bytes = length_b;
mask = computemask();
}
PatternPointer::PatternPointer(const PatternPointer& ref, size_t begin, size_t length, size_t* byteoffset, bool byteoffset_shiftbyone) { //slice constructor
//to be computed in bytes
size_t begin_b = (byteoffset != NULL) ? *byteoffset : 0;
size_t length_b = 0;
bool prevhigh = false;
size_t i = (byteoffset != NULL) ? *byteoffset : 0;
size_t _n = 0;
do {
if (i == ref.bytes) {
length_b = i - begin_b;
break;
}
unsigned char c = ref.data[i];
if (c < 128) {
//we have a token
_n++;
if ((_n == 1) && (byteoffset_shiftbyone) && (byteoffset != NULL))
*byteoffset = i + 1;
if (_n - begin == length) {
length_b = (i + 1) - begin_b;
break;
} else if ((c == ClassDecoder::delimiterclass) && (!prevhigh)) {
length_b = i - begin_b;
break;
}
i++;
if (_n == begin)
begin_b = i;
prevhigh = false;
} else {
prevhigh = true;
i++;
}
} while (1);
if ((byteoffset != NULL) && (!byteoffset_shiftbyone))
*byteoffset = i + 1;
data = ref.data + begin_b;
bytes = length_b;
mask = computemask();
/*std::cerr << "Created patternpointer: b=" << bytes << " n=" << this->n() << " (begin="<<begin<<",length="<<length<<")" <<endl;
this->out();
std::cerr << std::endl;*/
}
Pattern::Pattern(const Pattern& ref) { //copy constructor
if ((ref.data != NULL) && (ref.data[0] != 0)) {
const size_t s = ref.bytesize();
data = new unsigned char[s + 1];
memcpy(data, ref.data, s);
data[s] = ClassDecoder::delimiterclass;
} else {
data = NULL;
}
}
Pattern::Pattern(const PatternPointer& ref) { //constructor from patternpointer
if (ref.mask == 0) {
//NGRAM
data = new unsigned char[ref.bytesize() + 1];
memcpy(data, ref.data, ref.bytesize());
data[ref.bytesize()] = ClassDecoder::delimiterclass;
} else if (ref.isflexgram()) {
//FLEXGRAM
unsigned char tmpdata[ref.bytesize() + 1];
size_t _size = ref.flexcollapse(tmpdata);
data = new unsigned char[_size + 1];
memcpy(data, tmpdata, _size);
data[_size] = ClassDecoder::delimiterclass;
} else {
//SKIPGRAM
data = new unsigned char[ref.bytesize() + 1]; //may overallocate by a small margin
size_t _n = 0;
size_t cursor = 0;
bool skip = ref.isgap(_n);
for (size_t i = 0; i < ref.bytes; i++) {
const unsigned char c = ref.data[i];
if (c < 128) {
if (skip) {
data[cursor++] = ClassDecoder::skipclass;
} else {
data[cursor++] = c;
}
_n++;
skip = ref.isgap(_n);
} else if (!skip) {
data[cursor++] = c;
}
}
data[cursor++] = ClassDecoder::delimiterclass;
}
}
Pattern::Pattern(const PatternPointer& ref, size_t begin, size_t length, size_t* byteoffset, bool byteoffset_shiftbyone) { //slice constructor from patternpointer
//to be computed in bytes
size_t begin_b = (byteoffset != NULL) ? *byteoffset : 0;
size_t length_b = 0;
bool prevhigh = false;
size_t i = (byteoffset != NULL) ? *byteoffset : 0;
size_t _n = 0;
do {
const unsigned char c = ref.data[i];
if (c < 128) {
//we have a token
_n++;
if ((_n == 1) && (byteoffset_shiftbyone) && (byteoffset != NULL))
*byteoffset = i + 1;
if (_n - begin == length) {
length_b = (i + 1) - begin_b;
break;
} else if ((c == ClassDecoder::delimiterclass) && (!prevhigh)) {
length_b = i - begin_b;
break;
}
i++;
if (_n == begin)
begin_b = i;
prevhigh = false;
} else {
prevhigh = true;
i++;
}
} while (1);
if ((byteoffset != NULL) && (!byteoffset_shiftbyone))
*byteoffset = i + 1;
const size_t _size = length_b + 1;
data = new unsigned char[_size]; //may overallocate a bit for skipgrams/flexgrams
if (ref.mask == 0) {
//NGRAM
memcpy(data, ref.data + begin_b, length_b);
data[length_b] = ClassDecoder::delimiterclass;
} else {
//SKIPGRAM OR FLEXGRAM
const bool flex = ref.isflexgram();
_n = 0;
size_t cursor = 0;
bool skip = ref.isgap(_n);
for (size_t j = 0; j < ref.bytes; j++) {
const unsigned char c = ref.data[j];
if (c < 128) {
if ((_n >= begin) && (_n < begin + length)) {
if (flex) {
data[cursor++] = ClassDecoder::flexclass;
} else if (skip) {
data[cursor++] = ClassDecoder::skipclass;
} else {
data[cursor++] = c;
}
}
_n++;
if (_n >= begin + length)
break;
skip = ref.isgap(_n);
} else if (!skip) {
if ((_n >= begin) && (_n < begin + length)) {
data[cursor++] = c;
}
}
}
data[cursor++] = ClassDecoder::delimiterclass;
}
}
Pattern::~Pattern() {
if (data != NULL) {
delete[] data;
data = NULL;
}
}
bool Pattern::operator==(const Pattern& other) const {
if ((data == NULL) || (data[0] == 0)) {
return (other.data == NULL || other.data[0] == 0);
} else if ((other.data == NULL) || (other.data[0] == 0)) {
return false;
}
size_t i = 0;
do {
if (data[i] != other.data[i])
return false;
if (data[i] == 0)