forked from HIT-SCIR/ltp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.hpp
272 lines (229 loc) · 7.05 KB
/
template.hpp
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
/*
* A light weight template engine which support few syntax.
* It's designed for feature extraction in various NLP tasks.
* Speed is mainly concerned.
*/
#ifndef __TEMPLATE_HPP__
#define __TEMPLATE_HPP__
#include <iostream>
#include <string.h>
#include <stdlib.h>
namespace ltp {
namespace utility {
// a singleton used to story (token, id) mapping.
// A Template::Data should make a copy of the cache class.
template <typename T>
class __Template_Token_Cache {
public:
static __Template_Token_Cache * get_cache() {
if (0 == _instance) {
_instance = new __Template_Token_Cache;
}
return _instance;
}
int push_back(const char * key) {
// allocate new data
int new_num;
int len = strlen(key) + 1;
for (int i = 0; i < _num_tokens; ++ i) {
if (!strcmp(key, _tokens[i])) {
return i;
}
}
if (_cap_tokens <= (new_num = (_num_tokens + 1))) {
_cap_tokens = (new_num << 1);
char ** new_tokens = new char *[_cap_tokens];
if (_tokens) {
memcpy(new_tokens, _tokens, sizeof(char *)*_num_tokens);
delete [](_tokens);
}
_tokens = new_tokens;
}
_tokens[_num_tokens] = new char[len];
memcpy(_tokens[_num_tokens], key, len);
++ _num_tokens;
return _num_tokens - 1;
}
const char * index(int idx) {
if (idx < 0 || idx >= _num_tokens) {
return 0;
}
return _tokens[idx];
}
int num_tokens() {
return _num_tokens;
}
// this is shit that so many stars.
void copy(char ** & tokens) {
if (0 == tokens) {
tokens = new char *[_num_tokens];
}
for (int i = 0; i < _num_tokens; ++ i) {
int len = strlen(_tokens[i]) + 1;
tokens[i] = new char[len];
memcpy(tokens[i], _tokens[i], len);
}
}
protected:
__Template_Token_Cache() :
_tokens(0),
_num_tokens(0),
_cap_tokens(0) {}
private:
static __Template_Token_Cache * _instance;
char ** _tokens;
int _num_tokens;
int _cap_tokens;
};
template<typename T> __Template_Token_Cache<T> * __Template_Token_Cache<T>::_instance = NULL;
// The template class
class Template {
public:
// The template data class
class Data {
public:
/*
* Constructor for Template::Data
*/
// make a copy from the Token_Cache and linke all value
// to the key.
Data() : _keys(0), _values(0) {
_num_tokens = __Template_Token_Cache<void>::get_cache()->num_tokens();
__Template_Token_Cache<void>::get_cache()->copy( _keys );
_values = new char*[_num_tokens];
for (int i = 0; i < _num_tokens; ++ i) {
_values[i] = _keys[i];
}
}
~Data() {
for (int i = 0; i < _num_tokens; ++ i) {
if (_values[i] != _keys[i]) {
delete [](_values[i]);
}
delete [](_keys[i]);
}
delete [](_keys);
delete [](_values);
}
/*
* set (key, value) pair to the Template::Data
*
* @param[in] key the key
* @param[in] val the value
* @return bool true on success, otherwise false
*/
bool set(const char * key, const char * val) {
for (int i = 0; i < _num_tokens; ++ i) {
// i didnt check case like "pid={pid}", for speed concerns.
// users should guarantee no such template is used.
if (!strcmp(_keys[i], key)) {
int len = strlen(val) + 1;
char * new_key = new char[len];
memcpy(new_key, val, len);
if (_values[i] != _keys[i]) {
delete [](_values[i]);
}
_values[i] = new_key;
return true;
}
}
return false;
}
/*
* A string wrapper for bool set(const char * key, const char * val)
*
* @param[in] key the key
* @param[in] val the value
* @return bool true on success, otherwise false
*/
bool set(const char * key, const std::string & val) {
return set( key, val.c_str() );
}
const char * index(int i) const {
if (i < 0 || i >= _num_tokens) {
return 0;
}
return _values[i];
}
private:
char ** _keys;
char ** _values;
int _num_tokens;
};
public:
Template(const char * tempstr) : items(0), buffer(0) {
int len = strlen(tempstr);
buffer = new char[len + 1];
memcpy(buffer, tempstr, len + 1);
num_items = 0;
const char * s = NULL;
int right_bracket = -1;
int left_bracket = -1;
// get number of tokens in the template
for (s = buffer; *s; ++ s) {
if ((*s) == '{') {
left_bracket = s - buffer;
if (right_bracket + 1 < left_bracket) {
++ num_items;
}
}
if ((*s) == '}') {
right_bracket = s - buffer;
++ num_items;
}
}
items = new int[num_items];
right_bracket = -1;
int i = 0;
// loop over all the tokens and push them into the cache.
for (s = buffer; *s; ++ s) {
if ((*s) == '{') {
left_bracket = s - buffer;
if (right_bracket + 1 < left_bracket) {
buffer[left_bracket] = 0;
items[i] = __Template_Token_Cache<void>::get_cache()->push_back(
buffer + right_bracket + 1);
++ i;
}
}
if ((*s) == '}') {
right_bracket = s - buffer;
buffer[right_bracket] = 0;
items[i] = __Template_Token_Cache<void>::get_cache()->push_back(
buffer + left_bracket + 1);
++ i;
}
}
}
~Template() {
/*if (replacements) {
delete [](replacements);
}*/
if (items) {
delete [](items);
}
if (buffer) {
delete [](buffer);
}
}
/*
* Generate the template from templates and save it into a string
*
* @param[in] data the template data
* @param[out] ret the output string
*/
inline bool render(const Data & data, std::string & ret) {
ret.clear();
for (int i = 0; i < num_items; ++ i) {
ret.append( data.index(items[i]) );
}
return true;
}
private:
int num_items;
int * items;
char * buffer;
};
} // end for namespace utility
} // end for namespace ltp
#endif // end for __TEMPLATE_HPP__