#pragma once #if !defined(BAD_APPLE_OS_ALGORITHM_INCLUDED) #define BAD_APPLE_OS_ALGORITHM_INCLUDED #include #include #include namespace std { template constexpr const T& min(const T& left, const T& right) { return right < left ? right : left; } template constexpr const T& max(const T& left, const T& right) { return right > left ? right : left; } template 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 constexpr void sort(RandomIt first, RandomIt last) { sort(first, last, [](auto left, auto right) { return left < right; }); } template 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 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)