iterator.H 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // (c) Daniel Llorens - 2013-2014, 2016
  2. // This library is free software; you can redistribute it and/or modify it under
  3. // the terms of the GNU Lesser General Public License as published by the Free
  4. // Software Foundation; either version 3 of the License, or (at your option) any
  5. // later version.
  6. /// @file iterator.H
  7. /// @brief Array iterator for View, plus row-major STL iterator.
  8. #pragma once
  9. #include "ra/traits.H"
  10. #include "ra/opcheck.H"
  11. #include <iostream>
  12. #ifdef RA_CHECK_BOUNDS
  13. #define RA_CHECK_BOUNDS_RA_ITERATOR RA_CHECK_BOUNDS
  14. #else
  15. #ifndef RA_CHECK_BOUNDS_RA_ITERATOR
  16. #define RA_CHECK_BOUNDS_RA_ITERATOR 1
  17. #endif
  18. #endif
  19. #if RA_CHECK_BOUNDS_RA_ITERATOR==0
  20. #define CHECK_BOUNDS( cond )
  21. #else
  22. #define CHECK_BOUNDS( cond ) assert( cond )
  23. #endif
  24. namespace ra {
  25. template <class I> struct Print_iterator { I i; };
  26. template <class I> auto print_iterator(I && i) { return Print_iterator<I> { std::forward<I>(i) }; }
  27. template <class I>
  28. std::ostream & operator<<(std::ostream & o, Print_iterator<I> && p)
  29. {
  30. o << "<cellr " << p.i.cellr << "[";
  31. for (int k=0; k<p.i.rank(); ++k) {
  32. o << k << ": " << p.i.dim[k].size << ", " << p.i.dim[k].stride << (k+1!=p.i.rank() ? "; " : "");
  33. }
  34. o << "]>";
  35. return o;
  36. }
  37. // V is View (FIXME so why parameterize? apparently only for order-of-decl.)
  38. // Child is ra_iterator
  39. template <class V, rank_t cellr_, class Child>
  40. struct cell_iterator
  41. {
  42. constexpr static rank_t cellr_spec = cellr_;
  43. static_assert(cellr_spec!=RANK_ANY && cellr_spec!=RANK_BAD, "bad cell rank");
  44. constexpr static rank_t fullr = V::rank_s();
  45. constexpr static rank_t cellr = dependent_cell_rank(fullr, cellr_spec);
  46. constexpr static rank_t framer = dependent_frame_rank(fullr, cellr_spec);
  47. static_assert(cellr>=0 || cellr==RANK_ANY, "bad cell rank");
  48. static_assert(framer>=0 || framer==RANK_ANY, "bad frame rank");
  49. static_assert(fullr==cellr || gt_rank(fullr, cellr), "bad cell rank");
  50. constexpr static rank_t rank_s() { return framer; }
  51. constexpr rank_t rank() const { return dependent_frame_rank(rank_t(dim.size()), cellr_spec); }
  52. using shape_type = mp::If_<rank_s()==DIM_ANY, std::vector<dim_t>, Small<dim_t, rank_s()>>;
  53. using dims_p = decltype(std::declval<V const>().dim); // FIXME can this be & ?
  54. using atom_type = typename ra_traits<V>::value_type;
  55. using cell_type = View<atom_type, cellr>;
  56. using value_type = mp::If_<cellr==0, atom_type, cell_type>;
  57. dims_p dim; // FIXME take V & and ask through that. Should be const.
  58. cell_type c;
  59. constexpr cell_iterator() { c.p = nullptr; } // end marker only.
  60. constexpr cell_iterator(cell_iterator const & ci): dim(ci.dim), c { ci.c.dim, ci.c.p } {}
  61. constexpr cell_iterator & operator=(cell_iterator const & ci) = delete;
  62. // needed because View = View copies contents, not the View itself [ra11]
  63. constexpr cell_iterator & assign(cell_iterator const & ci) { dim = ci.dim; c.dim = ci.c.dim; c.p = ci.c.p; return *this; }
  64. // s_ is array's full shape; split it into dim/i (frame) and c (cell).
  65. template <class SS> constexpr
  66. cell_iterator(SS const & s_, atom_type * p_): dim(s_)
  67. {
  68. rank_t const rank = this->rank();
  69. c.p = p_; // see stl_iterator for the case of s_[0]=0, etc. [ra12].
  70. rank_t const cellr = dependent_cell_rank(rank_t(dim.size()), cellr_spec);
  71. resize(c.dim, cellr);
  72. for (int k=0; k<cellr; ++k) {
  73. c.dim[k] = s_[k+rank];
  74. }
  75. }
  76. constexpr static dim_t size_s()
  77. {
  78. constexpr dim_t fullsize_s = ra_traits<V>::size_s();
  79. return fullsize_s==DIM_ANY ? DIM_ANY : fullsize_s / ra_traits<cell_type>::size_s();
  80. }
  81. constexpr shape_type shape() const
  82. {
  83. rank_t const iend = rank();
  84. shape_type s(ra_traits<shape_type>::make(iend));
  85. for (rank_t i=0; i!=iend; ++i) {
  86. s[i] = dim[i].size;
  87. }
  88. return s;
  89. }
  90. constexpr dim_t size(int j) const { CHECK_BOUNDS(inside(j, rank())); return dim[j].size; }
  91. constexpr dim_t stride(int j) const { return j<rank() ? dim[j].stride : 0; }
  92. constexpr bool keep_stride(dim_t step, int z, int j) const { return step*stride(z)==stride(j); }
  93. constexpr void adv(rank_t k, dim_t d) { c.p += stride(k)*d; }
  94. };
  95. #define DEF_ASSIGNOPS(OP) \
  96. template <class X> void operator OP(X && x) \
  97. { for_each([](auto && y, auto && x) { std::forward<decltype(y)>(y) OP x; }, *this, x); } \
  98. template <class X> void operator OP(ra_iterator const & x) \
  99. { for_each([](auto && y, auto && x) { std::forward<decltype(y)>(y) OP x; }, *this, x); }
  100. template <class C>
  101. struct CellFlat
  102. {
  103. C c;
  104. constexpr void operator+=(dim_t const s) { c.p += s; }
  105. constexpr C & operator*() { return c; }
  106. };
  107. template <class V, rank_t cellr=0>
  108. struct ra_iterator: public cell_iterator<V, cellr, ra_iterator<V, cellr>>
  109. {
  110. using P = cell_iterator<V, cellr, ra_iterator<V, cellr>>;
  111. ra_iterator(): P() {};
  112. template <class SS> ra_iterator(SS const & s_, typename P::atom_type * p_): P(s_, p_) {};
  113. auto flat() const { return CellFlat<typename P::cell_type> { P::c }; }
  114. // Return type to allow either View & or View const & verb. Can't set self b/c original p isn't kept. TODO Think this over.
  115. template <class I>
  116. decltype(auto) at(I const & i_)
  117. {
  118. CHECK_BOUNDS(P::rank()<=dim_t(i_.size()) && "too few indices");
  119. return typename P::cell_type { P::c.dim, P::c.p+Indexer1::index_short(P::rank(), P::dim, i_) };
  120. }
  121. decltype(auto) operator_star() const { return P::c; }
  122. decltype(auto) operator_star() { return P::c; }
  123. FOR_EACH(DEF_ASSIGNOPS, =, *=, +=, -=, /=)
  124. };
  125. template <class V>
  126. struct ra_iterator<V, 0>: public cell_iterator<V, 0, ra_iterator<V, 0>>
  127. {
  128. using P = cell_iterator<V, 0, ra_iterator<V, 0>>;
  129. ra_iterator(): P() {};
  130. template <class SS> ra_iterator(SS const & s_, typename P::atom_type * p_): P(s_, p_) {};
  131. auto flat() const { return P::c.p; }
  132. template <class I>
  133. decltype(auto) at(I const & i_)
  134. {
  135. CHECK_BOUNDS(P::rank()<=rank_t(i_.size()) && "too few indices");
  136. return P::c.p[Indexer1::index_short(P::rank(), P::dim, i_)];
  137. }
  138. decltype(auto) operator_star() const { return *P::c.p; }
  139. decltype(auto) operator_star() { return *P::c.p; }
  140. FOR_EACH(DEF_ASSIGNOPS, =, *=, +=, -=, /=)
  141. };
  142. #undef DEF_ASSIGNOPS
  143. template <class Iterator>
  144. struct stl_iterator
  145. {
  146. using value_type = typename Iterator::value_type;
  147. using difference_type = dim_t;
  148. using pointer = value_type *;
  149. using reference = value_type &;
  150. using const_reference = value_type const &;
  151. using iterator_category = std::forward_iterator_tag;
  152. using shape_type = typename Iterator::P::shape_type;
  153. Iterator ii;
  154. shape_type i;
  155. stl_iterator(): ii() {};
  156. // be careful to avoid ii = it.ii, see [ra11] above.
  157. stl_iterator(stl_iterator const & it) = default;
  158. stl_iterator & operator=(stl_iterator const & it) { i=it.i; ii.assign(it.ii); return *this; }
  159. template <class SS> stl_iterator(SS const & s_, typename Iterator::P::atom_type * p_)
  160. : ii(s_, p_), i(ra::ra_traits<shape_type>::make(dependent_frame_rank(s_.size(), ii.cellr_spec), 0))
  161. {
  162. // [ra12] so begin()==end() for un-plied iterator on empty range. For plying sizes are used instead, so we don't care.
  163. for (auto dim: s_) {
  164. if (dim.size==0) {
  165. ii.c.p = nullptr;
  166. break;
  167. }
  168. }
  169. };
  170. template <class PP> bool operator==(PP const & j) const { return ii.c.p==j.ii.c.p; }
  171. template <class PP> bool operator!=(PP const & j) const { return ii.c.p!=j.ii.c.p; }
  172. // taken from Iterator to handle the special case for cellr=0.
  173. decltype(auto) operator*() const { return ii.operator_star(); }
  174. decltype(auto) operator*() { return ii.operator_star(); }
  175. stl_iterator & operator++()
  176. {
  177. next_in_cube(ii.rank(), ii.dim, i, ii.c.p);
  178. return *this;
  179. }
  180. };
  181. template <int cell_rank, class A> inline constexpr
  182. auto iter(A && a) { return a.template iter<cell_rank>(); }
  183. } // namespace ra
  184. #undef CHECK_BOUNDS
  185. #undef RA_CHECK_BOUNDS_RA_ITERATOR