Skip to content

Commit 3476100

Browse files
committed
Add remove_unused_variables_and_functions()
Minor version bump #24
1 parent 20c2615 commit 3476100

File tree

2 files changed

+116
-2
lines changed

2 files changed

+116
-2
lines changed

tests/tetests.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2425,6 +2425,101 @@ TEST_CASE("Is variable used", "[functions]")
24252425
CHECK_FALSE(p.is_variable_used(("zz")));
24262426
CHECK_FALSE(p.is_variable_used(("TRESS_L")));
24272427
}
2428+
2429+
TEST_CASE("Remove unused variables", "[variables]")
2430+
{
2431+
te_parser p;
2432+
// nothing in here yet
2433+
p.remove_unused_variables_and_functions();
2434+
2435+
p.set_variables_and_functions({
2436+
{"STRESS_L", static_cast<te_type>(10.1) },
2437+
{"P_LEVEL", static_cast<te_type>(.5) },
2438+
{"z", static_cast<te_type>(.75) } });
2439+
CHECK(p.get_variables_and_functions().size() == 3);
2440+
2441+
p.compile(("z + P_LEVEL"));
2442+
CHECK(p.is_variable_used(("Z")));
2443+
CHECK_FALSE(p.is_variable_used(("STRESS_L")));
2444+
CHECK(p.is_variable_used(("P_LEVEL")));
2445+
CHECK(p.success());
2446+
CHECK(p.get_variables_and_functions().size() == 3);
2447+
p.remove_unused_variables_and_functions();
2448+
CHECK(p.get_variables_and_functions().size() == 2);
2449+
2450+
p.compile(("P_LEVEL"));
2451+
CHECK(p.success());
2452+
CHECK_FALSE(p.is_variable_used(("Z")));
2453+
CHECK_FALSE(p.is_variable_used(("STRESS_L")));
2454+
CHECK(p.is_variable_used(("P_LEVEL")));
2455+
p.remove_unused_variables_and_functions();
2456+
CHECK(p.get_variables_and_functions().size() == 1);
2457+
2458+
p.compile(("5 + 2"));
2459+
p.remove_unused_variables_and_functions();
2460+
CHECK(p.get_variables_and_functions().empty());
2461+
2462+
// remove all at once
2463+
p.set_variables_and_functions({
2464+
{"STRESS_L", static_cast<te_type>(10.1) },
2465+
{"P_LEVEL", static_cast<te_type>(.5) },
2466+
{"z", static_cast<te_type>(.75) } });
2467+
p.compile(("5 + 2"));
2468+
p.remove_unused_variables_and_functions();
2469+
CHECK(p.get_variables_and_functions().empty());
2470+
2471+
// shouldn't do anything
2472+
p.remove_unused_variables_and_functions();
2473+
}
2474+
2475+
TEST_CASE("Remove unused functions", "[functions]")
2476+
{
2477+
te_parser p;
2478+
// nothing in here yet
2479+
p.remove_unused_variables_and_functions();
2480+
2481+
p.set_variables_and_functions({
2482+
{"STRESS_L", return5 },
2483+
{"P_LEVEL", __mult },
2484+
{"z", AddEm } });
2485+
CHECK(p.get_variables_and_functions().size() == 3);
2486+
2487+
p.compile(("z(2,5) + P_LEVEL(2,30,4,5)"));
2488+
CHECK(p.is_function_used(("Z")));
2489+
CHECK_FALSE(p.is_function_used(("STRESS_L")));
2490+
CHECK(p.is_function_used(("P_LEVEL")));
2491+
CHECK(p.success());
2492+
p.remove_unused_variables_and_functions();
2493+
CHECK(p.get_variables_and_functions().size() == 2);
2494+
2495+
p.compile(("P_LEVEL(2,30,4,5)"));
2496+
CHECK(p.success());
2497+
CHECK_FALSE(p.is_function_used(("Z")));
2498+
CHECK_FALSE(p.is_function_used(("STRESS_L")));
2499+
CHECK(p.is_function_used(("P_LEVEL")));
2500+
p.remove_unused_variables_and_functions();
2501+
CHECK(p.get_variables_and_functions().size() == 1);
2502+
2503+
p.compile(("5 + 2"));
2504+
p.remove_unused_variables_and_functions();
2505+
CHECK(p.get_variables_and_functions().empty());
2506+
2507+
// remove all at once
2508+
p.set_variables_and_functions({
2509+
{"STRESS_L", return5 },
2510+
{"P_LEVEL", __mult },
2511+
{"z", AddEm } });
2512+
p.compile(("5 + 2"));
2513+
p.remove_unused_variables_and_functions();
2514+
CHECK(p.get_variables_and_functions().empty());
2515+
2516+
// function shouldn't be recognized now
2517+
p.compile(("P_LEVEL(2,30,4,5)"));
2518+
CHECK_FALSE(p.success());
2519+
2520+
// shouldn't do anything
2521+
p.remove_unused_variables_and_functions();
2522+
}
24282523
#endif
24292524

24302525
TEST_CASE("Custom test", "[functions]")

tinyexpr.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@
8181
constexpr int TINYEXPR_CPP_MAJOR_VERSION = 1;
8282
constexpr int TINYEXPR_CPP_MINOR_VERSION = 0;
8383
constexpr int TINYEXPR_CPP_PATCH_VERSION = 0;
84-
constexpr int TINYEXPR_CPP_TWEAK_VERSION = 0;
85-
constexpr wchar_t TINYEXPR_CPP_COPYRIGHT[] = L"TinyExpr: Copyright (c) 2015-2020 Lewis Van Winkle\nTinyExpr++: Copyright (c) 2020-2025 Blake Madden";
84+
constexpr int TINYEXPR_CPP_TWEAK_VERSION = 1;
85+
constexpr wchar_t TINYEXPR_CPP_COPYRIGHT[] = L"TinyExpr: Copyright (c) 2015-2020 Lewis Van Winkle\n"
86+
"TinyExpr++: Copyright (c) 2020-2025 Blake Madden";
8687

8788
class te_parser;
8889

@@ -567,6 +568,24 @@ class te_parser
567568
}
568569
}
569570

571+
#ifndef TE_NO_BOOKKEEPING
572+
/// @brief Removes any custom variables and functions that weren't used in the last compilation.
573+
/// @details This can be useful if the parser is pre-loaded with a large number of
574+
/// variables and functions that needs to be pruned after the first expression is parsed.
575+
/// @warning After calling this, any custom variables and functions that weren't found
576+
/// in the previously parsed expression will no longer be available.
577+
void remove_unused_variables_and_functions()
578+
{
579+
for (auto funcIter = m_customFuncsAndVars.cbegin(); funcIter != m_customFuncsAndVars.cend();
580+
/* in loop*/)
581+
{
582+
funcIter = (is_function_used(funcIter->m_name) || is_variable_used(funcIter->m_name)) ?
583+
++funcIter :
584+
m_customFuncsAndVars.erase(funcIter);
585+
}
586+
}
587+
#endif
588+
570589
/** @brief Sets a custom function to resolve unknown symbols in an expression.
571590
@param usr The function to use to resolve unknown symbols.
572591
@param keepResolvedVariables @c true to cache any resolved variables into the parser.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy