1#ifndef NNGN_UTILS_RANGES_H
2#define NNGN_UTILS_RANGES_H
26 constexpr auto begin(
void)
const {
return this->
b; }
27 constexpr auto begin(
void) {
return this->
b; }
28 constexpr auto end(
void)
const {
return this->
e; }
29 constexpr auto end(
void) {
return this->
e; }
31 std::ranges::iterator_t<T>
b,
e;
42 template<std::ranges::range R>
45 if constexpr(std::ranges::sized_range<R>)
46 ret.reserve(std::ranges::size(
FWD(
r)));
47 for(
auto &&x :
FWD(
r))
53constexpr auto *
data_end(std::ranges::contiguous_range
auto &&
r) {
54 return std::data(
r) + std::size(
r);
66 const auto prev =
v.size();
68 for(std::size_t
i = prev;
i <
n; ++
i)
79template<std::
size_t N>
81 return [&
s]<
auto ...I>(std::index_sequence<I...>) {
82 return std::array{
s[I]...};
83 }(std::make_index_sequence<N>{});
86template<std::
size_t N>
88 return to_array<
N - 1>(
static_cast<const char*
>(
s));
91template<std::
size_t N>
92consteval auto to_array(
const std::ranges::contiguous_range
auto &
r) {
96template<std::
size_t N, std::ranges::view V>
98 using T = std::ranges::range_value_t<V>;
99 std::array<T, N> ret = {};
100 std::copy_n(std::ranges::begin(
v),
N,
begin(ret));
105 const std::ranges::contiguous_range
auto &
r,
const auto &x)
107 return std::ranges::less_equal{}(&*std::ranges::cbegin(
r), &x)
108 && std::ranges::less{}(&x, &*std::ranges::cend(
r));
112 const std::ranges::contiguous_range
auto &
r,
const auto &x)
114 return std::ranges::less_equal{}(&*std::ranges::cbegin(
r), &x)
115 && std::ranges::less_equal{}(&x, &*std::ranges::cend(
r));
118template<std::ranges::forward_range R,
typename Proj = std::
identity>
120 return std::adjacent_find(
121 std::ranges::begin(
FWD(
r)), std::ranges::end(
FWD(
r)),
122 [proj]<
typename T>(
const T &lhs,
const T &rhs) {
123 return proj(lhs) + proj(
T{1}) != proj(rhs);
128template<std::ranges::range R>
129constexpr void iota(R &&
r, std::ranges::range_value_t<R> &&x = {}) {
130 std::iota(std::ranges::begin(
r), std::ranges::end(
r),
FWD(x));
133constexpr auto reduce(std::ranges::range
auto &&
r) {
134 return std::reduce(std::ranges::begin(
r), std::ranges::end(
r));
138 std::ranges::range
auto &&
r,
auto &&
init = {},
auto &&op = std::plus<>{})
141 std::ranges::begin(
r), std::ranges::end(
r),
147 auto last =
v->end() - 1;
148 *p = std::move(*last);
153 std::input_iterator I,
154 std::output_iterator<std::iter_value_t<I>> O>
156 std::generate(df, dl, [
i =
f,
f,
l](
void)
mutable {
164 std::ranges::range
auto &&p, std::ranges::range
auto &&
r)
167 std::ranges::begin(
FWD(p)), std::ranges::end(
FWD(p)),
168 std::ranges::begin(
FWD(
r)), std::ranges::end(
FWD(
r)));
172constexpr T *
memcpy(
T *dst, std::ranges::contiguous_range
auto &&
r) {
173 const auto n = std::span{
FWD(
r)}.size_bytes();
174 std::memcpy(dst, std::data(
FWD(
r)),
n);
179constexpr T *
memmove(
T *dst, std::ranges::contiguous_range
auto &&
r) {
180 const auto n = std::span{
r}.size_bytes();
181 std::memmove(dst, std::data(
FWD(
r)),
n);
190template<
typename T>
inline constexpr bool enable_view;
191template<
typename T>
inline constexpr bool enable_borrowed_range;
196inline constexpr bool enable_view<nngn::owning_view<T>> =
true;
200inline constexpr bool enable_borrowed_range<nngn::owning_view<T>> =
true;
constexpr auto end(void) const
Definition ranges.h:28
constexpr auto begin(void)
Definition ranges.h:27
constexpr auto end(void)
Definition ranges.h:29
constexpr auto begin(void) const
Definition ranges.h:26
constexpr owning_view(const T &)=delete
constexpr owning_view(T &&r)
Definition ranges.h:25
std::ranges::iterator_t< T > b
Definition ranges.h:31
std::ranges::iterator_t< T > e
Definition ranges.h:31
assert
Definition debug.lua:3
function DEMO text end
Definition demo0.lua:6
for i
Definition font.lua:5
local n
Definition dump_lights.lua:5
local r
Definition gamma.lua:7
local N
Definition gamma.lua:5
init
Definition img_common.lua:34
local l
Definition light.lua:23
void const_time_erase(auto *v, auto *p)
Definition ranges.h:145
constexpr const R * begin(const T< R > &v)
Definition vec.h:207
constexpr auto * data_end(std::ranges::contiguous_range auto &&r)
Definition ranges.h:53
constexpr auto reduce(std::ranges::range auto &&r)
Definition ranges.h:133
consteval auto to_array(const char *s)
Definition ranges.h:80
constexpr void fill_with_pattern(I f, I l, O df, O dl)
Definition ranges.h:155
void set_capacity(V *v, size_t n)
Definition ranges.h:73
constexpr bool in_range(const std::ranges::contiguous_range auto &r, const auto &x)
Definition ranges.h:111
constexpr cast(U &&x)
Cast value to T with the strictest cast operator.
Definition utils.h:109
constexpr bool is_sequence(R &&r, Proj proj={})
Definition ranges.h:119
constexpr void iota(R &&r, std::ranges::range_value_t< R > &&x={})
Definition ranges.h:129
typedef const_iterator
Definition ranges.h:18
constexpr T * memcpy(T *dst, std::ranges::contiguous_range auto &&r)
Definition ranges.h:172
constexpr bool contains(const std::ranges::contiguous_range auto &r, const auto &x)
Definition ranges.h:104
constexpr T * memmove(T *dst, std::ranges::contiguous_range auto &&r)
Definition ranges.h:179
void resize_and_init(T *p, std::size_t n, auto &&f)
Definition ranges.h:64
move
Definition camera.lua:57
v[1]
Definition math.lua:22
#define FWD(...)
Definition utils.h:18
friend auto operator|(R &&r, range_to)
Definition ranges.h:43
local e
Definition math.lua:4
std::chrono::seconds s
Definition timing.cpp:6