Flow
Documentation for the Flow C++ Library
Loading...
Searching...
No Matches
flow_integral_iterator.h
Go to the documentation of this file.
1#pragma once
2#include <concepts>
3#include <iterator>
4
5namespace flow {
6
9 template <std::integral T>
12
13 public:
14 using value_type = T;
15 using difference_type = std::ptrdiff_t;
16 using reference = T; // yields by value
17 using pointer = void; // no real pointer
18 using iterator_category = std::random_access_iterator_tag;
19
20 explicit constexpr IntegralIterator(T value) noexcept : value_(value) {}
21
22 constexpr reference operator*() const noexcept {
23 return value_;
24 }
25
26 constexpr IntegralIterator& operator++() noexcept {
27 ++value_;
28 return *this;
29 }
30
31 constexpr IntegralIterator operator++(int) noexcept {
32 IntegralIterator tmp = *this;
33 ++*this;
34 return tmp;
35 }
36
37 constexpr IntegralIterator& operator--() noexcept {
38 --value_;
39 return *this;
40 }
41
42 constexpr IntegralIterator operator--(int) noexcept {
43 IntegralIterator tmp = *this;
44 --*this;
45 return tmp;
46 }
47
49 value_ += static_cast<T>(n);
50 return *this;
51 }
52
54 value_ -= static_cast<T>(n);
55 return *this;
56 }
57
58 constexpr reference operator[](difference_type n) const noexcept {
59 return value_ + static_cast<reference>(n);
60 }
61
62 constexpr difference_type operator-(const IntegralIterator& rhs) const noexcept {
63 return static_cast<difference_type>(value_) - static_cast<difference_type>(rhs.value_);
64 }
65
66 constexpr auto operator<=>(const IntegralIterator&) const = default;
67
68 friend void swap(IntegralIterator& lhs, IntegralIterator& rhs) noexcept {
69 using std::swap;
70 swap(lhs.value_, rhs.value_);
71 }
72 };
73
74 template <std::integral T>
76 typename IntegralIterator<T>::difference_type n) noexcept {
77 it += n;
78 return it;
79 }
80
81 template <std::integral T>
83 IntegralIterator<T> it) noexcept {
84 return it + n;
85 }
86
87 template <std::integral T>
89 typename IntegralIterator<T>::difference_type n) noexcept {
90 it -= n;
91 return it;
92 }
93}
An iterator that can iterate through a range of integer. Useful when the search space is too big to f...
constexpr reference operator[](difference_type n) const noexcept
constexpr IntegralIterator operator--(int) noexcept
constexpr IntegralIterator & operator-=(difference_type n) noexcept
constexpr IntegralIterator & operator+=(difference_type n) noexcept
constexpr IntegralIterator operator++(int) noexcept
constexpr IntegralIterator(T value) noexcept
constexpr reference operator*() const noexcept
constexpr IntegralIterator & operator++() noexcept
std::random_access_iterator_tag iterator_category
friend void swap(IntegralIterator &lhs, IntegralIterator &rhs) noexcept
constexpr IntegralIterator & operator--() noexcept
constexpr auto operator<=>(const IntegralIterator &) const =default
constexpr difference_type operator-(const IntegralIterator &rhs) const noexcept
constexpr IntegralIterator< T > operator+(IntegralIterator< T > it, typename IntegralIterator< T >::difference_type n) noexcept
constexpr IntegralIterator< T > operator-(IntegralIterator< T > it, typename IntegralIterator< T >::difference_type n) noexcept