Created
March 15, 2019 15:59
-
-
Save mklca/5b9d2f6aebd5032d92e0c6b335fb28a6 to your computer and use it in GitHub Desktop.
Sketch of Recorders
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename D, typename T> | |
struct base_last_match_recorder { | |
// Exploits CRTP to "up-reference" this instance from the perspective of the | |
// derived class D | |
void operator()(const T &v, int p) { | |
if(static_cast<D*>(this)->match(v)) { | |
static_cast<D*>(this)->position = p; | |
static_cast<D*>(this)->value = v; | |
} | |
} | |
}; | |
template<typename N> | |
struct min_recorder | |
: base_last_match_recorder<min_recorder<N>, N> | |
{ | |
using numeric_type = N; | |
numeric_type value; | |
int position; | |
constexpr min_recorder() noexcept | |
: value(std::numeric_limits<numeric_type>::max()) | |
, position(-1) | |
{} | |
constexpr bool match(numeric_type n) const { | |
return n < value; | |
} | |
}; | |
template<typename N> | |
struct max_recorder: base_last_match_recorder<max_recorder<N>, N> { | |
using numeric_type = N; | |
numeric_type value; | |
int position; | |
constexpr max_recorder() noexcept | |
: value(std::numeric_limits<numeric_type>::min()) | |
, position(-1) | |
{} | |
constexpr bool match(numeric_type n) const { | |
return n > value; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment