small.hh 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra - Arrays with static lengths/strides, cf big.hh.
  3. // (c) Daniel Llorens - 2013-2023
  4. // This library is free software; you can redistribute it and/or modify it under
  5. // the terms of the GNU Lesser General Public License as published by the Free
  6. // Software Foundation; either version 3 of the License, or (at your option) any
  7. // later version.
  8. #pragma once
  9. #include "ply.hh"
  10. namespace ra {
  11. // --------------------
  12. // STLIterator for both CellSmall & CellBig
  13. // FIXME make it work for any array iterator, as in ply_ravel, ply_index.
  14. // --------------------
  15. template <class S, class I, class P>
  16. constexpr void
  17. next_in_cube(rank_t const framer, S const & dimv, I & i, P & p)
  18. {
  19. for (int k=framer-1; k>=0; --k) {
  20. ++i[k];
  21. if (i[k]<dimv[k].len) {
  22. p += dimv[k].step;
  23. return;
  24. } else {
  25. i[k] = 0;
  26. p -= dimv[k].step*(dimv[k].len-1);
  27. }
  28. }
  29. p = nullptr;
  30. }
  31. template <int k, class lens, class steps, class I, class P>
  32. constexpr void
  33. next_in_cube(I & i, P & p)
  34. {
  35. if constexpr (k>=0) {
  36. ++i[k];
  37. if (i[k]<mp::ref<lens, k>::value) {
  38. p += mp::ref<steps, k>::value;
  39. } else {
  40. i[k] = 0;
  41. p -= mp::ref<steps, k>::value*(mp::ref<lens, k>::value-1);
  42. next_in_cube<k-1, lens, steps>(i, p);
  43. }
  44. } else {
  45. p = nullptr;
  46. }
  47. }
  48. template <class Iterator>
  49. struct STLIterator
  50. {
  51. using value_type = typename Iterator::value_type;
  52. using difference_type = dim_t;
  53. using pointer = value_type *;
  54. using reference = value_type &;
  55. using iterator_category = std::forward_iterator_tag;
  56. using shape_type = decltype(ra::shape(std::declval<Iterator>()));
  57. Iterator ii;
  58. shape_type i;
  59. STLIterator(STLIterator const & it) = default;
  60. constexpr STLIterator & operator=(STLIterator const & it)
  61. {
  62. i = it.i;
  63. ii.Iterator::~Iterator(); // no-op except for View<RANK_ANY>. Still...
  64. new (&ii) Iterator(it.ii); // avoid ii = it.ii [ra11]
  65. return *this;
  66. }
  67. STLIterator(Iterator const & ii_)
  68. : ii(ii_),
  69. // shape_type may be std::array or std::vector.
  70. i([&] {
  71. if constexpr (DIM_ANY==size_s<shape_type>()) {
  72. return shape_type(ii.rank(), 0);
  73. } else {
  74. return shape_type {0};
  75. }
  76. }())
  77. {
  78. // [ra12] Null p_ so begin()==end() for empty range. ply() uses lens so this doesn't matter.
  79. if (0==ra::size(ii)) {
  80. ii.c.p = nullptr;
  81. }
  82. };
  83. template <class PP> bool operator==(PP const & j) const { return ii.c.p==j.ii.c.p; }
  84. template <class PP> bool operator!=(PP const & j) const { return ii.c.p!=j.ii.c.p; }
  85. decltype(auto) operator*() const { if constexpr (0==Iterator::cellr) return *ii.c.p; else return ii.c; }
  86. decltype(auto) operator*() { if constexpr (0==Iterator::cellr) return *ii.c.p; else return ii.c; }
  87. STLIterator & operator++()
  88. {
  89. if constexpr (0==Iterator::rank_s()) { // when rank==0, DIM_ANY check isn't enough
  90. ii.c.p = nullptr;
  91. } else if constexpr (DIM_ANY != ra::size_s<Iterator>()) {
  92. next_in_cube<Iterator::rank()-1, typename Iterator::lens, typename Iterator::steps>(i, ii.c.p);
  93. } else {
  94. next_in_cube(ii.rank(), ii.dimv, i, ii.c.p);
  95. }
  96. return *this;
  97. }
  98. };
  99. // --------------------
  100. // Helpers for slicing
  101. // --------------------
  102. // FIXME condition should be zero rank, maybe convertibility, not is_integral
  103. template <class I> constexpr bool is_scalar_index = ra::is_zero_or_scalar<I>;
  104. template <class I>
  105. struct is_beatable_def
  106. {
  107. constexpr static bool value = is_scalar_index<I>;
  108. constexpr static int skip_src = 1; // one position
  109. constexpr static int skip = 0; // rank 0
  110. constexpr static int add = -1; // relative to source rank
  111. constexpr static bool static_p = value; // can the beating be resolved statically?
  112. };
  113. template <class I> requires (is_iota<I>)
  114. struct is_beatable_def<I>
  115. {
  116. constexpr static bool value = !std::is_same_v<dim_c<DIM_BAD>, decltype(I::n)>; // FIXME more robust
  117. constexpr static int skip_src = 1; // one position
  118. constexpr static int skip = 1; // rank 1
  119. constexpr static int add = 0; // relative to source rank
  120. constexpr static bool static_p = false; // FIXME see Iota with ct N, S
  121. };
  122. template <int n>
  123. struct is_beatable_def<dots_t<n>>
  124. {
  125. constexpr static bool value = true;
  126. constexpr static int skip_src = n;
  127. constexpr static int skip = n;
  128. constexpr static int add = 0; // relative to source rank
  129. constexpr static bool static_p = true;
  130. };
  131. template <int n>
  132. struct is_beatable_def<insert_t<n>>
  133. {
  134. constexpr static bool value = true;
  135. constexpr static int skip_src = 0;
  136. constexpr static int skip = n;
  137. constexpr static int add = n; // relative to source rank
  138. constexpr static bool static_p = true;
  139. };
  140. template <class I> using is_beatable = is_beatable_def<std::decay_t<I>>;
  141. // --------------------
  142. // Develop indices for Small
  143. // --------------------
  144. namespace indexer0 {
  145. template <class lens, class steps, class P, rank_t end, rank_t k=0>
  146. constexpr dim_t index(P const & p)
  147. {
  148. if constexpr (k==end) {
  149. return 0;
  150. } else {
  151. RA_CHECK(inside(p[k], mp::ref<lens, k>::value));
  152. return (p[k] * mp::ref<steps, k>::value) + index<lens, steps, P, end, k+1>(p);
  153. }
  154. }
  155. template <class lens, class steps, class P>
  156. constexpr dim_t shorter(P const & p) // for Container::at().
  157. {
  158. static_assert(mp::len<lens> >= size_s<P>(), "Too many indices.");
  159. return index<lens, steps, P, size_s<P>()>(p);
  160. }
  161. template <class lens, class steps, class P>
  162. constexpr dim_t longer(P const & p) // for IteratorConcept::at().
  163. {
  164. if constexpr (RANK_ANY==size_s<P>()) {
  165. RA_CHECK(mp::len<lens> <= p.size(), "Too few indices.");
  166. } else {
  167. static_assert(mp::len<lens> <= size_s<P>(), "Too few indices.");
  168. }
  169. return index<lens, steps, P, mp::len<lens>>(p);
  170. }
  171. } // namespace indexer0
  172. // --------------------
  173. // Small iterator
  174. // --------------------
  175. // TODO Refactor with CellBig / STLIterator
  176. // Used by CellBig / CellSmall.
  177. template <class C>
  178. struct CellFlat
  179. {
  180. C c;
  181. constexpr void operator+=(dim_t const s) { c.p += s; }
  182. constexpr C & operator*() { return c; }
  183. };
  184. // V is always SmallBase<SmallView, ...>
  185. template <class V, rank_t cellr_spec=0>
  186. struct CellSmall
  187. {
  188. static_assert(cellr_spec!=RANK_ANY && cellr_spec!=RANK_BAD, "bad cell rank");
  189. constexpr static rank_t fullr = ra::rank_s<V>();
  190. constexpr static rank_t cellr = dependent_cell_rank(fullr, cellr_spec);
  191. constexpr static rank_t framer = dependent_frame_rank(fullr, cellr_spec);
  192. static_assert(cellr>=0 || cellr==RANK_ANY, "bad cell rank");
  193. static_assert(framer>=0 || framer==RANK_ANY, "bad frame rank");
  194. static_assert(choose_rank(fullr, cellr)==fullr, "bad cell rank");
  195. using cell_lens = mp::drop<typename V::lens, framer>;
  196. using cell_steps = mp::drop<typename V::steps, framer>;
  197. using lens = mp::take<typename V::lens, framer>; // these are steps on atom_type * p !!
  198. using steps = mp::take<typename V::steps, framer>;
  199. using atom_type = std::remove_reference_t<decltype(*(std::declval<V>().data()))>;
  200. using cell_type = SmallView<atom_type, cell_lens, cell_steps>;
  201. using value_type = std::conditional_t<0==cellr, atom_type, cell_type>;
  202. cell_type c;
  203. constexpr CellSmall(CellSmall const & ci): c { ci.c.p } {}
  204. // see STLIterator for the case of s_[0]=0, etc. [ra12].
  205. constexpr CellSmall(atom_type * p_): c { p_ } {}
  206. RA_DEF_ASSIGNOPS_DEFAULT_SET
  207. constexpr static rank_t rank_s() { return framer; }
  208. constexpr static rank_t rank() { return framer; }
  209. constexpr static dim_t len_s(int k) { RA_CHECK(inside(k, rank_s())); return V::len(k); }
  210. constexpr static dim_t len(int k) { RA_CHECK(inside(k, rank())); return V::len(k); }
  211. constexpr static dim_t step(int k) { return k<rank() ? V::step(k) : 0; }
  212. constexpr static bool keep_step(dim_t st, int z, int j) { return st*step(z)==step(j); }
  213. constexpr void adv(rank_t k, dim_t d) { c.p += step(k)*d; }
  214. constexpr auto
  215. flat() const
  216. {
  217. if constexpr (0==cellr) {
  218. return c.p;
  219. } else {
  220. return CellFlat<cell_type> { c };
  221. }
  222. }
  223. constexpr decltype(auto)
  224. at(auto const & i) const
  225. {
  226. if constexpr (0==cellr) {
  227. return c.p[indexer0::longer<lens, steps>(i)];
  228. } else {
  229. return cell_type(c.p + indexer0::longer<lens, steps>(i));
  230. }
  231. }
  232. };
  233. // --------------------
  234. // Base for both small view & container
  235. // --------------------
  236. template <class lens_, class steps_, class ... I>
  237. struct FilterDims
  238. {
  239. using lens = lens_;
  240. using steps = steps_;
  241. };
  242. template <class lens_, class steps_, class I0, class ... I>
  243. struct FilterDims<lens_, steps_, I0, I ...>
  244. {
  245. constexpr static int s = is_beatable<I0>::skip;
  246. constexpr static int s_src = is_beatable<I0>::skip_src;
  247. using next = FilterDims<mp::drop<lens_, s_src>, mp::drop<steps_, s_src>, I ...>;
  248. using lens = mp::append<mp::take<lens_, s>, typename next::lens>;
  249. using steps = mp::append<mp::take<steps_, s>, typename next::steps>;
  250. };
  251. template <template <class ...> class Child_, class T_, class lens_, class steps_>
  252. struct SmallBase
  253. {
  254. using lens = lens_;
  255. using steps = steps_;
  256. using T = T_;
  257. using Child = Child_<T, lens, steps>;
  258. template <class TT> using BadDimension = int_c<(TT::value<0 || TT::value==DIM_ANY || TT::value==DIM_BAD)>;
  259. static_assert(!mp::apply<mp::orb, mp::map<BadDimension, lens>>::value, "Negative dimensions.");
  260. static_assert(mp::len<lens> == mp::len<steps>, "Mismatched lengths & steps."); // TODO static check on steps.
  261. constexpr static rank_t rank() { return mp::len<lens>; }
  262. constexpr static rank_t rank_s() { return mp::len<lens>; }
  263. constexpr static auto slens = mp::tuple_values<std::array<dim_t, rank()>, lens>();
  264. constexpr static auto ssteps = mp::tuple_values<std::array<dim_t, rank()>, steps>();
  265. constexpr static dim_t size() { return mp::apply<mp::prod, lens>::value; }
  266. constexpr static dim_t size_s() { return size(); }
  267. constexpr static dim_t len(int k) { return slens[k]; }
  268. constexpr static dim_t len_s(int k) { return slens[k]; }
  269. constexpr static dim_t step(int k) { return ssteps[k]; }
  270. constexpr static decltype(auto) shape() { return SmallView<ra::dim_t const, mp::int_list<rank_s()>, mp::int_list<1>>(slens.data()); }
  271. // allowing rank 1 for coord types
  272. constexpr static bool convertible_to_scalar = (size()==1); // rank()==0 || (rank()==1 && size()==1);
  273. template <dim_t len0, dim_t step0>
  274. constexpr static dim_t
  275. select(dim_t i0)
  276. {
  277. RA_CHECK(inside(i0, len0), "i0 ", i0, " len0 ", len0);
  278. return i0*step0;
  279. };
  280. template <dim_t len0, dim_t step0, int n>
  281. constexpr static dim_t
  282. select(dots_t<n> i0)
  283. {
  284. static_assert(n>=0, "Stretch dots not yet supported in Small.");
  285. return 0;
  286. }
  287. template <class lens, class steps>
  288. constexpr static dim_t
  289. select_loop()
  290. {
  291. return 0;
  292. }
  293. template <class lens, class steps, class I0, class ... I>
  294. constexpr static dim_t
  295. select_loop(I0 i0, I ... i)
  296. {
  297. constexpr int s_src = is_beatable<I0>::skip_src;
  298. return select<mp::first<lens>::value, mp::first<steps>::value>(i0)
  299. + select_loop<mp::drop<lens, s_src>, mp::drop<steps, s_src>>(i ...);
  300. }
  301. #define RA_CONST_OR_NOT(CONST) \
  302. constexpr T CONST * data() CONST { return static_cast<Child CONST &>(*this).p; } \
  303. template <class ... I> \
  304. constexpr decltype(auto) \
  305. operator()(I && ... i) CONST \
  306. { \
  307. if constexpr ((0 + ... + is_scalar_index<I>)==rank()) { \
  308. return data()[select_loop<lens, steps>(i ...)]; \
  309. } else if constexpr ((is_beatable<I>::static_p && ...)) { \
  310. using FD = FilterDims<lens, steps, I ...>; \
  311. return SmallView<T CONST, typename FD::lens, typename FD::steps> \
  312. (data()+select_loop<lens, steps>(i ...)); \
  313. } else { /* TODO partial beating */ \
  314. return from(*this, std::forward<I>(i) ...); \
  315. } \
  316. } \
  317. template <class ... I> \
  318. constexpr decltype(auto) \
  319. operator[](I && ... i) CONST \
  320. { \
  321. return (*this)(std::forward<I>(i) ...); \
  322. } \
  323. /* BUG I must be fixed size, otherwise we can't make out the output type. */ \
  324. template <class I> \
  325. constexpr decltype(auto) \
  326. at(I const & i) CONST \
  327. { \
  328. return SmallView<T CONST, mp::drop<lens, ra::size_s<I>()>, mp::drop<steps, ra::size_s<I>()>> \
  329. (data()+indexer0::shorter<lens, steps>(i)); \
  330. } \
  331. /* TODO support s(static ra::iota) */ \
  332. template <int ss, int oo=0> \
  333. constexpr auto \
  334. as() CONST \
  335. { \
  336. static_assert(rank()>=1, "bad rank for as<>"); \
  337. static_assert(ss>=0 && oo>=0 && ss+oo<=size(), "bad size for as<>"); \
  338. return SmallView<T CONST, mp::cons<int_c<ss>, mp::drop1<lens>>, steps>(this->data()+oo*this->step(0)); \
  339. } \
  340. T CONST & \
  341. back() CONST \
  342. { \
  343. static_assert(rank()==1 && size()>0, "back() is not available"); \
  344. return (*this)[size()-1]; \
  345. } \
  346. constexpr operator T CONST & () CONST requires (convertible_to_scalar) { return data()[0]; }
  347. FOR_EACH(RA_CONST_OR_NOT, /*const*/, const)
  348. #undef RA_CONST_OR_NOT
  349. // see same thing for View.
  350. #define DEF_ASSIGNOPS(OP) \
  351. template <class X> \
  352. requires (!mp::is_tuple_v<std::decay_t<X>>) \
  353. constexpr Child & \
  354. operator OP(X && x) \
  355. { \
  356. ra::start(static_cast<Child &>(*this)) OP x; \
  357. return static_cast<Child &>(*this); \
  358. }
  359. FOR_EACH(DEF_ASSIGNOPS, =, *=, +=, -=, /=)
  360. #undef DEF_ASSIGNOPS
  361. // braces don't match X &&
  362. constexpr Child &
  363. operator=(nested_arg<T, lens> const & x)
  364. {
  365. ra::iter<-1>(static_cast<Child &>(*this)) = mp::from_tuple<std::array<typename nested_tuple<T, lens>::sub, len(0)>>(x);
  366. return static_cast<Child &>(*this);
  367. }
  368. // braces row-major ravel for rank!=1
  369. constexpr Child &
  370. operator=(ravel_arg<T, lens> const & x_)
  371. {
  372. auto x = mp::from_tuple<std::array<T, size()>>(x_);
  373. std::copy(x.begin(), x.end(), this->begin());
  374. return static_cast<Child &>(*this);
  375. }
  376. template <rank_t c=0> using iterator = ra::CellSmall<SmallBase<SmallView, T, lens, steps>, c>;
  377. template <rank_t c=0> using const_iterator = ra::CellSmall<SmallBase<SmallView, T const, lens, steps>, c>;
  378. template <rank_t c=0> constexpr iterator<c> iter() { return data(); }
  379. template <rank_t c=0> constexpr const_iterator<c> iter() const { return data(); }
  380. // FIXME extend for cellr!=0?
  381. constexpr static bool steps_default = std::same_as<steps, default_steps<lens>>;
  382. constexpr auto begin() const { if constexpr (steps_default) return data(); else return STLIterator(iter()); }
  383. constexpr auto begin() { if constexpr (steps_default) return data(); else return STLIterator(iter()); }
  384. constexpr auto end() const { if constexpr (steps_default) return data()+size(); else return STLIterator(const_iterator<0>(nullptr)); }
  385. constexpr auto end() { if constexpr (steps_default) return data()+size(); else return STLIterator(iterator<0>(nullptr)); }
  386. };
  387. // ---------------------
  388. // Small view & container
  389. // ---------------------
  390. // Strides are compile time, so we can put most members in the view type.
  391. template <class T, class lens, class steps>
  392. struct SmallView: public SmallBase<SmallView, T, lens, steps>
  393. {
  394. using Base = SmallBase<SmallView, T, lens, steps>;
  395. using Base::operator=;
  396. T * p;
  397. constexpr SmallView(T * p_): p(p_) {}
  398. constexpr SmallView(SmallView const & s) = default;
  399. constexpr operator T & () { static_assert(Base::convertible_to_scalar); return p[0]; }
  400. constexpr operator T const & () const { static_assert(Base::convertible_to_scalar); return p[0]; };
  401. using ViewConst = SmallView<T const, lens, steps>;
  402. constexpr operator ViewConst () const requires (!std::is_const_v<T>) { return ViewConst(p); }
  403. constexpr SmallView & view() { return *this; }
  404. constexpr SmallView const & view() const { return *this; }
  405. };
  406. #if defined (__clang__)
  407. template <class T, int N> using extvector __attribute__((ext_vector_type(N))) = T;
  408. #else
  409. template <class T, int N> using extvector __attribute__((vector_size(N*sizeof(T)))) = T;
  410. #endif
  411. template <class Z>
  412. struct equal_to_t
  413. {
  414. template <class ... T> constexpr static bool value = (std::is_same_v<Z, T> || ...);
  415. };
  416. template <class T, size_t N>
  417. consteval size_t
  418. align_req()
  419. {
  420. if constexpr (equal_to_t<T>::template value<char, unsigned char,
  421. short, unsigned short,
  422. int, unsigned int,
  423. long, unsigned long,
  424. long long, unsigned long long,
  425. float, double>
  426. && 0<N && 0==(N & (N-1))) {
  427. return alignof(extvector<T, N>);
  428. } else {
  429. return alignof(T[N]);
  430. }
  431. }
  432. template <class T, class lens, class steps, class ... nested_args, class ... ravel_args>
  433. struct
  434. #if RA_DO_OPT_SMALLVECTOR==1
  435. alignas(align_req<T, mp::apply<mp::prod, lens>::value>())
  436. #else
  437. #endif
  438. SmallArray<T, lens, steps, std::tuple<nested_args ...>, std::tuple<ravel_args ...>>
  439. : public SmallBase<SmallArray, T, lens, steps>
  440. {
  441. using Base = SmallBase<SmallArray, T, lens, steps>;
  442. using Base::rank, Base::size;
  443. T p[Base::size()]; // cf what std::array does for zero size; wish zero size just worked :-/
  444. constexpr SmallArray() {}
  445. // braces don't match (X &&)
  446. constexpr SmallArray(nested_args const & ... x)
  447. {
  448. static_cast<Base &>(*this) = nested_arg<T, lens> { x ... };
  449. }
  450. // braces row-major ravel for rank!=1
  451. constexpr SmallArray(ravel_args const & ... x)
  452. {
  453. static_cast<Base &>(*this) = ravel_arg<T, lens> { x ... };
  454. }
  455. // needed if T isn't registered as scalar [ra44]
  456. constexpr SmallArray(T const & t)
  457. {
  458. for (auto & x: p) { x = t; }
  459. }
  460. // X && x makes this a better match than nested_args ... for 1 argument.
  461. template <class X>
  462. requires (!std::is_same_v<std::decay_t<X>, T> && !mp::is_tuple_v<std::decay_t<X>>)
  463. constexpr SmallArray(X && x)
  464. {
  465. static_cast<Base &>(*this) = x;
  466. }
  467. using View = SmallView<T, lens, steps>;
  468. using ViewConst = SmallView<T const, lens, steps>;
  469. constexpr View view() { return View(p); }
  470. constexpr ViewConst view() const { return ViewConst(p); }
  471. // conversion to const
  472. constexpr operator View () { return View(p); }
  473. constexpr operator ViewConst () const { return ViewConst(p); }
  474. };
  475. template <class A0, class ... A>
  476. SmallArray(A0, A ...) -> SmallArray<A0, mp::int_list<1+sizeof...(A)>>;
  477. // FIXME remove the need, also of (S, begin, end) in Container, once nested_tuple constructors work.
  478. template <class A, class I, class J>
  479. A ravel_from_iterators(I && begin, J && end)
  480. {
  481. A a;
  482. std::copy(std::forward<I>(begin), std::forward<J>(end), a.begin());
  483. return a;
  484. }
  485. // ---------------------
  486. // Builtin arrays
  487. // ---------------------
  488. template <class T, class I=mp::iota<std::rank_v<T>>>
  489. struct builtin_array_lens;
  490. template <class T, int ... I>
  491. struct builtin_array_lens<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_lens_t = typename builtin_array_lens<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 lens = builtin_array_lens_t<A>;
  502. using view = SmallView<E, lens>;
  503. };
  504. // forward declared in bootstrap.hh.
  505. template <class T> requires (is_builtin_array<T>)
  506. 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> requires (is_builtin_array<T>)
  513. struct ra_traits_def<T>
  514. {
  515. using S = typename builtin_array_types<T>::view;
  516. constexpr static decltype(auto) shape(T const & t) { return S::shape(); }
  517. constexpr static dim_t size(T const & t) { return S::size_s(); }
  518. constexpr static dim_t size_s() { return S::size_s(); }
  519. constexpr static rank_t rank(T const & t) { return S::rank(); }
  520. constexpr static rank_t rank_s() { return S::rank_s(); }
  521. };
  522. RA_IS_DEF(cv_smallview, (std::is_convertible_v<A, SmallView<typename A::T, typename A::lens, typename A::steps>>));
  523. // --------------------
  524. // Small view ops; cf View ops in big.hh.
  525. // TODO Merge with Reframe (eg beat(reframe(a)) -> transpose(a) ?)
  526. // --------------------
  527. template <class A, class i>
  528. struct axis_indices
  529. {
  530. template <class T> using match_index = int_c<(T::value==i::value)>;
  531. using I = mp::iota<mp::len<A>>;
  532. using type = mp::Filter_<mp::map<match_index, A>, I>;
  533. // allow dead axes (e.g. transpose<1>(rank 1 array)).
  534. // static_assert((mp::len<type>)>0, "dst axis doesn't appear in transposed axes list");
  535. };
  536. template <class axes_list, class src_lens, class src_steps>
  537. struct axes_list_indices
  538. {
  539. static_assert(mp::len<axes_list> == mp::len<src_lens>, "Bad size for transposed axes list.");
  540. constexpr static int talmax = mp::fold<mp::max, void, axes_list>::value;
  541. constexpr static int talmin = mp::fold<mp::min, void, axes_list>::value;
  542. static_assert(talmin >= 0, "Bad index in transposed axes list.");
  543. // allow dead axes (e.g. transpose<1>(rank 1 array)).
  544. // static_assert(talmax < mp::len<src_lens>, "bad index in transposed axes list");
  545. template <class dst_i> struct dst_indices_
  546. {
  547. using type = typename axis_indices<axes_list, dst_i>::type;
  548. template <class i> using lensi = mp::ref<src_lens, i::value>;
  549. template <class i> using stepsi = mp::ref<src_steps, i::value>;
  550. using step = mp::fold<mp::sum, void, mp::map<stepsi, type>>;
  551. using len = mp::fold<mp::min, void, mp::map<lensi, type>>;
  552. };
  553. template <class dst_i> using dst_indices = typename dst_indices_<dst_i>::type;
  554. template <class dst_i> using dst_len = typename dst_indices_<dst_i>::len;
  555. template <class dst_i> using dst_step = typename dst_indices_<dst_i>::step;
  556. using dst = mp::iota<(talmax>=0 ? (1+talmax) : 0)>;
  557. using type = mp::map<dst_indices, dst>;
  558. using lens = mp::map<dst_len, dst>;
  559. using steps = mp::map<dst_step, dst>;
  560. };
  561. template <int ... Iarg, class A>
  562. requires (cv_smallview<A>)
  563. constexpr auto
  564. transpose(A && a_)
  565. {
  566. decltype(auto) a = a_.view();
  567. using AA = typename std::decay_t<decltype(a)>;
  568. using ti = axes_list_indices<mp::int_list<Iarg ...>, typename AA::lens, typename AA::steps>;
  569. return SmallView<typename AA::T, typename ti::lens, typename ti::steps>(a.data());
  570. };
  571. template <class A>
  572. requires (cv_smallview<A>)
  573. constexpr auto
  574. diag(A && a)
  575. {
  576. return transpose<0, 0>(a);
  577. }
  578. // TODO generalize
  579. template <class A1, class A2>
  580. requires (cv_smallview<A1> || cv_smallview<A2>)
  581. constexpr auto
  582. cat(A1 && a1_, A2 && a2_)
  583. {
  584. if constexpr (cv_smallview<A1> && cv_smallview<A2>) {
  585. decltype(auto) a1 = a1_.view();
  586. decltype(auto) a2 = a2_.view();
  587. static_assert(1==a1.rank() && 1==a2.rank(), "Bad ranks for cat."); // gcc accepts a1.rank(), etc.
  588. using T = std::common_type_t<std::decay_t<decltype(a1[0])>, std::decay_t<decltype(a2[0])>>;
  589. Small<T, a1.size()+a2.size()> val;
  590. std::copy(a1.begin(), a1.end(), val.begin());
  591. std::copy(a2.begin(), a2.end(), val.begin()+a1.size());
  592. return val;
  593. } else if constexpr (cv_smallview<A1> && is_scalar<A2>) {
  594. decltype(auto) a1 = a1_.view();
  595. static_assert(1==a1.rank(), "bad ranks for cat");
  596. using T = std::common_type_t<std::decay_t<decltype(a1[0])>, A2>;
  597. Small<T, a1.size()+1> val;
  598. std::copy(a1.begin(), a1.end(), val.begin());
  599. val[a1.size()] = a2_;
  600. return val;
  601. } else if constexpr (is_scalar<A1> && cv_smallview<A2>) {
  602. decltype(auto) a2 = a2_.view();
  603. static_assert(1==a2.rank(), "bad ranks for cat");
  604. using T = std::common_type_t<A1, std::decay_t<decltype(a2[0])>>;
  605. Small<T, 1+a2.size()> val;
  606. val[0] = a1_;
  607. std::copy(a2.begin(), a2.end(), val.begin()+1);
  608. return val;
  609. } else {
  610. static_assert(mp::always_false<A1, A2>); /* p2593r0 */ \
  611. }
  612. }
  613. // FIXME should be local (constexpr lambda + mp::apply?)
  614. template <int s>
  615. struct explode_divop
  616. {
  617. template <class T> struct op_
  618. {
  619. static_assert((T::value/s)*s==T::value);
  620. using type = int_c<T::value / s>;
  621. };
  622. template <class T> using op = typename op_<T>::type;
  623. };
  624. template <class super_t, class A>
  625. requires (cv_smallview<A>)
  626. constexpr auto
  627. explode(A && a_)
  628. {
  629. // the returned type has steps in super_t, but to support general steps we'd need steps in T. Maybe FIXME?
  630. decltype(auto) a = a_.view();
  631. using AA = std::decay_t<decltype(a)>;
  632. static_assert(super_t::steps_default);
  633. constexpr rank_t ra = AA::rank_s();
  634. constexpr rank_t rb = super_t::rank_s();
  635. static_assert(std::is_same_v<mp::drop<typename AA::lens, ra-rb>, typename super_t::lens>);
  636. static_assert(std::is_same_v<mp::drop<typename AA::steps, ra-rb>, typename super_t::steps>);
  637. using csteps = mp::map<explode_divop<ra::size_s<super_t>()>::template op, mp::take<typename AA::steps, ra-rb>>;
  638. return SmallView<super_t, mp::take<typename AA::lens, ra-rb>, csteps>((super_t *) a.data());
  639. }
  640. } // namespace ra