-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathldpl-types.h
414 lines (389 loc) · 11.7 KB
/
ldpl-types.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
#ifndef LDPLTYPES
#define LDPLTYPES
#define ldpl_number double
#define ldpl_vector ldpl_map
#define ldpl_text chText
#include <string>
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <string.h>
#include <unordered_map>
#include <math.h>
using namespace std;
#ifndef CHTEXT
#define CHTEXT
class chText {
private:
vector<string> buffer;
string stringRep;
// Gets length of utf8-encoded c++ string
size_t chText_get_str_utf8length(const string cstr){
size_t len = cstr.size();
size_t utf8len = 0;
unsigned int c;
for (size_t i = 0; i < len; i++)
{
size_t l = 0;
c = (unsigned char) cstr[i];
if (c >= 0 && c <= 127) l = 0;
else if ((c & 0xE0) == 0xC0) l = 1;
else if ((c & 0xF0) == 0xE0) l = 2;
else if ((c & 0xF8) == 0xF0) l = 3;
else if (c>=248 && c<=255) return 0; //invalid utf8
i += l;
utf8len++;
}
return utf8len;
}
// Fills buffer with utf8-encoded c++ string
void createFromString(const string & cstr){
buffer.clear();
size_t cstrlen = cstr.size();
size_t chPos = 0;
for(size_t i = 0; i < cstrlen; ++i){
string ch = "";
char c = cstr[i];
if (c >= 0 && c <= 127){
ch += c;
}
else if ((c & 0xE0) == 0xC0){
ch += c;
ch += cstr[++i];
}
else if ((c & 0xF0) == 0xE0){
ch += c;
ch += cstr[++i];
ch += cstr[++i];
}
else if ((c & 0xF8) == 0xF0){
ch += c;
ch += cstr[++i];
ch += cstr[++i];
ch += cstr[++i];
}
buffer.push_back(ch);
chPos++;
}
}
// Fills buffer with utf8-encoded c++ string
void createFromChar(const char * cstr){
buffer.clear();
size_t cstrlen = strlen(cstr);
size_t chPos = 0;
for(size_t i = 0; i < cstrlen; ++i){
string ch = "";
char c = cstr[i];
if (c >= 0 && c <= 127){
ch += c;
}
else if ((c & 0xE0) == 0xC0){
ch += c;
ch += cstr[++i];
}
else if ((c & 0xF0) == 0xE0){
ch += c;
ch += cstr[++i];
ch += cstr[++i];
}
else if ((c & 0xF8) == 0xF0){
ch += c;
ch += cstr[++i];
ch += cstr[++i];
ch += cstr[++i];
}
buffer.push_back(ch);
chPos++;
}
}
public:
size_t size(){
return buffer.size();
}
bool empty(){
return buffer.empty();
}
size_t length(){
return size();
}
string str_rep(){
stringRep = "";
for(size_t i = 0; i < size(); ++i){
stringRep += buffer[i];
}
return stringRep;
}
// default constructor
chText(){}
// conversion from string (constructor):
chText (const string& x) {
createFromString(x);
}
// conversion from double (constructor):
chText (const double& f) {
std::string str = to_string (f);
str.erase(str.find_last_not_of('0') + 1, std::string::npos);
str.erase ( str.find_last_not_of('.') + 1, std::string::npos );
createFromString(str);
}
// conversion from string (assignment):
chText& operator= (const string& x) {
createFromString(x);
return *this;
}
// conversion from char * (constructor):
chText (const char * x) {
createFromChar(x);
}
// conversion from char * (assignment):
chText& operator= (const char * x) {
createFromChar(x);
return *this;
}
// conversion from char (constructor):
chText (char x) {
string a = "";
a += x;
createFromString(a);
}
// conversion from char (assignment):
chText& operator= (char x) {
string a = "";
a += x;
createFromString(a);
return *this;
}
// [] for reading
chText operator [](size_t i) const {
if(i >= buffer.size()){
cout << "Out-of-bounds index access." << endl;
exit(1);
}
chText c = buffer[i];
return c;
}
// [] for setting
string & operator [](int i) {
if(i >= buffer.size()){
cout << "Out-of-bounds index access." << endl;
exit(1);
}
return buffer[i];
}
// Output operator
friend ostream & operator << (ostream &out, const chText &c);
// + operator
friend chText operator+(const chText &c1, const chText &c2);
// + operator
friend chText operator+(const string &c1, const chText &c2);
// + operator
friend chText operator+(const chText &c1, const string &c2);
// + operator
friend chText operator+(const char * c1, const chText &c2);
// + operator
friend chText operator+(const chText &c1, const char * c2);
// < operator
friend bool operator<(const chText &c1, const chText &c2);
// > operator
friend bool operator>(const chText &c1, const chText &c2);
// Equality operator
friend bool operator==(const chText& ch1, const chText& ch2);
// Equality operator
friend bool operator==(const chText& ch1, const string& ch2);
// Equality operator
friend bool operator==(const chText& ch1, const char * ch2);
// Equality operator
friend bool operator==(const string& ch2, const chText& ch1);
// Equality operator
friend bool operator==(const char * ch2, const chText& ch1);
// Equality operator
friend bool operator==(const chText& ch1, const char ch2);
// Equality operator
friend bool operator==(const char ch2, const chText& ch1);
// Equality operator
friend bool operator!=(const chText& ch1, const chText& ch2);
// Load a file into this chText
bool loadFile(const string &fileName){
string line;
string fileContents = "";
ifstream myfile (fileName);
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
fileContents += line + "\\n";
}
myfile.close();
}
else return false;
createFromString(fileContents);
return true;
}
// += operator
chText & operator += (const chText & txt){
for(const string & s : txt.buffer){
buffer.push_back(s);
}
return *this;
}
// += operator
chText & operator += (const string & txt){
chText c2 = txt;
for(const string & s : c2.buffer){
buffer.push_back(s);
}
return *this;
}
// += operator
chText & operator += (const char * txt){
chText c2 = txt;
for(const string & s : c2.buffer){
buffer.push_back(s);
}
return *this;
}
bool isAlphanumeric(){
for(const string & s : buffer){
for(const char & c : s)
if(!isalnum(c)) return false;
}
return true;
}
bool isAlphanumeric(size_t from){
for(size_t i = from; i < size(); ++i){
for(const char & c : buffer[i])
if(!isalnum(c)) return false;
}
return true;
}
bool isNumber(){
string number = "";
for(size_t i = 0; i < size(); ++i){
number += buffer[i];
}
unsigned int firstchar = 0;
if(number[0] == '-') firstchar = 1;
if(number[firstchar] == '.') return false; //.12 is not a valid decimal in LDPL, 0.12 is.
if(number[firstchar] == '+') return false; //+5 is not a valid decimal in LDPL, 5 is.
istringstream iss(number);
double f;
iss >> f;
bool isNumber = iss.eof() && !iss.fail();
//If it is a number, it might be an octal literal (e.g. 025, 067, 003 etc)
//so we proceed to fix the original number to make it decimal.
if(isNumber){
string f_number = "";
unsigned int i;
for(i = 1; i < number.length(); ++i){
//If prev char not 0
if(number[i - 1] != '0'){
//if prev char is -, continue check
if(number[i - 1] == '-') f_number += '-';
//if prev char is number, break
else break;
}
//If prev number is 0
else if(number[i] == '.') {
f_number += '0';
}
}
f_number += number.substr(i - 1);
number = f_number;
return true;
}else{
return false;
}
}
double getNumber(){
string number = "";
for(size_t i = 0; i < size(); ++i){
number += buffer[i];
}
return stod(number);
}
chText substr(size_t from, size_t count){
chText newText;
for(size_t i = from; i < from + count; ++i){
if(i >= buffer.size()) break;
newText.buffer.push_back(buffer[i]);
}
return newText;
}
chText & erase(size_t from, size_t count){
for(size_t i = 0; i < count; ++i)
buffer.erase(buffer.begin() + from);
return *this;
}
chText substr(size_t from){
chText newText;
for(size_t i = from; i < from + buffer.size(); ++i){
if(i >= buffer.size()) break;
newText.buffer.push_back(buffer[i]);
}
return newText;
}
int compare(size_t from, size_t count, chText other){
chText newText;
for(size_t i = from; i < from + count; ++i){
if(i >= buffer.size()) break;
newText.buffer.push_back(buffer[i]);
}
if (newText == other) return 0;
if (newText.size() < other.size()) return -1;
else return 1;
}
int compare(chText other){
if (*this == other) return 0;
if (this->size() < other.size()) return -1;
else return 1;
}
};
ostream & operator << (ostream &out, const chText &c);
chText operator+(const chText &c1, const chText &c2);
chText operator+(const string &c1, const chText &c2);
chText operator+(const chText &c1, const string &str);
chText operator+(const char * c1, const chText &c2);
chText operator+(const chText &c1, const char * str);
bool operator==(const chText& ch1, const chText& ch2);
bool operator==(const string& c1, const chText& ch2);
bool operator==(const chText& ch1, const string& c2);
bool operator==(const char * c1, const chText& ch2);
bool operator==(const chText& ch1, const char * c2);
bool operator==(const char c1, const chText& ch2);
bool operator==(const chText& ch1, const char c2);
bool operator<(const chText &c1, const chText &c2);
bool operator>(const chText &c1, const chText &c2);
bool operator!=(const chText& ch1, const chText& ch2);
#endif
chText to_ldpl_string(double x);
#ifndef LDPLMAP
#define LDPLMAP
template<typename T>
struct ldpl_map {
unordered_map<string, T> inner_collection;
T& operator [] (chText i) {
return inner_collection[i.str_rep()];
}
T& operator [] (ldpl_number i) {
return inner_collection[to_ldpl_string(i).str_rep()];
}
};
#endif
#ifndef LDPLLIST
#define LDPLLIST
template<typename T>
struct ldpl_list {
vector<T> inner_collection;
T& operator [] (ldpl_number i) {
i = floor(i);
if (i < 0 || i >= inner_collection.size()) {
cerr << "Runtime Error: LIST index " << i << " out of range [0, "
<< inner_collection.size() << ")" << endl;
exit(1);
}
return inner_collection[i];
}
};
#endif
#endif