-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scalar.h
371 lines (298 loc) · 9.31 KB
/
Scalar.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
#include <iostream>
#include <stdio.h>
#include <sstream>
#include <string>
#include <regex.h>
#include <map>
#include <vector>
#include <typeinfo>
#include <stdexcept>
#include <assert.h>
using namespace std;
namespace pl {
#define
//cout << __PRETTY_FUNCTION__ << endl;
#define nl endl
#define sep << "," <<
#define pnl cout << endl
#define pBool std::pair<bool,number>
#define MAX_RE_MATCHES 10
typedef double number;
enum scalar_subtypes { NUMBER = 1, STRING = 2 };
//========================UTILS====================================
template<class T>
void say(T val) { cout << val << nl; }
pBool str2num(const string& str) {
istringstream is(str);
pBool rv(false,0);
if (str == "0E0") {//Zero but true
rv = std::make_pair(true,0);
} else {
is >> rv.second;//convert
//logical XOR : !fail != !0?
if ( !is.fail() != (rv.second == 0) ) rv.first = true;
};
return rv;
};
string num2str(const number n) {
ostringstream os;
os << n;
return os.str();
};
//========================Scalar====================================
class Scalar {
protected:
struct { string s; number n; } value;
scalar_subtypes sub_type;
scalar_subtypes get_type() const { return sub_type;}
const number get_num() const { return value.n; }
const string& get_str() const { return value.s; }
void set_type(const scalar_subtypes t) { sub_type = t;}
void set_num(const number& num) { value.n = num; set_type(NUMBER); }
void set_str(const string& str) {value.s = str; set_type(STRING); };
public:
regmatch_t matches[MAX_RE_MATCHES];//list of the matches in re-match
scalar_subtypes type() const { return get_type();}
bool is_num() const { return get_type() == NUMBER; }
bool is_str() const { return get_type() == STRING; }
const number num() const { return get_num(); }
const string& str() const { return get_str(); }
number to_num() const { return is_num() ? get_num() : str2num(get_str()).second; }
string to_str() const { return is_str() ? get_str() : num2str(get_num()); }
void num(const number& n) { set_num(n); }
void str(const string& s) { set_str(s);};
Scalar() { set_num(0); };
//copy constructor : Scalar $s = 55
Scalar(const Scalar& c) {
c.get_type() == NUMBER ? set_num(c.num()) : set_str(c.str());
}
Scalar(const string& x) { set_str(x); };
Scalar(const char* x) {
string s = x;//convert to string first
set_str(s);
};
Scalar(const number& n) { set_num(n); };//$s = 55
Scalar(const int& n) { set_num(n); };//Scalar $s = 0; zero-ambiugity assignment conv
friend ostream& operator << (ostream& out, const Scalar& s) {
s.is_num() ? out << s.num() : out << s.str();
return out;
};
operator number() const { return to_num(); }
operator float() const { return to_num(); }
operator int() const { return to_num(); }
operator char() const { return to_num(); }
operator string() const { return to_str(); }
//mimic boolean
operator bool() const { return is_num() ? num() != 0 : str2num(str()).first; }
Scalar& operator = (const Scalar& rhs) {
if (this == &rhs) return *this;//self-assignment no,no..!
assert(rhs.is_str() || rhs.is_num());
rhs.is_num() ? num(rhs.num()) : str(rhs.str());
return *this;
}
template<class T>
Scalar& operator = (const T& rhs) {
return *this = Scalar(rhs);
}
//++prefix: return ref
Scalar& operator ++ () {
if (is_num()) num(num()+1);
//if string nothig to do, just return
return *this;
}
//postfix++ : return copy
Scalar operator ++ (int) {
Scalar $rv(num());
++(*this);
return $rv;
}
Scalar& operator -- () {
if (is_num()) num(num()-1);
//if string nothig to do, just return
return *this;
}
//postfix--
Scalar operator -- (int) {
Scalar $rv(num());
--(*this);
return $rv;
}
//================== +,-,*,/ operators =========================
Scalar& operator += (const Scalar& rhs) {
if (is_num() && rhs.is_num()) { num(num() + rhs.num()); return *this; }
if (is_num() && rhs.is_str()) {
number n1 = rhs.to_num(); num( n1 ? num() + n1 : num() + 0);
return *this;
}
if (is_str() && rhs.is_num()) {
number n1 = to_num(); num( n1 ? n1 + rhs.num() : 0 + rhs.num());
return *this;
}
if (is_str() && rhs.is_str()) {
number n1 = to_num();
number n2 = rhs.to_num();
//logical XOR : first case str+str OR num+num, else ....
if (!n1 != !n2) { n1 && !n2 ? num(n1 + 0) : num(0 + n1); }
else { n1 && n2 ? num(n1 + n2) : num(n1 + n2); }
}
return *this;
}
template<class T>
Scalar operator + (const T& rhs) {
//first make a copy then do shortcut-summation
Scalar $rv = *this;
$rv += Scalar(rhs);
return $rv;
}
Scalar& operator -= (const Scalar& rhs) {
if (is_num() && rhs.is_num()) { num(num() - rhs.num()); return *this; }
if (is_num() && rhs.is_str()) {
number n1 = rhs.to_num(); num( n1 ? num() - n1 : num() - 0);
return *this;
}
if (is_str() && rhs.is_num()) {
number n1 = to_num(); num( n1 ? n1 - rhs.num() : 0 - rhs.num());
return *this;
}
if (is_str() && rhs.is_str()) {
number n1 = to_num();
number n2 = rhs.to_num();
//logical XOR : first case str-str OR num-num, else ....
if (!n1 != !n2) { n1 && !n2 ? num(n1 - 0) : num(0 - n1); }
else { n1 && n2 ? num(n1 - n2) : num(n1 - n2); }
}
return *this;
}
template<class T>
Scalar operator - (const T& rhs) {
Scalar $rv = *this;
$rv -= Scalar(rhs);
return $rv;
}
Scalar& operator *= (const Scalar& rhs) {
if (is_num() && rhs.is_num()) { num(num() * rhs.num()); return *this; }
if (is_num() && rhs.is_str()) {
number n1 = rhs.to_num(); num( n1 ? num() * n1 : num() * 0);
return *this;
}
if (is_str() && rhs.is_num()) {
number n1 = to_num(); num( n1 ? n1 * rhs.num() : 0 * rhs.num());
return *this;
}
if (is_str() && rhs.is_str()) {
number n1 = to_num();
number n2 = rhs.to_num();
//logical XOR : first case str*str OR num*num, else ....
if (!n1 != !n2) { n1 && !n2 ? num(n1 * 0) : num(0 * n1); }
else { n1 && n2 ? num(n1 * n2) : num(n1 * n2); }
}
return *this;
}
template<class T>
Scalar operator * (const T& rhs) {
Scalar $rv = *this;
$rv *= Scalar(rhs);
return $rv;
}
Scalar& operator /= (const Scalar& rhs) {
if (is_num() && rhs.is_num()) { num(num() / rhs.num()); return *this; }
if (is_num() && rhs.is_str()) {
number n1 = rhs.to_num(); num( n1 ? num() / n1 : throw std::overflow_error("Divide by zero exception"));
return *this;
}
if (is_str() && rhs.is_num()) {
number n1 = to_num(); num( n1 ? n1 / rhs.num() : 0 / rhs.num());
return *this;
}
if (is_str() && rhs.is_str()) {
number n1 = to_num();
number n2 = rhs.to_num();
//logical XOR : first case str/str OR num/num, else ....
if (!n1 != !n2) { n1 && !n2 ? throw std::overflow_error("Divide by zero exception") : num(0 / n1); }
else { n1 && n2 ? num(n1 / n2) : num(n1 / n2); }
}
return *this;
}
template<class T>
Scalar operator / (const T& rhs) {
Scalar $rv = *this;
$rv /= Scalar(rhs);
return $rv;
}
//================== comparison operators =========================
bool operator == (const Scalar& rhs) const {
if (is_num() && rhs.is_num()) return num() == rhs.num();
if (is_num() && rhs.is_str()) return num() == rhs.to_num();
if (is_str() && rhs.is_num()) return to_num() == rhs.num();
if (is_str() && rhs.is_str()) return str() == rhs.str();
return false;
}
template<class T>//T = number,string,Scalar
bool operator == (const T& rhs) const {
return *this == Scalar(rhs);
}
bool operator != (const Scalar& rhs) const {
return !(*this == rhs);
}
template<class T>
bool operator != (const T& rhs) const {
return *this != Scalar(rhs);
}
bool operator > (const Scalar& rhs) const {
if (is_num() && rhs.is_num()) return num() > rhs.num();
if (is_num() && rhs.is_str()) return true;//num is always bigger
if (is_str() && rhs.is_num()) return false;
if (is_str() && rhs.is_str()) return str() > rhs.str();//!FIXME
return false;
}
template<class T>
bool operator > (const T& rhs) const {
return *this > Scalar(rhs);
}
bool operator >= (const Scalar& rhs) const {
return (*this > rhs || *this == rhs);
}
template<class T>
bool operator >= (const T& rhs) const {
return *this >= Scalar(rhs);
}
bool operator <= (const Scalar& rhs) const {
return (*this < rhs || *this == rhs);
}
template<class T>
bool operator <= (const T& rhs) const {
return *this <= Scalar(rhs);
}
bool operator < (const Scalar& rhs) const {
return !(*this >= rhs);
}
template<class T>//T = number,string,Scalar
bool operator < (const T& rhs) const {
return (*this < Scalar(rhs));
}
//--- bitwise OR | - is used for concatenation i.e. Scalar1 | Scalar2 | Scalar3 | ....
Scalar operator | (const Scalar& rhs) {
return to_str() + rhs.to_str();
}
//=== regex match operator Ex.: if ( scalar_var ^= "lookfor") .....
bool operator ^=(const string& re_str) {
if (is_num()) return false;//nothing to match in a number
int rvc;//return value of compilation
regex_t exp;//compiled regex
rvc = regcomp(&exp,re_str.c_str(),REG_EXTENDED);//compile the re
if (rvc != 0) return false;//couldn't compile
int rv = regexec(&exp, str().c_str(), MAX_RE_MATCHES, matches, 0);
regfree(&exp);
return rv == 0 ? true : false;
};
void dump() const {
cout << " .type:" << type();
cout << ", .num:" << num();
cout << ", .str:" << str() << nl;
}
void dump(const char* var_name) const {
cout << "VAR:" << var_name;
dump();
}
};
};//end namespace