-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIndexProxyIter.hpp
More file actions
151 lines (122 loc) · 3.96 KB
/
IndexProxyIter.hpp
File metadata and controls
151 lines (122 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (c) 2024 Francesco Cavaliere
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#ifndef CAV_VECTORS_INDEXPROXYITER_HPP
#define CAV_VECTORS_INDEXPROXYITER_HPP
#include <cassert>
#include <concepts>
#include <cstddef>
#include <iterator>
#include "../comptime/syntactic_sugars.hpp"
#include "../comptime/test.hpp"
namespace cav {
template <typename T>
concept indexable = requires(T cont) {
{ cont.size() } -> std::integral;
{ cont[0] };
};
template <typename ContT>
struct IndexProxyIter {
using self = IndexProxyIter;
using iterator_category = typename std::random_access_iterator_tag;
using difference_type = ptrdiff_t;
using value_type = typename ContT::value_type;
using pointer = value_type*;
using reference = value_type&;
std::size_t idx = 0;
ContT* container = nullptr;
[[nodiscard]] constexpr operator IndexProxyIter<ContT const>() const {
return {idx, container};
}
[[nodiscard]] constexpr decl_auto operator*() const {
return container->operator[](idx);
}
[[nodiscard]] constexpr decl_auto operator[](ptrdiff_t n) const {
return (*container)[idx + n];
}
constexpr self& operator++() {
++idx;
return *this;
}
constexpr self operator++(int) {
++idx;
return {idx - 1, container};
}
constexpr self& operator--() {
--idx;
return *this;
}
constexpr self operator--(int) {
--idx;
return {idx + 1, container};
}
constexpr self& operator+=(ptrdiff_t n) {
idx += n;
return *this;
}
constexpr self& operator-=(ptrdiff_t n) {
idx -= n;
return *this;
}
[[nodiscard]] constexpr ptrdiff_t operator-(self other) const {
assert(container == other.container);
return idx - other.idx;
}
[[nodiscard]] constexpr bool operator==(self other) const {
assert(container == other.container);
return idx == other.idx;
}
[[nodiscard]] constexpr auto operator<=>(self other) const {
assert(container == other.container);
return idx <=> other.idx;
}
};
template <typename ContT>
IndexProxyIter(size_t, ContT*) -> IndexProxyIter<ContT>;
template <indexable ContT>
constexpr IndexProxyIter<ContT> operator+(IndexProxyIter<ContT> iter, ptrdiff_t n) {
return iter += n;
}
template <indexable ContT>
constexpr IndexProxyIter<ContT> operator+(ptrdiff_t n, IndexProxyIter<ContT> iter) {
return iter += n;
}
template <indexable ContT>
constexpr IndexProxyIter<ContT> operator-(IndexProxyIter<ContT> iter, ptrdiff_t n) {
return iter -= n;
}
template <indexable ContT>
constexpr IndexProxyIter<ContT> operator-(ptrdiff_t n, IndexProxyIter<ContT> iter) {
return iter -= n;
}
} // namespace cav
#ifdef CAV_COMP_TESTS
#include <vector>
namespace cav { namespace {
CAV_PASS(std::random_access_iterator<IndexProxyIter<std::vector<int>>>);
CAV_BLOCK_PASS({
auto vec = std::vector<int>{0, 1, 2, 3, 4};
auto beg = IndexProxyIter(0, &vec);
auto end = IndexProxyIter(5, &vec);
for (auto it = beg; int n : vec) {
assert(*it == n);
assert(it < end);
assert(std::distance(beg, it) == n);
++it;
}
});
} // namespace
} // namespace cav
#endif
#endif /* CAV_VECTORS_INDEXPROXYITER_HPP */