| 1 | #ifndef RANGES_H |
| 2 | #define RANGES_H |
| 3 | |
| 4 | #include <algorithm> |
| 5 | |
| 6 | // This is a (partial) reimplementation of c++20 'std::ranges' stuff. |
| 7 | // In the future we can: |
| 8 | // * Replace '#include "ranges.h"' with '#include <ranges>'. |
| 9 | // * Replace 'ranges::xxx()' with 'std::ranges::xxx()'. |
| 10 | |
| 11 | // Reimplementation of c++20 std::identity. |
| 12 | struct identity { |
| 13 | template<typename T> |
| 14 | [[nodiscard]] constexpr T&& operator()(T&& t) const noexcept { |
| 15 | return std::forward<T>(t); |
| 16 | } |
| 17 | }; |
| 18 | |
| 19 | namespace ranges { |
| 20 | |
| 21 | template<typename OutputRange, typename T> |
| 22 | void fill(OutputRange&& range, const T& value) |
| 23 | { |
| 24 | std::fill(std::begin(range), std::end(range), value); |
| 25 | } |
| 26 | |
| 27 | template<typename InputRange, typename T> |
| 28 | [[nodiscard]] auto find(InputRange&& range, const T& value) |
| 29 | { |
| 30 | return std::find(std::begin(range), std::end(range), value); |
| 31 | } |
| 32 | |
| 33 | template<typename InputRange, typename T, typename Proj> |
| 34 | [[nodiscard]] auto find(InputRange&& range, const T& value, Proj proj) |
| 35 | { |
| 36 | return find_if(std::forward<InputRange>(range), |
| 37 | [&](const auto& e) { return std::invoke(proj, e) == value; }); |
| 38 | } |
| 39 | |
| 40 | template<typename InputRange, typename UnaryPredicate> |
| 41 | [[nodiscard]] auto find_if(InputRange&& range, UnaryPredicate pred) |
| 42 | { |
| 43 | return std::find_if(std::begin(range), std::end(range), pred); |
| 44 | } |
| 45 | |
| 46 | template<typename ForwardRange, typename T, typename Compare = std::less<>, typename Proj = identity> |
| 47 | [[nodiscard]] auto lower_bound(ForwardRange&& range, const T& value, Compare comp = {}, Proj proj = {}) |
| 48 | { |
| 49 | auto comp2 = [&](const auto& x, const auto& y) { |
| 50 | return comp(std::invoke(proj, x), y); |
| 51 | }; |
| 52 | return std::lower_bound(std::begin(range), std::end(range), value, comp2); |
| 53 | } |
| 54 | |
| 55 | template<typename ForwardRange, typename T, typename Compare = std::less<>, typename Proj = identity> |
| 56 | [[nodiscard]] auto upper_bound(ForwardRange&& range, const T& value, Compare comp = {}, Proj proj = {}) |
| 57 | { |
| 58 | auto comp2 = [&](const auto& x, const auto& y) { |
| 59 | return comp(x, std::invoke(proj, y)); |
| 60 | }; |
| 61 | return std::upper_bound(std::begin(range), std::end(range), value, comp2); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | // Not part of standard c++ (but has been proposed for a future version) |
| 66 | // This is more convenient to use than find(), if you don't care where in the |
| 67 | // range the element is located. |
| 68 | template<typename ForwardRange, typename T> |
| 69 | [[nodiscard]] bool contains(ForwardRange&& range, const T& value) |
| 70 | { |
| 71 | return find(range, value) != std::end(range); |
| 72 | } |
| 73 | } // namespace ranges |
| 74 | |
| 75 | #endif |