#ifndef RANGES_H #define RANGES_H #include // This is a (partial) reimplementation of c++20 'std::ranges' stuff. // In the future we can: // * Replace '#include "ranges.h"' with '#include '. // * Replace 'ranges::xxx()' with 'std::ranges::xxx()'. // Reimplementation of c++20 std::identity. struct identity { template [[nodiscard]] constexpr T&& operator()(T&& t) const noexcept { return std::forward(t); } }; namespace ranges { template void fill(OutputRange&& range, const T& value) { std::fill(std::begin(range), std::end(range), value); } template [[nodiscard]] auto find(InputRange&& range, const T& value) { return std::find(std::begin(range), std::end(range), value); } template [[nodiscard]] auto find(InputRange&& range, const T& value, Proj proj) { return find_if(std::forward(range), [&](const auto& e) { return std::invoke(proj, e) == value; }); } template [[nodiscard]] auto find_if(InputRange&& range, UnaryPredicate pred) { return std::find_if(std::begin(range), std::end(range), pred); } template, typename Proj = identity> [[nodiscard]] auto lower_bound(ForwardRange&& range, const T& value, Compare comp = {}, Proj proj = {}) { auto comp2 = [&](const auto& x, const auto& y) { return comp(std::invoke(proj, x), y); }; return std::lower_bound(std::begin(range), std::end(range), value, comp2); } template, typename Proj = identity> [[nodiscard]] auto upper_bound(ForwardRange&& range, const T& value, Compare comp = {}, Proj proj = {}) { auto comp2 = [&](const auto& x, const auto& y) { return comp(x, std::invoke(proj, y)); }; return std::upper_bound(std::begin(range), std::end(range), value, comp2); } // Not part of standard c++ (but has been proposed for a future version) // This is more convenient to use than find(), if you don't care where in the // range the element is located. template [[nodiscard]] bool contains(ForwardRange&& range, const T& value) { return find(range, value) != std::end(range); } } // namespace ranges #endif