Honeycomb  0.1
Component-Model Framework
Range.h
Go to the documentation of this file.
1 // Honeycomb, Copyright (C) 2015 NewGamePlus Inc. Distributed under the Boost Software License v1.0.
2 #pragma once
3 
4 #include "Honey/Math/Numeral.h"
5 
6 namespace honey
7 {
8 
10 
13 
16 #define RANGE_ARG_MAX 3
17 
18 namespace mt
19 {
21  template<class T>
22  class isRange
23  {
24  template<class T_> static auto test(void*) -> decltype(begin(declval<T_>()), std::true_type());
25  template<class T_> static std::false_type test(...);
26  public:
27  static const bool value = isTrue<decltype(test<T>(nullptr))>::value;
28  };
29 
31  template<class Range> struct iterOf { typedef typename std::decay<decltype(honey::begin(declval<Range>()))>::type type; };
33  #define mt_iterOf(Range) typename std::decay<decltype(honey::begin(Range))>::type
34 
36  template<class Range> struct iter_endOf { typedef typename std::decay<decltype(honey::end(declval<Range>()))>::type type; };
38  #define mt_iter_endOf(Range) typename std::decay<decltype(honey::end(Range))>::type
39 
41  template<class Range> struct elemOf { typedef typename mt::removeRef<decltype(*honey::begin(declval<Range>()))>::type type; };
43  #define mt_elemOf(Range) typename mt::removeRef<decltype(*honey::begin(Range))>::type
44 
46  namespace priv
47  {
48  template<class Result, class T>
49  Result valAt(int cur, int end, T&& t) { assert(cur == end, "Index out of pack range"); return t; }
50  template<class Result, class T, class... Ts>
51  Result valAt(int cur, int end, T&& t, Ts&&... ts) { return cur == end ? t : valAt<Result>(cur+1, end, forward<Ts>(ts)...); }
52 
53  template<class Val, class T>
54  int valIndex(int cur, Val&& val, T&& t) { return val == t ? cur : -1; }
55  template<class Val, class T, class... Ts>
56  int valIndex(int cur, Val&& val, T&& t, Ts&&... ts) { return val == t ? cur : valIndex(cur+1, forward<Val>(val), forward<Ts>(ts)...); }
57  }
60  template<class Result = void, class... Ts>
62  typename std::conditional<std::is_same<Result, void>::value, mt::typeAt<0, Ts...>, Result>::type
63  valAt(int i, Ts&&... ts)
64  {
65  typedef typename std::conditional<std::is_same<Result, void>::value, mt::typeAt<0, Ts...>, Result>::type Result_;
66  return priv::valAt<Result_>(0, i, forward<Ts>(ts)...);
67  }
68 
70  template<class Val, class... Ts>
71  int valIndex(Val&& val, Ts&&... ts) { return priv::valIndex(0, forward<Val>(val), forward<Ts>(ts)...); }
72 }
73 
75 
78 template<class Iter>
79 auto seqToIter(Iter&& seq) -> typename std::enable_if<mt::isIterator<Iter>::value, Iter&&>::type
80  { return forward<Iter>(seq); }
82 template<class Range>
83 auto seqToIter(Range&& seq) -> typename std::enable_if<mt::isRange<Range>::value, mt_iterOf(seq)>::type
84  { return begin(seq); }
85 
87 template<class T1_, class T2_>
88 class Range_
89 {
90  template<class T1, class T2> friend class Range_;
91 public:
92  typedef typename std::decay<T1_>::type T1;
93  typedef typename std::decay<T2_>::type T2;
94 
95  Range_() = default;
96  template<class T1, class T2> Range_(T1&& first, T2&& last) : _first(forward<T1>(first)), _last(forward<T2>(last)) {}
97  template<class T1, class T2> Range_(const Range_<T1,T2>& rhs) { operator=(rhs); }
98  template<class T1, class T2> Range_(Range_<T1,T2>&& rhs) { operator=(move(rhs)); }
99 
100  template<class T1, class T2> Range_& operator=(const Range_<T1,T2>& rhs) { _first = rhs._first; _last = rhs._last; return *this; }
101  template<class T1, class T2> Range_& operator=(Range_<T1,T2>&& rhs) { _first = move(rhs._first); _last = move(rhs._last); return *this; }
102 
103  const T1& begin() const { return _first; }
104  T1& begin() { return _first; }
105  const T2& end() const { return _last; }
106  T2& end() { return _last; }
107 
108 private:
109  T1 _first;
110  T2 _last;
111 };
112 
114 template<class Iter1, class Iter2>
115 typename std::enable_if<mt::isIterator<Iter1>::value, Range_<Iter1,Iter2>>::type
116  range(Iter1&& first, Iter2&& last) { return Range_<Iter1,Iter2>(forward<Iter1>(first), forward<Iter2>(last)); }
117 
119 template<class T1, class T2>
120 Range_<T1,T2> range(pair<T1,T2>& p) { return Range_<T1,T2>(p.first, p.second); }
121 template<class T1, class T2>
122 Range_<T1,T2> range(const pair<T1,T2>& p) { return Range_<T1,T2>(p.first, p.second); }
123 
125 template<class T1, class T2>
126 Range_<T1,T2> range(tuple<T1,T2>& t) { return Range_<T1,T2>(get<0>(t), get<1>(t)); }
127 template<class T1, class T2>
128 Range_<T1,T2> range(const tuple<T1,T2>& t) { return Range_<T1,T2>(get<0>(t), get<1>(t)); }
129 
131 template<class Range>
132 auto reversed(Range&& range) -> Range_<std::reverse_iterator<mt_iterOf(range)>, std::reverse_iterator<mt_iter_endOf(range)>>
133 {
134  return honey::range(std::reverse_iterator<mt_iterOf(range)>(end(range)), std::reverse_iterator<mt_iter_endOf(range)>(begin(range)));
135 }
136 
137 //====================================================
139 #define map(...) __map()
140 #define OutSeq void*
141 
142 
149 OutSeq&& map(Range&&, Seqs&&..., OutSeq&&, Func&&);
150 #undef map
151 #undef OutSeq
152 
153 #define PARAMT(It) , class S##It
154 #define PARAMT_(It) , class S##It##_
155 #define PARAMT_SPEC(It) , S##It##_
156 #define PARAM(It) , S##It&& seq##It
157 #define SEQTOITER(It) auto it##It = seqToIter(seq##It);
158 #define NEXT(It) , ++it##It
159 #define ARG(It) , *it##It
160 #define FORWARDT(It) , typename std::decay<S##It>::type
161 #define FORWARD(It) , forward<S##It>(seq##It)
162 
163 namespace priv
164 {
165  template<class Range, class OutSeq, class... Seqs> struct map_impl;
166 }
167 
168 #define FUNC(It) \
169  namespace priv \
170  { \
171  template<class Range_, class OutSeq_ ITERATE_(1,It,PARAMT_)> \
172  struct map_impl<Range_, OutSeq_ ITERATE_(1,It,PARAMT_SPEC)> \
173  { \
174  template<class Range ITERATE_(1,It,PARAMT), class OutSeq, class Func> \
175  static OutSeq&& func(Range&& range ITERATE_(1,It,PARAM), OutSeq&& out, Func&& f) \
176  { \
177  auto it = begin(range); \
178  auto last = end(range); \
179  ITERATE_(1,It,SEQTOITER) \
180  auto out_it = seqToIter(out); \
181  for (; it != last; ++it ITERATE_(1,It,NEXT), ++out_it) \
182  *out_it = f(*it ITERATE_(1,It,ARG)); \
183  return forward<OutSeq>(out); \
184  } \
185  }; \
186  } \
187  \
188  template<class Range ITERATE_(1,It,PARAMT), class OutSeq, class Func> \
189  OutSeq&& map(Range&& range ITERATE_(1,It,PARAM), OutSeq&& out, Func&& f) \
190  { \
191  return priv::map_impl< typename std::decay<Range>::type, typename std::decay<OutSeq>::type \
192  ITERATE_(1,It,FORWARDT)> \
193  ::func( forward<Range>(range) ITERATE_(1,It,FORWARD), \
194  forward<OutSeq>(out), forward<Func>(f)); \
195  } \
196 
197 ITERATE(0, RANGE_ARG_MAX, FUNC)
198 #undef PARAMT
199 #undef PARAMT_
200 #undef PARAMT_SPEC
201 #undef PARAM
202 #undef SEQTOITER
203 #undef NEXT
204 #undef ARG
205 #undef FORWARDT
206 #undef FORWARD
207 #undef FUNC
208 
209 //====================================================
210 
211 //====================================================
213 #define reduce(...) __reduce()
214 #define Accum void*
215 
216 
223 Accum reduce(Range&&, Seqs&&..., Accum&& initVal, Func&&);
224 #undef reduce
225 #undef Accum
226 
227 #define PARAMT(It) , class S##It
228 #define PARAMT_(It) , class S##It##_
229 #define PARAMT_SPEC(It) , S##It##_
230 #define PARAM(It) , S##It&& seq##It
231 #define SEQTOITER(It) auto it##It = seqToIter(seq##It);
232 #define NEXT(It) , ++it##It
233 #define ARG(It) , *it##It
234 #define FORWARDT(It) , typename std::decay<S##It>::type
235 #define FORWARD(It) , forward<S##It>(seq##It)
236 
237 namespace priv
238 {
239  template<class Range, class Accum, class... Seqs> struct reduce_impl;
240 }
241 
242 #define FUNC(It) \
243  namespace priv \
244  { \
245  template<class Range_, class Accum_ ITERATE_(1,It,PARAMT_)> \
246  struct reduce_impl<Range_, Accum_ ITERATE_(1,It,PARAMT_SPEC)> \
247  { \
248  template<class Range ITERATE_(1,It,PARAMT), class Accum, class Func> \
249  static Accum_ func(Range&& range ITERATE_(1,It,PARAM), Accum&& initVal, Func&& f) \
250  { \
251  Accum_ a(forward<Accum>(initVal)); \
252  auto it = begin(range); \
253  auto last = end(range); \
254  ITERATE_(1,It,SEQTOITER) \
255  for (; it != last; ++it ITERATE_(1,It,NEXT)) \
256  a = f(a, *it ITERATE_(1,It,ARG)); \
257  return a; \
258  } \
259  }; \
260  } \
261  \
262  template< class Range ITERATE_(1,It,PARAMT), class Accum, class Func, \
263  class Accum_ = typename std::decay<Accum>::type> \
264  Accum_ reduce(Range&& range ITERATE_(1,It,PARAM), Accum&& initVal, Func&& f) \
265  { \
266  return priv::reduce_impl<typename std::decay<Range>::type, Accum_ ITERATE_(1,It,FORWARDT)> \
267  ::func( forward<Range>(range) ITERATE_(1,It,FORWARD), \
268  forward<Accum>(initVal), forward<Func>(f)); \
269  } \
270 
271 ITERATE(0, RANGE_ARG_MAX, FUNC)
272 #undef PARAMT
273 #undef PARAMT_
274 #undef PARAMT_SPEC
275 #undef PARAM
276 #undef SEQTOITER
277 #undef NEXT
278 #undef ARG
279 #undef FORWARDT
280 #undef FORWARD
281 #undef FUNC
282 
283 //====================================================
284 
285 //====================================================
287 #define find(...) __find()
288 #define Iter void*
289 
290 
296 Iter find(Range&&, Seqs&&..., Func&& pred);
297 #undef find
298 #undef Iter
299 
300 #define PARAMT(It) , class S##It
301 #define PARAM(It) , S##It&& seq##It
302 #define SEQTOITER(It) auto it##It = seqToIter(seq##It);
303 #define NEXT(It) , ++it##It
304 #define ARG(It) , *it##It
305 
306 #define FUNC(It) \
307  template<class Range ITERATE_(1,It,PARAMT), class Func> \
308  auto find(Range&& range ITERATE_(1,It,PARAM), Func&& pred) -> mt_iterOf(range) \
309  { \
310  auto it = begin(range); \
311  auto last = end(range); \
312  ITERATE_(1,It,SEQTOITER) \
313  for (; it != last; ++it ITERATE_(1,It,NEXT)) \
314  if (pred(*it ITERATE_(1,It,ARG))) break; \
315  return it; \
316  } \
317 
318 ITERATE(0, RANGE_ARG_MAX, FUNC)
319 #undef PARAMT
320 #undef PARAM
321 #undef SEQTOITER
322 #undef NEXT
323 #undef ARG
324 #undef FUNC
325 
326 //====================================================
327 
328 //====================================================
330 #define filter(...) __filter()
331 #define FilterIter int*
332 
333 
337 Range_<FilterIter, FilterIter> filter(Range&&, Seqs&&..., Func&& pred);
338 #undef filter
339 #undef FilterIter
340 
341 #define PARAMT(It) , class S##It
342 #define PARAM(It) , S##It&& seq##It
343 #define PARAM_REF(It) , const S##It& seq##It
344 #define SEQTOITER_PARAMT(It) , decltype(seqToIter(seq##It))
345 #define SEQTOITER(It) , seqToIter(seq##It)
346 #define MEMBER(It) S##It _seq##It;
347 #define MEMBER_INIT(It) , _seq##It(seq##It)
348 #define NEXT(It) , ++_seq##It
349 #define ARG(It) , *_seq##It
350 
351 #define FUNC(It) \
352  template<class Range ITERATE_(1,It,PARAMT), class Func> \
353  class FilterIter##It \
354  { \
355  public: \
356  typedef typename mt::iterOf<Range>::type Iter; \
357  typedef typename mt::iter_endOf<Range>::type IterEnd; \
358  typedef std::forward_iterator_tag iterator_category; \
359  typedef typename std::iterator_traits<Iter>::value_type value_type; \
360  typedef typename std::iterator_traits<Iter>::difference_type difference_type; \
361  typedef typename std::iterator_traits<Iter>::pointer pointer; \
362  typedef typename std::iterator_traits<Iter>::reference reference; \
363  \
364  FilterIter##It(const IterEnd& end, const Func& pred) : _it(end), _itEnd(end), _pred(pred) {} \
365  \
366  FilterIter##It(const Iter& begin, const IterEnd& end ITERATE_(1,It,PARAM_REF), const Func& pred) : \
367  _it(begin), _itEnd(end) ITERATE_(1,It,MEMBER_INIT), _pred(pred) { next(); } \
368  \
369  FilterIter##It& operator++() \
370  { \
371  assert(_it != _itEnd); \
372  ++_it ITERATE_(1,It,NEXT); \
373  next(); \
374  return *this; \
375  } \
376  FilterIter##It operator++(int) { auto tmp = *this; ++*this; return tmp; } \
377  \
378  bool operator==(const FilterIter##It& rhs) const { return _it == rhs._it; } \
379  bool operator!=(const FilterIter##It& rhs) const { return !operator==(rhs); } \
380  \
381  reference operator*() const { return *_it; } \
382  pointer operator->() const { return _it.operator->(); } \
383  operator Iter() const { return _it; } \
384  \
385  private: \
386  void next() \
387  { \
388  for (; _it != _itEnd; ++_it ITERATE_(1,It,NEXT)) \
389  if (_pred(*_it ITERATE_(1,It,ARG))) break; \
390  } \
391  \
392  Iter _it; \
393  IterEnd _itEnd; \
394  ITERATE_(1,It,MEMBER) \
395  Func _pred; \
396  }; \
397  \
398  template<class Range ITERATE_(1,It,PARAMT), class Func> \
399  auto filter(Range&& range ITERATE_(1,It,PARAM), Func&& pred) -> \
400  Range_< FilterIter##It<Range ITERATE_(1,It,SEQTOITER_PARAMT), Func>, \
401  FilterIter##It<Range ITERATE_(1,It,SEQTOITER_PARAMT), Func>> \
402  { \
403  typedef FilterIter##It<Range ITERATE_(1,It,SEQTOITER_PARAMT), Func> FilterIter; \
404  return honey::range(FilterIter(begin(range), end(range) ITERATE_(1,It,SEQTOITER), pred), \
405  FilterIter(end(range), pred)); \
406  } \
407 
408 ITERATE(0, RANGE_ARG_MAX, FUNC)
409 #undef PARAMT
410 #undef PARAM
411 #undef PARAM_REF
412 #undef SEQTOITER_PARAMT
413 #undef SEQTOITER
414 #undef MEMBER
415 #undef MEMBER_INIT
416 #undef NEXT
417 #undef ARG
418 #undef FUNC
419 
420 //====================================================
421 
423 template<class Range>
424 szt countOf(Range&& range) { return reduce(range, szt(0), [](szt a, auto&) { return ++a; }); }
425 
427 template<class Range>
428 void deleteRange(Range&& range) { for (auto& e : range) delete_(e); }
429 
431 template<class Range, class Alloc>
432 void deleteRange(Range&& range, Alloc&& a) { for (auto& e : range) delete_(e, a); }
433 
434 
436 template<class T>
437 class IntIter
438 {
439 public:
440  typedef std::random_access_iterator_tag iterator_category;
441  typedef T value_type;
442  typedef T difference_type;
443  typedef const T* pointer;
444  typedef const T& reference;
445 
446  IntIter() = default;
447  IntIter(T i) : _i(i) {}
448 
449  IntIter& operator++() { ++_i; return *this; }
450  IntIter& operator--() { --_i; return *this; }
451  IntIter operator++(int) { auto tmp = *this; ++*this; return tmp; }
452  IntIter operator--(int) { auto tmp = *this; --*this; return tmp; }
453  IntIter& operator+=(difference_type rhs) { _i += rhs; return *this; }
454  IntIter& operator-=(difference_type rhs) { _i -= rhs; return *this; }
455  IntIter operator+(difference_type rhs) const { auto tmp = *this; tmp+=rhs; return tmp; }
456  IntIter operator-(difference_type rhs) const { auto tmp = *this; tmp-=rhs; return tmp; }
457  difference_type operator-(const IntIter& rhs) const { return _i - rhs._i; }
458 
459  bool operator==(const IntIter& rhs) const { return _i == rhs._i; }
460  bool operator!=(const IntIter& rhs) const { return _i != rhs._i; }
461  bool operator< (const IntIter& rhs) const { return _i < rhs._i; }
462  bool operator> (const IntIter& rhs) const { return _i > rhs._i; }
463  bool operator<=(const IntIter& rhs) const { return _i <= rhs._i; }
464  bool operator>=(const IntIter& rhs) const { return _i >= rhs._i; }
465 
466  reference operator*() const { return _i; }
467  operator T() const { return _i; }
468 
469 private:
470  T _i;
471 };
472 
474 template<class Int, class Int2, class Int_ = typename std::common_type<Int,Int2>::type>
475 typename std::enable_if<std::is_integral<Int_>::value, Range_<IntIter<Int_>, IntIter<Int_>>>::type
476  range(Int begin, Int2 end)
477 {
478  // Make sure begin comes before end
479  return range(IntIter<Int_>(begin), IntIter<Int_>(end < begin ? begin : end));
480 }
481 
483 template<class Int>
484 typename std::enable_if<std::is_integral<Int>::value, Range_<IntIter<Int>, IntIter<Int>>>::type
485  range(Int end) { return range(Int(0), end); }
486 
487 
489 template<class T>
491 {
492 public:
493  typedef std::random_access_iterator_tag iterator_category;
494  typedef T value_type;
495  typedef T difference_type;
496  typedef const T* pointer;
497  typedef const T& reference;
498 
499  IntStepIter() = default;
500  IntStepIter(T i, T step) : _i(i), _step(step) {}
501 
502  IntStepIter& operator++() { _i += _step; return *this; }
503  IntStepIter& operator--() { _i -= _step; return *this; }
504  IntStepIter operator++(int) { auto tmp = *this; ++*this; return tmp; }
505  IntStepIter operator--(int) { auto tmp = *this; --*this; return tmp; }
506  IntStepIter& operator+=(difference_type rhs) { _i += rhs*_step; return *this; }
507  IntStepIter& operator-=(difference_type rhs) { _i -= rhs*_step; return *this; }
508  IntStepIter operator+(difference_type rhs) const { auto tmp = *this; tmp+=rhs*_step; return tmp; }
509  IntStepIter operator-(difference_type rhs) const { auto tmp = *this; tmp-=rhs*_step; return tmp; }
510  difference_type operator-(const IntStepIter& rhs) const { return _i - rhs._i; }
511 
512  bool operator==(const IntStepIter& rhs) const { return _i == rhs._i; }
513  bool operator!=(const IntStepIter& rhs) const { return _i != rhs._i; }
514  bool operator< (const IntStepIter& rhs) const { return _i < rhs._i; }
515  bool operator> (const IntStepIter& rhs) const { return _i > rhs._i; }
516  bool operator<=(const IntStepIter& rhs) const { return _i <= rhs._i; }
517  bool operator>=(const IntStepIter& rhs) const { return _i >= rhs._i; }
518 
519  reference operator*() const { return _i; }
520  operator T() const { return _i; }
521 
522 private:
523  T _i;
524  T _step;
525 };
526 
528 template<class Int, class Int2, class Int3, class Int_ = typename std::common_type<Int,Int2,Int3>::type>
529 typename std::enable_if<std::is_integral<Int_>::value, Range_<IntStepIter<Int_>, IntStepIter<Int_>>>::type
530  range(Int begin, Int2 end, Int3 step)
531 {
532  assert(step != 0);
533  // Make sure begin comes before end
534  Int_ end_ = step > 0 ? (end < begin ? begin : end) : (end > begin ? begin : end);
535  Int_ dif = end_ - begin;
536  return range(IntStepIter<Int_>(begin, step), IntStepIter<Int_>(begin + (dif/step + (dif%step!=0))*step, step));
537 }
538 
539 
541 template<class T>
542 class RealIter
543 {
544  typedef typename Numeral<T>::Int Int;
545 public:
546  typedef std::random_access_iterator_tag iterator_category;
547  typedef T value_type;
548  typedef Int difference_type;
549  typedef T* pointer;
550  typedef T reference;
551 
552  RealIter() = default;
553  RealIter(T begin, T step, Int i) : _begin(begin), _step(step), _i(i) {}
554 
555  RealIter& operator++() { ++_i; return *this; }
556  RealIter& operator--() { --_i; return *this; }
557  RealIter operator++(int) { auto tmp = *this; ++*this; return tmp; }
558  RealIter operator--(int) { auto tmp = *this; --*this; return tmp; }
559  RealIter& operator+=(difference_type rhs) { _i += rhs; return *this; }
560  RealIter& operator-=(difference_type rhs) { _i -= rhs; return *this; }
561  RealIter operator+(difference_type rhs) const { auto tmp = *this; tmp+=rhs; return tmp; }
562  RealIter operator-(difference_type rhs) const { auto tmp = *this; tmp-=rhs; return tmp; }
563  difference_type operator-(const RealIter& rhs) const { return _i - rhs._i; }
564 
565  bool operator==(const RealIter& rhs) const { assert(_begin == rhs._begin && _step == rhs._step); return _i == rhs._i; }
566  bool operator!=(const RealIter& rhs) const { assert(_begin == rhs._begin && _step == rhs._step); return _i != rhs._i; }
567  bool operator< (const RealIter& rhs) const { assert(_begin == rhs._begin && _step == rhs._step); return _i < rhs._i; }
568  bool operator> (const RealIter& rhs) const { assert(_begin == rhs._begin && _step == rhs._step); return _i > rhs._i; }
569  bool operator<=(const RealIter& rhs) const { assert(_begin == rhs._begin && _step == rhs._step); return _i <= rhs._i; }
570  bool operator>=(const RealIter& rhs) const { assert(_begin == rhs._begin && _step == rhs._step); return _i >= rhs._i; }
571 
572  reference operator*() const { return _begin + _i*_step; }
573  operator T() const { return _begin + _i*_step; }
574 
575 private:
576  T _begin;
577  T _step;
578  Int _i;
579 };
580 
582 template<class Real, class Real2, class Real3, class Real_ = typename std::common_type<Real,Real2,Real3>::type>
583 typename std::enable_if<std::is_floating_point<Real_>::value, Range_<RealIter<Real_>, RealIter<Real_>>>::type
584  range(Real begin, Real2 end, Real3 step = 1)
585 {
586  assert(step != 0);
587  // Make sure begin comes before end
588  Real_ end_ = step > 0 ? (end < begin ? begin : end) : (end > begin ? begin : end);
589  typedef typename Numeral<Real_>::Real_ Real__;
590  return range(RealIter<Real_>(begin, step, 0), RealIter<Real_>(begin, step, Real__::ceil((end_-begin)/step)));
591 }
592 
593 
595 template<class Iter, szt I, class IterCategory = typename Iter::iterator_category>
596 class TupleIter;
597 
598 template<class Iter, szt I>
599 class TupleIter<Iter, I, std::forward_iterator_tag>
600 {
601 public:
602  typedef std::forward_iterator_tag iterator_category;
603  typedef decltype(get<I>(*Iter())) reference;
604  typedef typename mt::removeRef<reference>::type value_type;
605  typedef typename std::iterator_traits<Iter>::difference_type difference_type;
606  typedef value_type* pointer;
607 
608  TupleIter() = default;
609  TupleIter(const Iter& i) : _i(i) {}
610 
611  TupleIter& operator++() { ++_i; return *this; }
612  TupleIter operator++(int) { auto tmp = *this; ++*this; return tmp; }
613 
614  bool operator==(const TupleIter& rhs) const { return _i == rhs._i; }
615  bool operator!=(const TupleIter& rhs) const { return !operator==(rhs); }
616 
617  reference operator*() const { return get<I>(*_i); }
618  pointer operator->() const { return &get<I>(*_i); }
619  operator Iter() const { return _i; }
620 
621 protected:
622  Iter _i;
623 };
624 
625 template<class Iter, szt I>
626 class TupleIter<Iter, I, std::bidirectional_iterator_tag> : public TupleIter<Iter, I, std::forward_iterator_tag>
627 {
629  using Super::_i;
630 
631 public:
632  typedef std::bidirectional_iterator_tag iterator_category;
633 
634  TupleIter() = default;
635  TupleIter(const Iter& i) : Super(i) {}
636 
637  TupleIter& operator++() { Super::operator++(); return *this; }
638  TupleIter& operator--() { --_i; return *this; }
639  TupleIter operator++(int) { auto tmp = *this; ++*this; return tmp; }
640  TupleIter operator--(int) { auto tmp = *this; --*this; return tmp; }
641 };
642 
644 template<class Range, class Iter>
645 class RingIter
646 {
647 public:
650  typedef std::bidirectional_iterator_tag iterator_category;
651  typedef typename std::iterator_traits<Iter>::value_type value_type;
652  typedef typename std::iterator_traits<Iter>::difference_type difference_type;
653  typedef typename std::iterator_traits<Iter>::pointer pointer;
654  typedef typename std::iterator_traits<Iter>::reference reference;
655 
656  RingIter(const RangeIter& begin, const RangeIterEnd& end, const Iter& cur, bool bEnd = false) :
657  _begin(begin), _end(end), _curBegin(cur), _cur(cur), _bEnd(bEnd)
658  {
659  if (!_bEnd) _bEnd = _begin == _end;
660  assert(_bEnd || _cur != _end);
661  }
662 
664  {
665  assert(!_bEnd);
666  ++_cur;
667  if (_cur == _end) _cur = _begin;
668  if (_cur == _curBegin) _bEnd = true;
669  return *this;
670  }
671 
673  {
674  assert(_begin != _end);
675  assert(_bEnd || _cur != _curBegin);
676  if (_bEnd) _bEnd = false;
677  if (_cur == _begin) _cur = _end;
678  --_cur;
679  return *this;
680  }
681 
682  RingIter operator++(int) { auto tmp = *this; ++*this; return tmp; }
683  RingIter operator--(int) { auto tmp = *this; --*this; return tmp; }
684 
685  bool operator==(const RingIter& rhs) const { return _cur == rhs._cur && _bEnd == rhs._bEnd; }
686  bool operator!=(const RingIter& rhs) const { return !operator==(rhs); }
687 
688  reference operator*() const { return *_cur; }
689  pointer operator->() const { return _cur.operator->(); }
690  operator const Iter&() const { return _cur; }
691 
692  const Iter& iter() const { return _cur; }
693 
694 private:
695  RangeIter _begin;
696  RangeIterEnd _end;
697  Iter _curBegin;
698  Iter _cur;
699  bool _bEnd;
700 };
701 
703 template<class Range, class Iter>
704 auto ringRange(Range&& range, const Iter& cur) ->
706 {
708  return honey::range(RingIter(begin(range), end(range), cur), RingIter(begin(range), end(range), cur, true));
709 }
710 
712 
713 }
T value_type
Definition: Range.h:547
bool operator<(const IntIter &rhs) const
Definition: Range.h:461
bool operator!=(const IntStepIter &rhs) const
Definition: Range.h:513
IntIter & operator-=(difference_type rhs)
Definition: Range.h:454
bool operator>=(const IntIter &rhs) const
Definition: Range.h:464
IntStepIter()=default
reference operator*() const
Definition: Range.h:688
std::bidirectional_iterator_tag iterator_category
Definition: Range.h:632
const T2 & end() const
Definition: Range.h:105
bool operator<=(const IntStepIter &rhs) const
Definition: Range.h:516
RingIter operator--(int)
Definition: Range.h:683
TupleIter operator--(int)
Definition: Range.h:640
mt::removeRef< decltype(*honey::begin(declval< Range >)))>::type type
Definition: Range.h:41
reference operator*() const
Definition: Range.h:617
bool operator==(const TupleIter &rhs) const
Definition: Range.h:614
bool operator!=(const RealIter &rhs) const
Definition: Range.h:566
RealIter operator--(int)
Definition: Range.h:558
std::iterator_traits< Iter >::pointer pointer
Definition: Range.h:653
std::random_access_iterator_tag iterator_category
Definition: Range.h:440
IntStepIter operator-(difference_type rhs) const
Definition: Range.h:509
IntIter operator+(difference_type rhs) const
Definition: Range.h:455
bool operator==(const RingIter &rhs) const
Definition: Range.h:685
T reference
Definition: Range.h:550
std::decay< decltype(honey::end(declval< Range >)))>::type type
Definition: Range.h:36
RingIter operator++(int)
Definition: Range.h:682
IntIter & operator++()
Definition: Range.h:449
IntStepIter operator++(int)
Definition: Range.h:504
Range_ & operator=(Range_< T1, T2 > &&rhs)
Definition: Range.h:101
IntStepIter operator+(difference_type rhs) const
Definition: Range.h:508
#define mt_iter_endOf(Range)
iter_endOf for values
Definition: Range.h:38
bool operator<(const IntStepIter &rhs) const
Definition: Range.h:514
std::forward_iterator_tag iterator_category
Definition: Range.h:602
Value< bool, std::is_same< T, std::true_type >::value > isTrue
Check if type is std::true_type.
Definition: Meta.h:35
Iter find(Range &&, Seqs &&..., Func &&pred)
Find an element in a series of sequences.
auto ringRange(Range &&range, const Iter &cur) -> Range_< RingIter< Range, Iter >, RingIter< Range, Iter >>
Create an iterator adapter range that does one full cyclic loop starting at cur through the range...
Definition: Range.h:704
Wrapper around an iterator with tuple value type. When dereferenced returns I'th element.
Definition: Range.h:596
int valIndex(Val &&val, Ts &&...ts)
Get index of first matching value in parameter pack, returns -1 if not found.
Definition: Range.h:71
void delete_(T *&p)
Destruct object, free memory and set pointer to null.
Definition: Allocator.h:75
bool operator>=(const IntStepIter &rhs) const
Definition: Range.h:517
STL namespace.
IntStepIter & operator++()
Definition: Range.h:502
Range_(Range_< T1, T2 > &&rhs)
Definition: Range.h:98
RealIter & operator-=(difference_type rhs)
Definition: Range.h:560
mt::removeRef< reference >::type value_type
Definition: Range.h:604
RealIter operator++(int)
Definition: Range.h:557
reference operator*() const
Definition: Range.h:572
IntIter & operator--()
Definition: Range.h:450
Get range element type.
Definition: Range.h:41
Accum reduce(Range &&, Seqs &&..., Accum &&initVal, Func &&)
Accumulate a series of sequences into an output.
T value_type
Definition: Range.h:441
difference_type operator-(const IntStepIter &rhs) const
Definition: Range.h:510
bool operator!=(const RingIter &rhs) const
Definition: Range.h:686
std::remove_reference< T > removeRef
Remove reference from type.
Definition: Meta.h:44
std::enable_if< mt::isIterator< Iter1 >::value, Range_< Iter1, Iter2 > >::type range(Iter1 &&first, Iter2 &&last)
Range from iterators [first, last)
Definition: Range.h:116
std::random_access_iterator_tag iterator_category
Definition: Range.h:493
pointer operator->() const
Definition: Range.h:689
const T & reference
Definition: Range.h:444
value_type * pointer
Definition: Range.h:606
IntStepIter & operator-=(difference_type rhs)
Definition: Range.h:507
std::decay< T1_ >::type T1
Definition: Range.h:92
IntIter & operator+=(difference_type rhs)
Definition: Range.h:453
const Iter & iter() const
Definition: Range.h:692
std::iterator_traits< Iter >::reference reference
Definition: Range.h:654
difference_type operator-(const IntIter &rhs) const
Definition: Range.h:457
TupleIter & operator++()
Definition: Range.h:611
Check if type is a range or a reference to one. A range is a type where std::begin(range) is a valid ...
Definition: Range.h:22
IntStepIter & operator+=(difference_type rhs)
Definition: Range.h:506
reference operator*() const
Definition: Range.h:519
const T * pointer
Definition: Range.h:496
RealIter()=default
RealIter & operator++()
Definition: Range.h:555
T difference_type
Definition: Range.h:442
std::decay< decltype(honey::begin(declval< Range >)))>::type type
Definition: Range.h:31
bool operator==(const RealIter &rhs) const
Definition: Range.h:565
RealIter operator+(difference_type rhs) const
Definition: Range.h:561
#define RANGE_ARG_MAX
Max args for range related variable argument functions.
Definition: Range.h:16
Range_(T1 &&first, T2 &&last)
Definition: Range.h:96
bool operator==(const IntStepIter &rhs) const
Definition: Range.h:512
const T * pointer
Definition: Range.h:443
#define mt_iterOf(Range)
iterOf for values
Definition: Range.h:33
T * pointer
Definition: Range.h:549
const T1 & begin() const
Definition: Range.h:103
mt::iter_endOf< Range >::type RangeIterEnd
Definition: Range.h:649
Int difference_type
Definition: Range.h:548
T difference_type
Definition: Range.h:495
Ring iterator. See ringRange() to create.
Definition: Range.h:645
#define assert(...)
Forwards to assert_#args. See assert_1(), assert_2().
Definition: Debug.h:24
Range_ & operator=(const Range_< T1, T2 > &rhs)
Definition: Range.h:100
RealIter & operator+=(difference_type rhs)
Definition: Range.h:559
IntIter operator--(int)
Definition: Range.h:452
std::random_access_iterator_tag iterator_category
Definition: Range.h:546
bool operator>=(const RealIter &rhs) const
Definition: Range.h:570
szt countOf(Range &&range)
Count number of elements in range.
Definition: Range.h:424
std::iterator_traits< Iter >::difference_type difference_type
Definition: Range.h:652
IntIter(T i)
Definition: Range.h:447
void deleteRange(Range &&range)
Delete all elements in range.
Definition: Range.h:428
std::decay< T2_ >::type T2
Definition: Range.h:93
pointer operator->() const
Definition: Range.h:618
RealIter & operator--()
Definition: Range.h:556
std::conditional< std::is_same< Result, void >::value, mt::typeAt< 0, Ts... >, Result >::type valAt(int i, Ts &&...ts)
Get value at index of parameter pack. All types must be convertible to Result, which defaults to the ...
Definition: Range.h:63
float Real
Real number type. See Real_ for real number operations and constants.
Definition: Real.h:21
Numeric type information, use numeral() to get instance safely from a static context.
Definition: Numeral.h:17
TupleIter operator++(int)
Definition: Range.h:612
bool operator!=(const IntIter &rhs) const
Definition: Range.h:460
size_t szt
Size type, shorthand for size_t.
Definition: Core.h:90
RingIter & operator--()
Definition: Range.h:672
IntIter()=default
typename priv::typeAt< 0, I, Ts... >::type typeAt
Get type at index of parameter pack.
Definition: Meta.h:106
bool operator<=(const IntIter &rhs) const
Definition: Range.h:463
bool operator>(const RealIter &rhs) const
Definition: Range.h:568
T1 & begin()
Definition: Range.h:104
Real number iterator. See range(Real, Real, Real) to create.
Definition: Range.h:542
bool operator==(const IntIter &rhs) const
Definition: Range.h:459
Get range iterator end type.
Definition: Range.h:36
IntIter operator++(int)
Definition: Range.h:451
Integer iterator with step size. See range(int, int, int) to create.
Definition: Range.h:490
#define ITERATE(Min, Max, Func)
Iterate calling Func(It, Args...) over range [Min, Max], where Min >= 0 and Max <= ITERATE_MAX...
Definition: Preprocessor.h:58
Numeral< Real >::Real_ Real_
Operations and constants for Real type. See Float_, Double_.
Definition: Real.h:25
bool operator<=(const RealIter &rhs) const
Definition: Range.h:569
Get range iterator begin type.
Definition: Range.h:31
TupleIter operator++(int)
Definition: Range.h:639
Iterator range. See range(Iter1&&, Iter2&&) to create.
Definition: Range.h:88
static const bool value
Definition: Range.h:27
Range_< FilterIter, FilterIter > filter(Range &&, Seqs &&..., Func &&pred)
Filter a range by ignoring undesired elements.
IntStepIter(T i, T step)
Definition: Range.h:500
IntIter operator-(difference_type rhs) const
Definition: Range.h:456
bool operator!=(const TupleIter &rhs) const
Definition: Range.h:615
RealIter operator-(difference_type rhs) const
Definition: Range.h:562
reference operator*() const
Definition: Range.h:466
mt::iterOf< Range >::type RangeIter
Definition: Range.h:648
IntStepIter & operator--()
Definition: Range.h:503
bool operator==(const String &lhs, const String &rhs)
Definition: String.h:139
difference_type operator-(const RealIter &rhs) const
Definition: Range.h:563
auto seqToIter(Iter &&seq) -> typename std::enable_if< mt::isIterator< Iter >::value, Iter && >::type
Convert a sequence to a forward iterator. Overload for iterator type. Returns the iterator itself...
Definition: Range.h:79
std::bidirectional_iterator_tag iterator_category
Definition: Range.h:650
RealIter(T begin, T step, Int i)
Definition: Range.h:553
std::iterator_traits< Iter >::value_type value_type
Definition: Range.h:651
T value_type
Definition: Range.h:494
Incremental integer iterator (step size = 1). See range(int, int) to create.
Definition: Range.h:437
const T & reference
Definition: Range.h:497
bool operator<(const RealIter &rhs) const
Definition: Range.h:567
Range_()=default
bool operator>(const IntIter &rhs) const
Definition: Range.h:462
std::iterator_traits< Iter >::difference_type difference_type
Definition: Range.h:605
T2 & end()
Definition: Range.h:106
Global Honeycomb namespace.
Range_(const Range_< T1, T2 > &rhs)
Definition: Range.h:97
IntStepIter operator--(int)
Definition: Range.h:505
RingIter(const RangeIter &begin, const RangeIterEnd &end, const Iter &cur, bool bEnd=false)
Definition: Range.h:656
bool operator>(const IntStepIter &rhs) const
Definition: Range.h:515
RingIter & operator++()
Definition: Range.h:663
OutSeq && map(Range &&, Seqs &&..., OutSeq &&, Func &&)
Transform a series of sequences into an output.
auto reversed(Range &&range) -> Range_< std::reverse_iterator< mt_iterOf(range)>, std::reverse_iterator< mt_iter_endOf(range)>>
Reverse a range. Begin/End iterators must be bidirectional.
Definition: Range.h:132