Py2Cpp 1.6.3; VERSION ${PROJECT_VERSION}
Loading...
Searching...
No Matches
range.hpp
Go to the documentation of this file.
1
9#pragma once
10
11#include <cstddef>
12#include <iterator>
13// #include <type_traits>
14
15#if __cpp_constexpr >= 201304
16# define CONSTEXPR14 constexpr
17#else
18# define CONSTEXPR14 inline
19#endif
20
21namespace py {
22
31 template <typename T> struct RangeIterator {
32 using iterator_category = std::input_iterator_tag;
33 using difference_type = std::ptrdiff_t;
34 using value_type = T;
35 using pointer = const T*; // or also value_type*
36 using reference = const T&; // or also value_type&
37 using const_reference = const T&; // or also value_type&
38 using key_type = T; // luk:
39
40 T i;
41
53 constexpr auto operator!=(const RangeIterator& other) const -> bool {
54 return this->i != other.i;
55 }
56
68 constexpr auto operator==(const RangeIterator& other) const -> bool {
69 return this->i == other.i;
70 }
71
79 CONSTEXPR14 auto operator*() const -> const_reference { return this->i; }
80
89 ++this->i;
90 return *this;
91 }
92
102 auto temp = *this;
103 ++(*this);
104 return temp;
105 }
106 };
107
116 template <typename T> struct Range {
117 public:
119 using value_type = T;
120 using key_type = T;
121
124
132 constexpr auto begin() const -> iterator { return iterator{this->start}; }
133
141 constexpr auto end() const -> iterator { return iterator{this->stop}; }
142
150 constexpr auto empty() const -> bool { return this->stop == this->start; }
151
159 constexpr auto size() const -> size_t {
160 return static_cast<size_t>(this->stop - this->start);
161 }
162
171 constexpr auto operator[](size_t n) const -> T {
172 return T(this->start + n);
173 } // no bounds checking
174
181 constexpr auto contains(T n) const noexcept -> bool {
182 return !(n < this->start) && n < this->stop;
183 }
184 };
185
198 template <typename T> CONSTEXPR14 auto range(T start, T stop) -> Range<T> {
199 if (stop < start) {
200 stop = start;
201 }
202 return Range<T>{start, stop};
203 }
204
216 template <typename T> CONSTEXPR14 auto range(T stop) -> Range<T> { return range(T(0), stop); }
217
218} // namespace py
#define CONSTEXPR14
Definition fractions.hpp:18
Python-like utilities and data structures for C++.
Definition dict.hpp:18
CONSTEXPR14 auto range(T start, T stop) -> Range< T >
range(T start, T stop)
Definition range.hpp:198
Iterator for range-based sequences.
Definition range.hpp:31
T key_type
Definition range.hpp:38
std::input_iterator_tag iterator_category
Definition range.hpp:32
T value_type
Definition range.hpp:34
const T & const_reference
Definition range.hpp:37
CONSTEXPR14 auto operator*() const -> const_reference
Definition range.hpp:79
constexpr auto operator==(const RangeIterator &other) const -> bool
Equal to operator.
Definition range.hpp:68
T i
Definition range.hpp:40
CONSTEXPR14 auto operator++(int) -> RangeIterator
Post-increment operator.
Definition range.hpp:101
constexpr auto operator!=(const RangeIterator &other) const -> bool
Not equal to.
Definition range.hpp:53
std::ptrdiff_t difference_type
Definition range.hpp:33
CONSTEXPR14 auto operator++() -> RangeIterator &
Pre-increment operator.
Definition range.hpp:88
const T * pointer
Definition range.hpp:35
const T & reference
Definition range.hpp:36
Python-like range implementation.
Definition range.hpp:116
T value_type
Definition range.hpp:119
T start
Definition range.hpp:122
constexpr auto end() const -> iterator
Get iterator to the end of the range.
Definition range.hpp:141
T key_type
Definition range.hpp:120
constexpr auto contains(T n) const noexcept -> bool
Check if the range contains a value.
Definition range.hpp:181
constexpr auto begin() const -> iterator
Get iterator to the beginning of the range.
Definition range.hpp:132
constexpr auto size() const -> size_t
Get the size of the range.
Definition range.hpp:159
constexpr auto empty() const -> bool
Check if the range is empty.
Definition range.hpp:150
T stop
Definition range.hpp:123
constexpr auto operator[](size_t n) const -> T
Get element at index.
Definition range.hpp:171