-
Notifications
You must be signed in to change notification settings - Fork 1
/
mp_script.h
143 lines (132 loc) · 4.07 KB
/
mp_script.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
#pragma once
#include "sm_descape.h"
namespace mp {
using namespace ac;
template < size_t MAX_BYTE_QUEUE_SIZE = 64>
class normalize_t : public NestUnescape< MAX_BYTE_QUEUE_SIZE, 16>
{
};
template < size_t MAX_BYTE_QUEUE_SIZE, size_t TrairMask>
class char_trait_filter_t {
protected:
uint8_t bytes_[MAX_BYTE_QUEUE_SIZE];
uint16_t size_ = 0;
public:
static const size_t TraitsToSelect = TrairMask;
size_t forward(mem_bound_t& mb) {
const uint8_t* scan = mb.data;
for (; scan < mb.tail; ++scan) {
uint8_t ch = *scan;
uint8_t tr = CharTrait::Table[ch];
if (tr & TraitsToSelect) {
bytes_[size_++] = ch;
if (size_ == MAX_BYTE_QUEUE_SIZE) {
size_ = 0;
mb.data = scan + 1;
return MAX_BYTE_QUEUE_SIZE;
}
}
}
mb.data = scan;
return 0;
}
inline size_t forward(uint8_t ch) {
uint8_t tr = CharTrait::Table[ch];
if (tr & (CharTrait::Alpha | CharTrait::DecChar)) {
bytes_[size_++] = ch;
if (size_ == MAX_BYTE_QUEUE_SIZE) {
size_ = 0;
return MAX_BYTE_QUEUE_SIZE;
}
}
return 0;
}
inline size_t finsh() {
return size_;
}
inline const void* data() {
return bytes_;
}
};
template < size_t MAX_BYTE_QUEUE_SIZE = 64>
class alphanum_t :
public char_trait_filter_t<
MAX_BYTE_QUEUE_SIZE,
CharTrait::Alpha | CharTrait::DecChar
>
{
};
template < size_t MAX_BYTE_QUEUE_SIZE = 64>
class operator_t :
public char_trait_filter_t<
MAX_BYTE_QUEUE_SIZE,
CharTrait::OPTR
>
{
};
template <typename Machine >
class forward_match_t {
protected:
const Machine& machine_;
typename Machine::MatchStateId state_ = 0;
public:
forward_match_t(Machine& machine) : machine_(machine) {
}
void forward(mem_bound_t& mb) {
ac::match(machine_, state_, mb, false);
}
void finsh() {
}
};
template < typename FROM, typename TO>
class forward_chain_t {
protected:
FROM& from_ = 0;
TO& to_ = 0;
size_t intput_ = 0, output_ = 0;
public:
forward_chain_t(FROM& from, TO& to) : from_(from), to_(to) {
}
void forward(mem_bound_t& mb)
{
intput_ += mem_bound_size(mb);
while (mem_bound_size(mb)) {
size_t ready = from_.forward(mb);
if (!ready) continue;
output_ += ready;
mem_bound_t out;
mem_bound_init(out, from_.data(), ready);
while (mem_bound_size(out))
to_.forward(out);
}
}
void finsh() {
size_t ready = from_.finsh();
output_ += ready;
if (ready) {
mem_bound_t out;
mem_bound_init(out, from_.data(), ready);
while (mem_bound_size(out))
to_.forward(out);
}
to_.finsh();
}
};
template <typename T >
void speed_test(T& forwarder, const std::string& text, size_t loop) {
const char* class_name = typeid(T).name();
double total = text.size() * loop;
ac::mem_bound_t in;
size_t start = GetTickCount();
for (size_t k = 0; k < loop; ++k) {
ac::mem_bound_init(in, text);
while (ac::mem_bound_size(in)) {
forwarder.forward(in);
}
}
forwarder.finsh();
double durtion = GetTickCount() - start;
printf("%s, durtion: %zd ms, forward: %0.2f MB, speed; %0.2f Mb/s\n",
class_name, (size_t)durtion, total / 1024 / 1024, total / durtion * 1000 / 1024 / 1024 * 8);
}
};