SILICON
Loading...
Searching...
No Matches
ranges_wrapper.hpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2026. Giulio Cocconi
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19#include <ranges>
20#include <vector>
21#include <version>
22
23#ifndef __cpp_lib_ranges_zip
24 #include <range/v3/functional/pipeable.hpp>
25 #include <range/v3/view/empty.hpp>
26 #include <range/v3/view/sliding.hpp>
27 #include <range/v3/view/transform.hpp>
28 #include <tuple>
29 #include <utility>
30#endif
31
32#ifndef __cpp_lib_ranges_slide
33 #include <range/v3/view/sliding.hpp>
34#endif
35
36#ifndef __cpp_lib_ranges_chunk
37 #include <range/v3/view/chunk.hpp>
38#endif
39
40namespace SILICON::views {
41
42#ifndef __cpp_lib_ranges_zip
43template <std::size_t N>
44inline constexpr auto adjacent = ranges::make_pipeable([](auto&& r) {
45 if constexpr (N == 0) {
46 return ranges::views::empty<std::tuple<>>;
47 } else {
48 return std::forward<decltype(r)>(r) | ranges::views::sliding(N)
49 | ranges::views::transform([](auto window) {
50 return [&]<std::size_t... I>(std::index_sequence<I...>) {
51 return std::tuple<decltype(*ranges::next(ranges::begin(window), I))...>(
52 *ranges::next(ranges::begin(window), I)...);
53 }(std::make_index_sequence<N>{});
54 });
55 }
56});
57#else
58template <std::size_t N> inline constexpr auto adjacent = std::views::adjacent<N>;
59#endif
60
61#ifndef __cpp_lib_ranges_enumerate
62static inline constexpr auto enumerate = [](auto&& r) {
63 return std::views::zip(std::views::iota(0, static_cast<ptrdiff_t>(r.size())), r);
64};
65
66template <typename R> auto operator|(R&& r, decltype(SILICON::views::enumerate))
67{
68 return SILICON::views::enumerate(std::forward<R>(r));
69}
70#else
71inline constexpr auto enumerate = std::views::enumerate;
72#endif
73
74#ifndef __cpp_lib_ranges_slide
75inline constexpr auto slide = ranges::views::sliding;
76#else
77inline constexpr auto slide = std::views::slide;
78#endif
79
80#ifndef __cpp_lib_ranges_chunk
81inline constexpr auto chunk = ranges::views::chunk;
82#else
83inline constexpr auto chunk = std::views::chunk;
84#endif
85
86} // namespace SILICON::views
constexpr auto adjacent
constexpr auto slide
auto operator|(R &&r, decltype(SILICON::views::enumerate))
constexpr auto chunk