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
|
/*
* count.h -- definition of the counting scanner
*
* Copyright (c) 2007-2010, Dmitry Prokoptsev <dprokoptsev@gmail.com>,
* Alexander Gololobov <agololobov@gmail.com>
*
* This file is part of Pire, the Perl Incompatible
* Regular Expressions library.
*
* Pire is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pire is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser Public License for more details.
* You should have received a copy of the GNU Lesser Public License
* along with Pire. If not, see <http://www.gnu.org/licenses>.
*/
#ifndef PIRE_EXTRA_COUNT_H
#define PIRE_EXTRA_COUNT_H
#include <contrib/libs/pire/pire/scanners/loaded.h>
#include <contrib/libs/pire/pire/fsm.h>
#include <algorithm>
namespace Pire {
class Fsm;
namespace Impl {
template<class T>
class ScannerGlueCommon;
template<class T>
class CountingScannerGlueTask;
class NoGlueLimitCountingScannerGlueTask;
template <class AdvancedScanner>
AdvancedScanner MakeAdvancedCountingScanner(const Fsm& re, const Fsm& sep, bool* simple);
};
template<size_t I>
class IncrementPerformer {
public:
template<typename State, typename Action>
PIRE_FORCED_INLINE PIRE_HOT_FUNCTION
static void Do(State& s, Action mask)
{
if (mask & (1 << (I - 1))) {
Increment(s);
}
IncrementPerformer<I - 1>::Do(s, mask);
}
private:
template<typename State>
PIRE_FORCED_INLINE PIRE_HOT_FUNCTION
static void Increment(State& s)
{
++s.m_current[I - 1];
}
};
template<>
class IncrementPerformer<0> {
public:
template<typename State, typename Action>
PIRE_FORCED_INLINE PIRE_HOT_FUNCTION
static void Do(State&, Action)
{
}
};
template<size_t I>
class ResetPerformer {
public:
template<typename State, typename Action>
PIRE_FORCED_INLINE PIRE_HOT_FUNCTION
static void Do(State& s, Action mask)
{
if (mask & (1 << (LoadedScanner::MAX_RE_COUNT + (I - 1))) && s.m_current[I - 1]) {
Reset(s);
}
ResetPerformer<I - 1>::Do(s, mask);
}
private:
template<typename State>
PIRE_FORCED_INLINE PIRE_HOT_FUNCTION
static void Reset(State& s)
{
s.m_total[I - 1] = ymax(s.m_total[I - 1], s.m_current[I - 1]);
s.m_current[I - 1] = 0;
}
};
template<>
class ResetPerformer<0> {
public:
template<typename State, typename Action>
PIRE_FORCED_INLINE PIRE_HOT_FUNCTION
static void Do(State&, Action)
{
}
};
/**
* A scanner which counts occurences of the
* given regexp separated by another regexp
* in input text.
*/
template<class DerivedScanner, class State>
class BaseCountingScanner: public LoadedScanner {
public:
enum {
IncrementAction = 1,
ResetAction = 2,
FinalFlag = 0,
DeadFlag = 1,
};
void Initialize(State& state) const
{
state.m_state = m.initial;
memset(&state.m_current, 0, sizeof(state.m_current));
memset(&state.m_total, 0, sizeof(state.m_total));
state.m_updatedMask = 0;
}
PIRE_FORCED_INLINE PIRE_HOT_FUNCTION
void TakeAction(State& s, Action a) const
{
static_cast<const DerivedScanner*>(this)->template TakeActionImpl<MAX_RE_COUNT>(s, a);
}
bool CanStop(const State&) const { return false; }
Char Translate(Char ch) const
{
return m_letters[static_cast<size_t>(ch)];
}
Action NextTranslated(State& s, Char c) const
{
Transition x = reinterpret_cast<const Transition*>(s.m_state)[c];
s.m_state += SignExtend(x.shift);
return x.action;
}
Action Next(State& s, Char c) const
{
return NextTranslated(s, Translate(c));
}
Action Next(const State& current, State& n, Char c) const
{
n = current;
return Next(n, c);
}
bool Final(const State& /*state*/) const { return false; }
bool Dead(const State&) const { return false; }
using LoadedScanner::Swap;
size_t StateIndex(const State& s) const { return StateIdx(s.m_state); }
protected:
using LoadedScanner::Init;
using LoadedScanner::InternalState;
template<size_t ActualReCount>
void PerformIncrement(State& s, Action mask) const
{
if (mask) {
IncrementPerformer<ActualReCount>::Do(s, mask);
s.m_updatedMask |= ((size_t)mask) << MAX_RE_COUNT;
}
}
template<size_t ActualReCount>
void PerformReset(State& s, Action mask) const
{
mask &= s.m_updatedMask;
if (mask) {
ResetPerformer<ActualReCount>::Do(s, mask);
s.m_updatedMask &= (Action)~mask;
}
}
void Next(InternalState& s, Char c) const
{
Transition x = reinterpret_cast<const Transition*>(s)[Translate(c)];
s += SignExtend(x.shift);
}
};
template <size_t MAX_RE_COUNT>
class CountingState {
public:
size_t Result(int i) const { return ymax(m_current[i], m_total[i]); }
private:
using InternalState = LoadedScanner::InternalState;
InternalState m_state;
ui32 m_current[MAX_RE_COUNT];
ui32 m_total[MAX_RE_COUNT];
size_t m_updatedMask;
template <class DerivedScanner, class State>
friend class BaseCountingScanner;
template<size_t I>
friend class IncrementPerformer;
template<size_t I>
friend class ResetPerformer;
#ifdef PIRE_DEBUG
friend yostream& operator << (yostream& s, const State& state)
{
s << state.m_state << " ( ";
for (size_t i = 0; i < MAX_RE_COUNT; ++i)
s << state.m_current[i] << '/' << state.m_total[i] << ' ';
return s << ')';
}
#endif
};
class CountingScanner : public BaseCountingScanner<CountingScanner, CountingState<LoadedScanner::MAX_RE_COUNT>> {
public:
using State = CountingState<MAX_RE_COUNT>;
enum {
Matched = 2,
};
CountingScanner() {}
CountingScanner(const Fsm& re, const Fsm& sep);
static CountingScanner Glue(const CountingScanner& a, const CountingScanner& b, size_t maxSize = 0);
template<size_t ActualReCount>
PIRE_FORCED_INLINE PIRE_HOT_FUNCTION
void TakeActionImpl(State& s, Action a) const
{
if (a & IncrementMask)
PerformIncrement<ActualReCount>(s, a);
if (a & ResetMask)
PerformReset<ActualReCount>(s, a);
}
private:
Action RemapAction(Action action)
{
if (action == (Matched | DeadFlag))
return 1;
else if (action == DeadFlag)
return 1 << MAX_RE_COUNT;
else
return 0;
}
friend void BuildScanner<CountingScanner>(const Fsm&, CountingScanner&);
friend class Impl::ScannerGlueCommon<CountingScanner>;
friend class Impl::CountingScannerGlueTask<CountingScanner>;
};
class AdvancedCountingScanner : public BaseCountingScanner<AdvancedCountingScanner, CountingState<LoadedScanner::MAX_RE_COUNT>> {
public:
using State = CountingState<MAX_RE_COUNT>;
AdvancedCountingScanner() {}
AdvancedCountingScanner(const Fsm& re, const Fsm& sep, bool* simple = nullptr);
static AdvancedCountingScanner Glue(const AdvancedCountingScanner& a, const AdvancedCountingScanner& b, size_t maxSize = 0);
template<size_t ActualReCount>
PIRE_FORCED_INLINE PIRE_HOT_FUNCTION
void TakeActionImpl(State& s, Action a) const
{
if (a & ResetMask) {
PerformReset<ActualReCount>(s, a);
}
if (a & IncrementMask) {
PerformIncrement<ActualReCount>(s, a);
}
}
private:
Action RemapAction(Action action)
{
Action result = 0;
if (action & ResetAction) {
result = 1 << MAX_RE_COUNT;
}
if (action & IncrementAction) {
result |= 1;
}
return result;
}
friend class Impl::ScannerGlueCommon<AdvancedCountingScanner>;
friend class Impl::CountingScannerGlueTask<AdvancedCountingScanner>;
friend AdvancedCountingScanner Impl::MakeAdvancedCountingScanner<AdvancedCountingScanner>(const Fsm&, const Fsm&, bool*);
};
class NoGlueLimitCountingState {
public:
size_t Result(int i) const { return ymax(m_current[i], m_total[i]); }
void Initialize(size_t initial, size_t regexpsCount) {
m_state = initial;
m_current.assign(regexpsCount, 0);
m_total.assign(regexpsCount, 0);
}
PIRE_FORCED_INLINE PIRE_HOT_FUNCTION
void Reset(size_t regexpId) {
m_current[regexpId] = 0;
}
PIRE_FORCED_INLINE PIRE_HOT_FUNCTION
void Increment(size_t regexp_id) {
++m_current[regexp_id];
m_total[regexp_id] = ymax(m_total[regexp_id], m_current[regexp_id]);
}
template<size_t I>
friend class IncrementPerformer;
template<size_t I>
friend class ResetPerformer;
private:
LoadedScanner::InternalState m_state;
TVector<ui32> m_current;
TVector<ui32> m_total;
template <class DerivedScanner, class State>
friend class BaseCountingScanner;
#ifdef PIRE_DEBUG
yostream& operator << (yostream& s, const State& state)
{
s << state.m_state << " ( ";
for (size_t i = 0; i < state.m_current.size(); ++i)
s << state.m_current[i] << '/' << state.m_total[i] << ' ';
return s << ')';
}
#endif
};
class NoGlueLimitCountingScanner : public BaseCountingScanner<NoGlueLimitCountingScanner, NoGlueLimitCountingState> {
public:
using State = NoGlueLimitCountingState;
using ActionIndex = ui32;
using TActionsBuffer = std::unique_ptr<ActionIndex[]>;
private:
TActionsBuffer ActionsBuffer;
const ActionIndex* Actions = nullptr;
bool AdvancedScannerCompatibilityMode = false;
public:
NoGlueLimitCountingScanner() = default;
NoGlueLimitCountingScanner(const Fsm& re, const Fsm& sep, bool* simple = nullptr);
NoGlueLimitCountingScanner(const NoGlueLimitCountingScanner& rhs)
: BaseCountingScanner(rhs)
, AdvancedScannerCompatibilityMode(rhs.AdvancedScannerCompatibilityMode)
{
if (rhs.ActionsBuffer) {
Y_ASSERT(rhs.Actions);
ActionsBuffer = TActionsBuffer(new ActionIndex [*rhs.Actions]);
std::copy_n(rhs.ActionsBuffer.get(), *rhs.Actions, ActionsBuffer.get());
Actions = ActionsBuffer.get();
} else {
Actions = rhs.Actions;
}
}
NoGlueLimitCountingScanner(NoGlueLimitCountingScanner&& other) : BaseCountingScanner() {
Swap(other);
}
NoGlueLimitCountingScanner& operator=(NoGlueLimitCountingScanner rhs) {
Swap(rhs);
return *this;
}
void Swap(NoGlueLimitCountingScanner& s) {
LoadedScanner::Swap(s);
DoSwap(ActionsBuffer, s.ActionsBuffer);
DoSwap(Actions, s.Actions);
DoSwap(AdvancedScannerCompatibilityMode, s.AdvancedScannerCompatibilityMode);
}
void Initialize(State& state) const
{
state.Initialize(m.initial, RegexpsCount());
}
template <size_t ActualReCount>
PIRE_FORCED_INLINE PIRE_HOT_FUNCTION
void TakeActionImpl(State& s, Action a) const
{
if (!a) {
return;
}
if (AdvancedScannerCompatibilityMode) {
AdvancedScannerTakeActionImpl<ActualReCount>(s, a);
return;
}
// Note: it's important to perform resets before increments,
// as it's possible for one repetition group to stop and another begin at the same symbol
if (Actions) {
auto action = Actions + a;
for (auto reset_count = *action++; reset_count--;) {
s.Reset(*action++);
}
for (auto inc_count = *action++; inc_count--;) {
s.Increment(*action++);
}
} else {
Y_ASSERT(RegexpsCount() == 1);
if (a & ResetAction) {
s.Reset(0);
}
if (a & IncrementAction) {
s.Increment(0);
}
}
}
void Save(yostream* s) const;
void Load(yistream* s);
const void* Mmap(const void* ptr, size_t size);
static NoGlueLimitCountingScanner Glue(const NoGlueLimitCountingScanner& a, const NoGlueLimitCountingScanner& b, size_t maxSize = 0);
private:
Action RemapAction(Action action)
{
return action;
}
template <class Iterator>
void GetActions(Action a, ActionIndex id_shift, Iterator output_resets, Iterator output_increments) const {
if (!a) {
return;
}
if (!Actions) {
if (a & ResetAction) {
*output_resets++ = id_shift;
}
if (a & NoGlueLimitCountingScanner::IncrementAction) {
*output_increments++ = id_shift;
}
return;
}
auto action = Actions + a;
for (auto output : {output_resets, output_increments}) {
for (auto count = *action++; count--;) {
*output++ = *action++ + id_shift;
}
}
}
void AcceptActions(const TVector<ActionIndex>& actions) {
Y_ASSERT(!Actions);
Y_ASSERT(!actions.empty());
Y_ASSERT(actions[0] == actions.size());
ActionsBuffer = TActionsBuffer(new ActionIndex[actions.size()]);
std::copy(actions.begin(), actions.end(), ActionsBuffer.get());
Actions = ActionsBuffer.get();
}
template <size_t ActualReCount>
void AdvancedScannerTakeActionImpl(State& s, Action a) const {
if (a & ResetMask) {
ResetPerformer<ActualReCount>::Do(s, a);
}
if (a & IncrementMask) {
IncrementPerformer<ActualReCount>::Do(s, a);
}
}
friend class Impl::ScannerGlueCommon<NoGlueLimitCountingScanner>;
friend class Impl::CountingScannerGlueTask<NoGlueLimitCountingScanner>;
friend class Impl::NoGlueLimitCountingScannerGlueTask;
friend NoGlueLimitCountingScanner Impl::MakeAdvancedCountingScanner<NoGlueLimitCountingScanner>(const Fsm&, const Fsm&, bool*);
};
}
#endif
|