19template <
typename axis_t,
typename value_t>
24 uint8_t lastBinUpperIndex = 1U;
28 value_t lastOutput = 0;
29 uint8_t cacheTime = 0U;
31 constexpr Table2DCache(
void) =
default;
37template <
typename axis_t,
typename value_t>
38static inline bool cacheExpired(
const Table2DCache<axis_t, value_t> &cache) {
45 constexpr Bin(
const T *array, uint8_t binUpperIndex)
46 : upperIndex(binUpperIndex)
47 , _upperValue(array[binUpperIndex])
48 , _lowerValue(array[binUpperIndex-1U])
51 constexpr Bin(uint8_t binUpperIndex,
const T upperValue,
const T lowerValue)
52 : upperIndex(binUpperIndex)
53 , _upperValue(upperValue)
54 , _lowerValue(lowerValue)
58 const T upperValue(
void)
const noexcept {
return _upperValue; }
59 const T lowerValue(
void)
const noexcept {
return _lowerValue; }
61 static bool withinBin(
const T &value,
const T &min,
const T&max)
noexcept {
62 return (value <= max) && (value > min);
64 bool withinBin(
const T value)
const noexcept {
65 return withinBin(value, lowerValue(), upperValue());
74template <
typename TIter,
typename TValue>
75static inline uint8_t findBinUpperIndex(TIter pStart, TIter pEnd,
const TValue &value)
77 auto it = std::lower_bound(pStart, pEnd, value);
83 return std::distance(pStart, it);
85 return std::distance(pStart, pEnd)-1U;
88template <
typename TIter,
typename TValue>
89static inline Bin<TValue> findCachedBin(uint8_t cachedUpperIndex, TIter pStart, TIter pEnd,
const TValue &value)
92#if !defined(UNIT_TEST)
94 auto bin = Bin<TValue>(pStart, cachedUpperIndex);
95 if (bin.withinBin(value))
105 return Bin<TValue>(pStart, findBinUpperIndex(pStart, pEnd, value));
111template <
typename axis_t,
typename value_t>
112static inline value_t
interpolate(
const axis_t axisValue,
const Bin<axis_t> &axisBin,
const Bin<value_t> &valueBin)
114 return map(axisValue, axisBin.lowerValue(), axisBin.upperValue(), valueBin.lowerValue(), valueBin.upperValue());
119uint8_t
interpolate(
const uint8_t axisValue,
const Bin<uint8_t> &axisBin,
const Bin<uint8_t> &valueBin);
135template <
typename axis_t,
typename value_t, u
int8_t sizeT>
143 mutable _table2d_detail::Table2DCache<axis_t, value_t>
cache;
145 constexpr table2D(axis_t (*pAxisBin)[sizeT], value_t (*pCurve)[sizeT])
153 value_t
getValue(
const axis_t axisValue)
const noexcept {
155#if !defined(UNIT_TEST)
158 if( (axisValue ==
cache.lastInput) && (!cacheExpired(
cache)) )
160 return cache.lastOutput;
166 if(axisValue >=
axis[sizeT-1U])
169 cache.lastBinUpperIndex = sizeT-1U;
172 else if (axisValue <=
axis[0])
175 cache.lastBinUpperIndex = 1U;
180 auto xBin = _table2d_detail::findCachedBin(
cache.lastBinUpperIndex,
axis,
axis+sizeT, axisValue);
183 if (axisValue==xBin.upperValue())
186 cache.lastBinUpperIndex = xBin.upperIndex;
193 cache.lastBinUpperIndex = xBin.upperIndex;
198 cache.lastInput = axisValue;
200 return cache.lastOutput;
214template <
typename axis_t,
typename value_t, u
int8_t sizeT>
218 return fromTable->getValue(axisValue);
Definition table2d.cpp:13
uint8_t getCacheTime(void)
Definition table2d.cpp:15
uint8_t interpolate(const uint8_t axisValue, const Bin< uint8_t > &axisBin, const Bin< uint8_t > &valueBin)
Definition table2d.cpp:24
Workaround when min() & max() are defined as macros, which generates compile errors in most C++ stand...
#define UNUSED(x)
Used to suppress unused parameter compiler warnings.
Definition preprocessor.h:10
A 2D table.
Definition table2d.h:137
_table2d_detail::Table2DCache< axis_t, value_t > cache
Definition table2d.h:143
static constexpr size_type size(void)
Definition table2d.h:151
constexpr table2D(axis_t(*pAxisBin)[sizeT], value_t(*pCurve)[sizeT])
Definition table2d.h:145
axis_t(& axis)[sizeT]
Definition table2d.h:141
uint8_t size_type
Definition table2d.h:138
value_t(& values)[sizeT]
Definition table2d.h:140
value_t getValue(const axis_t axisValue) const noexcept
Definition table2d.h:153
static value_t table2D_getValue(const table2D< axis_t, value_t, sizeT > *fromTable, const axis_t axisValue) noexcept
Interpolate a value from a 2d table.
Definition table2d.h:215