mn_iterator.hpp
Go to the documentation of this file.
1 
19 #ifndef MINLIB_77a7a837_8b20_44ce_8a17_e432d8f6f902_H_
20 #define MINLIB_77a7a837_8b20_44ce_8a17_e432d8f6f902_H_
21 
22 #include "mn_config.hpp"
23 #include "mn_def.hpp"
24 
26 #define for_each(type,i,ctr) for (type i = (ctr).begin(); i != (ctr).end(); ++ i)
28 #define each_for(type,i,ctr) for (type i = (ctr).rbegin(); i != (ctr).rend(); ++ i)
29 
30 
31 namespace mn {
32  struct input_iterator_tag {};
37 
38 
39 
40  template<typename IterT>
41  struct iterator_traits {
42  using iterator_category = typename IterT::iterator_category ;
43  using difference_type = typename IterT::difference_type ;
44  using value_type = typename IterT::value_type ;
45  using pointer = typename IterT::pointer;
46  using reference = typename IterT::reference ;
47  };
48 
49  template<typename T>
50  struct iterator_traits<T*> {
53  using value_type = T;
54  using pointer = value_type*;
55  using reference = value_type& ;
56  };
57 
58  //-----------------------------------------------------------------------------
59 
60  namespace internal {
61  template<typename TIter>
63  distance(TIter first, TIter last, mn::random_access_iterator_tag) {
64  return last - first;
65  }
66 
67  template<typename TIter>
69  distance(TIter first, TIter last, mn::input_iterator_tag) {
71 
72  while (first != last) {
73  ++first;
74  ++_n;
75  }
76  return _n;
77  }
78 
79  template<typename TIter, typename TDistance = typename iterator_traits<TIter>::difference_type>
80  constexpr void advance(TIter& iter, TDistance d, mn::random_access_iterator_tag) {
81  iter += d;
82  }
83  template<typename TIter, typename TDistance = typename iterator_traits<TIter>::difference_type>
84  constexpr void advance(TIter& iter, TDistance d, mn::bidirectional_iterator_tag) {
85  if (d >= 0) {
86  while (d--) ++iter;
87  } else {
88  while (d++) --iter;
89  }
90  }
91  template<typename TIter, typename TDistance = typename iterator_traits<TIter>::difference_type>
92  constexpr void advance(TIter& iter, TDistance d, mn::input_iterator_tag) {
93  assert(d >= 0);
94  while (d--) ++iter;
95  }
96  }
97 
98  template<typename TIter>
100  distance(TIter first, TIter last) {
101  return internal::distance(first, last, typename iterator_traits<TIter>::iterator_category() );
102  }
103 
104  template<typename TIter, typename TDistance = typename iterator_traits<TIter>::difference_type>
105  constexpr void advance(TIter& iter, TDistance d) {
107  }
108 
109 
110  template<typename TForwardIter,
111  typename TDistance = typename iterator_traits<TForwardIter>::difference_type>
112  TForwardIter next(TForwardIter x, TDistance n = 1) {
113  advance<TForwardIter, TDistance> (x, n);
114  return x;
115  }
116 
117  template<typename TBidirectionalIter,
118  typename TDistance = typename iterator_traits<TBidirectionalIter>::difference_type>
119  TBidirectionalIter prev(TBidirectionalIter x, TDistance n = 1) {
120  advance<TBidirectionalIter, TDistance> (x, -n);
121  return x;
122  }
123 
124  template <typename TIterator>
126  public:
128 
133  using iterator = TIterator;
134 
136 
137  constexpr reverse_iterator ()
138  : m_itter() {}
139  constexpr explicit reverse_iterator (TIterator iter)
140  : m_itter (iter) {}
141 
142  constexpr iterator base () const {
143  return m_itter;
144  }
145 
146  constexpr reference operator* () const {
147  iterator prev (m_itter); --prev;
148  return *prev;
149  }
150  constexpr pointer operator-> () const {
151  return &(operator*()); }
152 
153  constexpr self_type& operator++ () {
154  -- m_itter; return *this; }
155 
156  constexpr self_type& operator-- () {
157  ++ m_itter; return *this; }
158 
159  constexpr self_type operator++ (int) {
160  self_type prev (*this); -- m_itter; return prev; }
161 
162  constexpr self_type operator-- (int) {
163  self_type prev (*this); ++ m_itter; return prev; }
164 
165  constexpr self_type& operator+= (size_t n) {
166  m_itter -= n; return *this; }
167 
168  constexpr self_type& operator-= (size_t n) {
169  m_itter += n; return *this; }
170 
171  constexpr self_type operator+ (size_t n) const {
172  return self_type (m_itter - n); }
173 
174  constexpr self_type operator- (size_t n) const {
175  return self_type (m_itter + n); }
176 
177  constexpr bool operator== (const self_type& iter) const {
178  return m_itter == iter.m_itter; }
179 
180  constexpr bool operator< (const self_type& iter) const {
181  return iter.m_itter < m_itter; }
182 
183  constexpr reference operator[] (size_t n) const {
184  return *(*this + n); }
185 
186  constexpr difference_type operator - (const self_type& i) const {
187  return mn::distance (i.m_itter, m_itter); }
188  protected:
190  };
191 
192  template <typename TCon>
194  public:
195  using container_type = TCon;
197 
198  using value_type = typename container_type::value_type;
199  using difference_type = typename container_type::difference_type ;
200  using pointer = typename container_type::pointer;
201  using reference = typename container_type::reference;
202  using const_reference = typename container_type::const_reference;
203  using iterator = typename container_type::iterator;
204 
205  constexpr explicit insert_iterator (container_type& ctr, iterator itr)
206  : m_con (ctr), m_iter (m_iter) {}
207 
209  m_iter = m_con.insert (m_iter, v); return *this;
210  }
211 
212  constexpr insert_iterator& operator* (void) {
213  return *this;
214  }
215 
216  constexpr insert_iterator& operator++ (void) {
217  ++ m_iter; return *this;
218  }
219 
220  constexpr insert_iterator operator++ (int) {
221  insert_iterator prev (*this);
222  ++m_iter; return prev;
223  }
224  protected:
227  };
228 
229 
230 
231  template <typename TCon>
233  public:
234  using container_type = TCon;
236 
237  using value_type = typename container_type::value_type;
238  using difference_type = typename container_type::difference_type ;
239  using pointer = typename container_type::pointer;
240  using reference = typename container_type::reference;
241  using const_reference = typename container_type::const_reference;
242  using iterator = typename container_type::iterator;
243 
244  constexpr explicit back_insert_iterator (container_type& ctr)
245  : m_con (ctr) {}
246 
248  m_con.push_back (v); return *this;
249  }
250 
251  constexpr back_insert_iterator& operator* () { return *this; }
252  constexpr back_insert_iterator& operator++ () { return *this; }
253  constexpr back_insert_iterator operator++ (int){ return *this; }
254  protected:
256  };
257 
258  template <class TCon>
260  public:
261  using container_type = TCon;
263 
264  using value_type = typename container_type::value_type;
265  using difference_type = typename container_type::difference_type ;
266  using pointer = typename container_type::pointer;
267  using reference = typename container_type::reference;
268  using const_reference = typename container_type::const_reference;
269  using iterator = typename container_type::iterator;
270 
271  constexpr explicit front_insert_iterator (container_type& ctr)
272  : m_con (ctr) {}
273 
275  m_con.push_front (v);
276  return *this;
277  }
278 
279  constexpr front_insert_iterator& operator* () { return *this; }
280  constexpr front_insert_iterator& operator++ () { return *this; }
281  constexpr front_insert_iterator operator++ (int) { return *this; }
282  protected:
284  };
285 
286  template <typename TIterator>
288  public:
290 
291  using iterator = TIterator;
292 
298 
299  constexpr move_iterator () : m_itr() { }
300  constexpr explicit move_iterator (iterator iter) : m_itr (iter) {}
301 
302  constexpr iterator base () const { return m_itr; }
303 
304  constexpr bool operator== (const move_iterator& iter) const
305  { return m_itr == iter.m_itr; }
306 
307  constexpr bool operator< (const move_iterator& iter) const
308  { return m_itr < iter.m_itr; }
309 
310  constexpr reference operator* () const
311  { return move(*m_itr); }
312  constexpr pointer operator-> () const
313  { return &*m_itr; }
315  { ++m_itr; return *this; }
317  { --m_itr; return *this; }
318  constexpr move_iterator operator++ (int)
319  { move_iterator r (*this); ++ m_itr; return r; }
320  constexpr move_iterator operator-- (int)
321  { move_iterator r (*this); -- m_itr; return r; }
322 
323  constexpr move_iterator& operator+= (size_t n)
324  { m_itr += n; return *this; }
325 
326  constexpr move_iterator& operator-= (size_t n)
327  { m_itr -= n; return *this; }
328 
329  constexpr move_iterator operator+ (size_t n) const
330  { return move_iterator (m_itr - n); }
331 
332  constexpr move_iterator operator- (size_t n) const
333  { return move_iterator (m_itr + n); }
334 
335  constexpr reference operator[] (uint32_t n) const
336  { return move(*(*this + n)); }
337 
338  constexpr difference_type operator- (const move_iterator& i) const
339  { return distance (m_itr, i.m_itr); }
340  protected:
342  };
343 
344 
345 
346  template <typename TIterator>
348  return reverse_iterator<TIterator>(itr);
349  }
350 
351  template <typename TCon>
352  constexpr insert_iterator<TCon> inserter (TCon& ctr, typename TCon::iterator itr) {
353  return insert_iterator<TCon> (ctr, itr);
354  }
355 
356  template <class TCon>
358  return back_insert_iterator<TCon> (ctr);
359  }
360 
361  template <class TCon>
363  return front_insert_iterator<TCon> (ctr);
364  }
365 
366  template <class TCon>
367  constexpr typename TCon::iterator remove_const_it (typename TCon::const_iterator itr, TCon& ) {
368  return const_cast<typename TCon::iterator>( itr);
369  }
370 
371  template <typename TIterator>
372  constexpr move_iterator<TIterator> make_move_iterator (TIterator itr) {
373  return move_iterator<TIterator>(itr);
374  }
375 
376 }
377 
378 #endif
Definition: mn_iterator.hpp:232
constexpr back_insert_iterator & operator++()
Definition: mn_iterator.hpp:252
constexpr back_insert_iterator(container_type &ctr)
Definition: mn_iterator.hpp:244
typename container_type::iterator iterator
Definition: mn_iterator.hpp:242
container_type & m_con
Definition: mn_iterator.hpp:255
typename container_type::value_type value_type
Definition: mn_iterator.hpp:237
typename container_type::const_reference const_reference
Definition: mn_iterator.hpp:241
typename container_type::difference_type difference_type
Definition: mn_iterator.hpp:238
typename container_type::reference reference
Definition: mn_iterator.hpp:240
typename container_type::pointer pointer
Definition: mn_iterator.hpp:239
TCon container_type
Definition: mn_iterator.hpp:234
back_insert_iterator & operator=(const_reference v)
Definition: mn_iterator.hpp:247
constexpr back_insert_iterator & operator*()
Definition: mn_iterator.hpp:251
Definition: mn_iterator.hpp:259
typename container_type::iterator iterator
Definition: mn_iterator.hpp:269
constexpr front_insert_iterator(container_type &ctr)
Definition: mn_iterator.hpp:271
constexpr front_insert_iterator & operator++()
Definition: mn_iterator.hpp:280
typename container_type::pointer pointer
Definition: mn_iterator.hpp:266
typename container_type::const_reference const_reference
Definition: mn_iterator.hpp:268
container_type & m_con
Definition: mn_iterator.hpp:283
typename container_type::reference reference
Definition: mn_iterator.hpp:267
typename container_type::difference_type difference_type
Definition: mn_iterator.hpp:265
front_insert_iterator & operator=(const_reference v)
Definition: mn_iterator.hpp:274
typename container_type::value_type value_type
Definition: mn_iterator.hpp:264
TCon container_type
Definition: mn_iterator.hpp:261
constexpr front_insert_iterator & operator*()
Definition: mn_iterator.hpp:279
Definition: mn_iterator.hpp:193
container_type & m_con
Definition: mn_iterator.hpp:225
typename container_type::value_type value_type
Definition: mn_iterator.hpp:198
insert_iterator & operator=(const_reference v)
Definition: mn_iterator.hpp:208
constexpr insert_iterator(container_type &ctr, iterator itr)
Definition: mn_iterator.hpp:205
typename container_type::const_reference const_reference
Definition: mn_iterator.hpp:202
typename container_type::difference_type difference_type
Definition: mn_iterator.hpp:199
iterator m_iter
Definition: mn_iterator.hpp:226
constexpr insert_iterator & operator*(void)
Definition: mn_iterator.hpp:212
typename container_type::pointer pointer
Definition: mn_iterator.hpp:200
typename container_type::iterator iterator
Definition: mn_iterator.hpp:203
constexpr insert_iterator & operator++(void)
Definition: mn_iterator.hpp:216
typename container_type::reference reference
Definition: mn_iterator.hpp:201
TCon container_type
Definition: mn_iterator.hpp:195
Definition: mn_iterator.hpp:287
constexpr bool operator<(const move_iterator &iter) const
Definition: mn_iterator.hpp:307
TIterator iterator
Definition: mn_iterator.hpp:291
constexpr reference operator[](uint32_t n) const
Definition: mn_iterator.hpp:335
typename iterator_traits< TIterator >::pointer pointer
Definition: mn_iterator.hpp:295
constexpr pointer operator->() const
Definition: mn_iterator.hpp:312
constexpr move_iterator(iterator iter)
Definition: mn_iterator.hpp:300
constexpr bool operator==(const move_iterator &iter) const
Definition: mn_iterator.hpp:304
constexpr move_iterator operator-(size_t n) const
Definition: mn_iterator.hpp:332
iterator m_itr
Definition: mn_iterator.hpp:341
typename iterator_traits< TIterator >::value_type value_type
Definition: mn_iterator.hpp:293
constexpr move_iterator & operator++()
Definition: mn_iterator.hpp:314
constexpr move_iterator & operator--()
Definition: mn_iterator.hpp:316
constexpr move_iterator()
Definition: mn_iterator.hpp:299
constexpr move_iterator & operator-=(size_t n)
Definition: mn_iterator.hpp:326
typename iterator_traits< TIterator >::iterator_category iterator_category
Definition: mn_iterator.hpp:297
typename iterator_traits< TIterator >::difference_type difference_type
Definition: mn_iterator.hpp:294
constexpr reference operator*() const
Definition: mn_iterator.hpp:310
constexpr iterator base() const
Definition: mn_iterator.hpp:302
constexpr move_iterator operator+(size_t n) const
Definition: mn_iterator.hpp:329
constexpr move_iterator & operator+=(size_t n)
Definition: mn_iterator.hpp:323
value_type && reference
Definition: mn_iterator.hpp:296
Definition: mn_iterator.hpp:125
typename iterator_traits< TIterator >::iterator_category iterator_category
Definition: mn_iterator.hpp:132
typename iterator_traits< TIterator >::difference_type difference_type
Definition: mn_iterator.hpp:129
reverse_iterator< TIterator > self_type
Definition: mn_iterator.hpp:135
constexpr reverse_iterator(TIterator iter)
Definition: mn_iterator.hpp:139
iterator m_itter
Definition: mn_iterator.hpp:189
typename iterator_traits< TIterator >::value_type value_type
Definition: mn_iterator.hpp:127
constexpr reference operator[](size_t n) const
Definition: mn_iterator.hpp:183
typename iterator_traits< TIterator >::pointer pointer
Definition: mn_iterator.hpp:130
constexpr self_type operator-(size_t n) const
Definition: mn_iterator.hpp:174
constexpr reverse_iterator()
Definition: mn_iterator.hpp:137
constexpr self_type & operator++()
Definition: mn_iterator.hpp:153
constexpr self_type operator+(size_t n) const
Definition: mn_iterator.hpp:171
TIterator iterator
Definition: mn_iterator.hpp:133
constexpr self_type & operator+=(size_t n)
Definition: mn_iterator.hpp:165
constexpr pointer operator->() const
Definition: mn_iterator.hpp:150
constexpr iterator base() const
Definition: mn_iterator.hpp:142
constexpr bool operator<(const self_type &iter) const
Definition: mn_iterator.hpp:180
constexpr bool operator==(const self_type &iter) const
Definition: mn_iterator.hpp:177
constexpr self_type & operator--()
Definition: mn_iterator.hpp:156
constexpr self_type & operator-=(size_t n)
Definition: mn_iterator.hpp:168
constexpr reference operator*() const
Definition: mn_iterator.hpp:146
typename iterator_traits< TIterator >::reference reference
Definition: mn_iterator.hpp:131
constexpr iterator_traits< TIter >::difference_type distance(TIter first, TIter last, mn::random_access_iterator_tag)
Definition: mn_iterator.hpp:63
constexpr void advance(TIter &iter, TDistance d, mn::random_access_iterator_tag)
Definition: mn_iterator.hpp:80
struct mn::memory::detail::ptr_difference T
Definition: mn_atomic_singleton.hpp:38
Definition: mn_allocator_typetraits.hpp:25
typename IterT::pointer pointer
Definition: mn_iterator.hpp:45
T value_type
Definition: mn_iterator.hpp:53
constexpr insert_iterator< TCon > inserter(TCon &ctr, typename TCon::iterator itr)
Definition: mn_iterator.hpp:352
constexpr iterator_traits< TIter >::difference_type distance(TIter first, TIter last)
Definition: mn_iterator.hpp:100
TBidirectionalIter prev(TBidirectionalIter x, TDistance n=1)
Definition: mn_iterator.hpp:119
long ptrdiff_t
Definition: mn_def.hpp:49
constexpr back_insert_iterator< TCon > back_inserter(TCon &ctr)
Definition: mn_iterator.hpp:357
value_type * pointer
Definition: mn_iterator.hpp:54
constexpr move_iterator< TIterator > make_move_iterator(TIterator itr)
Definition: mn_iterator.hpp:372
typename IterT::reference reference
Definition: mn_iterator.hpp:46
constexpr TCon::iterator remove_const_it(typename TCon::const_iterator itr, TCon &)
Definition: mn_iterator.hpp:367
typename IterT::value_type value_type
Definition: mn_iterator.hpp:44
typename IterT::difference_type difference_type
Definition: mn_iterator.hpp:43
value_type & reference
Definition: mn_iterator.hpp:55
constexpr front_insert_iterator< TCon > front_inserter(TCon &ctr)
Definition: mn_iterator.hpp:362
TForwardIter next(TForwardIter x, TDistance n=1)
Definition: mn_iterator.hpp:112
constexpr void advance(TIter &iter, TDistance d)
Definition: mn_iterator.hpp:105
typename IterT::iterator_category iterator_category
Definition: mn_iterator.hpp:42
constexpr reverse_iterator< TIterator > make_reverse_iterator(TIterator itr)
Definition: mn_iterator.hpp:347
void move(const T *src, const T *last, T *dest)
Definition: mn_algorithm.hpp:100
ptrdiff_t difference_type
Definition: mn_iterator.hpp:52
Definition: mn_iterator.hpp:41
Definition: mn_iterator.hpp:33
Definition: mn_iterator.hpp:35
Definition: mn_iterator.hpp:34
Definition: mn_iterator.hpp:32
Definition: mn_iterator.hpp:36