1#ifndef CLICK_TINYEXPR_HH
2#define CLICK_TINYEXPR_HH
4#include <tinyexpr/tinyexpr.h>
8static double square_wave(
double x) {
9 if ((
int)(x * 2) % 2 < 1)
15static double t_min(
double a,
double b) {
22static double t_max(
double a,
double b) {
31class TinyExpr {
public:
33 static TinyExpr compile(
String expr,
int n_vars) {
35 static constexpr const char*
const var_names[] = {
"x",
"y",
"z"};
38 e._n_vars = n_vars + 3;
39 e._vars = (te_variable*)CLICK_LALLOC(
sizeof(te_variable) * e._n_vars);
40 e._vars_values = (
double*)CLICK_LALLOC(
sizeof(
double) * e._n_vars);
41 for (
int i = 0; i < n_vars; i++) {
42 e._vars[i] = {var_names[i], &e._vars_values[i], 0};
44 e._vars[n_vars] = (te_variable){
"squarewave", (
const void*)square_wave, TE_FUNCTION1};
45 e._vars[n_vars + 1] = (te_variable){
"min", (
const void*)t_min, TE_FUNCTION2};
46 e._vars[n_vars + 2] = (te_variable){
"max", (
const void*)t_max, TE_FUNCTION2};
48 e._expr = te_compile(expr.
c_str(), e._vars, e._n_vars, &error);
53 double eval(
double x) {
55 return te_eval(_expr);
59 return te_eval(_expr);
62 TinyExpr(
const TinyExpr& expr) {
64 if (_expr == expr._expr)
69 _n_vars = expr._n_vars;
71 _vars_values = expr._vars_values;
72 _use_count = expr._use_count;
73 _use_count = _use_count + 1;
76 operator bool()
const {
81 if (_expr && --*_use_count == 0) {
83 CLICK_LFREE(_vars, _n_vars);
84 CLICK_LFREE(_vars_values, _n_vars);
85 CLICK_LFREE(_use_count,
sizeof(
unsigned));
89 TinyExpr() : _expr(0) {
90 _use_count = (
unsigned*)CLICK_LALLOC(
sizeof(
unsigned));
A string of characters.
Definition string.hh:22
const char * c_str() const
Null-terminate the string.
Definition string.hh:519