27 template<
typename Iter1,
typename Iter2>
30 auto temp = std::move(*a);
35 template<
typename InputIt,
typename UnaryPredicate>
36 [[nodiscard]] constexpr InputIt
find_if_not(InputIt first, InputIt last, UnaryPredicate p)
38 for (; first != last; ++first) {
46 template<
typename ForwardIt,
typename UnaryPredicate>
47 [[nodiscard]] constexpr ForwardIt
partition(ForwardIt first, ForwardIt last, UnaryPredicate p)
50 if (first == last)
return first;
52 for (ForwardIt i = first + 1; i != last; ++i) {
63 template<
typename RAIt,
typename Compare = std::less<>>
64 constexpr
void sort(RAIt first, RAIt last, Compare cmp = Compare{})
66 auto const N = last - first;
68 auto const pivot = *(first +
N / 2);
70 first, last, [&](
const auto& elem) {
return cmp(elem, pivot); });
72 middle1, last, [&](
const auto& elem) {
return !cmp(pivot, elem); });
77 template<
typename RandomAccessRange,
typename Compare = std::less<>>
78 constexpr
void sort(RandomAccessRange&& range, Compare cmp = Compare{})
83 template<
typename InputIt1,
typename InputIt2>
85 InputIt2 first2, InputIt2 last2)
87 for (; (first1 != last1) && (first2 != last2); ++first1, ++first2) {
88 if (*first1 < *first2)
return true;
89 if (*first2 < *first1)
return false;
91 return (first1 == last1) && (first2 != last2);
94 template<
typename ForwardIt,
typename T>
95 constexpr
void replace(ForwardIt first, ForwardIt last,
const T& old_value,
const T& new_value)
97 while (first != last) {
98 if (*first == old_value) {
104 template<
typename ForwardRange,
typename T>
105 constexpr
void replace(ForwardRange& range,
const T& old_value,
const T& new_value)
110 template<
typename ForwardIt,
typename T>
111 constexpr
void fill(ForwardIt first, ForwardIt last,
const T& value)
113 while (first != last) {
118 template<
typename ForwardRange,
typename T>
119 constexpr
void fill(ForwardRange& range,
const T& value)
125 [[nodiscard]] constexpr T
abs(T
t)
127 return (
t >= 0) ?
t : -
t;
132 #if (defined(__GNUC__) && !defined(__clang__))
136 template<
int> [[nodiscard]] constexpr
double sin (
double x) {
return std::sin (
x); }
137 template<
int> [[nodiscard]] constexpr
double cos (
double x) {
return std::cos (
x); }
138 template<
int,
int> [[nodiscard]] constexpr
double log (
double x) {
return std::log (
x); }
140 template<
int,
int> [[nodiscard]] constexpr
double log10(
double x) {
return std::log10(
x); }
141 template<
int> [[nodiscard]] constexpr
double exp (
double x) {
return std::exp (
x); }
143 template<
int,
int> [[nodiscard]] constexpr
double pow(
double x,
double y) {
return std::pow(
x, y); }
150 [[nodiscard]] constexpr
double upow(
double x,
unsigned u)
161 [[nodiscard]] constexpr
double ipow(
double x,
int i)
166 template<
int ITERATIONS>
167 [[nodiscard]] constexpr
double exp(
double x)
180 for (
auto k :
xrange(ITERATIONS)) {
187 int p = (i >= 0) ? i : -i;
201 return x - int(
x / y) * y;
204 template<
int ITERATIONS>
211 for (
int k = 1; k < (1 + 4 * ITERATIONS); ) {
225 template<
int ITERATIONS>
232 for (
int k = 2; k < (2 + 4 * ITERATIONS); ) {
246 template<
int ITERATIONS>
247 [[nodiscard]] constexpr
double sin(
double x)
274 return sign * cos_iter<ITERATIONS>(
x);
276 return sign * sin_iter<ITERATIONS>(
x);
280 template<
int ITERATIONS>
281 [[nodiscard]] constexpr
double cos(
double x)
307 return sign * sin_iter<ITERATIONS>(
x);
309 return sign * cos_iter<ITERATIONS>(
x);
315 template<
int E_ITERATIONS,
int L_ITERATIONS>
316 [[nodiscard]] constexpr
double log(
double x)
324 repeat(L_ITERATIONS, [&] {
325 auto ey = cstd::exp<E_ITERATIONS>(y);
326 y = y + 2.0 * (
x - ey) / (
x + ey);
331 template<
int E_ITERATIONS,
int L_ITERATIONS>
332 [[nodiscard]] constexpr
double log2(
double x)
334 return cstd::log<E_ITERATIONS, L_ITERATIONS>(
x) /
M_LN2;
337 template<
int E_ITERATIONS,
int L_ITERATIONS>
338 [[nodiscard]] constexpr
double log10(
double x)
340 return cstd::log<E_ITERATIONS, L_ITERATIONS>(
x) /
M_LN10;
343 template<
int E_ITERATIONS,
int L_ITERATIONS>
344 [[nodiscard]] constexpr
double pow(
double x,
double y)
346 return cstd::exp<E_ITERATIONS>(cstd::log<E_ITERATIONS, L_ITERATIONS>(
x) * y);
349 template<
int ITERATIONS>
350 [[nodiscard]] constexpr
double exp2(
double x)
352 return cstd::exp<ITERATIONS>(
M_LN2 *
x);
355 [[nodiscard]] constexpr
double round(
double x)
357 return (
x >= 0) ? int(
x + 0.5)
361 [[nodiscard]] constexpr
float round(
float x)
363 return (
x >= 0) ? int(
x + 0.5f)
367 [[nodiscard]] constexpr
double sqrt(
double x)
372 while (curr != prev) {
374 curr = 0.5 * (curr +
x / curr);
constexpr double pow(double x, double y)
constexpr double log(double x)
constexpr ForwardIt partition(ForwardIt first, ForwardIt last, UnaryPredicate p)
constexpr void sort(RAIt first, RAIt last, Compare cmp=Compare{})
constexpr float round(float x)
constexpr double sin_iter(double x)
constexpr void fill(ForwardIt first, ForwardIt last, const T &value)
constexpr double simple_fmod(double x, double y)
constexpr double exp2(double x)
constexpr double sqrt(double x)
constexpr void iter_swap(Iter1 a, Iter2 b)
constexpr double round(double x)
constexpr double exp(double x)
constexpr bool lexicographical_compare(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2)
constexpr double cos_iter(double x)
constexpr double ipow(double x, int i)
constexpr InputIt find_if_not(InputIt first, InputIt last, UnaryPredicate p)
constexpr double log10(double x)
constexpr void replace(ForwardIt first, ForwardIt last, const T &old_value, const T &new_value)
constexpr double sin(double x)
constexpr double cos(double x)
constexpr double log2(double x)
constexpr double upow(double x, unsigned u)
constexpr KeyMatrixPosition x
Keyboard bindings.
constexpr void repeat(T n, Op op)
Repeat the given operation 'op' 'n' times.
constexpr auto xrange(T e)
constexpr auto begin(const zstring_view &x)
constexpr auto end(const zstring_view &x)