small.hh 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file small.hh
  3. /// @brief Arrays with static dimensions. Compare with View, Container.
  4. // (c) Daniel Llorens - 2013-2016, 2018-2020
  5. // This library is free software; you can redistribute it and/or modify it under
  6. // the terms of the GNU Lesser General Public License as published by the Free
  7. // Software Foundation; either version 3 of the License, or (at your option) any
  8. // later version.
  9. #pragma once
  10. #include "ra/expr.hh"
  11. #include "ra/mpdebug.hh"
  12. namespace ra {
  13. // --------------------
  14. // Develop indices for Small
  15. // --------------------
  16. template <class sizes, class strides>
  17. struct Indexer0
  18. {
  19. static_assert(mp::len<sizes> == mp::len<strides>, "mismatched sizes & strides");
  20. template <rank_t end, rank_t k, class P>
  21. constexpr static dim_t index_p_(dim_t const c, P const & p)
  22. {
  23. static_assert(k>=0 && k<=end, "Bad index");
  24. if constexpr (k==end) {
  25. return c;
  26. } else {
  27. RA_CHECK(inside(p[k], mp::first<sizes>::value));
  28. return Indexer0<mp::drop1<sizes>, mp::drop1<strides>>::template
  29. index_p_<end, k+1>(c + p[k] * mp::first<strides>::value, p);
  30. }
  31. }
  32. template <class P>
  33. constexpr static dim_t index_p(P const & p) // for Container::at().
  34. {
  35. // gcc accepts p.size(), but I also need P = std::array to work. See also below.
  36. static_assert(mp::len<sizes> >= size_s<P>(), "Too many indices");
  37. return index_p_<size_s<P>(), 0>(0, p);
  38. }
  39. template <class P>
  40. constexpr static dim_t index_short(P const & p) // for RaIterator::at().
  41. {
  42. if constexpr (size_s<P>()!=RANK_ANY) {
  43. static_assert(mp::len<sizes> <= size_s<P>(), "Too few indices");
  44. return index_p_<mp::len<sizes>, 0>(0, p);
  45. } else {
  46. RA_CHECK(mp::len<sizes> <= p.size());
  47. return index_p_<mp::len<sizes>, 0>(0, p);
  48. }
  49. }
  50. };
  51. // --------------------
  52. // Small iterator
  53. // --------------------
  54. // TODO Refactor with cell_iterator / STLIterator for View?
  55. // V is always SmallBase<SmallView, ...>
  56. template <class V, rank_t cellr_=0>
  57. struct cell_iterator_small
  58. {
  59. constexpr static rank_t cellr_spec = cellr_;
  60. static_assert(cellr_spec!=RANK_ANY && cellr_spec!=RANK_BAD, "bad cell rank");
  61. constexpr static rank_t fullr = V::rank_s();
  62. constexpr static rank_t cellr = dependent_cell_rank(fullr, cellr_spec);
  63. constexpr static rank_t framer = dependent_frame_rank(fullr, cellr_spec);
  64. static_assert(cellr>=0 || cellr==RANK_ANY, "bad cell rank");
  65. static_assert(framer>=0 || framer==RANK_ANY, "bad frame rank");
  66. static_assert(fullr==cellr || gt_rank(fullr, cellr), "bad cell rank");
  67. constexpr static rank_t rank_s() { return framer; }
  68. constexpr static rank_t rank() { return framer; }
  69. using cell_sizes = mp::drop<typename V::sizes, framer>;
  70. using cell_strides = mp::drop<typename V::strides, framer>;
  71. using sizes = mp::take<typename V::sizes, framer>; // these are strides on atom_type * p !!
  72. using strides = mp::take<typename V::strides, framer>;
  73. using shape_type = std::array<dim_t, framer>;
  74. using atom_type = typename V::value_type;
  75. using cell_type = SmallView<atom_type, cell_sizes, cell_strides>;
  76. using value_type = std::conditional_t<0==cellr, atom_type, cell_type>;
  77. using frame_type = SmallView<int, sizes, strides>; // only to compute ssizes
  78. cell_type c;
  79. constexpr cell_iterator_small(cell_iterator_small const & ci): c { ci.c.p } {}
  80. // see STLIterator for the case of s_[0]=0, etc. [ra12].
  81. constexpr cell_iterator_small(atom_type * p_): c { p_ } {}
  82. constexpr static dim_t size(int k) { RA_CHECK(inside(k, rank())); return V::size(k); }
  83. constexpr static dim_t size_s(int k) { RA_CHECK(inside(k, rank_s())); return V::size(k); }
  84. constexpr static dim_t stride(int k) { return k<rank() ? V::stride(k) : 0; }
  85. constexpr static bool keep_stride(dim_t st, int z, int j)
  86. {
  87. return st*(z<rank() ? stride(z) : 0)==(j<rank() ? stride(j) : 0);
  88. }
  89. constexpr void adv(rank_t k, dim_t d) { c.p += (k<rank()) * stride(k)*d; }
  90. constexpr auto flat() const
  91. {
  92. if constexpr (0==cellr) {
  93. return c.p;
  94. } else {
  95. return CellFlat<cell_type> { c };
  96. }
  97. }
  98. // 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.
  99. template <class I>
  100. constexpr decltype(auto) at(I const & i_)
  101. {
  102. RA_CHECK(rank()<=dim_t(i_.size()), "too few indices ", dim_t(i_.size()), " for rank ", rank());
  103. if constexpr (0==cellr) {
  104. return c.p[Indexer0<sizes, strides>::index_short(i_)];
  105. } else {
  106. return cell_type(c.p + Indexer0<sizes, strides>::index_short(i_));
  107. }
  108. }
  109. FOR_EACH(RA_DEF_ASSIGNOPS, =, *=, +=, -=, /=)
  110. };
  111. // --------------------
  112. // STLIterator for both cell_iterator_small & cell_iterator
  113. // FIXME make it work for any array iterator, as in ply_ravel, ply_index.
  114. // --------------------
  115. template <class S, class I, class P>
  116. inline void next_in_cube(rank_t const framer, S const & dim, I & i, P & p)
  117. {
  118. for (int k=framer-1; k>=0; --k) {
  119. ++i[k];
  120. if (i[k]<dim[k].size) {
  121. p += dim[k].stride;
  122. return;
  123. } else {
  124. i[k] = 0;
  125. p -= dim[k].stride*(dim[k].size-1);
  126. }
  127. }
  128. p = nullptr;
  129. }
  130. template <int k, class sizes, class strides, class I, class P> void
  131. next_in_cube(I & i, P & p)
  132. {
  133. if constexpr (k>=0) {
  134. ++i[k];
  135. if (i[k]<mp::ref<sizes, k>::value) {
  136. p += mp::ref<strides, k>::value;
  137. } else {
  138. i[k] = 0;
  139. p -= mp::ref<strides, k>::value*(mp::ref<sizes, k>::value-1);
  140. next_in_cube<k-1, sizes, strides>(i, p);
  141. }
  142. } else {
  143. p = nullptr;
  144. }
  145. }
  146. template <class Iterator>
  147. struct STLIterator
  148. {
  149. using value_type = typename Iterator::value_type;
  150. using difference_type = dim_t;
  151. using pointer = value_type *;
  152. using reference = value_type &;
  153. using iterator_category = std::forward_iterator_tag;
  154. using shape_type = typename Iterator::shape_type;
  155. Iterator ii;
  156. shape_type i;
  157. STLIterator(STLIterator const & it) = default;
  158. constexpr STLIterator & operator=(STLIterator const & it)
  159. {
  160. i = it.i;
  161. ii.Iterator::~Iterator(); // no-op except for View<RANK_ANY>. Still...
  162. new (&ii) Iterator(it.ii); // avoid ii = it.ii [ra11]
  163. return *this;
  164. }
  165. STLIterator(Iterator const & ii_)
  166. : ii(ii_),
  167. // shape_type may be std::array or std::vector.
  168. i([&]()
  169. { if constexpr (DIM_ANY==size_s<shape_type>()) {
  170. return shape_type(ii.rank(), 0);
  171. } else {
  172. return shape_type {0};
  173. }
  174. }())
  175. {
  176. // [ra12] Null p_ so begin()==end() for empty range. ply() uses sizes so this doesn't matter.
  177. if (ii.c.p!=nullptr && 0==ra::size(ii)) {
  178. ii.c.p = nullptr;
  179. }
  180. };
  181. template <class PP> bool operator==(PP const & j) const { return ii.c.p==j.ii.c.p; }
  182. template <class PP> bool operator!=(PP const & j) const { return ii.c.p!=j.ii.c.p; }
  183. decltype(auto) operator*() const { if constexpr (0==Iterator::cellr) return *ii.c.p; else return ii.c; }
  184. decltype(auto) operator*() { if constexpr (0==Iterator::cellr) return *ii.c.p; else return ii.c; }
  185. STLIterator & operator++()
  186. {
  187. if constexpr (0==Iterator::rank_s()) { // when rank==0, DIM_ANY check isn't enough :-/
  188. ii.c.p = nullptr;
  189. } else if constexpr (DIM_ANY != ra::size_s<Iterator>()) {
  190. next_in_cube<Iterator::rank()-1, typename Iterator::sizes, typename Iterator::strides>(i, ii.c.p);
  191. } else {
  192. next_in_cube(ii.rank(), ii.dim, i, ii.c.p);
  193. }
  194. return *this;
  195. }
  196. };
  197. template <class T> STLIterator<T> stl_iterator(T && t) { return STLIterator<T>(std::forward<T>(t)); }
  198. // --------------------
  199. // Base for both small view & container
  200. // --------------------
  201. template <class sizes_, class strides_, class ... I>
  202. struct FilterDims
  203. {
  204. using sizes = sizes_;
  205. using strides = strides_;
  206. };
  207. template <class sizes_, class strides_, class I0, class ... I>
  208. struct FilterDims<sizes_, strides_, I0, I ...>
  209. {
  210. constexpr static int s = is_beatable<I0>::skip;
  211. constexpr static int s_src = is_beatable<I0>::skip_src;
  212. using next = FilterDims<mp::drop<sizes_, s_src>, mp::drop<strides_, s_src>, I ...>;
  213. using sizes = mp::append<mp::take<sizes_, s>, typename next::sizes>;
  214. using strides = mp::append<mp::take<strides_, s>, typename next::strides>;
  215. };
  216. template <dim_t size0, dim_t stride0> inline
  217. constexpr dim_t select(dim_t i0)
  218. {
  219. RA_CHECK(inside(i0, size0));
  220. return i0*stride0;
  221. };
  222. template <dim_t size0, dim_t stride0, int n> inline
  223. constexpr dim_t select(dots_t<n> i0)
  224. {
  225. return 0;
  226. }
  227. template <class sizes, class strides> inline
  228. constexpr dim_t select_loop()
  229. {
  230. return 0;
  231. }
  232. template <class sizes, class strides, class I0, class ... I> inline
  233. constexpr dim_t select_loop(I0 i0, I ... i)
  234. {
  235. constexpr int s_src = is_beatable<I0>::skip_src;
  236. return select<mp::first<sizes>::value, mp::first<strides>::value>(i0)
  237. + select_loop<mp::drop<sizes, s_src>, mp::drop<strides, s_src>>(i ...);
  238. }
  239. template <class T> struct mat_uv { T uu, uv, vu, vv; };
  240. template <class T> struct mat_xy { T xx, xy, yx, yy; };
  241. template <class T> struct mat_uvz { T uu, uv, uz, vu, vv, vz, zu, zv, zz; };
  242. template <class T> struct mat_xyz { T xx, xy, xz, yx, yy, yz, zx, zy, zz; };
  243. template <class T> struct mat_xyzw { T xx, xy, xz, xw, yx, yy, yz, yw, zx, zy, zz, zw, wx, wy, wz, ww; };
  244. template <class T> struct vec_uv { T u, v; };
  245. template <class T> struct vec_xy { T x, y; };
  246. template <class T> struct vec_tp { T t, p; };
  247. template <class T> struct vec_uvz { T u, v, z; };
  248. template <class T> struct vec_xyz { T x, y, z; };
  249. template <class T> struct vec_rtp { T r, t, p; };
  250. template <class T> struct vec_xyzw { T u, v, z, w; };
  251. template <template <class ...> class Child_,
  252. class T, class sizes_, class strides_>
  253. struct SmallBase
  254. {
  255. using sizes = sizes_;
  256. using strides = strides_;
  257. using value_type = T;
  258. template <class TT> using BadDimension = mp::int_t<(TT::value<0 || TT::value==DIM_ANY || TT::value==DIM_BAD)>;
  259. static_assert(!mp::apply<mp::orb, mp::map<BadDimension, sizes>>::value, "negative dimensions");
  260. static_assert(mp::len<sizes> == mp::len<strides>, "bad strides"); // TODO full static check on strides.
  261. using Child = Child_<T, sizes, strides>;
  262. constexpr static rank_t rank() { return mp::len<sizes>; }
  263. constexpr static rank_t rank_s() { return mp::len<sizes>; }
  264. constexpr static dim_t size() { return mp::apply<mp::prod, sizes>::value; }
  265. constexpr static dim_t size_s() { return size(); }
  266. constexpr static auto ssizes = mp::tuple_values<std::array<dim_t, rank()>, sizes>();
  267. constexpr static auto sstrides = mp::tuple_values<std::array<dim_t, rank()>, strides>();
  268. constexpr static dim_t size(int k) { return ssizes[k]; }
  269. constexpr static dim_t size_s(int k) { return ssizes[k]; }
  270. constexpr static dim_t stride(int k) { return sstrides[k]; }
  271. constexpr T * data() { return static_cast<Child &>(*this).p; }
  272. constexpr T const * data() const { return static_cast<Child const &>(*this).p; }
  273. // Specialize for rank() integer-args -> scalar, same in ra::View in big.hh.
  274. #define SUBSCRIPTS(CONST) \
  275. template <class ... I> \
  276. requires ((0 + ... + std::is_integral_v<I>)<rank() && (is_beatable<I>::static_p && ...)) \
  277. constexpr auto operator()(I ... i) CONST \
  278. { \
  279. using FD = FilterDims<sizes, strides, I ...>; \
  280. return SmallView<T CONST, typename FD::sizes, typename FD::strides> \
  281. (data()+select_loop<sizes, strides>(i ...)); \
  282. } \
  283. template <class ... I> \
  284. requires ((0 + ... + std::is_integral_v<I>)==rank()) \
  285. constexpr decltype(auto) operator()(I ... i) CONST \
  286. { \
  287. return data()[select_loop<sizes, strides>(i ...)]; \
  288. } /* TODO More than one selector... */ \
  289. template <class ... I> \
  290. requires (!is_beatable<I>::static_p || ...) \
  291. constexpr auto operator()(I && ... i) CONST \
  292. { \
  293. return from(*this, std::forward<I>(i) ...); \
  294. } \
  295. /* BUG I must be fixed size, otherwise we can't make out the output type. */ \
  296. template <class I> \
  297. constexpr auto at(I const & i) CONST \
  298. { \
  299. return SmallView<T CONST, mp::drop<sizes, ra::size_s<I>()>, mp::drop<strides, ra::size_s<I>()>> \
  300. (data()+Indexer0<sizes, strides>::index_p(i)); \
  301. } \
  302. constexpr decltype(auto) operator[](dim_t const i) CONST \
  303. { \
  304. return (*this)(i); \
  305. }
  306. FOR_EACH(SUBSCRIPTS, /*const*/, const)
  307. #undef SUBSCRIPTS
  308. // see same thing for View.
  309. #define DEF_ASSIGNOPS(OP) \
  310. template <class X> \
  311. requires (!mp::is_tuple_v<std::decay_t<X>>) \
  312. constexpr Child & operator OP(X && x) \
  313. { \
  314. ra::start(static_cast<Child &>(*this)) OP x; \
  315. return static_cast<Child &>(*this); \
  316. }
  317. FOR_EACH(DEF_ASSIGNOPS, =, *=, +=, -=, /=)
  318. #undef DEF_ASSIGNOPS
  319. // braces don't match X &&
  320. constexpr Child & operator=(nested_arg<T, sizes> const & x)
  321. {
  322. ra::iter<-1>(static_cast<Child &>(*this)) = mp::from_tuple<std::array<typename nested_tuple<T, sizes>::sub, size(0)>>(x);
  323. return static_cast<Child &>(*this);
  324. }
  325. // braces row-major ravel for rank!=1
  326. constexpr Child & operator=(ravel_arg<T, sizes> const & x_)
  327. {
  328. auto x = mp::from_tuple<std::array<T, size()>>(x_);
  329. auto b = this->begin();
  330. for (auto const & xx: x) { *b = xx; ++b; } // std::copy will be constexpr in c++20
  331. return static_cast<Child &>(*this);
  332. }
  333. // TODO would replace by s(ra::iota) if that could be made constexpr
  334. #define DEF_AS(CONST) \
  335. template <int ss, int oo=0> \
  336. constexpr auto as() CONST \
  337. { \
  338. static_assert(rank()>=1, "bad rank for as<>"); \
  339. static_assert(ss>=0 && oo>=0 && ss+oo<=size(), "bad size for as<>"); \
  340. return SmallView<T CONST, mp::cons<mp::int_t<ss>, mp::drop1<sizes>>, strides>(this->data()+oo*this->stride(0)); \
  341. }
  342. FOR_EACH(DEF_AS, /* const */, const)
  343. #undef DEF_AS
  344. template <rank_t c=0> using iterator = ra::cell_iterator_small<SmallBase<SmallView, T, sizes, strides>, c>;
  345. template <rank_t c=0> using const_iterator = ra::cell_iterator_small<SmallBase<SmallView, T const, sizes, strides>, c>;
  346. template <rank_t c=0> constexpr iterator<c> iter() { return data(); }
  347. template <rank_t c=0> constexpr const_iterator<c> iter() const { return data(); }
  348. // FIXME see if we need to extend this for cellr!=0.
  349. // template <class P> using STLIterator = std::conditional_t<have_default_strides, P, STLIterator<Iterator<P>>>;
  350. constexpr static bool have_default_strides = std::same_as<strides, default_strides<sizes>>;
  351. template <class I, class P> using pick_STLIterator = std::conditional_t<have_default_strides, P, ra::STLIterator<I>>;
  352. using STLIterator = pick_STLIterator<iterator<0>, T *>;
  353. using STLConstIterator = pick_STLIterator<const_iterator<0>, T const *>;
  354. // TODO In C++17 begin() end() may be different types, at least for ranged for
  355. // (https://en.cppreference.com/w/cpp/language/range-for).
  356. // See if we can use this to simplify end() and !=end() test.
  357. // TODO With default strides I can just return p. Make sure to test before changing this.
  358. constexpr STLIterator begin() { if constexpr (have_default_strides) return data(); else return iter(); }
  359. constexpr STLConstIterator begin() const { if constexpr (have_default_strides) return data(); else return iter(); }
  360. constexpr STLIterator end() { if constexpr (have_default_strides) return data()+size(); else return iterator<0>(nullptr); }
  361. constexpr STLConstIterator end() const { if constexpr (have_default_strides) return data()+size(); else return const_iterator<0>(nullptr); }
  362. // allowing rank 1 for coord types
  363. constexpr static bool convertible_to_scalar = size()==1; // rank()==0 || (rank()==1 && size()==1);
  364. // Renames.
  365. #define COMP_RENAME_C(name__, req_dim0__, req_dim1__, CONST) \
  366. operator name__<T> CONST &() CONST \
  367. { \
  368. static_assert(std::same_as<strides, default_strides<mp::int_list<req_dim0__, req_dim1__>>>, \
  369. "renames only on default strides"); \
  370. static_assert(size(0)==req_dim0__ && size(1)==req_dim1__, "dimension error"); \
  371. return reinterpret_cast<name__<T> CONST &>(*this); \
  372. }
  373. #define COMP_RENAME(name__, dim0__, dim1__) \
  374. COMP_RENAME_C(name__, dim0__, dim1__, /* const */) \
  375. COMP_RENAME_C(name__, dim0__, dim1__, const)
  376. COMP_RENAME(mat_xy, 2, 2)
  377. COMP_RENAME(mat_uv, 2, 2)
  378. COMP_RENAME(mat_xyz, 3, 3)
  379. COMP_RENAME(mat_uvz, 3, 3)
  380. COMP_RENAME(mat_xyzw, 4, 4)
  381. #undef COMP_RENAME
  382. #undef COMP_RENAME_C
  383. #define COMP_RENAME_C(name__, dim0__, CONST) \
  384. operator name__<T> CONST &() CONST \
  385. { \
  386. static_assert(std::same_as<strides, default_strides<mp::int_list<dim0__>>>, \
  387. "renames only on default strides"); \
  388. static_assert(size(0)==dim0__, "dimension error"); \
  389. return reinterpret_cast<name__<T> CONST &>(*this); \
  390. }
  391. #define COMP_RENAME(name__, dim0__) \
  392. COMP_RENAME_C(name__, dim0__, /* const */) \
  393. COMP_RENAME_C(name__, dim0__, const)
  394. COMP_RENAME(vec_xy, 2)
  395. COMP_RENAME(vec_uv, 2)
  396. COMP_RENAME(vec_tp, 2)
  397. COMP_RENAME(vec_xyz, 3)
  398. COMP_RENAME(vec_uvz, 3)
  399. COMP_RENAME(vec_rtp, 3)
  400. COMP_RENAME(vec_xyzw, 4)
  401. #undef COMP_RENAME
  402. #undef COMP_RENAME_C
  403. };
  404. // ---------------------
  405. // Small view & container
  406. // ---------------------
  407. // Strides are compile time, so we can put most members in the view type.
  408. template <class T, class sizes, class strides>
  409. struct SmallView: public SmallBase<SmallView, T, sizes, strides>
  410. {
  411. using Base = SmallBase<SmallView, T, sizes, strides>;
  412. using Base::rank, Base::size, Base::operator=;
  413. T * p;
  414. constexpr SmallView(T * p_): p(p_) {}
  415. constexpr SmallView(SmallView const & s): p(s.p) {}
  416. constexpr operator T & () { static_assert(Base::convertible_to_scalar); return p[0]; }
  417. constexpr operator T const & () const { static_assert(Base::convertible_to_scalar); return p[0]; };
  418. };
  419. template <class T, class sizes, class strides, class ... nested_args, class ... ravel_args>
  420. struct SmallArray<T, sizes, strides, std::tuple<nested_args ...>, std::tuple<ravel_args ...>>
  421. : public SmallBase<SmallArray, T, sizes, strides>
  422. {
  423. using Base = SmallBase<SmallArray, T, sizes, strides>;
  424. using Base::rank, Base::size;
  425. T p[Base::size()];
  426. constexpr SmallArray(): p() {}
  427. // braces don't match (X &&)
  428. constexpr SmallArray(nested_args const & ... x): p()
  429. {
  430. static_cast<Base &>(*this) = nested_arg<T, sizes> { x ... };
  431. }
  432. // braces row-major ravel for rank!=1
  433. constexpr SmallArray(ravel_args const & ... x): p()
  434. {
  435. static_cast<Base &>(*this) = ravel_arg<T, sizes> { x ... };
  436. }
  437. constexpr SmallArray(T const & t): p() // needed if T isn't registered as scalar [ra44]
  438. {
  439. for (auto & x: p) { x = t; } // std::fill will be constexpr in c++20
  440. }
  441. // X && x makes this a better match than nested_args ... for 1 argument.
  442. template <class X>
  443. requires (!std::is_same_v<T, std::decay_t<X>> && !mp::is_tuple_v<std::decay_t<X>>)
  444. constexpr SmallArray(X && x): p()
  445. {
  446. static_cast<Base &>(*this) = x;
  447. }
  448. constexpr operator SmallView<T, sizes, strides> () { return SmallView<T, sizes, strides>(p); }
  449. constexpr operator SmallView<T const, sizes, strides> const () { return SmallView<T const, sizes, strides>(p); }
  450. // BUG these make SmallArray<T, N> std::is_convertible to T even though conversion isn't possible b/c of the assert.
  451. #define DEF_SCALARS(CONST) \
  452. constexpr operator T CONST & () CONST { static_assert(Base::convertible_to_scalar); return p[0]; } \
  453. T CONST & back() CONST \
  454. { \
  455. static_assert(Base::rank()==1, "bad rank for back"); \
  456. static_assert(Base::size()>0, "bad size for back"); \
  457. return (*this)[Base::size()-1]; \
  458. }
  459. FOR_EACH(DEF_SCALARS, /* const */, const)
  460. #undef DEF_SCALARS
  461. };
  462. // FIXME unfortunately necessary. Try to remove the need, also of (S, begin, end) in Container, once the nested_tuple constructors work.
  463. template <class A, class I, class J>
  464. A ravel_from_iterators(I && begin, J && end)
  465. {
  466. A a;
  467. std::copy(std::forward<I>(begin), std::forward<J>(end), a.begin());
  468. return a;
  469. }
  470. // FIXME Type_ seems superfluous
  471. template <template <class ...> class Type_, class T, class sizes, class strides>
  472. struct ra_traits_small
  473. {
  474. using V = Type_<T, sizes, strides>;
  475. constexpr static auto shape(V const & v) { return SmallView<ra::dim_t const, mp::int_list<V::rank_s()>, mp::int_list<1>>(V::ssizes.data()); }
  476. constexpr static dim_t size(V const & v) { return v.size(); }
  477. constexpr static rank_t rank(V const & v) { return V::rank(); }
  478. constexpr static dim_t size_s() { return V::size(); }
  479. constexpr static rank_t rank_s() { return V::rank(); };
  480. };
  481. template <class T, class sizes, class strides>
  482. struct ra_traits_def<SmallArray<T, sizes, strides>>: public ra_traits_small<SmallArray, T, sizes, strides> {};
  483. template <class T, class sizes, class strides>
  484. struct ra_traits_def<SmallView<T, sizes, strides>>: public ra_traits_small<SmallView, T, sizes, strides> {};
  485. // ---------------------
  486. // Support for builtin arrays
  487. // ---------------------
  488. template <class T, class I=mp::iota<std::rank_v<T>>>
  489. struct builtin_array_sizes;
  490. template <class T, int ... I>
  491. struct builtin_array_sizes<T, mp::int_list<I ...>>
  492. {
  493. using type = mp::int_list<std::extent_v<T, I> ...>;
  494. };
  495. template <class T> using builtin_array_sizes_t = typename builtin_array_sizes<T>::type;
  496. template <class T>
  497. struct builtin_array_types
  498. {
  499. using A = std::remove_volatile_t<std::remove_reference_t<T>>; // preserve const
  500. using E = std::remove_all_extents_t<A>;
  501. using sizes = builtin_array_sizes_t<A>;
  502. using view = SmallView<E, sizes>;
  503. };
  504. // forward declared in type.hh.
  505. template <class T> requires (is_builtin_array<T>)
  506. inline constexpr auto
  507. start(T && t)
  508. {
  509. using Z = builtin_array_types<T>;
  510. return typename Z::view((typename Z::E *)(t)).iter();
  511. }
  512. template <class T>
  513. requires (is_builtin_array<T>)
  514. struct ra_traits_def<T>
  515. {
  516. using S = typename builtin_array_types<T>::view;
  517. constexpr static decltype(auto) shape(T const & t) { return ra::shape(start(t)); } // FIXME messy
  518. constexpr static dim_t size(T const & t) { return ra::size_s<S>(); }
  519. constexpr static dim_t size_s() { return ra::size_s<S>(); }
  520. constexpr static rank_t rank(T const & t) { return S::rank(); }
  521. constexpr static rank_t rank_s() { return S::rank_s(); }
  522. };
  523. // --------------------
  524. // Small ops; cf view-ops.hh.
  525. // FIXME maybe there, or separate file.
  526. // TODO See if this can be merged with Reframe (e.g. beat(reframe(a)) -> transpose(a) ?)
  527. // --------------------
  528. template <class A, class i>
  529. struct axis_indices
  530. {
  531. template <class T> using match_index = mp::int_t<(T::value==i::value)>;
  532. using I = mp::iota<mp::len<A>>;
  533. using type = mp::Filter_<mp::map<match_index, A>, I>;
  534. // don't enforce, so allow dead axes (e.g. in transpose<1>(rank 1 array)).
  535. // static_assert((mp::len<type>)>0, "dst axis doesn't appear in transposed axes list");
  536. };
  537. template <class axes_list, class src_sizes, class src_strides>
  538. struct axes_list_indices
  539. {
  540. static_assert(mp::len<axes_list> == mp::len<src_sizes>, "bad size for transposed axes list");
  541. constexpr static int talmax = mp::fold<mp::max, void, axes_list>::value;
  542. constexpr static int talmin = mp::fold<mp::min, void, axes_list>::value;
  543. static_assert(talmin >= 0, "bad index in transposed axes list");
  544. // don't enforce, so allow dead axes (e.g. in transpose<1>(rank 1 array)).
  545. // static_assert(talmax < mp::len<src_sizes>, "bad index in transposed axes list");
  546. template <class dst_i> struct dst_indices_
  547. {
  548. using type = typename axis_indices<axes_list, dst_i>::type;
  549. template <class i> using sizesi = mp::ref<src_sizes, i::value>;
  550. template <class i> using stridesi = mp::ref<src_strides, i::value>;
  551. using stride = mp::fold<mp::sum, void, mp::map<stridesi, type>>;
  552. using size = mp::fold<mp::min, void, mp::map<sizesi, type>>;
  553. };
  554. template <class dst_i> using dst_indices = typename dst_indices_<dst_i>::type;
  555. template <class dst_i> using dst_size = typename dst_indices_<dst_i>::size;
  556. template <class dst_i> using dst_stride = typename dst_indices_<dst_i>::stride;
  557. using dst = mp::iota<(talmax>=0 ? (1+talmax) : 0)>;
  558. using type = mp::map<dst_indices, dst>;
  559. using sizes = mp::map<dst_size, dst>;
  560. using strides = mp::map<dst_stride, dst>;
  561. };
  562. #define DEF_TRANSPOSE(CONST) \
  563. template <int ... Iarg, template <class ...> class Child, class T, class sizes, class strides> \
  564. inline auto transpose(SmallBase<Child, T, sizes, strides> CONST & a) \
  565. { \
  566. using ti = axes_list_indices<mp::int_list<Iarg ...>, sizes, strides>; \
  567. return SmallView<T CONST, typename ti::sizes, typename ti::strides>(a.data()); \
  568. }; \
  569. \
  570. template <template <class ...> class Child, class T, class sizes, class strides> \
  571. inline auto diag(SmallBase<Child, T, sizes, strides> CONST & a) \
  572. { \
  573. return transpose<0, 0>(a); \
  574. }
  575. FOR_EACH(DEF_TRANSPOSE, /* const */, const)
  576. #undef DEF_TRANSPOSE
  577. // TODO Used by ProductRule; waiting for proper generalization.
  578. template <template <class ...> class Child1, class T1, class sizes1, class strides1,
  579. template <class ...> class Child2, class T2, class sizes2, class strides2>
  580. auto cat(SmallBase<Child1, T1, sizes1, strides1> const & a1, SmallBase<Child2, T2, sizes2, strides2> const & a2)
  581. {
  582. using A1 = SmallBase<Child1, T1, sizes1, strides1>;
  583. using A2 = SmallBase<Child2, T2, sizes2, strides2>;
  584. static_assert(A1::rank()==1 && A2::rank()==1, "bad ranks for cat"); // gcc accepts a1.rank(), etc.
  585. using T = std::decay_t<decltype(a1[0])>;
  586. Small<T, A1::size()+A2::size()> val;
  587. std::copy(a1.begin(), a1.end(), val.begin());
  588. std::copy(a2.begin(), a2.end(), val.begin()+a1.size());
  589. return val;
  590. }
  591. template <template <class ...> class Child1, class T1, class sizes1, class strides1, class A2>
  592. requires (is_scalar<A2>)
  593. auto cat(SmallBase<Child1, T1, sizes1, strides1> const & a1, A2 const & a2)
  594. {
  595. using A1 = SmallBase<Child1, T1, sizes1, strides1>;
  596. static_assert(A1::rank()==1, "bad ranks for cat");
  597. using T = std::decay_t<decltype(a1[0])>;
  598. Small<T, A1::size()+1> val;
  599. std::copy(a1.begin(), a1.end(), val.begin());
  600. val[a1.size()] = a2;
  601. return val;
  602. }
  603. template <class A1, template <class ...> class Child2, class T2, class sizes2, class strides2>
  604. requires (is_scalar<A1>)
  605. auto cat(A1 const & a1, SmallBase<Child2, T2, sizes2, strides2> const & a2)
  606. {
  607. using A2 = SmallBase<Child2, T2, sizes2, strides2>;
  608. static_assert(A2::rank()==1, "bad ranks for cat");
  609. using T = std::decay_t<decltype(a2[0])>;
  610. Small<T, 1+A2::size()> val;
  611. val[0] = a1;
  612. std::copy(a2.begin(), a2.end(), val.begin()+1);
  613. return val;
  614. }
  615. // FIXME should be local (constexpr lambda + mp::apply?)
  616. template <int s> struct explode_divop
  617. {
  618. template <class T> struct op_
  619. {
  620. static_assert((T::value/s)*s==T::value);
  621. using type = mp::int_t<T::value / s>;
  622. };
  623. template <class T> using op = typename op_<T>::type;
  624. };
  625. // See view-ops.hh:explode, collapse. FIXME support real->complex, etc.
  626. template <class super_t,
  627. template <class ...> class Child, class T, class sizes, class strides>
  628. auto explode(SmallBase<Child, T, sizes, strides> & a)
  629. {
  630. using ta = SmallBase<Child, T, sizes, strides>;
  631. // the returned type has strides in super_t, but to support general strides we'd need strides in T. Maybe FIXME?
  632. static_assert(super_t::have_default_strides);
  633. constexpr rank_t ra = ta::rank_s();
  634. constexpr rank_t rb = super_t::rank_s();
  635. static_assert(std::is_same_v<mp::drop<sizes, ra-rb>, typename super_t::sizes>);
  636. static_assert(std::is_same_v<mp::drop<strides, ra-rb>, typename super_t::strides>);
  637. using cstrides = mp::map<explode_divop<ra::size_s<super_t>()>::template op, mp::take<strides, ra-rb>>;
  638. return SmallView<super_t, mp::take<sizes, ra-rb>, cstrides>((super_t *) a.data());
  639. }
  640. } // namespace ra