Skip to content

Commit 063a7c7

Browse files
committed
improve has_type_at
1 parent 4d8e7b5 commit 063a7c7

File tree

4 files changed

+30
-75
lines changed

4 files changed

+30
-75
lines changed

Framework/Core/include/Framework/ASoA.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2292,8 +2292,8 @@ class TableNG
22922292
inline arrow::ChunkedArray* getIndexToKey()
22932293
{
22942294
if constexpr (framework::has_type_conditional<is_binding_compatible, Key>(external_index_columns_t{})) {
2295-
using IC = framework::pack_element_t<framework::has_type_at_conditional<is_binding_compatible, Key>(external_index_columns_t{}), external_index_columns_t>;
2296-
return mColumnChunks[framework::has_type_at<IC>(persistent_columns_t{})];
2295+
using IC = framework::pack_element_t<framework::has_type_at_conditional_v<is_binding_compatible, Key>(external_index_columns_t{}), external_index_columns_t>;
2296+
return mColumnChunks[framework::has_type_at_v<IC>(persistent_columns_t{})];
22972297
} else if constexpr (std::is_same_v<table_t, Key>) {
22982298
return nullptr;
22992299
} else {
@@ -2680,8 +2680,8 @@ class Table
26802680
inline arrow::ChunkedArray* getIndexToKey()
26812681
{
26822682
if constexpr (framework::has_type_conditional<is_binding_compatible, Key>(external_index_columns_t{})) {
2683-
using IC = framework::pack_element_t<framework::has_type_at_conditional<is_binding_compatible, Key>(external_index_columns_t{}), external_index_columns_t>;
2684-
return mColumnChunks[framework::has_type_at<IC>(persistent_columns_t{})];
2683+
using IC = framework::pack_element_t<framework::has_type_at_conditional_v<is_binding_compatible, Key>(external_index_columns_t{}), external_index_columns_t>;
2684+
return mColumnChunks[framework::has_type_at_v<IC>(persistent_columns_t{})];
26852685
} else if constexpr (std::is_same_v<table_t, Key>) {
26862686
return nullptr;
26872687
} else {

Framework/Core/include/Framework/AnalysisHelpers.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,8 @@ namespace
447447
template <typename T, typename Key>
448448
inline std::shared_ptr<arrow::ChunkedArray> getIndexToKey(arrow::Table* table)
449449
{
450-
using IC = framework::pack_element_t<framework::has_type_at_conditional<soa::is_binding_compatible, Key>(typename T::external_index_columns_t{}), typename T::external_index_columns_t>;
451-
return table->column(framework::has_type_at<IC>(typename T::persistent_columns_t{}));
450+
using IC = framework::pack_element_t<framework::has_type_at_conditional_v<soa::is_binding_compatible, Key>(typename T::external_index_columns_t{}), typename T::external_index_columns_t>;
451+
return table->column(framework::has_type_at_v<IC>(typename T::persistent_columns_t{}));
452452
}
453453

454454
template <typename C>

Framework/Foundation/include/Framework/Pack.h

Lines changed: 20 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,12 @@ constexpr std::size_t pack_size(pack<Ts...> const&)
3333
template <std::size_t I, typename T>
3434
struct pack_element;
3535

36-
#ifdef __clang__
36+
#if __has_builtin(__type_pack_element)
3737
template <std::size_t I, typename... Ts>
3838
struct pack_element<I, pack<Ts...>> {
3939
using type = __type_pack_element<I, Ts...>;
4040
};
4141
#else
42-
4342
// recursive case
4443
template <std::size_t I, typename Head, typename... Tail>
4544
struct pack_element<I, pack<Head, Tail...>>
@@ -224,74 +223,30 @@ bool consteval has_type_conditional(framework::pack<Us...>)
224223
template <template <typename, typename> typename Condition, typename T, typename P>
225224
inline constexpr bool has_type_conditional_v = has_type_conditional<Condition, T>(P{});
226225

227-
template <typename T>
228-
constexpr size_t has_type_at(pack<> const&)
229-
{
230-
return static_cast<size_t>(-1);
231-
}
232-
233-
template <typename T, typename T1, typename... Ts>
234-
constexpr size_t has_type_at(pack<T1, Ts...> const&)
235-
{
236-
if constexpr (std::is_same_v<T, T1>) {
237-
return 0;
238-
} else if constexpr (has_type<T>(pack<Ts...>{})) {
239-
return 1 + has_type_at<T>(pack<Ts...>{});
240-
}
241-
return sizeof...(Ts) + 2;
242-
}
243-
244-
template <template <typename, typename> typename Condition, typename T>
245-
constexpr size_t has_type_at_conditional(pack<>&&)
226+
template <typename T, typename... Ts>
227+
consteval size_t has_type_at_v(pack<Ts...>)
246228
{
247-
return static_cast<size_t>(-1);
229+
constexpr size_t size = sizeof...(Ts);
230+
constexpr bool found[size] = { std::same_as<T, Ts> ... };
231+
for (size_t i = 0; i < size; ++i) {
232+
if (found[i]) {
233+
return i;
234+
}
235+
}
236+
return size + 1;
248237
}
249238

250-
template <template <typename, typename> typename Condition, typename T, typename T1, typename... Ts>
251-
constexpr size_t has_type_at_conditional(pack<T1, Ts...>&&)
239+
template <template <typename, typename> typename Condition, typename T, typename... Ts>
240+
consteval size_t has_type_at_conditional_v(pack<Ts...>)
252241
{
253-
if constexpr (Condition<T, T1>::value) {
254-
return 0;
255-
} else if constexpr (has_type_conditional_v<Condition, T, pack<Ts...>>) {
256-
return 1 + has_type_at_conditional<Condition, T>(pack<Ts...>{});
242+
constexpr size_t size = sizeof...(Ts);
243+
constexpr bool found[size] = { Condition<T, Ts>::value ... };
244+
for (size_t i = 0; i < size; ++i) {
245+
if (found[i]) {
246+
return i;
247+
}
257248
}
258-
return sizeof...(Ts) + 2;
259-
}
260-
261-
namespace
262-
{
263-
template <std::size_t I, typename T>
264-
struct indexed {
265-
using type = T;
266-
constexpr static std::size_t index = I;
267-
};
268-
269-
template <typename Is, typename... Ts>
270-
struct indexer;
271-
272-
template <std::size_t... Is, typename... Ts>
273-
struct indexer<std::index_sequence<Is...>, Ts...>
274-
: indexed<Is, Ts>... {
275-
};
276-
277-
template <typename T, std::size_t I>
278-
indexed<I, T> select(indexed<I, T>);
279-
280-
template <typename W, typename... Ts>
281-
constexpr std::size_t has_type_at_t = decltype(select<W>(
282-
indexer<std::index_sequence_for<Ts...>, Ts...>{}))::index;
283-
} // namespace
284-
285-
template <typename W>
286-
constexpr std::size_t has_type_at_v(o2::framework::pack<>)
287-
{
288-
return -1;
289-
}
290-
291-
template <typename W, typename... Ts>
292-
constexpr std::size_t has_type_at_v(o2::framework::pack<Ts...>)
293-
{
294-
return has_type_at_t<W, Ts...>;
249+
return size + 1;
295250
}
296251

297252
/// Intersect two packs

Framework/Foundation/test/test_FunctionalHelpers.cxx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ TEST_CASE("TestOverride")
3737
static_assert(has_type_conditional_v<std::is_same, double, pack<int, float>> == false, "double should not be in the pack");
3838

3939
pack<float, char, int, bool> pck;
40-
static_assert(has_type_at<int>(pck) == 2, "int should be at 2");
41-
static_assert(has_type_at<double>(pck) == pack_size(pck) + 1, "double is not in the pack so the function returns size + 1");
42-
static_assert(has_type_at_conditional<std::is_same, bool>(pack<int, float, bool>()) == 2, "bool should be at 2");
43-
static_assert(has_type_at_conditional<std::is_same, bool>(pack<int, float, double>()) == 3 + 1, "bool is not in the pack so the function returns size + 1");
40+
static_assert(has_type_at_v<int>(pck) == 2, "int should be at 2");
41+
static_assert(has_type_at_v<double>(pck) == pack_size(pck) + 1, "double is not in the pack so the function returns size + 1");
42+
static_assert(has_type_at_conditional_v<std::is_same, bool>(pack<int, float, bool>()) == 2, "bool should be at 2");
43+
static_assert(has_type_at_conditional_v<std::is_same, bool>(pack<int, float, double>()) == 3 + 1, "bool is not in the pack so the function returns size + 1");
4444

4545
static_assert(std::is_same_v<selected_pack<is_int_t, int, float, char>, pack<int>>, "selector should select int");
4646
static_assert(std::is_same_v<selected_pack<is_int_t, int, int, float, char>, pack<int, int>>, "selector should select int");

0 commit comments

Comments
 (0)