FireSTARR
Loading...
Searching...
No Matches
LookupTable.h
1/* Copyright (c) Queen's Printer for Ontario, 2020. */
2/* Copyright (c) His Majesty the King in Right of Canada as represented by the Minister of Natural Resources, 2025. */
3
4/* SPDX-License-Identifier: AGPL-3.0-or-later */
5
6#pragma once
7#include "Util.h"
8#define LOOKUP_TABLES_OFF 1
9#undef LOOKUP_TABLES_OFF
10namespace fs::util
11{
18template <MathSize (*Fct)(const MathSize), int IndexDigits = 3, int Precision = 1>
20{
21#ifndef LOOKUP_TABLES_OFF
25 using ValuesArray = array<MathSize, pow_int<IndexDigits>(10) * pow_int<Precision>(10)>;
34 [[nodiscard]] constexpr ValuesArray makeValues()
35 {
36 // FIX: would prefer consteval but c++26 or external library is required for cmath functions
37 ValuesArray values{};
38 for (size_t i = 0; i < values.size(); ++i)
39 {
40 const auto value = i / static_cast<MathSize>(pow_int<Precision>(10));
41 values[i] = Fct(value);
42 }
43 return values;
44 }
45#endif
46public:
47 constexpr explicit LookupTable() noexcept
48#ifndef LOOKUP_TABLES_OFF
50#endif
51 {
52 }
53 ~LookupTable() = default;
54 LookupTable(LookupTable&& rhs) noexcept = delete;
55 LookupTable(const LookupTable& rhs) noexcept = delete;
56 LookupTable& operator=(LookupTable&& rhs) noexcept = delete;
57 LookupTable& operator=(const LookupTable& rhs) noexcept = delete;
63 [[nodiscard]] constexpr MathSize operator()(const MathSize value) const
64 {
65#ifndef LOOKUP_TABLES_OFF
66 return values_.at(static_cast<size_t>(value * pow_int<Precision>(10)));
67#else
68 return Fct(value);
69#endif
70 }
71};
72}
A table initialized using the given function ranging over the number of digits and precision.
Definition LookupTable.h:20
constexpr MathSize operator()(const MathSize value) const
Get result of function lookup table was initialized with for given value.
Definition LookupTable.h:63
constexpr ValuesArray makeValues()
Call function with range of values with given precision.
Definition LookupTable.h:34
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:25
const ValuesArray values_
Array of values from calling function.
Definition LookupTable.h:29