| 1 |
/* |
| 2 |
* This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>. |
| 3 |
* It is copyright by its individual contributors, as recorded in the |
| 4 |
* project's Git history. See COPYING.txt at the top level for license |
| 5 |
* terms and a link to the Git history. |
| 6 |
*/ |
| 7 |
|
| 8 |
/* |
| 9 |
* std::ranges is a C++20 feature. |
| 10 |
* |
| 11 |
* gcc-10.1 (released 2020-05-07) shipped std::ranges support. |
| 12 |
* |
| 13 |
* clang-14 (released 2022-03-25) lacks sufficient std::ranges support, and |
| 14 |
* requires a preprocessor symbol to unlock the parts it provides. |
| 15 |
* |
| 16 |
* clang-15 (released 2022-09-06) defines sufficient std::ranges support, but |
| 17 |
* is still incomplete, and still requires a preprocessor symbol to unlock |
| 18 |
* them. |
| 19 |
* |
| 20 |
* clang-14 is currently standard on OS X. |
| 21 |
* |
| 22 |
* When Rebirth is built with clang, this header provides a minimal |
| 23 |
* implementation to allow calling code to use those features, but does not |
| 24 |
* attempt to provide the constraints that a standards-conforming |
| 25 |
* implementation would provide. |
| 26 |
* |
| 27 |
* When Rebirth is built with gcc, this header provides `using` declarations to |
| 28 |
* pull in the compiler's `std::ranges` implementation, which will include the |
| 29 |
* standards-conforming constraints. |
| 30 |
*/ |
| 31 |
|
| 32 |
#pragma once |
| 33 |
#include <algorithm> |
| 34 |
#include <ranges> |
| 35 |
|
| 36 |
#ifdef __clang__ |
| 37 |
#include <functional> /* std::invoke */ |
| 38 |
#endif |
| 39 |
|
| 40 |
namespace ranges { |
| 41 |
|
| 42 |
#ifdef __clang__ |
| 43 |
template <typename R> |
| 44 |
concept range = true; |
| 45 |
|
| 46 |
template <typename R> |
| 47 |
concept borrowed_range = true; |
| 48 |
|
| 49 |
template <typename iterator, typename sentinel = iterator> |
| 50 |
class subrange |
| 51 |
{ |
| 52 |
iterator b; |
| 53 |
sentinel e; |
| 54 |
public: |
| 55 |
subrange(iterator b, sentinel e) : |
| 56 |
b(b), e(e) |
| 57 |
{ |
| 58 |
} |
| 59 |
subrange(auto &&r) : |
| 60 |
b(std::ranges::begin(r)), e(std::ranges::end(r)) |
| 61 |
{ |
| 62 |
} |
| 63 |
iterator begin() const { return b; } |
| 64 |
sentinel end() const { return e; } |
| 65 |
auto size() const |
| 66 |
{ |
| 67 |
return std::distance(b, e); |
| 68 |
} |
| 69 |
bool empty() const |
| 70 |
{ |
| 71 |
return b == e; |
| 72 |
} |
| 73 |
}; |
| 74 |
|
| 75 |
subrange(auto &r) -> subrange<decltype(std::ranges::begin(r)), decltype(std::ranges::end(r))>; |
| 76 |
|
| 77 |
template <typename IB, typename V> |
| 78 |
requires( |
| 79 |
requires(IB b, V v) { |
| 80 |
*b == v; |
| 81 |
} |
| 82 |
) |
| 83 |
auto find(IB b, auto e, V v) |
| 84 |
{ |
| 85 |
for (; b != e; ++b) |
| 86 |
if (*b == v) |
| 87 |
break; |
| 88 |
return b; |
| 89 |
} |
| 90 |
|
| 91 |
auto find(auto b, auto e, auto v, auto project) |
| 92 |
{ |
| 93 |
for (; b != e; ++b) |
| 94 |
if (std::invoke(project, *b) == v) |
| 95 |
break; |
| 96 |
return b; |
| 97 |
} |
| 98 |
|
| 99 |
auto find(auto &r, auto v) |
| 100 |
{ |
| 101 |
return find(std::ranges::begin(r), std::ranges::end(r), v); |
| 102 |
} |
| 103 |
|
| 104 |
template <typename R, typename P> |
| 105 |
requires( |
| 106 |
requires(R &r, P p) { |
| 107 |
std::invoke(p, *std::ranges::begin(r)); |
| 108 |
} |
| 109 |
) |
| 110 |
auto find(R &r, auto v, P project) |
| 111 |
{ |
| 112 |
return find(std::ranges::begin(r), std::ranges::end(r), v, project); |
| 113 |
} |
| 114 |
|
| 115 |
auto find_if(auto b, auto e, auto predicate) |
| 116 |
{ |
| 117 |
for (; b != e; ++b) |
| 118 |
if (predicate(*b)) |
| 119 |
break; |
| 120 |
return b; |
| 121 |
} |
| 122 |
|
| 123 |
auto find_if(auto &r, auto predicate) |
| 124 |
{ |
| 125 |
return find_if(std::ranges::begin(r), std::ranges::end(r), predicate); |
| 126 |
} |
| 127 |
|
| 128 |
#else |
| 129 |
|
| 130 |
using std::ranges::range; |
| 131 |
using std::ranges::borrowed_range; |
| 132 |
using std::ranges::subrange; |
| 133 |
using std::ranges::find; |
| 134 |
using std::ranges::find_if; |
| 135 |
|
| 136 |
#endif |
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
#ifdef __clang__ |
| 141 |
|
| 142 |
template <typename iterator, typename sentinel> |
| 143 |
inline constexpr bool std::ranges::enable_borrowed_range<ranges::subrange<iterator, sentinel>> = true; |
| 144 |
|
| 145 |
#endif |