-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathexprtk_vector_resize_inline_example.cpp
155 lines (122 loc) · 5.2 KB
/
exprtk_vector_resize_inline_example.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
/*
**************************************************************
* C++ Mathematical Expression Toolkit Library *
* *
* Vector Resize Inline Within Expression Example *
* 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 <cstdio>
#include <string>
#include <vector>
#include <map>
#include "exprtk.hpp"
template <typename T>
class my_vv_size_handler_t final : public exprtk::igeneric_function<T>
{
public:
typedef typename exprtk::igeneric_function<T> igfun_t;
typedef typename igfun_t::parameter_list_t parameter_list_t;
typedef typename igfun_t::generic_type generic_type;
typedef typename generic_type::vector_view vector_t;
typedef typename generic_type::scalar_view scalar_t;
typedef exprtk::vector_view<T>* vv_ptr_t;
typedef std::map<void*,vv_ptr_t> map_t;
using exprtk::igeneric_function<T>::operator();
my_vv_size_handler_t()
: exprtk::igeneric_function<T>("VT")
{}
inline T operator()(parameter_list_t parameters) override
{
vector_t vector(parameters[0]);
size_t new_size = static_cast<std::size_t>(scalar_t(parameters[1])());
void* key = static_cast<void*>(&vector[0]);
typename map_t::iterator itr = vector_map_.find(key);
if (itr == vector_map_.end())
{
printf("my_vv_size_handler - Error: failed to find vector_view for address: %p\n",key);
return T(0);
}
exprtk::vector_view<T>& vv = *itr->second;
if (vv.base_size() < new_size)
{
printf("my_vv_size_handler - Error: vector_view for address: %p new size of %d greater than base_size of %d\n",
key,
static_cast<unsigned int>(new_size),
static_cast<unsigned int>(vv.base_size()));
return T(0);
}
return vv.set_size(new_size) ? T(1) : T(0);
}
void register_vector_view(exprtk::vector_view<T>& vec_view)
{
vector_map_[vec_view.data()] = &vec_view;
}
private:
map_t vector_map_;
};
template <typename T>
void vector_inline_resize_example()
{
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;
const std::string inline_resize_expression =
" var vec_original_size := v[]; "
" "
" for (var i := 1; i <= 2 * vec_original_size; i += 1) "
" { "
" if (resize(v,i) == true and v[] == i and sum(2v) == (i^2 + i)) "
" { "
" println('Success: set size: ', i ,' v[] = ', v[]); "
" } "
" else "
" { "
" println('Error: Failed to resize vector v to size: ', i); "
" } "
" }; ";
std::vector<T> v0 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
std::vector<T> v1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
exprtk::vector_view<T> vv = exprtk::make_vector_view(v0, v0.size());
exprtk::rtl::io::package<T> io_package;
my_vv_size_handler_t<T> vv_size_handler;
vv_size_handler.register_vector_view(vv);
symbol_table_t symbol_table;
symbol_table.add_vector ("v", vv);
symbol_table.add_function("resize", vv_size_handler);
symbol_table.add_package (io_package);
expression_t expression;
expression.register_symbol_table(symbol_table);
parser_t parser;
if (!parser.compile(inline_resize_expression, expression))
{
printf("Error: %s\tExpression: %s\n",
parser.error().c_str(),
inline_resize_expression.c_str());
return;
}
vv.rebase(v0.data());
vv_size_handler.register_vector_view(vv);
expression.value();
vv.rebase(v1.data());
vv_size_handler.register_vector_view(vv);
expression.value();
}
int main()
{
vector_inline_resize_example<double>();
return 0;
}
/*
Build:
c++ -pedantic-errors -Wall -Wextra -Werror -O3 -DNDEBUG -o exprtk_vector_inline_resize_example exprtk_vector_inline_resize_example.cpp -L/usr/lib -lstdc++ -lm
*/