97 lines
2.1 KiB
Plaintext

#pragma once
#if !defined(BAD_APPLE_OS_ALGORITHM_INCLUDED)
#define BAD_APPLE_OS_ALGORITHM_INCLUDED
#include <cstddef>
#include <iterator>
#include <utility>
namespace std
{
template<typename T>
constexpr const T& min(const T& left, const T& right)
{
return right < left ? right : left;
}
template<typename T>
constexpr const T& max(const T& left, const T& right)
{
return right > left ? right : left;
}
template<typename RandomIt, class Compare>
constexpr void sort(RandomIt first, RandomIt last, Compare comp)
{
// TODO: be smarter
const size_t size = last - first;
size_t sorted = 1;
while (sorted < size)
{
// find insertion pos
size_t pos = 0;
while (pos < sorted)
{
if (comp(*(first + sorted), *(first + pos)))
{
break;
}
++pos;
}
// bail if already sorted
if (pos == sorted)
{
++sorted;
continue;
}
// insert at pos
for (size_t idx = pos; idx < sorted; ++idx)
{
swap(*(first + idx), *(first + sorted));
}
++sorted;
}
}
template<typename RandomIt>
constexpr void sort(RandomIt first, RandomIt last)
{
sort(first, last, [](auto left, auto right) { return left < right; });
}
template<typename ForwardIt, typename UnaryPredicate>
constexpr ForwardIt find_if(ForwardIt first, ForwardIt last, UnaryPredicate pred)
{
for (auto it = first; it != last; ++it)
{
if (pred(*it))
{
return it;
}
}
return last;
}
template<typename ForwardIt, typename UnaryPredicate>
constexpr ForwardIt remove_if(ForwardIt first, ForwardIt last, UnaryPredicate pred)
{
auto found = find_if(first, last, pred);
if (found != last)
{
for (auto tomove = next(found); tomove != last; ++tomove)
{
if (!pred(*tomove))
{
*found = move(*tomove);
++found;
}
}
}
return found;
}
}
#endif // !defined(BAD_APPLE_OS_ALGORITHM_INCLUDED)