Skip to content

Simplify reverse tuple iteration #1161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 0 additions & 34 deletions dev/functional/index_sequence_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
#include <utility> // std::index_sequence, std::make_index_sequence

#include "../functional/cxx_universal.h"
#ifdef SQLITE_ORM_RELAXED_CONSTEXPR_SUPPORTED
#include <array>
#endif

namespace sqlite_orm {
namespace internal {
Expand All @@ -17,37 +14,6 @@ namespace sqlite_orm {
return I;
}

#ifdef SQLITE_ORM_RELAXED_CONSTEXPR_SUPPORTED
/**
* Reorder the values of an index_sequence according to the positions from a second sequence.
*/
template<size_t... Value, size_t... IdxOfValue>
SQLITE_ORM_CONSTEVAL auto reorder_index_sequence(std::index_sequence<Value...>,
std::index_sequence<IdxOfValue...>) {
constexpr std::array<size_t, sizeof...(Value)> values{Value...};
return std::index_sequence<values[sizeof...(Value) - 1u - IdxOfValue]...>{};
}

template<size_t Value, size_t IdxOfValue>
SQLITE_ORM_CONSTEVAL std::index_sequence<Value> reorder_index_sequence(std::index_sequence<Value>,
std::index_sequence<IdxOfValue>) {
return {};
}

inline SQLITE_ORM_CONSTEVAL std::index_sequence<> reorder_index_sequence(std::index_sequence<>,
std::index_sequence<>) {
return {};
}

/**
* Reverse the values of an index_sequence.
*/
template<size_t... Idx>
SQLITE_ORM_CONSTEVAL auto reverse_index_sequence(std::index_sequence<Idx...>) {
return reorder_index_sequence(std::index_sequence<Idx...>{}, std::make_index_sequence<sizeof...(Idx)>{});
}
#endif

template<class... Seq>
struct flatten_idxseq {
using type = std::index_sequence<>;
Expand Down
4 changes: 3 additions & 1 deletion dev/tuple_helper/tuple_iteration.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ namespace sqlite_orm {
template<bool reversed = false, class Tpl, size_t... Idx, class L>
void iterate_tuple(const Tpl& tpl, std::index_sequence<Idx...>, L&& lambda) {
if constexpr(reversed) {
iterate_tuple(tpl, reverse_index_sequence(std::index_sequence<Idx...>{}), std::forward<L>(lambda));
// nifty fold expression trick: make use of guaranteed right-to-left evaluation order when folding over operator=
int sink;
((lambda(std::get<Idx>(tpl)), sink) = ... = 0);
} else {
(lambda(std::get<Idx>(tpl)), ...);
}
Expand Down
39 changes: 3 additions & 36 deletions include/sqlite_orm/sqlite_orm.h
Original file line number Diff line number Diff line change
Expand Up @@ -1114,10 +1114,6 @@ namespace sqlite_orm {

// #include "../functional/cxx_universal.h"

#ifdef SQLITE_ORM_RELAXED_CONSTEXPR_SUPPORTED
#include <array>
#endif

namespace sqlite_orm {
namespace internal {
/**
Expand All @@ -1128,37 +1124,6 @@ namespace sqlite_orm {
return I;
}

#ifdef SQLITE_ORM_RELAXED_CONSTEXPR_SUPPORTED
/**
* Reorder the values of an index_sequence according to the positions from a second sequence.
*/
template<size_t... Value, size_t... IdxOfValue>
SQLITE_ORM_CONSTEVAL auto reorder_index_sequence(std::index_sequence<Value...>,
std::index_sequence<IdxOfValue...>) {
constexpr std::array<size_t, sizeof...(Value)> values{Value...};
return std::index_sequence<values[sizeof...(Value) - 1u - IdxOfValue]...>{};
}

template<size_t Value, size_t IdxOfValue>
SQLITE_ORM_CONSTEVAL std::index_sequence<Value> reorder_index_sequence(std::index_sequence<Value>,
std::index_sequence<IdxOfValue>) {
return {};
}

inline SQLITE_ORM_CONSTEVAL std::index_sequence<> reorder_index_sequence(std::index_sequence<>,
std::index_sequence<>) {
return {};
}

/**
* Reverse the values of an index_sequence.
*/
template<size_t... Idx>
SQLITE_ORM_CONSTEVAL auto reverse_index_sequence(std::index_sequence<Idx...>) {
return reorder_index_sequence(std::index_sequence<Idx...>{}, std::make_index_sequence<sizeof...(Idx)>{});
}
#endif

template<class... Seq>
struct flatten_idxseq {
using type = std::index_sequence<>;
Expand Down Expand Up @@ -9574,7 +9539,9 @@ namespace sqlite_orm {
template<bool reversed = false, class Tpl, size_t... Idx, class L>
void iterate_tuple(const Tpl& tpl, std::index_sequence<Idx...>, L&& lambda) {
if constexpr(reversed) {
iterate_tuple(tpl, reverse_index_sequence(std::index_sequence<Idx...>{}), std::forward<L>(lambda));
// nifty fold expression trick: make use of guaranteed right-to-left evaluation order when folding over operator=
int sink;
((lambda(std::get<Idx>(tpl)), sink) = ... = 0);
} else {
(lambda(std::get<Idx>(tpl)), ...);
}
Expand Down