FireSTARR
Loading...
Searching...
No Matches
LookupTable.h
1/* SPDX-License-Identifier: AGPL-3.0-or-later */
2#ifndef FS_LOOKUPTABLE_H
3#define FS_LOOKUPTABLE_H
4#include "stdafx.h"
5#include "Util.h"
6#define LOOKUP_TABLES_OFF 1
7#undef LOOKUP_TABLES_OFF
8namespace fs
9{
17template <MathSize (*Fct)(const MathSize), int IndexDigits = 3, int Precision = 1>
19{
20#ifndef LOOKUP_TABLES_OFF
24 using ValuesArray = array<MathSize, pow_int<IndexDigits>(10) * pow_int<Precision>(10)>;
33 [[nodiscard]] constexpr ValuesArray makeValues()
34 {
35 // FIX: would prefer consteval but c++26 or external library is required for cmath functions
36 ValuesArray values{};
37 for (size_t i = 0; i < values.size(); ++i)
38 {
39 const auto value = i / static_cast<MathSize>(pow_int<Precision>(10));
40 values[i] = Fct(value);
41 }
42 return values;
43 }
44#endif
45public:
46 constexpr explicit LookupTable() noexcept
47#ifndef LOOKUP_TABLES_OFF
49#endif
50 { }
51 ~LookupTable() = default;
52 LookupTable(LookupTable&& rhs) noexcept = delete;
53 LookupTable(const LookupTable& rhs) noexcept = delete;
54 LookupTable& operator=(LookupTable&& rhs) noexcept = delete;
55 LookupTable& operator=(const LookupTable& rhs) noexcept = delete;
61 [[nodiscard]] constexpr MathSize operator()(const MathSize value) const
62 {
63 return values_.at(static_cast<size_t>(value * pow_int<Precision>(10)));
64 }
65};
66}
67#endif
A table initialized using the given function ranging over the number of digits and precision.
Definition LookupTable.h:19
const ValuesArray values_
Array of values from calling function.
Definition LookupTable.h:28
constexpr MathSize operator()(const MathSize value) const
Get result of function lookup table was initialized with for given value.
Definition LookupTable.h:61
array< MathSize, pow_int< IndexDigits >(10) *pow_int< Precision >(10)> ValuesArray
Array with enough space for function called with specific number of digits and precision.
Definition LookupTable.h:24
constexpr ValuesArray makeValues()
Call function with range of values with given precision.
Definition LookupTable.h:33