Skip to content

Commit a821ee6

Browse files
committed
Combine enum and integer caching logic in StaticCache
1 parent 0be06d3 commit a821ee6

File tree

1 file changed

+15
-45
lines changed

1 file changed

+15
-45
lines changed

src/Utilities/StaticCache.hpp

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "Utilities/ErrorHandling/Assert.hpp"
1111
#include "Utilities/Gsl.hpp"
1212
#include "Utilities/Requires.hpp"
13+
#include "Utilities/TMPL.hpp"
1314
#include "Utilities/TypeTraits/IsInteger.hpp"
1415

1516
/// \ingroup UtilitiesGroup
@@ -35,6 +36,7 @@ struct CacheEnumeration {
3536
constexpr static size_t size = sizeof...(Enums);
3637
using value_type = EnumerationType;
3738
static constexpr std::array<value_type, size> values{Enums...};
39+
using value_list = tmpl::integral_list<EnumerationType, Enums...>;
3840
};
3941

4042
/// \ingroup UtilitiesGroup
@@ -102,7 +104,7 @@ class StaticCache {
102104
if (UNLIKELY(array_location == std::numeric_limits<size_t>::max())) {
103105
ERROR("Uncached enumeration value: " << parameter);
104106
}
105-
return std::tuple<size_t, Range>{array_location, Range{}};
107+
return std::tuple{array_location, typename Range::value_list{}};
106108
} else {
107109
static_assert(
108110
tt::is_integer_v<std::remove_cv_t<T1>>,
@@ -120,12 +122,14 @@ class StaticCache {
120122
<< Range::start +
121123
static_cast<decltype(Range::start)>(Range::size));
122124
}
123-
124-
return std::make_tuple(
125-
static_cast<typename Range::value_type>(parameter),
126-
std::integral_constant<typename Range::value_type, Range::start>{},
127-
std::make_integer_sequence<typename Range::value_type,
128-
Range::size>{});
125+
return std::tuple{
126+
// unsigned cast is safe since this is an index into an array
127+
static_cast<size_t>(
128+
static_cast<typename Range::value_type>(parameter) -
129+
Range::start),
130+
tmpl::make_sequence<
131+
tmpl::integral_constant<typename Range::value_type, Range::start>,
132+
Range::size>{}};
129133
}
130134
}
131135

@@ -135,52 +139,18 @@ class StaticCache {
135139
return cached_object;
136140
}
137141

138-
template <typename... IntegralConstantValues, auto IndexOffset, auto... Is,
142+
template <typename... IntegralConstantValues, typename... IntegralConstants,
139143
typename... Args>
140144
const T& unwrap_cache(
141-
std::tuple<
142-
std::remove_cv_t<decltype(IndexOffset)>,
143-
std::integral_constant<std::remove_cv_t<decltype(IndexOffset)>,
144-
IndexOffset>,
145-
std::integer_sequence<std::remove_cv_t<decltype(IndexOffset)>, Is...>>
146-
parameter0,
147-
Args... parameters) const {
148-
// note that the act of assigning to the specified function pointer type
149-
// fixes the template arguments that need to be inferred.
150-
static const std::array<
151-
const T& (StaticCache<Generator, T, Ranges...>::*)(Args...) const,
152-
sizeof...(Is)>
153-
cache{{&StaticCache<Generator, T, Ranges...>::unwrap_cache<
154-
IntegralConstantValues...,
155-
std::integral_constant<decltype(IndexOffset),
156-
Is + IndexOffset>>...}};
157-
// The array `cache` holds pointers to member functions, so we dereference
158-
// the pointer and invoke it on `this`.
159-
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ > 10 && __GNUC__ < 14
160-
#pragma GCC diagnostic push
161-
#pragma GCC diagnostic ignored "-Warray-bounds"
162-
#endif
163-
return (this->*gsl::at(cache, std::get<0>(parameter0) - IndexOffset))(
164-
parameters...);
165-
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ > 10 && __GNUC__ < 14
166-
#pragma GCC diagnostic pop
167-
#endif
168-
}
169-
170-
template <typename... IntegralConstantValues, typename EnumType,
171-
EnumType... EnumValues, typename... Args>
172-
const T& unwrap_cache(
173-
std::tuple<size_t, CacheEnumeration<EnumType, EnumValues...>>
174-
parameter0,
145+
std::tuple<size_t, tmpl::list<IntegralConstants...>> parameter0,
175146
Args... parameters) const {
176147
// note that the act of assigning to the specified function pointer type
177148
// fixes the template arguments that need to be inferred.
178149
static const std::array<
179150
const T& (StaticCache<Generator, T, Ranges...>::*)(Args...) const,
180-
sizeof...(EnumValues)>
151+
sizeof...(IntegralConstants)>
181152
cache{{&StaticCache<Generator, T, Ranges...>::unwrap_cache<
182-
IntegralConstantValues...,
183-
std::integral_constant<EnumType, EnumValues>>...}};
153+
IntegralConstantValues..., IntegralConstants>...}};
184154
// The array `cache` holds pointers to member functions, so we dereference
185155
// the pointer and invoke it on `this`.
186156
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ > 10 && __GNUC__ < 14

0 commit comments

Comments
 (0)