Skip to content

Commit

Permalink
feat: "blessing" copy_n
Browse files Browse the repository at this point in the history
  • Loading branch information
malachib committed Nov 22, 2024
1 parent 18a9d98 commit 425f828
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 19 deletions.
32 changes: 15 additions & 17 deletions src/estd/algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace estd {

// Shamelessly lifted from https://en.cppreference.com/w/cpp/algorithm/fill_n
template<class OutputIt, class Size, class T>
inline OutputIt fill_n(OutputIt first, Size count, const T& value)
ESTD_CPP_CONSTEXPR(20) OutputIt fill_n(OutputIt first, Size count, const T& value)
{
#if FEATURE_ESTD_ALGORITHM_OPT
return std::fill_n(first, count, value);
Expand Down Expand Up @@ -89,7 +89,7 @@ ForwardIt min_element(ForwardIt first, ForwardIt last,
#endif

template<class InputIt, class OutputIt>
inline OutputIt copy(InputIt first, InputIt last,
ESTD_CPP_CONSTEXPR(20) OutputIt copy(InputIt first, InputIt last,
OutputIt d_first)
{
#if FEATURE_ESTD_ALGORITHM_OPT
Expand All @@ -106,12 +106,16 @@ inline OutputIt copy(InputIt first, InputIt last,
// has a more complex implementation, but unsure why. Maybe they want to avoid incrementing the source
// iterator unnecessarily?
template <class InputIt, class Size, class OutputIt>
inline OutputIt copy_n(InputIt first, Size count, OutputIt result)
ESTD_CPP_CONSTEXPR(20) OutputIt copy_n(InputIt first, Size count, OutputIt result)
{
#if FEATURE_ESTD_ALGORITHM_OPT
return std::copy_n(first, count, result);
#else
while(count--)
*result++ = *first++;

return result;
#endif
}


Expand Down Expand Up @@ -160,26 +164,20 @@ InputIt find_if(InputIt first, InputIt last, UnaryPredicate p)
return last;
}

template<class T>
#ifdef FEATURE_CPP_CONSTEXPR_METHOD
constexpr
#endif
const T& clamp( const T& v, const T& lo, const T& hi )
{
return clamp( v, lo, hi, estd::less<T>() );
}


template<class T, class Compare>
#if defined(FEATURE_CPP_CONSTEXPR_METHOD) && !defined(ESP8266)
constexpr
#endif
const T& clamp( const T& v, const T& lo, const T& hi, Compare comp )
ESTD_CPP_CONSTEXPR(17) const T& clamp( const T& v, const T& lo, const T& hi, Compare comp )
{
return assert( !comp(hi, lo) ),
comp(v, lo) ? lo : comp(hi, v) ? hi : v;
}

template<class T>
ESTD_CPP_CONSTEXPR(17) const T& clamp( const T& v, const T& lo, const T& hi )
{
return estd::clamp( v, lo, hi, estd::less<T>() );
}


// UNTESTED
template<class InputIt1, class InputIt2>
#ifdef FEATURE_CPP_CONSTEXPR_METHOD
Expand Down
23 changes: 23 additions & 0 deletions src/estd/internal/macro/c++11_emul.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,29 @@
#define ESTD_CPP_CONSTEXPR_RET inline
#endif

// Take pages out of GCC playbook
// DEBT: These belong elsewhere, not in c++_emul per se
#if __cplusplus >= 202002L
#define ESTD_CPP20_CONSTEXPR constexpr
#else
#define ESTD_CPP20_CONSTEXPR
#endif

#if __cplusplus >= 201703L
#define ESTD_CPP17_CONSTEXPR constexpr
#else
#define ESTD_CPP17_CONSTEXPR
#endif

#if __cplusplus >= 201402L
#define ESTD_CPP14_CONSTEXPR constexpr
#else
#define ESTD_CPP14_CONSTEXPR
#endif


#define ESTD_CPP_CONSTEXPR(v) ESTD_CPP ## v ## _CONSTEXPR

#if __cpp_ref_qualifiers
#define ESTD_CPP_REFQ &
#else
Expand Down
2 changes: 1 addition & 1 deletion src/estd/internal/raw/type_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct type_identity { typedef T type; };
template<class T, T v>
struct integral_constant
{
static CONSTEXPR T value = v;
static constexpr T value = v;
typedef T value_type;
typedef integral_constant type; // using injected-class-name

Expand Down
2 changes: 1 addition & 1 deletion test/catch/ios-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ typedef internal::streambuf<dummy_streambuf_impl> dummy_streambuf;
TEST_CASE("ios")
{
const char raw_str[] = "raw 'traditional' output\n";
CONSTEXPR int raw_str_len = sizeof(raw_str) - 1;
constexpr int raw_str_len = sizeof(raw_str) - 1;

/* This old cute and clever function-detector method no longer employed.
* Ended up being more complicated than not using it in the end
Expand Down

0 comments on commit 425f828

Please sign in to comment.