alpaqa 1.0.0a18
Nonconvex constrained optimization
Loading...
Searching...
No Matches
iter-adapter.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <concepts>
4#include <iterator>
5#include <ranges>
6#include <type_traits>
7#include <utility>
8
9namespace alpaqa::util {
10
11template <class It>
13 // P2325R3: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2325r3.html
14 iter_range_adapter() = default;
15 iter_range_adapter(It it) : it{std::forward<It>(it)} {}
17
18 struct sentinel_t {};
19
20 struct iter_t : std::remove_cvref_t<It> {
21 // P2325R3: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2325r3.html
22 iter_t() = default;
23 iter_t(It it) : std::remove_cvref_t<It>{std::forward<It>(it)} {}
24
25 bool operator!=(sentinel_t) const { return static_cast<bool>(*this); }
26 bool operator==(sentinel_t) const { return !static_cast<bool>(*this); }
27 // TODO: For Clang bug
28 friend bool operator!=(sentinel_t s, const iter_t &i) { return i != s; }
29 friend bool operator==(sentinel_t s, const iter_t &i) { return i == s; }
30
32 this->std::remove_cvref_t<It>::operator++();
33 return *this;
34 }
35 iter_t operator++(int i) const {
36 this->std::remove_cvref_t<It>::operator++(i);
37 return *this;
38 }
39 const iter_t &operator*() const { return *this; }
41 using pointer = iter_t *;
42 using reference = iter_t &;
43 using difference_type = std::ptrdiff_t;
44 };
45
47 return iter_t{it};
48 }
49 auto begin() && -> std::input_or_output_iterator auto {
50 return iter_t{std::forward<It>(it)};
51 }
52 auto end() const /* -> std::sentinel_for<iter_t> auto */ {
53 return sentinel_t{};
54 }
55};
56
57} // namespace alpaqa::util
58
59// Make iter_range_adapter available as an std::ranges view.
60// Iterators remain valid even if the range is destroyed.
61// Assume that the user takes care of lifetime, similar to std::span.
62#ifndef DOXYGEN
63template <class It>
64inline constexpr bool ::std::ranges::enable_borrowed_range<
66#endif
constexpr const auto inf
Definition config.hpp:112
friend bool operator==(sentinel_t s, const iter_t &i)
friend bool operator!=(sentinel_t s, const iter_t &i)
auto begin() &&-> std::input_or_output_iterator auto
auto begin() const &-> std::input_or_output_iterator auto