Skip to content
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

Select optimization: Avoid backtracking on mutually exclusive branches #158

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions include/ctre/atoms_characters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ template <auto V> struct character {
};

template <typename... Content> struct negative_set {
template <typename CharT> CTRE_FORCE_INLINE static constexpr bool match_char(CharT value) noexcept {
template <typename CharT> CTRE_FORCE_INLINE static constexpr bool match_char([[maybe_unused]] CharT value) noexcept {
return !(Content::match_char(value) || ... || false);
}
};

template <typename... Content> struct set {
template <typename CharT> CTRE_FORCE_INLINE static constexpr bool match_char(CharT value) noexcept {
template <typename CharT> CTRE_FORCE_INLINE static constexpr bool match_char([[maybe_unused]] CharT value) noexcept {
return (Content::match_char(value) || ... || false);
}
};

template <auto... Cs> struct enumeration : set<character<Cs>...> { };

template <typename... Content> struct negate {
template <typename CharT> CTRE_FORCE_INLINE static constexpr bool match_char(CharT value) noexcept {
template <typename CharT> CTRE_FORCE_INLINE static constexpr bool match_char([[maybe_unused]] CharT value) noexcept {
return !(Content::match_char(value) || ... || false);
}
};
Expand Down
27 changes: 24 additions & 3 deletions include/ctre/evaluation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,32 @@ constexpr CTRE_FORCE_INLINE R evaluate(const Iterator begin, Iterator current, c
// matching select in patterns
template <typename R, typename Iterator, typename EndIterator, typename HeadOptions, typename... TailOptions, typename... Tail>
constexpr CTRE_FORCE_INLINE R evaluate(const Iterator begin, Iterator current, const EndIterator end, const flags & f, R captures, ctll::list<select<HeadOptions, TailOptions...>, Tail...>) noexcept {
if (auto r = evaluate(begin, current, end, f, captures, ctll::list<HeadOptions, Tail...>())) {
return r;
#ifndef CTRE_DISABLE_GREEDY_OPT
if constexpr (sizeof...(TailOptions) > 0 && !collides(calculate_first(sequence<HeadOptions, Tail...>{}), calculate_first(sequence<select<TailOptions...>, Tail...>{}))) {
using set_type = decltype(transform_into_set(calculate_first(sequence<HeadOptions, Tail...>{})));
if constexpr (::std::is_same_v<set<>, set_type>) {
if (auto r = evaluate(begin, current, end, f, captures, ctll::list<HeadOptions, Tail...>())) {
return r;
} else {
return evaluate(begin, current, end, f, captures, ctll::list<select<TailOptions...>, Tail...>());
}
} else {
if (auto r = evaluate(begin, current, end, f, captures, ctll::list<set_type, end_cycle_mark>{})) {
return evaluate(begin, current, end, f, captures, ctll::list<HeadOptions, Tail...>{});
} else {
return evaluate(begin, current, end, f, captures, ctll::list<select<TailOptions...>, Tail...>());
}
}
} else {
return evaluate(begin, current, end, f, captures, ctll::list<select<TailOptions...>, Tail...>());
#endif
if (auto r = evaluate(begin, current, end, f, captures, ctll::list<HeadOptions, Tail...>())) {
return r;
} else {
return evaluate(begin, current, end, f, captures, ctll::list<select<TailOptions...>, Tail...>());
}
#ifndef CTRE_DISABLE_GREEDY_OPT
}
#endif
}

template <typename R, typename Iterator, typename EndIterator, typename... Tail>
Expand Down
21 changes: 21 additions & 0 deletions include/ctre/first.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,21 @@ template <typename CB> constexpr void negative_helper(ctre::negative_set<>, CB &
}
}

template <typename... Content>
constexpr auto transform_into_set(ctll::list<Content...>) {
return ctre::set<decltype(transform_into_set(Content{}))...>{};
}

template <typename T>
constexpr auto transform_into_set(T) {
return T{};
}

template<>
constexpr auto transform_into_set(can_be_anything) {
return ctre::char_range<std::numeric_limits<int64_t>::min(), std::numeric_limits<int64_t>::max()>{};
}

// simple fixed set
// TODO: this needs some optimizations
template <size_t Capacity> class point_set {
Expand Down Expand Up @@ -469,6 +484,9 @@ template <size_t Capacity> class point_set {
constexpr bool check(can_be_anything) {
return used > 0;
}
constexpr bool check(empty) {
return used == 0;
}
template <typename... Content> constexpr bool check(ctre::negative_set<Content...> nset) {
bool collision = false;
negative_helper(nset, [&](int64_t low, int64_t high){
Expand Down Expand Up @@ -497,6 +515,9 @@ template <size_t Capacity> class point_set {
constexpr void populate(can_be_anything) {
insert(std::numeric_limits<int64_t>::min(), std::numeric_limits<int64_t>::max());
}
constexpr void populate(empty) {
//do nothing
}
template <typename... Content> constexpr void populate(ctre::negative_set<Content...> nset) {
negative_helper(nset, [&](int64_t low, int64_t high){
this->insert(low, high);
Expand Down