-
Notifications
You must be signed in to change notification settings - Fork 1
/
string_handler.cpp
716 lines (593 loc) · 15.8 KB
/
string_handler.cpp
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
/*
* string_handler.cpp
*
* Contains all kind of functions
* used to arrange/handle the strings
*
* Created on: 20 Jun 2016
* Author: obenomar
*/
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <Eigen/Dense>
#include "string_handler.h"
using Eigen::VectorXd;
using Eigen::VectorXi;
using Eigen::MatrixXd;
std::string strtrim(const std::string& str){
/*
* Small function that remove white space at the end and at
* the begining of a string.
* The original program was taken from http://stackoverflow.com/questions/1798112/removing-leading-and-trailing-spaces-from-a-string
* Modified in order to not use the C++11 standard:
* - const auto --> const string
* - optional argument for the separator is now hardcoded
*/
std::string tmp, whitespace = " \t";
size_t strBegin = str.find_first_not_of(whitespace);
if (strBegin == std::string::npos)
return ""; // no content
size_t strEnd = str.find_last_not_of(whitespace);
size_t strRange = strEnd - strBegin + 1;
tmp=str.substr(strBegin, strRange);
std::vector<char> bytes(tmp.begin(), tmp.end());
if(int(bytes[bytes.size()-1]) == 13){ // Check that CR (return) is not a last character. If so, it has to be removed
//std::cout << "Reducing size by 1" << std::endl;
return str.substr(strBegin, strRange-1);
} else{
//std::cout << "Keeping full size..." << std::endl;
return str.substr(strBegin, strRange);
}
return "";
}
std::vector<std::string> strsplit2(const std::string str, const std::string delimiters){
/*
* Take a string and split it each time one of the listed delimiters is detected
*/
std::string str0=strtrim(str);
size_t pos=0;
std::vector<std::string> str_splitted;
while ((pos = str0.find_first_of(delimiters)) != std::string::npos) {
str_splitted.push_back(str0.substr(0, pos)); // get the substring
str0.erase(0, pos + delimiters.length());
/*if(pos + delimiters.length() <= 1){
str0.erase(0, pos + delimiters.length());
} else{
str0.erase(0, pos + delimiters.length()-1);
}
*/
str0=strtrim(str0); // remove any extra white space at the begining before iterating
}
str_splitted.push_back(str0); // do not forget to add the end of the string
return str_splitted;
}
std::vector<std::string> strsplit(const std::string str, const std::string delimiters){
//
// Take a string and split it each time one of the listed delimiters is detected
//
std::string str0=strtrim(str);
size_t pos=0;
std::vector<std::string> str_splitted;
while ((pos = str0.find_first_of(delimiters)) != std::string::npos) {
if (pos !=0){
str_splitted.push_back(strtrim(str0.substr(0, pos))); // get the substring
}
str0.erase(0, pos +1);
str0=strtrim(str0); // remove any extra white space at the begining before iterating
}
if (pos !=0){
str_splitted.push_back(str0); // do not forget to add the end of the string
}
return str_splitted;
}
std::vector<double> where(std::vector<double> vec, std::string condition, double value, bool return_values){
/*
* If return_values == 0:
* Gives the indexes of values of an array that fullfil the condition
* If return_values == 1:
* Gives the values of an array that fullfil the condition
*
* The condition can be the operator "=", "!=", ">", "<", ">=" or "<="
*/
std::vector<double> index, values;
if(return_values == 0){
if(condition == "="){
for(int i=0; i<vec.size(); i++){
if(vec[i] == value){
index.push_back(i);
//std::cout << "vec[" << i << "]= " << vec[i] << std::endl;
}
}
}
if(condition == "!="){
for(int i=0; i<vec.size(); i++){
if(vec[i] != value){
index.push_back(i);
}
}
}
if(condition == ">"){
for(int i=0; i<vec.size(); i++){
if(vec[i] > value){
index.push_back(i);
}
}
}
if(condition == "<"){
for(int i=0; i<vec.size(); i++){
if(vec[i] < value){
index.push_back(i);
}
}
}
if(condition == ">="){
for(int i=0; i<vec.size(); i++){
if(vec[i] >= value){
index.push_back(i);
}
}
}
if(condition == "<="){
for(int i=0; i<vec.size(); i++){
if(vec[i] <= value){
index.push_back(i);
}
}
}
return index;
} else {
if(condition == "="){
for(int i=0; i<vec.size(); i++){
if(vec[i] == value){
values.push_back(vec[i]);
}
}
}
if(condition == "!="){
for(int i=0; i<vec.size(); i++){
if(vec[i] != value){
values.push_back(vec[i]);
}
}
}
if(condition == ">"){
for(int i=0; i<vec.size(); i++){
if(vec[i] > value){
values.push_back(vec[i]);
}
}
}
if(condition == "<"){
for(int i=0; i<vec.size(); i++){
if(vec[i] < value){
values.push_back(vec[i]);
}
}
}
if(condition == ">="){
for(int i=0; i<vec.size(); i++){
if(vec[i] >= value){
values.push_back(vec[i]);
}
}
}
if(condition == "<="){
for(int i=0; i<vec.size(); i++){
if(vec[i] <= value){
values.push_back(vec[i]);
}
}
}
return values;
}
}
VectorXi where_strXi(std::vector<std::string> vec, std::string value){
/*
* Gives the indexes of values of an array that match the value
*
*/
int cpt;
VectorXi index(vec.size());
cpt=0;
for(int i=0; i<vec.size(); i++){
if(vec[i] == value){
index[cpt]=i;
cpt=cpt+1;
}
}
index.conservativeResize(cpt);
/*
std::cout << "in where_str()" << std::endl;
std::cout << index << std::endl;
exit(EXIT_SUCCESS);
*/
return index;
}
std::vector<int> where_str(std::vector<std::string> vec, std::string value){
/*
* Gives the indexes of values of an array that match the value
*
*/
int cpt;
std::vector<int> index;
index.resize(vec.size());
cpt=0;
for(int i=0; i<vec.size(); i++){
if(vec[i] == value){
index[cpt]=i;
cpt=cpt+1;
}
}
index.resize(cpt);
return index;
}
std::vector<int> where_dbl(std::vector<double> vec, double value, double tolerance){
/*
* Gives the indexes of values of an array that match the value.
* A tolerance parameter allows you to control how close the match
* is considered as acceptable. The tolerance is in the same unit
* as the value
*
*/
int cpt;
std::vector<int> index;
index.resize(vec.size());
cpt=0;
for(int i=0; i<vec.size(); i++){
if(vec[i] > value - tolerance && vec[i] < value + tolerance){
index[cpt]=i;
cpt=cpt+1;
}
}
if(cpt >=1){
index.resize(cpt);
} else{
index.resize(1);
index[0]=-1;
}
return index;
}
VectorXi where_dbl(VectorXd vec, double value, double tolerance){
/*
* Gives the indexes of values of an array that match the value.
* A tolerance parameter allows you to control how close the match
* is considered as acceptable. The tolerance is in the same unit
* as the value
*
*/
int cpt;
VectorXi index_out;
std::vector<int> index;
index.resize(vec.size());
cpt=0;
for(int i=0; i<vec.size(); i++){
if(vec[i] > value - tolerance && vec[i] < value + tolerance){
index[cpt]=i;
cpt=cpt+1;
}
}
if(cpt >=1){
index_out.resize(cpt);
for(int i=0; i<cpt; i++){
index_out[i]=index[i];
}
} else{
index_out.resize(1);
index_out[0]=-1;
}
return index_out;
}
VectorXi where_in_range(VectorXd vec, double value_min, double value_max, bool strict){
/*
* Gives the indexes of values of an array within a specified range
*
*/
int cpt;
VectorXi index_out;
std::vector<int> index;
index.resize(vec.size());
cpt=0;
for(int i=0; i<vec.size(); i++){
if (strict == 1){
if(vec[i] > value_min && vec[i] < value_max){
index[cpt]=i;
cpt=cpt+1;
}
} else{
if(vec[i] >= value_min && vec[i] <= value_max){
index[cpt]=i;
cpt=cpt+1;
}
}
}
if(cpt >=1){
index_out.resize(cpt);
for(int i=0; i<cpt; i++){
index_out[i]=index[i];
}
} else{
index_out.resize(1);
index_out[0]=-1;
}
return index_out;
}
std::vector<int> where_in_range(std::vector<double> vec, double value_min, double value_max, bool strict){
/*
* Gives the indexes of values of an array within a range.
* If strict = 1 values have to be strictly within the range.
*
*/
int cpt;
std::vector<int> index;
index.resize(vec.size());
cpt=0;
for(int i=0; i<vec.size(); i++){
if (strict == 1){
if(vec[i] > value_min && vec[i] < value_max){
index[cpt]=i;
cpt=cpt+1;
}
} else{
if(vec[i] >= value_min && vec[i] <= value_max){
index[cpt]=i;
cpt=cpt+1;
}
}
}
if(cpt >=1){
index.resize(cpt);
} else{
index.resize(1);
index[0]=-1;
}
return index;
}
VectorXi where_int(VectorXi vec, int value){
/*
* Gives the indexes of values of an array that match the value.
*
*/
int cpt;
VectorXi index_out;
std::vector<int> index;
index.resize(vec.size());
cpt=0;
for(int i=0; i<vec.size(); i++){
if(vec[i] == value){
index[cpt]=i;
cpt=cpt+1;
}
}
if(cpt >=1){
index_out.resize(cpt);
for(int i=0; i<cpt; i++){
index_out[i]=index[i];
}
} else{
index_out.resize(1);
index_out[0]=-1;
}
return index_out;
}
std::vector<int> where_int(std::vector<int> vec, int value){
/*
* Gives the indexes of values of an array that match the value.
*
*/
int cpt;
std::vector<int> index;
index.resize(vec.size());
cpt=0;
for(int i=0; i<vec.size(); i++){
if(vec[i] == value){
index[cpt]=i;
cpt=cpt+1;
}
}
if(cpt >=1){
index.resize(cpt);
} else{
index.resize(1);
index[0]=-1;
}
return index;
}
std::vector<double> vectXd_to_vec(VectorXd vecXd_in){
std::vector<double> vec;
vec.resize(vecXd_in.size());
VectorXd::Map(&vec[0], vecXd_in.size()) = vecXd_in;
return vec;
}
VectorXi str_to_Xiarr(const std::string str, const std::string delimiters){
int int_v;
std::vector<std::string> words;
std::vector<int> arri;
VectorXi Xiarr;
words=strsplit(str, delimiters);
for(int i=0; i<words.size(); i++){
if(strtrim(words[i]) != ""){
std::stringstream(words[i]) >> int_v;
arri.push_back(int_v);
}
}
Xiarr.resize(arri.size());
for(int i=0; i<arri.size(); i++){
Xiarr[i]=arri[i];
}
return Xiarr;
}
// Version of str_to_Xdarr imported from Diagnotics.cpp
VectorXd str_to_Xdarr(const std::string str, const std::string delimiters){
std::vector<std::string> words;
Eigen::VectorXd Xdarr;
words=strsplit(str, delimiters);
Xdarr.resize(words.size());
for(int i=0; i<words.size(); i++){
if(strtrim(words[i]) != ""){ // Added on 1 May 2018 ... check the impact
std::stringstream(words[i]) >> Xdarr[i];
}
}
return Xdarr;
}
std::vector<double> str_to_dblarr(const std::string str, const std::string delimiters){
std::vector<std::string> words;
std::vector<double> dblarr;
words=strsplit(str, delimiters);
dblarr.resize(words.size());
for(int i=0; i<words.size(); i++){
std::stringstream(strtrim(words[i])) >> dblarr[i];
}
return dblarr;
}
std::vector<double> arrstr_to_arrdbl(const std::vector<std::string> vals_strs){
/*
* Used to extract vector<double> from an array of strings (vector<string>).
* The terminator indicates the symbol that indicate the end of a line (beyond it is comments)
* The delimiter indicates how the values are separated (e.g. with a "," or with " ")
*/
std::vector<double> dbl_out;
dbl_out.resize(vals_strs.size());
for (int i=0; i<vals_strs.size(); i++){
std::stringstream(vals_strs[i]) >> dbl_out[i];
}
return dbl_out;
}
VectorXd arrstr_to_Xdarrdbl(const std::vector<std::string> vals_strs){
/*
* Used to extract VectorXd from an array of strings (vector<string>).
*/
VectorXd dbl_out;
dbl_out.resize(vals_strs.size());
for (int i=0; i<vals_strs.size(); i++){
std::stringstream(vals_strs[i]) >> dbl_out[i];
}
return dbl_out;
}
VectorXi arrstr_to_Xiarr(const std::vector<std::string> vals_strs){
/*
* Used to extract VectorXd from an array of strings (vector<string>).
*/
VectorXi int_out;
int_out.resize(vals_strs.size());
for (int i=0; i<vals_strs.size(); i++){
std::stringstream(vals_strs[i]) >> int_out[i];
}
return int_out;
}
std::vector<double> str_to_arrdbl(const std::string str, const std::string delimiters){
/*
* Used to extract arrays of values from a string.
* The terminator indicates the symbol that indicate the end of a line (beyond it is comments)
* The delimiter indicates how the values are separated (e.g. with a "," or with " ")
*/
double dbl_v;
std::string str0;
std::vector<std::string> vals_strs;
std::vector<double> dbl_out;
str0=strtrim(str); // remove blanks and convert to a stream
vals_strs=strsplit(str0, delimiters); // get all values in a string
for (int i=0; i<vals_strs.size(); i++){
if(strtrim(vals_strs[i]) !=""){
std::stringstream(vals_strs[i]) >> dbl_v;
dbl_out.push_back(dbl_v);
}
}
return dbl_out;
}
std::string dbl_to_str(const double ind){
std::stringstream ss;
ss.str(std::string());
ss << ind;
return ss.str();
}
std::string int_to_str(const int value){
std::ostringstream convert; // stream used for the conversion
convert << value; // insert the textual representation of 'Number' in the characters in the stream
return convert.str(); // set 'Result' to the contents of the stream
}
std::string lng_to_str(const long ind){
std::stringstream ss;
ss.str(std::string());
ss << ind;
return ss.str();
}
double str_to_dbl(const std::string str){
double dbl_out;
std::stringstream(strtrim(str)) >> dbl_out;
return dbl_out;
}
int str_to_int(const std::string str){
int int_out;
std::stringstream(strtrim(str)) >> int_out;
return int_out;
}
long str_to_long(const std::string str){
long long_out;
std::stringstream(strtrim(str)) >> long_out;
return long_out;
}
bool str_to_bool(const std::string str){
bool bool_out;
std::stringstream(strtrim(str)) >> bool_out;
return bool_out;
}
std::vector<int> str_to_arrint(const std::string str, const std::string delimiters){
/*
* Used to extract arrays of values from a string.
* The terminator indicates the symbol that indicate the end of a line (beyond it is comments)
* The delimiter indicates how the values are separated (e.g. with a "," or with " ")
*/
int int_v;
std::string str0;
std::vector<std::string> vals_strs;
std::vector<int> int_out;
str0=strtrim(str); // remove blanks and convert to a stream
vals_strs=strsplit(str0, delimiters); // get all values in a string
for (int i=0; i<vals_strs.size(); i++){
if(strtrim(vals_strs[i]) != ""){
std::stringstream(vals_strs[i]) >> int_v;
int_out.push_back(int_v);
}
}
return int_out;
}
std::vector<double> filter_range(const std::vector<double> param_in, const std::vector<double> f_in, const double fmin, const double fmax){
/* This function takes a vector of parameters (param_in) and returns only its values at
The indexes that matches the range condition fmin < f_in < fmax
Inputs:
param_in: input vector to be filtered
f_in: vector that contains the values that define the filtering condition
fmin: minimum valid value of f_in (lower bound for the rejection)
fmax: maximum valid value of f_in (upper bound for the rejection)
*/
std::vector<int> pos_OK;
std::vector<double> param_out;
pos_OK=where_in_range(f_in, fmin, fmax, 1);
for (int i=0; i<pos_OK.size(); i++){
if (pos_OK[i] != -1){
param_out.push_back(param_in[pos_OK[i]]);
}
}
return param_out;
}
std::vector<bool> filter_range(const std::vector<bool> param_in, const std::vector<double> f_in, const double fmin, const double fmax){
/* This function takes a vector of parameters (param_in) and returns only its values at
The indexes that matches the range condition fmin < f_in < fmax
Inputs:
param_in: input vector to be filtered
f_in: vector that contains the values that define the filtering condition
fmin: minimum valid value of f_in (lower bound for the rejection)
fmax: maximum valid value of f_in (upper bound for the rejection)
*/
std::vector<int> pos_OK;
std::vector<bool> param_out;
pos_OK=where_in_range(f_in, fmin, fmax, 1);
for (int i=0; i<pos_OK.size(); i++){
if (pos_OK[i] != -1){
param_out.push_back(param_in[pos_OK[i]]);
}
}
return param_out;
}