small.hh 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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. constexpr rank_t
  12. rank_sum(rank_t a, rank_t b) { return (ANY==a || ANY==b) ? ANY : a+b; }
  13. constexpr rank_t
  14. rank_diff(rank_t a, rank_t b) { return (ANY==a || ANY==b) ? ANY : a-b; }
  15. // cr>=0 is cell rank. -cr>0 is frame rank. TODO How to say frame rank 0.
  16. constexpr rank_t
  17. rank_cell(rank_t r, rank_t cr) { return cr>=0 ? cr /* indep */ : r==ANY ? ANY /* defer */ : (r+cr); }
  18. constexpr rank_t
  19. rank_frame(rank_t r, rank_t cr) { return r==ANY ? ANY /* defer */ : cr>=0 ? (r-cr) /* indep */ : -cr; }
  20. struct Dim { dim_t len, step; };
  21. inline std::ostream &
  22. operator<<(std::ostream & o, Dim const & dim) { return (o << "[Dim " << dim.len << " " << dim.step << "]"); }
  23. constexpr bool
  24. is_c_order_dimv(auto const & dimv, bool unitstep=true)
  25. {
  26. bool steps = true;
  27. dim_t s = 1;
  28. int k = dimv.size();
  29. if (!unitstep) {
  30. while (--k>=0 && 1==dimv[k].len) {}
  31. if (k<=0) { return true; }
  32. s = dimv[k].step*dimv[k].len;
  33. }
  34. while (--k>=0) {
  35. steps = steps && (1==dimv[k].len || dimv[k].step==s);
  36. s *= dimv[k].len;
  37. }
  38. return s==0 || steps;
  39. }
  40. constexpr bool
  41. is_c_order(auto const & v, bool unitstep=true) { return is_c_order_dimv(v.dimv, unitstep); }
  42. constexpr dim_t
  43. filldim(auto & dimv, auto && shape)
  44. {
  45. map(&Dim::len, dimv) = shape;
  46. dim_t s = 1;
  47. for (int k=dimv.size(); --k>=0;) {
  48. dimv[k].step = s;
  49. RA_CHECK(dimv[k].len>=0, "Bad len[", k, "] ", dimv[k].len, ".");
  50. s *= dimv[k].len;
  51. }
  52. return s;
  53. }
  54. // FIXME parameterize Small on dimv, then simplify.
  55. template <class lens>
  56. struct default_steps_
  57. {
  58. constexpr static int rank = mp::len<lens>;
  59. constexpr static auto dimv = [] { std::array<Dim, rank> dimv; filldim(dimv, mp::tuple2array<dim_t, lens>()); return dimv; } ();
  60. using type = decltype([] { return std::apply([](auto ... k) { return mp::int_list<dimv[k].step ...> {}; }, mp::iota<rank> {}); } ());
  61. };
  62. template <class lens> using default_steps = typename default_steps_<lens>::type;
  63. constexpr dim_t
  64. shape(auto const & v, auto && e)
  65. {
  66. dim_t k = with_len(ra::rank(v), RA_FWD(e));
  67. RA_CHECK(inside(k, ra::rank(v)), "Bad axis ", k, " for rank ", ra::rank(v), ".");
  68. return v.len(k);
  69. }
  70. template <class A>
  71. constexpr void
  72. resize(A & a, dim_t s)
  73. {
  74. if constexpr (ANY==size_s<A>()) {
  75. RA_CHECK(s>=0, "Bad resize ", s, ".");
  76. a.resize(s);
  77. } else {
  78. RA_CHECK(s==start(a).len(0) || BAD==s, "Bad resize ", s, " needing ", start(a).len(0), ".");
  79. }
  80. }
  81. // --------------------
  82. // slicing helpers
  83. // --------------------
  84. template <int n=BAD> struct dots_t { static_assert(n>=0 || BAD==n); };
  85. template <int n=BAD> constexpr dots_t<n> dots = dots_t<n>();
  86. constexpr auto all = dots<1>;
  87. template <int n> struct insert_t { static_assert(n>=0); };
  88. template <int n=1> constexpr insert_t<n> insert = insert_t<n>();
  89. template <class I> constexpr bool is_scalar_index = ra::is_zero_or_scalar<I>;
  90. struct beatable_t
  91. {
  92. bool rt, ct; // beatable at all and statically
  93. int src, dst, add; // axes on src, dst, and dst-src
  94. };
  95. template <class I> constexpr beatable_t beatable_def
  96. = { .rt=is_scalar_index<I>, .ct=is_scalar_index<I>, .src=1, .dst=0, .add=-1 };
  97. template <int n> constexpr beatable_t beatable_def<dots_t<n>>
  98. = { .rt=true, .ct = true, .src=n, .dst=n, .add=0 };
  99. template <int n> constexpr beatable_t beatable_def<insert_t<n>>
  100. = { .rt=true, .ct = true, .src=0, .dst=n, .add=n };
  101. template <class I> requires (is_iota<I>) constexpr beatable_t beatable_def<I>
  102. = { .rt=(BAD!=I::nn), .ct=std::decay_t<decltype(with_len(ic<1>, std::declval<I>()))>::constant,
  103. .src=1, .dst=1, .add=0 };
  104. template <class I> constexpr beatable_t beatable = beatable_def<std::decay_t<I>>;
  105. template <class II, int drop, class Op>
  106. constexpr decltype(auto)
  107. from_partial(Op && op)
  108. {
  109. if constexpr (drop==mp::len<II>) {
  110. return RA_FWD(op);
  111. } else {
  112. return wrank(mp::append<mp::makelist<drop, ic_t<0>>, mp::drop<II, drop>> {},
  113. from_partial<II, drop+1>(RA_FWD(op)));
  114. }
  115. }
  116. // TODO should be able to do better by slicing at each dimension, etc. But verb<>'s innermost op must be rank 0.
  117. template <class A, class ... I>
  118. constexpr decltype(auto)
  119. from(A && a, I && ... i)
  120. {
  121. if constexpr (0==sizeof...(i)) {
  122. return RA_FWD(a)();
  123. } else if constexpr (1==sizeof...(i)) {
  124. // support dynamic rank for 1 arg only (see test in test/from.cc).
  125. return map(RA_FWD(a), RA_FWD(i) ...);
  126. } else {
  127. return map(from_partial<mp::tuple<ic_t<rank_s<I>()> ...>, 1>(RA_FWD(a)), RA_FWD(i) ...);
  128. }
  129. }
  130. template <int k=0, class V>
  131. constexpr decltype(auto)
  132. maybe_len(V && v)
  133. {
  134. if constexpr (ANY!=std::decay_t<V>::len_s(k)) {
  135. return ic<std::decay_t<V>::len_s(k)>;
  136. } else {
  137. return v.len(k);
  138. }
  139. }
  140. template <int N, class KK=mp::iota<N>> struct unbeat;
  141. template <int N, int ... k>
  142. struct unbeat<N, mp::int_list<k ...>>
  143. {
  144. constexpr static decltype(auto)
  145. op(auto && v, auto && ... i)
  146. {
  147. return from(RA_FWD(v), with_len(maybe_len<k>(v), RA_FWD(i)) ...);
  148. }
  149. };
  150. // --------------------
  151. // develop indices
  152. // --------------------
  153. template <rank_t k, rank_t end>
  154. constexpr dim_t
  155. indexer(auto const & q, auto && pp, auto const & ss0, dim_t c)
  156. {
  157. if constexpr (k==end) {
  158. return c;
  159. } else {
  160. auto pk = *pp;
  161. RA_CHECK(inside(pk, q.len(k)) || (BAD==q.len(k) && 0==q.step(k)));
  162. return pp.mov(ss0), indexer<k+1, end>(q, pp, ss0, c + (q.step(k) * pk));
  163. }
  164. }
  165. constexpr dim_t
  166. indexer(rank_t end, auto const & q, auto && pp, auto const & ss0)
  167. {
  168. dim_t c = 0;
  169. for (rank_t k=0; k<end; ++k, pp.mov(ss0)) {
  170. auto pk = *pp;
  171. RA_CHECK(inside(pk, q.len(k)) || (BAD==q.len(k) && 0==q.step(k)));
  172. c += q.step(k) * pk;
  173. }
  174. return c;
  175. }
  176. template <class Q, class P>
  177. constexpr dim_t
  178. longer(Q const & q, P const & pp)
  179. {
  180. decltype(auto) p = start(pp);
  181. if constexpr (ANY==rank_s<P>()) {
  182. RA_CHECK(1==rank(p), "Bad rank ", rank(p), " for subscript.");
  183. } else {
  184. static_assert(1==rank_s<P>(), "Bad rank for subscript.");
  185. }
  186. if constexpr (ANY==size_s<P>() || ANY==rank_s<Q>()) {
  187. RA_CHECK(p.len(0) >= q.rank(), "Too few indices.");
  188. } else {
  189. static_assert(size_s<P>() >= rank_s<Q>(), "Too few indices.");
  190. }
  191. if constexpr (ANY==rank_s<Q>()) {
  192. return indexer(q.rank(), q, p, p.step(0));
  193. } else {
  194. return indexer<0, rank_s<Q>()>(q, p, p.step(0), 0);
  195. }
  196. }
  197. // --------------------
  198. // view iterators. TODO Take iterator like Ptr does and Views should, not raw pointers
  199. // --------------------
  200. template <auto f, auto dimv, int cellr, int framer=0>
  201. using ctuple = decltype(std::apply([](auto ... i) { return mp::int_list<std::invoke(f, dimv[i]) ...> {}; }, mp::iota<cellr, framer> {}));
  202. template <class lens, class steps>
  203. constexpr static auto cdimv = mp::tuple2array<Dim, mp::zip<lens, steps>, [](auto i) { return std::make_from_tuple<Dim>(i); }>();
  204. template <class T, class lens, class steps> struct ViewSmall;
  205. template <class T, class Dimv, class Spec>
  206. struct CellSmall
  207. {
  208. constexpr static auto dimv = Dimv::value;
  209. constexpr static rank_t spec = Spec::value;
  210. constexpr static rank_t fullr = ssize(dimv);
  211. constexpr static rank_t cellr = rank_cell(fullr, spec);
  212. constexpr static rank_t framer = rank_frame(fullr, spec);
  213. static_assert(spec!=ANY && spec!=BAD && choose_rank(fullr, cellr)==fullr, "Bad cell rank.");
  214. // FIXME Small take dimv instead of lens/steps
  215. using ctype = ViewSmall<T, ctuple<&Dim::len, dimv, cellr, framer>, ctuple<&Dim::step, dimv, cellr, framer>>;
  216. ctype c;
  217. consteval static rank_t rank() { return framer; }
  218. constexpr static dim_t len(int k) { return dimv[k].len; } // len(0<=k<rank) or step(0<=k)
  219. constexpr static dim_t len_s(int k) { return len(k); }
  220. constexpr static dim_t step(int k) { return k<rank() ? dimv[k].step : 0; }
  221. constexpr static bool keep_step(dim_t st, int z, int j) { return st*step(z)==step(j); }
  222. constexpr CellSmall(T * p): c { p } {}
  223. };
  224. template <class T, rank_t RANK=ANY> struct ViewBig;
  225. template <class T, class Dimv, class Spec>
  226. struct CellBig
  227. {
  228. constexpr static rank_t spec = maybe_any<Spec>;
  229. constexpr static rank_t fullr = size_s<Dimv>();
  230. constexpr static rank_t cellr = is_constant<Spec> ? rank_cell(fullr, spec) : ANY;
  231. constexpr static rank_t framer = is_constant<Spec> ? rank_frame(fullr, spec) : ANY;
  232. using ctype = ViewBig<T, cellr>;
  233. Dimv dimv;
  234. ctype c;
  235. [[no_unique_address]] Spec const dspec = {};
  236. consteval static rank_t rank() requires (ANY!=framer) { return framer; }
  237. constexpr rank_t rank() const requires (ANY==framer) { return rank_frame(dimv.size(), dspec); }
  238. constexpr dim_t len(int k) const { return dimv[k].len; } // len(0<=k<rank) or step(0<=k)
  239. constexpr static dim_t len_s(int k) { return ANY; }
  240. constexpr dim_t step(int k) const { return k<rank() ? dimv[k].step : 0; }
  241. constexpr bool keep_step(dim_t st, int z, int j) const { return st*step(z)==step(j); }
  242. constexpr CellBig(T * cp, Dimv const & dimv_, Spec dspec_ = Spec {})
  243. : dimv(dimv_), dspec(dspec_)
  244. {
  245. c.cp = cp;
  246. rank_t dcellr = rank_cell(dimv.size(), dspec);
  247. rank_t dframer = rank();
  248. RA_CHECK(0<=dframer && 0<=dcellr, "Bad cell rank ", dcellr, " for array rank ", ssize(dimv), ").");
  249. resize(c.dimv, dcellr);
  250. for (int k=0; k<dcellr; ++k) {
  251. c.dimv[k] = dimv[dframer+k];
  252. }
  253. }
  254. };
  255. template <class T, class Dimv, class Spec>
  256. struct Cell: public std::conditional_t<is_constant<Dimv>, CellSmall<T, Dimv, Spec>, CellBig<T, Dimv, Spec>>
  257. {
  258. using Base = std::conditional_t<is_constant<Dimv>, CellSmall<T, Dimv, Spec>, CellBig<T, Dimv, Spec>>;
  259. using Base::Base, Base::cellr, Base::framer, Base::c, Base::step;
  260. using ctype = Base::ctype;
  261. static_assert(cellr>=0 || cellr==ANY, "Bad cell rank.");
  262. static_assert(framer>=0 || framer==ANY, "Bad frame rank.");
  263. RA_ASSIGNOPS_SELF(Cell)
  264. RA_ASSIGNOPS_DEFAULT_SET
  265. constexpr decltype(auto) at(auto const & i) const requires (0==cellr) { return c.cp[longer(*this, i)]; }
  266. constexpr decltype(auto) at(auto const & i) const requires (0!=cellr) { ctype cc(c); cc.cp += longer(*this, i); return cc; }
  267. constexpr void adv(rank_t k, dim_t d) { c.cp += step(k)*d; }
  268. constexpr decltype(auto) operator*() const requires (0==cellr) { return *(c.cp); }
  269. constexpr ctype const & operator*() const requires (0!=cellr) { return c; }
  270. constexpr auto save() const { return c.cp; }
  271. constexpr void load(decltype(c.cp) cp) { c.cp = cp; }
  272. constexpr void mov(dim_t d) { c.cp += d; }
  273. };
  274. // ---------------------
  275. // nested braces for Small initializers. Cf braces_def for in big.hh.
  276. // ---------------------
  277. template <class T, class lens>
  278. struct nested_arg { using sub = noarg; };
  279. template <class T, class lens>
  280. struct small_args { using nested = std::tuple<noarg>; };
  281. template <class T, class lens> requires (0<mp::len<lens>)
  282. struct small_args<T, lens>
  283. {
  284. using nested = mp::makelist<mp::ref<lens, 0>::value, typename nested_arg<T, lens>::sub>;
  285. };
  286. template <class T, class lens, class steps, class nested_args = small_args<T, lens>::nested>
  287. struct SmallArray;
  288. template <class T, dim_t ... lens>
  289. using Small = SmallArray<T, mp::int_list<lens ...>, default_steps<mp::int_list<lens ...>>>;
  290. template <class T, int S0, int ... S>
  291. struct nested_arg<T, mp::int_list<S0, S ...>>
  292. {
  293. using sub = std::conditional_t<0==sizeof...(S), T, Small<T, S ...>>;
  294. };
  295. // ---------------------
  296. // Small view & container
  297. // ---------------------
  298. template <class lens_, class steps_, class ... I>
  299. struct FilterDims
  300. {
  301. using lens = lens_;
  302. using steps = steps_;
  303. };
  304. template <class lens_, class steps_, class I0, class ... I> requires (!is_iota<I0>)
  305. struct FilterDims<lens_, steps_, I0, I ...>
  306. {
  307. constexpr static bool stretch = (beatable<I0>.dst==BAD);
  308. static_assert(!stretch || ((beatable<I>.dst!=BAD) && ...), "Cannot repeat stretch index.");
  309. constexpr static int dst = stretch ? (mp::len<lens_> - (0 + ... + beatable<I>.src)) : beatable<I0>.dst;
  310. constexpr static int src = stretch ? (mp::len<lens_> - (0 + ... + beatable<I>.src)) : beatable<I0>.src;
  311. using next = FilterDims<mp::drop<lens_, src>, mp::drop<steps_, src>, I ...>;
  312. using lens = mp::append<mp::take<lens_, dst>, typename next::lens>;
  313. using steps = mp::append<mp::take<steps_, dst>, typename next::steps>;
  314. };
  315. template <class lens_, class steps_, class I0, class ... I> requires (is_iota<I0>)
  316. struct FilterDims<lens_, steps_, I0, I ...>
  317. {
  318. constexpr static int dst = beatable<I0>.dst;
  319. constexpr static int src = beatable<I0>.src;
  320. using next = FilterDims<mp::drop<lens_, src>, mp::drop<steps_, src>, I ...>;
  321. using lens = mp::append<mp::int_list<I0::nn>, typename next::lens>;
  322. using steps = mp::append<mp::int_list<(mp::ref<steps_, 0>::value * I0::gets())>, typename next::steps>;
  323. };
  324. template <class T_, class lens_, class steps_>
  325. struct SmallBase
  326. {
  327. using lens = lens_;
  328. using steps = steps_;
  329. using T = T_;
  330. static_assert(mp::len<lens> == mp::len<steps>, "Mismatched lengths & steps.");
  331. consteval static rank_t rank() { return mp::len<lens>; }
  332. constexpr static auto dimv = cdimv<lens, steps>;
  333. constexpr static auto theshape = mp::tuple2array<dim_t, lens>();
  334. consteval static dim_t size() { return std::apply([](auto ... s) { return (s * ... * 1); }, theshape); }
  335. constexpr static dim_t len(int k) { return dimv[k].len; }
  336. consteval static dim_t size_s() { return size(); }
  337. constexpr static dim_t len_s(int k) { return len(k); }
  338. constexpr static dim_t step(int k) { return dimv[k].step; }
  339. consteval static decltype(auto) shape() { return theshape; }
  340. // TODO check steps
  341. static_assert(std::apply([](auto ... s) { return ((0<=s) && ...); }, theshape), "Bad shape.");
  342. constexpr static dim_t len0 = rank()>0 ? len(0) : 0;
  343. constexpr static bool defsteps = is_c_order_dimv(dimv);
  344. };
  345. template <class T, class lens, class steps>
  346. struct ViewSmall: public SmallBase<T, lens, steps>
  347. {
  348. using Base = SmallBase<T, lens, steps>;
  349. using Base::rank, Base::size, Base::dimv;
  350. using Base::len, Base::len_s, Base::step, Base::len0, Base::defsteps;
  351. using sub = typename nested_arg<T, lens>::sub;
  352. T * cp;
  353. template <rank_t c=0> using iterator = Cell<T, ic_t<dimv>, ic_t<c>>;
  354. using ViewConst = ViewSmall<T const, lens, steps>;
  355. constexpr operator ViewConst () const requires (!std::is_const_v<T>) { return ViewConst(cp); }
  356. constexpr ViewSmall const & view() const { return *this; }
  357. constexpr ViewSmall(T * cp_): cp(cp_) {}
  358. // cf RA_ASSIGNOPS_SELF [ra38] [ra34]
  359. ViewSmall const & operator=(ViewSmall const & x) const { start(*this) = x; return *this; }
  360. constexpr ViewSmall(ViewSmall const & s) = default;
  361. template <class X> requires (!std::is_same_v<std::decay_t<X>, T>)
  362. constexpr ViewSmall const & operator=(X && x) const { start(*this) = x; return *this; }
  363. #define ASSIGNOPS(OP) \
  364. constexpr ViewSmall const & operator OP(auto && x) const { start(*this) OP x; return *this; }
  365. FOR_EACH(ASSIGNOPS, *=, +=, -=, /=)
  366. #undef ASSIGNOPS
  367. // needed if T isn't registered as scalar [ra44]
  368. constexpr ViewSmall const &
  369. operator=(T const & t) const
  370. {
  371. start(*this) = ra::scalar(t); return *this;
  372. }
  373. // nested braces
  374. constexpr ViewSmall const &
  375. operator=(sub (&&x)[len0]) const requires (0<rank() && 0!=len0 && (1!=rank() || 1!=len0))
  376. {
  377. ra::iter<-1>(*this) = x; return *this;
  378. }
  379. // row-major ravel braces
  380. constexpr ViewSmall const &
  381. operator=(T (&&x)[size()]) const requires ((rank()>1) && (size()>1))
  382. {
  383. std::ranges::copy(std::ranges::subrange(x), begin()); return *this;
  384. }
  385. template <int k>
  386. constexpr static dim_t
  387. select(dim_t i)
  388. {
  389. RA_CHECK(inside(i, len(k)), "Bad index ", i, " in len[", k, "]=", len(k), ".");
  390. return step(k)*i;
  391. }
  392. template <int k>
  393. constexpr static dim_t
  394. select(is_iota auto i)
  395. {
  396. if constexpr ((1>=i.n ? 1 : (i.s<0 ? -i.s : i.s)*(i.n-1)+1) > len(k)) { // FIXME c++23 std::abs
  397. static_assert(always_false<k>, "Bad index.");
  398. } else {
  399. RA_CHECK(inside(i, len(k)), "Bad index iota [", i.n, " ", i.i, " ", i.s, "] in len[", k, "]=", len(k), ".");
  400. }
  401. return 0==i.n ? 0 : step(k)*i.i;
  402. }
  403. template <int k, int n>
  404. constexpr static dim_t
  405. select(dots_t<n> i)
  406. {
  407. return 0;
  408. }
  409. template <int k, class I0, class ... I>
  410. constexpr static dim_t
  411. select_loop(I0 && i0, I && ... i)
  412. {
  413. constexpr int nn = (BAD==beatable<I0>.src) ? (rank() - k - (0 + ... + beatable<I>.src)) : beatable<I0>.src;
  414. return select<k>(with_len(ic<len(k)>, RA_FWD(i0)))
  415. + select_loop<k + nn>(RA_FWD(i) ...);
  416. }
  417. template <int k>
  418. consteval static dim_t
  419. select_loop() { return 0; }
  420. template <class ... I>
  421. constexpr decltype(auto)
  422. operator()(I && ... i) const
  423. {
  424. constexpr int stretch = (0 + ... + (beatable<I>.dst==BAD));
  425. static_assert(stretch<=1, "Cannot repeat stretch index.");
  426. if constexpr ((0 + ... + is_scalar_index<I>)==rank()) {
  427. return cp[select_loop<0>(i ...)];
  428. // FIXME with_len before this, cf is_constant_iota
  429. } else if constexpr ((beatable<I>.ct && ...)) {
  430. using FD = FilterDims<lens, steps, std::decay_t<I> ...>;
  431. return ViewSmall<T, typename FD::lens, typename FD::steps> (cp + select_loop<0>(i ...));
  432. // TODO partial beating
  433. } else {
  434. // FIXME must forward *this so that expr can hold to it (c++23 deducing this).
  435. // Container's view is self so we get away with a ref, but here we create new temp views on every Small::view() call.
  436. return unbeat<sizeof...(I)>::op(ViewSmall(*this), RA_FWD(i) ...);
  437. }
  438. }
  439. constexpr decltype(auto)
  440. operator[](auto && ... i) const { return (*this)(RA_FWD(i) ...); } // see above about forwarding
  441. template <class I>
  442. constexpr decltype(auto)
  443. at(I && i) const
  444. {
  445. // can't say 'frame rank 0' so -size wouldn't work. What about ra::len
  446. constexpr rank_t crank = rank_diff(rank(), ra::size_s<I>());
  447. static_assert(crank>=0); // to make out the output type
  448. return iter<crank>().at(RA_FWD(i));
  449. }
  450. // maybe remove if ic becomes easier to use
  451. template <int ss, int oo=0> constexpr auto as() const { return operator()(ra::iota(ra::ic<ss>, oo)); }
  452. constexpr T * data() const { return cp; }
  453. template <rank_t c=0> constexpr iterator<c> iter() const { return cp; }
  454. constexpr auto begin() const { if constexpr (defsteps) return cp; else return STLIterator(iter()); }
  455. constexpr auto end() const requires (defsteps) { return cp+size(); }
  456. constexpr static auto end() requires (!defsteps) { return std::default_sentinel; }
  457. constexpr T & back() const { static_assert(rank()>=1 && size()>0, "No back()."); return cp[size()-1]; }
  458. constexpr operator T & () const { static_assert(1==size(), "Bad conversion to scalar."); return cp[0]; }
  459. };
  460. #if defined (__clang__)
  461. template <class T, int N> using extvector __attribute__((ext_vector_type(N))) = T;
  462. #else
  463. template <class T, int N> using extvector __attribute__((vector_size(N*sizeof(T)))) = T;
  464. #endif
  465. template <class Z, class ... T> constexpr static bool equal_to_any = (std::is_same_v<Z, T> || ...);
  466. template <class T, size_t N>
  467. consteval size_t
  468. align_req()
  469. {
  470. if constexpr (equal_to_any<T, char, unsigned char, short, unsigned short,
  471. int, unsigned int, long, unsigned long, long long, unsigned long long,
  472. float, double>
  473. && 0<N && 0==(N & (N-1))) {
  474. return alignof(extvector<T, N>);
  475. } else {
  476. return alignof(T[N]);
  477. }
  478. }
  479. template <class T, class lens, class steps, class ... nested_args>
  480. struct
  481. #if RA_DO_OPT_SMALLVECTOR==1
  482. alignas(align_req<T, std::apply([](auto && ... i) { return (i * ... * 1); }, lens {})>())
  483. #else
  484. #endif
  485. SmallArray<T, lens, steps, std::tuple<nested_args ...>>
  486. : public SmallBase<T, lens, steps>
  487. {
  488. using Base = SmallBase<T, lens, steps>;
  489. using Base::rank, Base::size, Base::len0;
  490. T cp[size()]; // cf what std::array does for zero size; wish zero size just worked :-/
  491. using View = ViewSmall<T, lens, steps>;
  492. using ViewConst = ViewSmall<T const, lens, steps>;
  493. constexpr View view() { return View(cp); }
  494. constexpr ViewConst view() const { return ViewConst(cp); }
  495. constexpr operator View () { return View(cp); }
  496. constexpr operator ViewConst () const { return ViewConst(cp); }
  497. constexpr SmallArray() {}
  498. constexpr SmallArray(ra::none_t) {}
  499. // needed if T isn't registered as scalar [ra44]
  500. constexpr SmallArray(T const & t)
  501. {
  502. for (auto & x: cp) { x = t; }
  503. }
  504. // nested braces FIXME p1219??
  505. constexpr SmallArray(nested_args const & ... x)
  506. requires ((0<rank() && 0!=Base::len(0) && (1!=rank() || 1!=Base::len(0))))
  507. {
  508. view() = { x ... };
  509. }
  510. // row-major ravel braces
  511. constexpr SmallArray(T const & x0, std::convertible_to<T> auto const & ... x)
  512. requires ((rank()>1) && (size()>1) && ((1+sizeof...(x))==size()))
  513. {
  514. view() = { static_cast<T>(x0), static_cast<T>(x) ... };
  515. }
  516. // X && x makes this a better match than nested_args ... for 1 argument.
  517. template <class X> requires (!std::is_same_v<std::decay_t<X>, T>)
  518. constexpr SmallArray(X && x)
  519. {
  520. view() = RA_FWD(x);
  521. }
  522. #define ASSIGNOPS(OP) \
  523. constexpr decltype(auto) operator OP(auto && x) { view() OP RA_FWD(x); return *this; }
  524. FOR_EACH(ASSIGNOPS, =, *=, +=, -=, /=)
  525. #undef ASSIGNOPS
  526. #define RA_CONST_OR_NOT(CONST) \
  527. constexpr T CONST & back() CONST { return view().back(); } \
  528. constexpr T CONST * data() CONST { return view().data(); } \
  529. constexpr operator T CONST & () CONST { return view(); } \
  530. constexpr decltype(auto) operator()(auto && ... a) CONST { return view()(RA_FWD(a) ...); } \
  531. constexpr decltype(auto) operator[](auto && ... a) CONST { return view()(RA_FWD(a) ...); } \
  532. constexpr decltype(auto) at(auto && i) CONST { return view().at(RA_FWD(i)); } \
  533. template <int ss, int oo=0> constexpr decltype(auto) as() CONST { return view().template as<ss, oo>(); } \
  534. template <rank_t c=0> constexpr auto iter() CONST { return view().template iter<c>(); } \
  535. constexpr auto begin() CONST { return view().begin(); } \
  536. constexpr auto end() CONST { return view().end(); }
  537. FOR_EACH(RA_CONST_OR_NOT, /*not const*/, const)
  538. #undef RA_CONST_OR_NOT
  539. };
  540. template <class A0, class ... A> SmallArray(A0, A ...) -> Small<A0, 1+sizeof...(A)>;
  541. // FIXME tagged ravel constructor. Then we can pass any rank 1 thing not just iterator pairs.
  542. template <class A>
  543. constexpr auto
  544. ravel_from_iterators(auto && begin, auto && end)
  545. {
  546. A a;
  547. std::copy(RA_FWD(begin), RA_FWD(end), a.begin());
  548. return a;
  549. }
  550. // ---------------------
  551. // builtin arrays.
  552. // ---------------------
  553. template <class T>
  554. constexpr auto
  555. peel(T && t)
  556. {
  557. static_assert(0 < std::extent_v<std::remove_cvref_t<T>, 0>);
  558. if constexpr (1 < std::rank_v<std::remove_cvref_t<T>>) {
  559. return peel(*std::data(RA_FWD(t)));
  560. } else {
  561. return std::data(t);
  562. }
  563. }
  564. constexpr auto
  565. start(is_builtin_array auto && t)
  566. {
  567. using A = std::remove_reference_t<decltype(t)>; // preserve const
  568. using lens = decltype(std::apply([](auto ... i) { return mp::int_list<std::extent_v<A, i> ...> {}; },
  569. mp::iota<std::rank_v<A>> {}));
  570. return ViewSmall<std::remove_all_extents_t<A>, lens, default_steps<lens>>(peel(t)).iter();
  571. }
  572. // --------------------
  573. // Small view ops, see View ops in big.hh.
  574. // FIXME Merge transpose(ViewBig), Reframe (eg beat(reframe(a)) -> transpose(a) ?)
  575. // --------------------
  576. constexpr void
  577. transpose_filldim(auto const & s, auto const & src, auto & dst)
  578. {
  579. std::ranges::fill(dst, Dim { BAD, 0 });
  580. for (int k=0; int sk: s) {
  581. dst[sk].step += src[k].step;
  582. dst[sk].len = dst[sk].len>=0 ? std::min(dst[sk].len, src[k].len) : src[k].len;
  583. ++k;
  584. }
  585. }
  586. RA_IS_DEF(cv_smallview, (std::is_convertible_v<A, ViewSmall<typename A::T, typename A::lens, typename A::steps>>));
  587. template <int ... Iarg>
  588. constexpr auto
  589. transpose(cv_smallview auto && a_)
  590. {
  591. decltype(auto) a = a_.view();
  592. using AA = typename std::decay_t<decltype(a)>;
  593. constexpr std::array<dim_t, sizeof...(Iarg)> s = { Iarg ... };
  594. constexpr auto src = cdimv<typename AA::lens, typename AA::steps>;
  595. static_assert(src.size()==s.size(), "Bad size for transposed axes list.");
  596. constexpr rank_t dstrank = (0==ra::size(s)) ? 0 : 1 + *std::ranges::max_element(s);
  597. constexpr auto dst = [&]() { std::array<Dim, dstrank> dst; transpose_filldim(s, src, dst); return dst; }();
  598. return ViewSmall<typename AA::T, ctuple<&Dim::len, dst, dstrank>, ctuple<&Dim::step, dst, dstrank>>(a.data());
  599. }
  600. constexpr auto
  601. diag(cv_smallview auto && a)
  602. {
  603. return transpose<0, 0>(a);
  604. }
  605. template <class super_t>
  606. constexpr auto
  607. explode(cv_smallview auto && a_)
  608. {
  609. // result has steps in super_t, but to support general steps we'd need steps in T. FIXME?
  610. decltype(auto) a = a_.view();
  611. using AA = std::decay_t<decltype(a)>;
  612. static_assert(super_t::defsteps);
  613. constexpr rank_t ra = ra::rank_s<AA>();
  614. constexpr rank_t rb = rank_s<super_t>();
  615. static_assert(std::is_same_v<mp::drop<typename AA::lens, ra-rb>, typename super_t::lens>);
  616. static_assert(std::is_same_v<mp::drop<typename AA::steps, ra-rb>, typename super_t::steps>);
  617. constexpr dim_t supers = ra::size_s<super_t>();
  618. using csteps = decltype(std::apply([](auto ... i)
  619. {
  620. static_assert(((i==(i/supers)*supers) && ...));
  621. return mp::int_list<(i/supers) ...> {};
  622. }, mp::take<typename AA::steps, ra-rb> {}));
  623. return ViewSmall<super_t, mp::take<typename AA::lens, ra-rb>, csteps>((super_t *) a.data());
  624. }
  625. // TODO generalize
  626. template <class A1, class A2> requires (cv_smallview<A1> || cv_smallview<A2>)
  627. constexpr auto
  628. cat(A1 && a1_, A2 && a2_)
  629. {
  630. if constexpr (cv_smallview<A1> && cv_smallview<A2>) {
  631. decltype(auto) a1 = a1_.view();
  632. decltype(auto) a2 = a2_.view();
  633. static_assert(1==a1.rank() && 1==a2.rank(), "Bad ranks for cat.");
  634. using T = std::common_type_t<std::decay_t<decltype(a1[0])>, std::decay_t<decltype(a2[0])>>;
  635. Small<T, a1.size()+a2.size()> val;
  636. std::copy(a1.begin(), a1.end(), val.begin());
  637. std::copy(a2.begin(), a2.end(), val.begin()+a1.size());
  638. return val;
  639. } else if constexpr (cv_smallview<A1> && is_scalar<A2>) {
  640. decltype(auto) a1 = a1_.view();
  641. static_assert(1==a1.rank(), "Bad ranks for cat.");
  642. using T = std::common_type_t<std::decay_t<decltype(a1[0])>, decltype(a2_)>;
  643. Small<T, a1.size()+1> val;
  644. std::copy(a1.begin(), a1.end(), val.begin());
  645. val[a1.size()] = a2_;
  646. return val;
  647. } else if constexpr (is_scalar<A1> && cv_smallview<A2>) {
  648. decltype(auto) a2 = a2_.view();
  649. static_assert(1==a2.rank(), "Bad ranks for cat.");
  650. using T = std::common_type_t<decltype(a1_), std::decay_t<decltype(a2[0])>>;
  651. Small<T, 1+a2.size()> val;
  652. val[0] = a1_;
  653. std::copy(a2.begin(), a2.end(), val.begin()+1);
  654. return val;
  655. } else {
  656. static_assert(always_false<A1, A2>);
  657. }
  658. }
  659. } // namespace ra