From 3501146e35aab9da0d32b53fdf5050f3d2b3328e Mon Sep 17 00:00:00 2001 From: Gabriel Ozouf Date: Tue, 1 Dec 2020 15:21:37 +0100 Subject: [PATCH] [shared/continuous_function_cache] Detach cache from function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When three functions or more, the third function and later don't use caching. But it is still possible for them to have been linked to a cache (for instance if previous functions had been deactivated earlier). To avoid a function tapping into another function's cache, we reset the cache. e.g. Define 3 functions f(x)=1, g(x)=2, h(x)=3, and deactivate f. Draw the graph, come back, reactivate f, then draw again. ---> The space between y=2 and y=3 will be filled with the color of h, as h "remembers" using cache n°2, wich currently contains the values of function g. --- apps/shared/continuous_function_cache.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/apps/shared/continuous_function_cache.cpp b/apps/shared/continuous_function_cache.cpp index 9b3657960..1c578e44b 100644 --- a/apps/shared/continuous_function_cache.cpp +++ b/apps/shared/continuous_function_cache.cpp @@ -10,14 +10,16 @@ constexpr int ContinuousFunctionCache::k_numberOfAvailableCaches; // public void ContinuousFunctionCache::PrepareForCaching(void * fun, ContinuousFunctionCache * cache, float tMin, float tStep) { + ContinuousFunction * function = static_cast(fun); + if (!cache) { /* ContinuousFunctionStore::cacheAtIndex has returned a nullptr : the index * of the function we are trying to draw is greater than the number of - * available caches, so we do nothing.*/ + * available caches, so we just tell the function to not lookup any cache. */ + function->setCache(nullptr); return; } - ContinuousFunction * function = static_cast(fun); if (tStep < 3 * k_cacheHitTolerance) { /* If tStep is lower than twice the tolerance, we risk shifting the index * by 1 for cache hits. As an added safety, we add another buffer of @@ -29,9 +31,7 @@ void ContinuousFunctionCache::PrepareForCaching(void * fun, ContinuousFunctionCa if (function->cache() != cache) { cache->clear(); function->setCache(cache); - } - - if (tStep != 0. && tStep != cache->step()) { + } else if (tStep != 0. && tStep != cache->step()) { cache->clear(); }