-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathexprtk_groups_examples.cpp
516 lines (423 loc) · 16.5 KB
/
exprtk_groups_examples.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
/*
**************************************************************
* C++ Mathematical Expression Toolkit Library *
* *
* Group Example (add, create, contains, size, get, erase) *
* Author: Arash Partow (1999-2024) *
* URL: https://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
* Free use of the Mathematical Expression Toolkit Library is *
* permitted under the guidelines and in accordance with the *
* most current version of the MIT License. *
* https://www.opensource.org/licenses/MIT *
* SPDX-License-Identifier: MIT *
* *
**************************************************************
*/
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cstdio>
#include <iterator>
#include <map>
#include <set>
#include <string>
#include "exprtk.hpp"
namespace groups_impl
{
template <typename T>
struct groups_collection
{
bool add_group(const std::string& group_name, const std::set<T>& set)
{
const auto itr = groups.find(group_name);
if (itr != groups.end())
{
return false;
}
groups[group_name] = set;
return true;
}
bool contains(const std::string& group_name) const
{
return groups.count(group_name) > 0;
}
using set_t = std::set<T>;
using ptr_t = set_t*;
ptr_t get(const std::string group_name)
{
auto itr = groups.find(group_name);
if (itr == groups.end())
{
return nullptr;
}
return &(itr->second);
}
std::map<std::string, std::set<T>> groups;
};
template <typename T>
class create final : public exprtk::igeneric_function<T>
{
public:
using igfun_t = typename exprtk::igeneric_function<T>;
using parameter_list_t = typename igfun_t::parameter_list_t;
using generic_type = typename igfun_t::generic_type;
using string_t = typename generic_type::string_view;
using strgrps_cllctn = groups_collection<std::string>;
using exprtk::igeneric_function<T>::operator();
create(groups_collection<std::string>& groups)
: exprtk::igeneric_function<T>("S")
, groups_(groups)
{ exprtk::rtl::io::file::details::perform_check<T>(); }
inline T operator() (parameter_list_t parameters) override
{
const std::string group_name = to_str(string_t(parameters[0]));
if (
!groups_.contains(group_name) &&
!groups_.add_group(group_name, {})
)
{
return T(0);
}
auto ptr = groups_.get(group_name);
const std::size_t ptr_size = sizeof(ptr);
T result = T(0);
std::memcpy(reinterpret_cast<char*>(&result ),
reinterpret_cast<char*>(&ptr),
ptr_size);
return result;
}
strgrps_cllctn groups_;
};
template <typename SetType, typename T>
SetType* make_set_handle(T v)
{
const std::size_t ptr_size = sizeof(SetType*);
SetType* ptr = reinterpret_cast<SetType*>(0);
std::memcpy(reinterpret_cast<exprtk::details::char_ptr>(&ptr),
reinterpret_cast<exprtk::details::char_ptr>(&v ),
ptr_size);
return ptr;
}
template <typename T>
class add final : public exprtk::igeneric_function<T>
{
public:
using igfun_t = typename exprtk::igeneric_function<T>;
using parameter_list_t = typename igfun_t::parameter_list_t;
using generic_type = typename igfun_t::generic_type;
using scalar_t = typename generic_type::scalar_view;
using string_t = typename generic_type::string_view;
using strgrps_cllctn = groups_collection<std::string>;
using exprtk::igeneric_function<T>::operator();
add(groups_collection<std::string>& groups)
: exprtk::igeneric_function<T>("TS*")
, groups_(groups)
{ exprtk::rtl::io::file::details::perform_check<T>(); }
inline T operator() (parameter_list_t parameters) override
{
auto ptr = scalar_t(parameters[0])();
assert(ptr != T(0));
auto& set = *make_set_handle<strgrps_cllctn::set_t>(ptr);
return process(set, parameters);
}
private:
inline T process(strgrps_cllctn::set_t& set, const parameter_list_t& parameters)
{
for (std::size_t i = 1; i < parameters.size(); ++i)
{
set.insert(to_str(string_t(parameters[i])));
}
return T(1);
}
strgrps_cllctn groups_;
};
template <typename T>
class contains final : public exprtk::igeneric_function<T>
{
public:
using igfun_t = typename exprtk::igeneric_function<T>;
using parameter_list_t = typename igfun_t::parameter_list_t;
using generic_type = typename igfun_t::generic_type;
using scalar_t = typename generic_type::scalar_view;
using string_t = typename generic_type::string_view;
using strgrps_cllctn = groups_collection<std::string>;
using exprtk::igeneric_function<T>::operator();
contains(groups_collection<std::string>& groups)
: exprtk::igeneric_function<T>("TS*")
, groups_(groups)
{ exprtk::rtl::io::file::details::perform_check<T>(); }
inline T operator() (parameter_list_t parameters) override
{
auto ptr = scalar_t(parameters[0])();
assert(ptr != T(0));
const auto& set = *make_set_handle<strgrps_cllctn::set_t>(ptr);
return process(set, parameters);
}
private:
inline T process(const strgrps_cllctn::set_t& set, parameter_list_t& parameters) const
{
for (std::size_t i = 1; i < parameters.size(); ++i)
{
if (set.count(to_str(string_t(parameters[i]))))
{
return T(1);
}
}
return T(0);
}
strgrps_cllctn groups_;
};
template <typename T>
class size final : public exprtk::igeneric_function<T>
{
public:
using igfun_t = typename exprtk::igeneric_function<T>;
using parameter_list_t = typename igfun_t::parameter_list_t;
using generic_type = typename igfun_t::generic_type;
using scalar_t = typename generic_type::scalar_view;
using string_t = typename generic_type::string_view;
using strgrps_cllctn = groups_collection<std::string>;
using exprtk::igeneric_function<T>::operator();
size(groups_collection<std::string>& groups)
: exprtk::igeneric_function<T>("T")
, groups_(groups)
{ exprtk::rtl::io::file::details::perform_check<T>(); }
inline T operator() (parameter_list_t parameters) override
{
auto ptr = scalar_t(parameters[0])();
assert(ptr != T(0));
const auto& set = *make_set_handle<strgrps_cllctn::set_t>(ptr);
return T(set.size());
}
private:
strgrps_cllctn groups_;
};
template <typename T>
class get final : public exprtk::igeneric_function<T>
{
public:
using igfun_t = typename exprtk::igeneric_function<T>;
using parameter_list_t = typename igfun_t::parameter_list_t;
using generic_type = typename igfun_t::generic_type;
using scalar_t = typename generic_type::scalar_view;
using string_t = typename generic_type::string_view;
using strgrps_cllctn = groups_collection<std::string>;
using exprtk::igeneric_function<T>::operator();
get(groups_collection<std::string>& groups)
: igfun_t("TT",igfun_t::e_rtrn_string)
, groups_(groups)
{ exprtk::rtl::io::file::details::perform_check<T>(); }
inline T operator() (std::string& result, parameter_list_t parameters) override
{
const auto ptr = scalar_t(parameters[0])();
const int index = static_cast<int>(scalar_t(parameters[1])());
assert(ptr != T(0));
auto& set = *make_set_handle<strgrps_cllctn::set_t>(ptr);
if (
(index >= 0) &&
(static_cast<std::size_t>(index) >= set.size())
)
{
return T(0);
}
auto itr = set.begin();
std::advance(itr,index);
result = *itr;
return T(1);
}
private:
strgrps_cllctn groups_;
};
template <typename T>
class erase final : public exprtk::igeneric_function<T>
{
public:
using igfun_t = typename exprtk::igeneric_function<T>;
using parameter_list_t = typename igfun_t::parameter_list_t;
using generic_type = typename igfun_t::generic_type;
using scalar_t = typename generic_type::scalar_view;
using string_t = typename generic_type::string_view;
using strgrps_cllctn = groups_collection<std::string>;
using exprtk::igeneric_function<T>::operator();
erase(groups_collection<std::string>& groups)
: exprtk::igeneric_function<T>("TS*")
, groups_(groups)
{ exprtk::rtl::io::file::details::perform_check<T>(); }
inline T operator() (parameter_list_t parameters) override
{
auto ptr = scalar_t(parameters[0])();
assert(ptr != T(0));
auto& set = *make_set_handle<strgrps_cllctn::set_t>(ptr);
return process(set,parameters);
}
private:
inline T process(strgrps_cllctn::set_t& set, parameter_list_t& parameters)
{
std::size_t count = 0;
for (std::size_t i = 1; i < parameters.size(); ++i)
{
count += set.erase(to_str(string_t(parameters[i])));
}
return T(count);
}
strgrps_cllctn groups_;
};
} // namespace groups_impl
template <typename T>
void groups_example()
{
using symbol_table_t = exprtk::symbol_table<T>;
using expression_t = exprtk::expression<T>;
using parser_t = exprtk::parser<T>;
using strset_t = std::set<std::string>;
strset_t girl_names;
strset_t boy_names;
girl_names.insert("jane");
girl_names.insert("jill");
boy_names.insert("jack");
boy_names.insert("jim" );
boy_names.insert("john");
groups_impl::groups_collection<std::string> names_groups;
names_groups.add_group("girls", girl_names);
names_groups.add_group("boys", boy_names );
assert(names_groups.contains("girls"));
assert(names_groups.contains("boys" ));
groups_impl::create <T> group_create (names_groups);
groups_impl::add <T> group_add (names_groups);
groups_impl::contains<T> group_contains(names_groups);
groups_impl::size <T> group_size (names_groups);
groups_impl::get <T> group_get (names_groups);
groups_impl::erase <T> group_erase (names_groups);
exprtk::rtl::io::println<T> println;
symbol_table_t symbol_table;
expression_t expression;
parser_t parser;
symbol_table.add_function("create" , group_create );
symbol_table.add_function("add" , group_add );
symbol_table.add_function("contains", group_contains);
symbol_table.add_function("size" , group_size );
symbol_table.add_function("get" , group_get );
symbol_table.add_function("erase" , group_erase );
symbol_table.add_function("println" , println );
expression.register_symbol_table(symbol_table);
const std::string program =
" /* Load the groups from symbol table */ "
" var girls := create('girls'); "
" var boys := create('boys' ); "
" "
" if (not(girls)) "
" { "
" println('failed to create girls set!'); "
" return []; "
" }; "
" "
" if (not(boys)) "
" { "
" println('failed to create boys set!'); "
" return []; "
" }; "
" "
" var name := 'john'; "
" "
" if (contains(girls, name)) "
" { "
" println(name, ' in girls set'); "
" } "
" else "
" { "
" println(name, ' NOT in girls set'); "
" }; "
" "
" if (contains(boys, name)) "
" { "
" println(name, ' in boys set'); "
" } "
" else "
" { "
" println(name, ' NOT in boys set'); "
" }; "
" "
" /* Create a group from scratch */ "
" var pets := create('pets'); "
" "
" add(pets, 'buddy'); "
" add(pets, 'fluffy', 'snowy'); "
" add(pets, 'ziggy', 'monty', 'teddy'); "
" "
" if (contains(pets, name)) "
" { "
" println(name, ' in pets set'); "
" } "
" else "
" { "
" println(name, ' NOT in pets set'); "
" }; "
" "
" if (contains(pets, name, 'fluffy')) "
" { "
" println(name, ' or fluffy in pets set'); "
" } "
" else "
" { "
" println(name, ' or fluffy NOT in pets set'); "
" }; "
" "
" println('List of \\'pets\\':'); "
" "
" for (var i := 0; i < size(pets); i += 1) "
" { "
" println('[', i ,']: ', get(pets,i)); "
" }; "
" "
" if (erase(pets, 'fluffy', 'teddy') != 2) "
" { "
" println('Failed to erase fluffy and teddy'); "
" return []; "
" }; "
" "
" println('List of \\'pets\\':'); "
" "
" for (var i := 0; i < size(pets); i += 1) "
" { "
" println('[', i ,']: ', get(pets,i)); "
" }; "
" ";
if (!parser.compile(program, expression))
{
printf("Error: %s\tExpression: %s\n",
parser.error().c_str(),
program.c_str());
return;
}
expression.value();
// Benchmark
//{
// constexpr auto rounds = 100000;
//
// const auto begin = std::chrono::system_clock::now();
// {
// for (std::size_t i = 0; i < rounds; ++i)
// {
// expression.value();
// }
// }
// const auto end = std::chrono::system_clock::now();
//
// const auto total_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();
//
// printf("[*] total time: %5.4fsec rate: %8.4feval/sec\n",
// total_time_ms / 1000.0,
// (1000.0 * rounds) / total_time_ms);
//}
}
int main()
{
groups_example<double>();
return 0;
}
/*
Build:
c++ -pedantic-errors -Wall -Wextra -Werror -std=c++17 -O3 -DNDEBUG -o exprtk_groups_examples exprtk_groups_examples.cpp -lm
*/