| e8555c89 Andy McCall |
1 |
#ifndef RANGES_HH |
| 2 |
#define RANGES_HH |
| 3 |
|
| 4 |
#include <algorithm> |
| 5 |
#include <bit> |
| 6 |
#include <cassert> |
| 7 |
#include <cstdint> |
| 8 |
#include <functional> |
| 9 |
#include <iterator> |
| 10 |
#include <memory> |
| 11 |
#include <ranges> |
| 12 |
#include <span> |
| 13 |
#include <version> |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
namespace ranges { |
| 28 |
|
| 29 |
|
| 30 |
template<std::forward_iterator ForwardIt, typename T> |
| 31 |
constexpr void iota(ForwardIt first, ForwardIt last, T value) |
| 32 |
{ |
| 33 |
while (first != last) { |
| 34 |
*first++ = value; |
| 35 |
++value; |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
template<std::ranges::forward_range Range, typename T> |
| 40 |
constexpr void iota(Range&& range, T&& value) |
| 41 |
{ |
| 42 |
::ranges::iota(std::ranges::begin(range), std::ranges::end(range), std::forward<T>(value)); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
template<std::ranges::input_range Range, typename Proj = std::identity> |
| 48 |
bool all_equal(Range&& range, Proj proj = {}) |
| 49 |
{ |
| 50 |
auto it = std::ranges::begin(range); |
| 51 |
auto et = std::ranges::end(range); |
| 52 |
if (it == et) return true; |
| 53 |
|
| 54 |
auto val = std::invoke(proj, *it); |
| 55 |
for (++it; it != et; ++it) { |
| 56 |
if (std::invoke(proj, *it) != val) { |
| 57 |
return false; |
| 58 |
} |
| 59 |
} |
| 60 |
return true; |
| 61 |
} |
| 62 |
|
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
template<std::ranges::sized_range Input, std::ranges::sized_range Output> |
| 72 |
constexpr auto copy_to_range(Input&& in, Output&& out) |
| 73 |
{ |
| 74 |
assert(std::ranges::size(in) <= std::ranges::size(out)); |
| 75 |
|
| 76 |
|
| 77 |
auto f = std::ranges::begin(in); |
| 78 |
auto l = std::ranges::end(in); |
| 79 |
auto o = std::ranges::begin(out); |
| 80 |
while (f != l) { |
| 81 |
*o++ = *f++; |
| 82 |
} |
| 83 |
return o; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
template<std::ranges::forward_range Range, typename T, typename Compare = std::less<>, typename Proj = std::identity> |
| 102 |
[[nodiscard]] auto* binary_find(Range&& range, const T& value, Compare comp = {}, Proj proj = {}) |
| 103 |
{ |
| 104 |
auto it = std::ranges::lower_bound(range, value, comp, proj); |
| 105 |
return ((it != std::ranges::end(range)) && (!std::invoke(comp, value, std::invoke(proj, *it)))) |
| 106 |
? std::to_address(it) |
| 107 |
: nullptr; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
template<std::ranges::range Range> |
| 115 |
[[nodiscard]] constexpr auto subspan(Range&& range, size_t offset, size_t count = std::dynamic_extent) |
| 116 |
{ |
| 117 |
return std::span(std::forward<Range>(range)).subspan(offset, count); |
| 118 |
} |
| 119 |
|
| 120 |
template<size_t Count, std::ranges::range Range> |
| 121 |
[[nodiscard]] constexpr auto subspan(Range&& range, size_t offset = 0) |
| 122 |
{ |
| 123 |
return std::span(std::forward<Range>(range)).subspan(offset).template first<Count>(); |
| 124 |
} |
| 125 |
|
| 126 |
template<typename T, size_t Size> |
| 127 |
[[nodiscard]] inline auto as_byte_span(std::span<T, Size> s) |
| 128 |
{ |
| 129 |
return std::span{std::bit_cast<const uint8_t*>(s.data()), s.size_bytes()}; |
| 130 |
} |
| 131 |
|
| 132 |
#endif |