NIM 跨平台 C++ SDK
载入中...
搜索中...
未找到
ne_stl_iterator.h
浏览该文件的文档.
1#ifndef NE_STL_INCLUDENE_STL_ITERATOR_H
2#define NE_STL_INCLUDENE_STL_ITERATOR_H
3
4#include "ne_stl_build.h"
5
6namespace nstd {
7
8template <class TElement>
10 TElement* first_;
11 const std::size_t* size_;
12 std::size_t pos_;
13 _iterator_type(TElement* first_ = nullptr, const std::size_t* size_ = nullptr, std::size_t pos_ = 0)
14 : first_(first_)
15 , size_(size_)
16 , pos_(pos_) {}
17};
18template <class TElement>
20
21template <class TElement>
23public:
24 using value_type = TElement;
25 using iterator_category = std::bidirectional_iterator_tag;
26 using difference_type = size_t;
27 using pointer = TElement*;
28 using reference = TElement&;
29 _const_iterator() = default;
34 const TElement& operator*() const { return retrieve(); }
35 const TElement& operator[](int32_t i) const { return _randomAccess(i); }
37 _increment();
38 return *this;
39 }
41 _const_iterator old = *this;
42 _increment();
43 return old;
44 }
46 _decrement();
47 return *this;
48 }
50 _const_iterator old = *this;
51 _decrement();
52 return old;
53 }
54
55 bool operator==(const _const_iterator& rhs) const { return current_.first_ == rhs.current_.first_ && current_.pos_ == rhs.current_.pos_; }
56 bool operator!=(const _const_iterator& rhs) const { return !(*this == rhs); }
57 _const_iterator operator+(std::size_t i) {
58 _const_iterator toReturn;
59 _add(toReturn, i);
60 return toReturn;
61 }
62 _const_iterator operator-(std::size_t i) {
63 _const_iterator toReturn;
64 _subtract(toReturn, i);
65 return toReturn;
66 }
67
68protected:
70 TElement& retrieve() const { return *(current_.first_ + current_.pos_); }
71 void _increment() {
72 if (current_.pos_ > *current_.size_) {
73 throw;
74 }
75 ++current_.pos_;
76 }
77 void _decrement() {
78 if (current_.pos_ == 0) {
79 throw;
80 }
81 --current_.pos_;
82 }
83 TElement& _randomAccess(std::size_t i) const {
84 if (i < 0 || i >= *current_.size_) {
85 throw;
86 }
87 return *(current_.first_ + i);
88 }
89 void _add(_const_iterator& toReturn, std::size_t i) {
90 if (current_.pos_ + i > *current_.size_) {
91 throw;
92 }
93 toReturn = *this;
94 toReturn.current_.pos_ += i;
95 }
96 void _subtract(_const_iterator& toReturn, std::size_t i) {
97 if (current_.pos_ < i) {
98 throw;
99 }
100 toReturn = *this;
101 toReturn.current_.pos_ -= i;
102 }
103};
104template <class TElement>
106
107template <class TElement>
108class _iterator : public nstd::_const_iterator<TElement> {
110
111public:
112 _iterator() = default;
115
116 TElement& operator*() { return _parent_t::retrieve(); }
117 const TElement& operator*() const { return _parent_t::operator*(); }
118 TElement& operator[](int32_t i) { return _parent_t::_randomAccess(i); }
119 const TElement& operator[](int32_t i) const { return _parent_t::operator[](i); }
122 return *this;
123 }
125 _iterator old = *this;
127 return old;
128 }
131 return *this;
132 }
134 _iterator old = *this;
136 return old;
137 }
138 _iterator operator+(std::size_t i) {
139 _iterator toReturn;
140 _parent_t::_add(toReturn, i);
141 return toReturn;
142 }
143 _iterator operator-(std::size_t i) {
144 _iterator toReturn;
145 _parent_t::_subtract(toReturn, i);
146 return toReturn;
147 }
148 std::size_t operator-(_iterator right) { return 0; }
149};
150template <class TElement>
152
153template <class TElement>
155template <class TElement>
157
158template <class TElement>
161
162public:
166
167 const TElement& operator*() const { return _parent_t::operator*(); }
168 const TElement& operator[](int32_t i) const { return _parent_t::operator[](i); }
170 _increment();
171 return *this;
172 }
174 _const_reverse_iterator old = *this;
175 _increment();
176 return old;
177 }
179 _decrement();
180 return *this;
181 }
183 _const_reverse_iterator old = *this;
184 _decrement();
185 return old;
186 }
187
190 _add(toReturn, i);
191 return toReturn;
192 }
195 _subtract(toReturn, i);
196 return toReturn;
197 }
198
199protected:
200 void _increment() {
201 if (_parent_t::current_.pos_ == 0) {
203 return;
204 } else if (_parent_t::current_.pos_ == *_parent_t::current_.size_) {
205 throw;
206 }
208 }
209 void _decrement() {
210 if (_parent_t::current_.pos_ == *_parent_t::current_.size_) {
211 _parent_t::current_.pos_ = 0;
212 return;
213 }
214 if (_parent_t::current_.pos_ + 1 == *_parent_t::current_.size_) {
215 throw;
216 }
218 }
219 void _add(_const_reverse_iterator& toReturn, std::size_t i) {
220 if (_parent_t::current_.pos_ == *_parent_t::current_.size_ && !i) {
221 throw;
222 }
223 if (this->current_.pos_ == i - 1) {
225 return;
226 } else if (this->current_.pos_ < i - 1)
227 throw;
228 toReturn = *this;
229 toReturn.current_.pos_ -= i;
230 }
231 void _subtract(_const_reverse_iterator& toReturn, std::size_t i) {
232 if (*_parent_t::current_.size_ <= _parent_t::current_.pos_ + i)
233 throw;
234 toReturn = *this;
235 toReturn.current_.pos_ += i;
236 }
237};
238template <class TElement>
240
241template <class TElement>
244
245public:
246 _reverse_iterator() = default;
249
250 TElement& operator*() { return _parent_t::retrieve(); }
251 const TElement& operator*() const { return _parent_t::operator*(); }
252 TElement& operator[](int32_t i) { return _parent_t::_randomAccess(i); }
253 const char& operator[](int32_t i) const { return _parent_t::operator[](i); }
256 return *this;
257 }
259 _reverse_iterator old = *this;
261 return old;
262 }
265 return *this;
266 }
268 _reverse_iterator old = *this;
270 return old;
271 }
273 _reverse_iterator toReturn;
274 _parent_t::_add(toReturn, i);
275 return toReturn;
276 }
278 _reverse_iterator toReturn;
279 _parent_t::_subtract(toReturn, i);
280 return toReturn;
281 }
282 std::size_t operator-(_reverse_iterator right) { return 0; }
283};
284template <class TElement>
286
287} // namespace nstd
288
289#endif // NE_STL_ITERATOR_H
Definition ne_stl_iterator.h:22
TElement * pointer
Definition ne_stl_iterator.h:27
_const_iterator operator-(std::size_t i)
Definition ne_stl_iterator.h:62
std::bidirectional_iterator_tag iterator_category
Definition ne_stl_iterator.h:25
_const_iterator & operator--()
Definition ne_stl_iterator.h:45
size_t difference_type
Definition ne_stl_iterator.h:26
TElement & reference
Definition ne_stl_iterator.h:28
const TElement & operator[](int32_t i) const
Definition ne_stl_iterator.h:35
void _subtract(_const_iterator &toReturn, std::size_t i)
Definition ne_stl_iterator.h:96
void _decrement()
Definition ne_stl_iterator.h:77
TElement & retrieve() const
Definition ne_stl_iterator.h:70
_const_iterator operator++(int32_t)
Definition ne_stl_iterator.h:40
_const_iterator operator--(int32_t)
Definition ne_stl_iterator.h:49
nstd::iterator_type< TElement > current_
Definition ne_stl_iterator.h:69
nstd::iterator_type< TElement > & current()
Definition ne_stl_iterator.h:32
TElement & _randomAccess(std::size_t i) const
Definition ne_stl_iterator.h:83
TElement value_type
Definition ne_stl_iterator.h:24
void _increment()
Definition ne_stl_iterator.h:71
const TElement & operator*() const
Definition ne_stl_iterator.h:34
const nstd::iterator_type< TElement > & current() const
Definition ne_stl_iterator.h:33
_const_iterator & operator++()
Definition ne_stl_iterator.h:36
_const_iterator(nstd::iterator_type< TElement > p)
Definition ne_stl_iterator.h:30
_const_iterator operator+(std::size_t i)
Definition ne_stl_iterator.h:57
bool operator==(const _const_iterator &rhs) const
Definition ne_stl_iterator.h:55
bool operator!=(const _const_iterator &rhs) const
Definition ne_stl_iterator.h:56
void _add(_const_iterator &toReturn, std::size_t i)
Definition ne_stl_iterator.h:89
Definition ne_stl_iterator.h:159
_const_reverse_iterator & operator--()
Definition ne_stl_iterator.h:178
_const_reverse_iterator & operator++()
Definition ne_stl_iterator.h:169
void _subtract(_const_reverse_iterator &toReturn, std::size_t i)
Definition ne_stl_iterator.h:231
const TElement & operator[](int32_t i) const
Definition ne_stl_iterator.h:168
_const_reverse_iterator(nstd::iterator_type< TElement > p)
Definition ne_stl_iterator.h:164
void _increment()
Definition ne_stl_iterator.h:200
_const_reverse_iterator operator--(int32_t)
Definition ne_stl_iterator.h:182
const TElement & operator*() const
Definition ne_stl_iterator.h:167
void _decrement()
Definition ne_stl_iterator.h:209
_const_reverse_iterator operator+(std::size_t i)
Definition ne_stl_iterator.h:188
_const_reverse_iterator operator++(int32_t)
Definition ne_stl_iterator.h:173
void _add(_const_reverse_iterator &toReturn, std::size_t i)
Definition ne_stl_iterator.h:219
_const_reverse_iterator operator-(std::size_t i)
Definition ne_stl_iterator.h:193
Definition ne_stl_iterator.h:108
_iterator()=default
_iterator operator++(int32_t)
Definition ne_stl_iterator.h:124
std::size_t operator-(_iterator right)
Definition ne_stl_iterator.h:148
_iterator operator-(std::size_t i)
Definition ne_stl_iterator.h:143
const TElement & operator[](int32_t i) const
Definition ne_stl_iterator.h:119
_iterator & operator++()
Definition ne_stl_iterator.h:120
const TElement & operator*() const
Definition ne_stl_iterator.h:117
TElement & operator[](int32_t i)
Definition ne_stl_iterator.h:118
_iterator(nstd::iterator_type< TElement > p)
Definition ne_stl_iterator.h:113
_iterator operator--(int32_t)
Definition ne_stl_iterator.h:133
_iterator & operator--()
Definition ne_stl_iterator.h:129
_iterator operator+(std::size_t i)
Definition ne_stl_iterator.h:138
TElement & operator*()
Definition ne_stl_iterator.h:116
Definition ne_stl_iterator.h:242
_reverse_iterator & operator--()
Definition ne_stl_iterator.h:263
const char & operator[](int32_t i) const
Definition ne_stl_iterator.h:253
_reverse_iterator operator+(std::size_t i)
Definition ne_stl_iterator.h:272
_reverse_iterator & operator++()
Definition ne_stl_iterator.h:254
_reverse_iterator operator--(int32_t)
Definition ne_stl_iterator.h:267
const TElement & operator*() const
Definition ne_stl_iterator.h:251
_reverse_iterator(nstd::iterator_type< TElement > p)
Definition ne_stl_iterator.h:247
std::size_t operator-(_reverse_iterator right)
Definition ne_stl_iterator.h:282
TElement & operator[](int32_t i)
Definition ne_stl_iterator.h:252
TElement & operator*()
Definition ne_stl_iterator.h:250
_reverse_iterator operator++(int32_t)
Definition ne_stl_iterator.h:258
_reverse_iterator operator-(std::size_t i)
Definition ne_stl_iterator.h:277
Definition ne_stl_any.h:7
Definition ne_stl_iterator.h:9
std::size_t pos_
Definition ne_stl_iterator.h:12
_iterator_type(TElement *first_=nullptr, const std::size_t *size_=nullptr, std::size_t pos_=0)
Definition ne_stl_iterator.h:13
TElement * first_
Definition ne_stl_iterator.h:10
const std::size_t * size_
Definition ne_stl_iterator.h:11