iterator-small.H 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // (c) Daniel Llorens - 2017
  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 SmallView, 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_SMALL RA_CHECK_BOUNDS
  14. #else
  15. #ifndef RA_CHECK_BOUNDS_RA_ITERATOR_SMALL
  16. #define RA_CHECK_BOUNDS_RA_ITERATOR_SMALL 1
  17. #endif
  18. #endif
  19. #if RA_CHECK_BOUNDS_RA_ITERATOR_SMALL==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_small { I i; };
  26. template <class I> auto print_iterator_small(I && i) { return Print_iterator_small<I> { std::forward<I>(i) }; }
  27. template <class I>
  28. std::ostream & operator<<(std::ostream & o, Print_iterator_small<I> && p)
  29. {
  30. o << "we'll see";
  31. return o;
  32. }
  33. // V is SmallBase (FIXME so why parameterize? apparently only for order-of-decl.)
  34. // Child is ra_iterator_small
  35. template <class V, rank_t cellr_, class Child>
  36. struct cell_iterator_small
  37. {
  38. constexpr static rank_t cellr_spec = cellr_;
  39. static_assert(cellr_spec!=RANK_ANY && cellr_spec!=RANK_BAD, "bad cell rank");
  40. constexpr static rank_t fullr = V::rank_s();
  41. constexpr static rank_t cellr = dependent_cell_rank(fullr, cellr_spec);
  42. constexpr static rank_t framer = dependent_frame_rank(fullr, cellr_spec);
  43. static_assert(cellr>=0 || cellr==RANK_ANY, "bad cell rank");
  44. static_assert(framer>=0 || framer==RANK_ANY, "bad frame rank");
  45. static_assert(fullr==cellr || gt_rank(fullr, cellr), "bad cell rank");
  46. constexpr static rank_t rank_s() { return framer; }
  47. constexpr rank_t rank() const { return dependent_frame_rank(rank_t(dim.size()), cellr_spec); }
  48. using shape_type = mp::If_<rank_s()==DIM_ANY, std::vector<dim_t>, Small<dim_t, rank_s()>>;
  49. using dims_p = decltype(std::declval<V const>().dim); // FIXME can this be & ?
  50. using atom_type = typename ra_traits<V>::value_type;
  51. using cell_type = SmallView<atom_type, mp::Drop_<typename V::sizes, typename V::strides> >;
  52. using value_type = mp::If_<cellr==0, atom_type, cell_type>;
  53. dims_p dim; // FIXME take V & and ask through that. Should be const.
  54. cell_type c;
  55. constexpr cell_iterator_small() { c.p = nullptr; } // end marker only.
  56. constexpr cell_iterator_small(cell_iterator_small const & ci): dim(ci.dim), c { ci.c.dim, ci.c.p } {}
  57. constexpr cell_iterator_small & operator=(cell_iterator_small const & ci) = delete;
  58. // needed because View = View copies contents, not the View itself [ra11]
  59. constexpr cell_iterator_small & assign(cell_iterator_small const & ci) { dim = ci.dim; c.dim = ci.c.dim; c.p = ci.c.p; return *this; }
  60. // s_ is array's full shape; split it into dim/i (frame) and c (cell).
  61. template <class SS> constexpr
  62. cell_iterator_small(SS const & s_, atom_type * p_): dim(s_)
  63. {
  64. rank_t const rank = this->rank();
  65. c.p = p_; // see stl_iterator for the case of s_[0]=0, etc. [ra12].
  66. rank_t const cellr = dependent_cell_rank(rank_t(dim.size()), cellr_spec);
  67. resize(c.dim, cellr);
  68. for (int k=0; k<cellr; ++k) {
  69. c.dim[k] = s_[k+rank];
  70. }
  71. }
  72. constexpr static dim_t size_s()
  73. {
  74. constexpr dim_t fullsize_s = ra_traits<V>::size_s();
  75. return fullsize_s==DIM_ANY ? DIM_ANY : fullsize_s / ra_traits<cell_type>::size_s();
  76. }
  77. constexpr shape_type shape() const
  78. {
  79. rank_t const iend = rank();
  80. shape_type s(ra_traits<shape_type>::make(iend));
  81. for (rank_t i=0; i!=iend; ++i) {
  82. s[i] = dim[i].size;
  83. }
  84. return s;
  85. }
  86. constexpr dim_t size(int j) const { CHECK_BOUNDS(inside(j, rank())); return dim[j].size; }
  87. constexpr dim_t stride(int j) const { return j<rank() ? dim[j].stride : 0; }
  88. constexpr bool keep_stride(dim_t step, int z, int j) const { return step*stride(z)==stride(j); }
  89. constexpr void adv(rank_t k, dim_t d) { c.p += stride(k)*d; }
  90. };
  91. #define DEF_ASSIGNOPS(OP) \
  92. template <class X> void operator OP(X && x) \
  93. { for_each([](auto && y, auto && x) { std::forward<decltype(y)>(y) OP x; }, *this, x); } \
  94. template <class X> void operator OP(ra_iterator_small const & x) \
  95. { for_each([](auto && y, auto && x) { std::forward<decltype(y)>(y) OP x; }, *this, x); }
  96. template <class C>
  97. struct CellFlat
  98. {
  99. C c;
  100. constexpr void operator+=(dim_t const s) { c.p += s; }
  101. constexpr C & operator*() { return c; }
  102. };
  103. template <class V, rank_t cellr=0>
  104. struct ra_iterator_small: public cell_iterator_small<V, cellr, ra_iterator_small<V, cellr>>
  105. {
  106. using P = cell_iterator_small<V, cellr, ra_iterator_small<V, cellr>>;
  107. ra_iterator_small(): P() {};
  108. template <class SS> ra_iterator_small(SS const & s_, typename P::atom_type * p_): P(s_, p_) {};
  109. auto flat() const { return CellFlat<typename P::cell_type> { P::c }; }
  110. // 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.
  111. template <class I>
  112. decltype(auto) at(I const & i_)
  113. {
  114. CHECK_BOUNDS(P::rank()<=dim_t(i_.size()) && "too few indices");
  115. return typename P::cell_type { P::c.dim, P::c.p+Indexer1::index_short(P::rank(), P::dim, i_) };
  116. }
  117. decltype(auto) operator_star() const { return P::c; }
  118. decltype(auto) operator_star() { return P::c; }
  119. FOR_EACH(DEF_ASSIGNOPS, =, *=, +=, -=, /=)
  120. };
  121. template <class V>
  122. struct ra_iterator_small<V, 0>: public cell_iterator_small<V, 0, ra_iterator_small<V, 0>>
  123. {
  124. using P = cell_iterator_small<V, 0, ra_iterator_small<V, 0>>;
  125. ra_iterator_small(): P() {};
  126. template <class SS> ra_iterator_small(SS const & s_, typename P::atom_type * p_): P(s_, p_) {};
  127. auto flat() const { return P::c.p; }
  128. template <class I>
  129. decltype(auto) at(I const & i_)
  130. {
  131. CHECK_BOUNDS(P::rank()<=rank_t(i_.size()) && "too few indices");
  132. return P::c.p[Indexer1::index_short(P::rank(), P::dim, i_)];
  133. }
  134. decltype(auto) operator_star() const { return *P::c.p; }
  135. decltype(auto) operator_star() { return *P::c.p; }
  136. FOR_EACH(DEF_ASSIGNOPS, =, *=, +=, -=, /=)
  137. };
  138. #undef DEF_ASSIGNOPS
  139. template <class Iterator>
  140. struct stl_iterator_small
  141. {
  142. using value_type = typename Iterator::value_type;
  143. using difference_type = dim_t;
  144. using pointer = value_type *;
  145. using reference = value_type &;
  146. using const_reference = value_type const &;
  147. using iterator_category = std::forward_iterator_tag;
  148. using shape_type = typename Iterator::P::shape_type;
  149. Iterator ii;
  150. shape_type i;
  151. stl_iterator_small(): ii() {};
  152. // be careful to avoid ii = it.ii, see [ra11] above.
  153. stl_iterator_small(stl_iterator_small const & it) = default;
  154. stl_iterator_small & operator=(stl_iterator_small const & it) { i=it.i; ii.assign(it.ii); return *this; }
  155. template <class SS> stl_iterator_small(SS const & s_, typename Iterator::P::atom_type * p_)
  156. : ii(s_, p_), i(ra::ra_traits<shape_type>::make(dependent_frame_rank(s_.size(), ii.cellr_spec), 0))
  157. {
  158. // [ra12] so begin()==end() for un-plied iterator on empty range. For plying sizes are used instead, so we don't care.
  159. for (auto dim: s_) {
  160. if (dim.size==0) {
  161. ii.c.p = nullptr;
  162. break;
  163. }
  164. }
  165. };
  166. template <class PP> bool operator==(PP const & j) const { return ii.c.p==j.ii.c.p; }
  167. template <class PP> bool operator!=(PP const & j) const { return ii.c.p!=j.ii.c.p; }
  168. // taken from Iterator to handle the special case for cellr=0.
  169. decltype(auto) operator*() const { return ii.operator_star(); }
  170. decltype(auto) operator*() { return ii.operator_star(); }
  171. stl_iterator_small & operator++()
  172. {
  173. next_in_cube(ii.rank(), ii.dim, i, ii.c.p);
  174. return *this;
  175. }
  176. };
  177. template <int cell_rank, class A> inline constexpr
  178. auto iter(A && a) { return a.template iter<cell_rank>(); }
  179. } // namespace ra
  180. #undef CHECK_BOUNDS
  181. #undef RA_CHECK_BOUNDS_RA_ITERATOR_SMALL