-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcvar.cc
More file actions
274 lines (245 loc) · 6.93 KB
/
cvar.cc
File metadata and controls
274 lines (245 loc) · 6.93 KB
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
/*
* Variant Variables (used by CExp)
*
* This program is distributed under the terms of the GPL v3.0 or later
* Download the GNU Public License (GPL) from <https://www.gnu.org>
*
* Copyright(C) 2025, Free Software Foundation, Inc.
* Written by Nicholas Christopoulos <mailto:netnic@proton.me>
*/
#ifndef CVAR_H
#define CVAR_H 0x10000
#include <cstddef>
#include <cstring>
#include <cstdarg>
#include <string>
#include <stdexcept>
#include <unordered_map>
#ifndef CEXP_DATA_T
#define CEXP_DATA_T
typedef ssize_t int_t;
typedef size_t uint_t;
typedef double real_t;
#endif
typedef enum { v_int, v_float, v_str, v_matrix } cvar_type_t;
#include "cmatrix.cc"
class CExp;
class CVar {
private:
cvar_type_t type; // type of data in this block
int_t i; // integer value
real_t f; // floating point value
std::string s; // string value
CMatrix m; // matrix value
#ifdef CEXP_USE_PROPS
std::unordered_map<std::string,CVar> props; // properties (if used)
#endif
public:
CVar() { type = v_int; i = 0; f = 0; }
CVar(int n) { type = v_int; i = n; f = 0; }
CVar(int_t n) { type = v_int; i = n; f = 0; }
CVar(uint_t n) { type = v_int; i = n; f = 0; }
CVar(real_t n) { type = v_float; i = 0; f = n; }
CVar(const std::string& sp) { type = v_str; i = 0; f = 0; s = sp; }
CVar(const CMatrix& mp) { type = v_matrix; i = 0; f = 0; m = mp; }
CVar(const CVar& src) {
type = src.type;
#ifdef CEXP_USE_PROPS
props = src.props;
#endif
switch ( type ) {
case v_int: i = src.i; break;
case v_float: f = src.f; break;
case v_str: s = src.s; break;
case v_matrix:
if ( src.m.size() )
m = src.m;
else if ( m.size() )
m.clear();
break;
}
}
virtual ~CVar() { clear(); }
void clear() {
type = v_int;
i = 0; f = 0;
if ( !s.empty() ) s.clear();
if ( m.size() ) m.clear();
#ifdef CEXP_USE_PROPS
if ( !props.empty() )
props.clear();
#endif
}
const CVar& operator=(const CVar& src) {
clear();
type = src.type;
#ifdef CEXP_USE_PROPS
props = src.props;
#endif
switch ( type ) {
case v_int: i = src.i; break;
case v_float: f = src.f; break;
case v_str: s = src.s; break;
case v_matrix: m = src.m; break;
break;
}
return *this;
}
void seti(int_t n) { clear(); type = v_int; i = n; }
void seti(uint_t n) { clear(); type = v_int; i = n; }
void setf(real_t n) { clear(); type = v_float; f = n; }
void sets(const std::string& _s) { clear(); type = v_str; s = _s; }
void setm(const CMatrix& _m) { clear(); type = v_matrix; m = _m; }
int_t geti() const {
switch ( type ) {
case v_int: return i;
case v_float: return (int_t) f;
case v_str: return atoll(s.c_str());
case v_matrix: throw std::runtime_error("matrix cannot converted to integer");
}
throw std::runtime_error("data type corrupted");
}
real_t getf() const {
switch ( type ) {
case v_int: return i;
case v_float: return f;
case v_str: return strtod(s.c_str(), NULL);
case v_matrix: throw std::runtime_error("matrix cannot converted to real number");
}
throw std::runtime_error("data type corrupted");
}
std::string gets() const {
switch ( type ) {
case v_int: return std::to_string(i);
case v_float: return std::to_string(f);
case v_str: return s;
case v_matrix: {
std::string z = "[";
for ( size_t r = 0; r < m.rows(); r ++ ) {
for ( size_t c = 0; c < m.cols(); c ++ ) {
z = z + std::to_string(m.get(r,c));
if ( c < m.cols() - 1 )
z += ",";
}
if ( r < m.rows() - 1 )
z += ";";
}
z += "]";
return z;
}
}
throw std::runtime_error("data type corrupted");
}
inline const CMatrix& getm() const { return m; }
void print() const;
void print(const char *fmt, ...) const;
friend class CExp;
friend CVar operator+(const CVar& a, const CVar& b);
friend CVar operator-(const CVar& a, const CVar& b);
friend CVar operator*(const CVar& a, const CVar& b);
friend CVar operator/(const CVar& a, const CVar& b);
friend CVar operator%(const CVar& a, const CVar& b);
};
CVar operator+(const CVar& a, const CVar& b);
CVar operator-(const CVar& a, const CVar& b);
CVar operator*(const CVar& a, const CVar& b);
CVar operator/(const CVar& a, const CVar& b);
CVar operator%(const CVar& a, const CVar& b);
#endif
/* -------------------------------------------------------------------------------- */
#ifdef CVAR_IMPL
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define CMATRIX_IMPL
#include "cmatrix.cc"
using namespace std;
//
CVar operator+(const CVar& a, const CVar& b) {
if ( a.type == v_int && b.type == v_int )
return (a.i + b.i);
else if ( a.type == v_str && b.type == v_str )
return (a.s + b.s);
else if ( a.type == v_matrix && b.type == v_matrix )
return (a.m + b.m);
else if ( a.type == v_matrix && b.type <= v_str )
return (a.m + b.getf());
else if ( a.type <= v_str && b.type <= v_str )
return (a.getf() + b.getf());
throw runtime_error("Type mismatch");
}
CVar operator-(const CVar& a, const CVar& b) {
if ( a.type == v_int && b.type == v_int )
return (a.i - b.i);
else if ( a.type == v_matrix && b.type == v_matrix )
return (a.m - b.m);
else if ( a.type == v_matrix && b.type <= v_str )
return (a.m - b.getf());
else if ( a.type <= v_str && b.type <= v_str )
return (a.getf() - b.getf());
throw runtime_error("Type mismatch");
}
CVar operator*(const CVar& a, const CVar& b) {
if ( a.type == v_int && b.type == v_int )
return (a.i * b.i);
else if ( a.type == v_matrix && b.type == v_matrix )
return (a.m * b.m);
else if ( a.type == v_matrix && b.type <= v_float )
return (a.m * b.getf());
else if ( a.type <= v_float && b.type == v_matrix )
return (b.m * a.getf());
else if ( a.type <= v_str && b.type <= v_str )
return (a.getf() * b.getf());
throw runtime_error("Type mismatch");
}
CVar operator/(const CVar& a, const CVar& b) {
if ( b.getf() == 0.0 )
throw runtime_error("Division by zero");
if ( a.type <= v_str && b.type <= v_str )
return (a.getf() / b.getf());
if ( a.type == v_matrix && b.type <= v_str )
return (a.m / b.getf());
throw runtime_error("Type mismatch");
}
CVar operator%(const CVar& a, const CVar& b) {
if ( b.geti() == 0 )
throw runtime_error("Division by zero");
if ( a.type <= v_str && b.type <= v_str )
return (a.geti() % b.geti());
if ( a.type == v_matrix && b.type <= v_str )
return (a.m % b.geti());
throw runtime_error("Type mismatch");
}
void CVar::print() const {
switch ( type ) {
case v_int:
printf("%zd\n", i);
break;
case v_float:
printf("%g\n", f);
break;
case v_str:
printf("%s\n", s.c_str());
break;
case v_matrix:
for ( size_t r = 0; r < m.rows(); r ++ ) {
printf("|\t");
for ( size_t c = 0; c < m.cols(); c ++ )
printf("%g\t", m.get(r, c));
printf("|\n");
}
break;
default:
throw runtime_error("variable type is wrong!");
}
}
void CVar::print(const char *fmt, ...) const {
char buf[LINE_MAX];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, LINE_MAX, fmt, ap);
va_end(ap);
printf("%s", buf);
print();
}
#endif // implementation