Skip to content

Commit

Permalink
optimization: transform select into set as much as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Andersama committed Jan 2, 2021
1 parent 9a37e55 commit 661ab01
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
36 changes: 33 additions & 3 deletions include/ctre/actions/options.inc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,41 @@ template <typename Parameters> static constexpr auto apply(pcre::push_empty, ctl

// make_alternate (A|B)
template <auto V, typename A, typename B, typename... Ts, typename Parameters> static constexpr auto apply(pcre::make_alternate, ctll::term<V>, pcre_context<ctll::list<B, A, Ts...>, Parameters> subject) {
return pcre_context{ctll::push_front(select<A,B>(), ctll::list<Ts...>()), subject.parameters};
if constexpr (MatchesCharacter<A>::template value<char32_t> && MatchesCharacter<B>::template value<char32_t>) {
auto new_set = push_back_into_set(A(), set<B>());
return pcre_context{ctll::push_front(new_set, ctll::list<Ts...>()), subject.parameters};
} else {
return pcre_context{ctll::push_front(select<A,B>(), ctll::list<Ts...>()), subject.parameters};
}
}

// make_alternate (As..)|B => (As..|B)
template <auto V, typename A, typename B, typename... Bs, typename... Ts, typename Parameters> static constexpr auto apply(pcre::make_alternate, ctll::term<V>, pcre_context<ctll::list<ctre::select<B, Bs...>, A, Ts...>, Parameters> subject) {
if constexpr (MatchesCharacter<A>::template value<char32_t> && MatchesCharacter<B>::template value<char32_t>) {
auto new_set = push_back_into_set(A(), set<B>());
return pcre_context{ctll::push_front(select<decltype(new_set), Bs...>(), ctll::list<Ts...>()), subject.parameters};
} else {
return pcre_context{ctll::push_front(select<A,Bs...>(), ctll::list<Ts...>()), subject.parameters};
}
}

template <auto V, typename A, typename B, typename... Options, typename... Bs, typename... Ts, typename Parameters> static constexpr auto apply(pcre::make_alternate, ctll::term<V>, pcre_context<ctll::list<ctre::select<set<Options...>, Bs...>, A, Ts...>, Parameters> subject) {
if constexpr (MatchesCharacter<A>::template value<char32_t> && MatchesCharacter<B>::template value<char32_t>) {
auto new_set = push_back_into_set(A(), set<Options...>());
return pcre_context{ctll::push_front(select<decltype(new_set), Bs...>(), ctll::list<Ts...>()), subject.parameters};
} else {
return pcre_context{ctll::push_front(select<A, set<Options...>, Bs...>(), ctll::list<Ts...>()), subject.parameters};
}
}

// make_alternate (As..)|B => (As..|B)
template <auto V, typename A, typename... Bs, typename... Ts, typename Parameters> static constexpr auto apply(pcre::make_alternate, ctll::term<V>, pcre_context<ctll::list<ctre::select<Bs...>, A, Ts...>, Parameters> subject) {
return pcre_context{ctll::push_front(select<A,Bs...>(), ctll::list<Ts...>()), subject.parameters};
template <auto V, typename A, typename... Bs, typename... Ts, typename Parameters> static constexpr auto apply(pcre::make_alternate, ctll::term<V>, pcre_context<ctll::list<ctre::set<Bs...>, A, Ts...>, Parameters> subject) {
if constexpr (MatchesCharacter<A>::template value<char32_t>) {
auto new_set = push_back_into_set(A(), set<Bs...>());
return pcre_context{ctll::push_front(new_set, ctll::list<Ts...>()), subject.parameters};
} else {
return pcre_context{ctll::push_front(select<A, set<Bs...>>(), ctll::list<Ts...>()), subject.parameters};
}
}


Expand Down
17 changes: 16 additions & 1 deletion include/ctre/actions/set.inc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,22 @@
// UTILITY
// add into set if not exists
template <template <typename...> typename SetType, typename T, typename... As, bool Exists = (std::is_same_v<T, As> || ... || false)> static constexpr auto push_back_into_set(T, SetType<As...>) -> ctll::conditional<Exists, SetType<As...>, SetType<As...,T>> { return {}; }

// merge two sets
template<typename B, typename... Bs, typename... As>
static constexpr auto push_back_into_set(ctre::set<B, Bs...>, ctre::set<As...>) {
if constexpr (sizeof...(Bs) == 0)
return push_back_into_set<ctre::set>(B{}, ctre::set<As...>{});
else
return push_back_into_set(ctre::set<Bs...>{}, push_back_into_set<ctre::set>(B{}, ctre::set<As...>{}));
}
// avoid creating wrapped set
template<typename B, typename... Bs, typename... As>
static constexpr auto push_back_into_set(ctre::set<B, Bs...>, ctre::set<ctre::set<As...>>) {
if constexpr (sizeof...(Bs) == 0)
return push_back_into_set<ctre::set>(B{}, ctre::set<As...>{});
else
return push_back_into_set(ctre::set<Bs...>{}, push_back_into_set<ctre::set>(B{}, ctre::set<As...>{}));
}
//template <template <typename...> typename SetType, typename A, typename BHead, typename... Bs> struct set_merge_helper {
// using step = decltype(push_back_into_set<SetType>(BHead(), A()));
// using type = ctll::conditional<(sizeof...(Bs) > 0), set_merge_helper<SetType, step, Bs...>, step>;
Expand Down

0 comments on commit 661ab01

Please sign in to comment.