big.hh 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra - Arrays with dynamic lengths/strides, cf small.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 "small.hh"
  10. #include <memory>
  11. #include <complex> // for View ops
  12. namespace ra {
  13. // --------------------
  14. // Big iterator
  15. // --------------------
  16. template <class T, rank_t RANK=ANY> struct View;
  17. // TODO Refactor with CellSmall. Clear up Dimv's type. Should I use span/Ptr?
  18. // TODO Take iterator like Ptr does and View should, not raw pointers
  19. template <class T, class Dimv, class Spec=ic_t<0>>
  20. struct CellBig
  21. {
  22. constexpr static rank_t spec = maybe_any<Spec>;
  23. constexpr static rank_t fullr = size_s<Dimv>();
  24. constexpr static rank_t cellr = is_constant<Spec> ? rank_cell(fullr, spec) : ANY;
  25. constexpr static rank_t framer = is_constant<Spec> ? rank_frame(fullr, spec) : ANY;
  26. static_assert(cellr>=0 || cellr==ANY, "Bad cell rank.");
  27. static_assert(framer>=0 || framer==ANY, "Bad frame rank.");
  28. using ctype = View<T, cellr>;
  29. using value_type = std::conditional_t<0==cellr, T, ctype>;
  30. ctype c;
  31. Dimv dimv;
  32. [[no_unique_address]] Spec const dspec = {};
  33. consteval static rank_t rank() requires (ANY!=framer) { return framer; }
  34. constexpr rank_t rank() const requires (ANY==framer) { return rank_frame(std::ssize(dimv), dspec); }
  35. #pragma GCC diagnostic push // gcc 12.2 and 13.2 with RA_DO_CHECK=0 and -fno-sanitize=all
  36. #pragma GCC diagnostic warning "-Warray-bounds"
  37. constexpr dim_t len(int k) const { return dimv[k].len; } // len(0<=k<rank) or step(0<=k)
  38. #pragma GCC diagnostic pop
  39. constexpr static dim_t len_s(int k) { return ANY; }
  40. constexpr dim_t step(int k) const { return k<rank() ? dimv[k].step : 0; }
  41. constexpr void adv(rank_t k, dim_t d) { c.cp += step(k)*d; }
  42. constexpr bool keep_step(dim_t st, int z, int j) const { return st*step(z)==step(j); }
  43. // see STLIterator for len(0)=0, etc. [ra12].
  44. constexpr CellBig(T * cp, Dimv const & dimv_, Spec dspec_ = Spec {})
  45. : dimv(dimv_), dspec(dspec_)
  46. {
  47. c.cp = cp;
  48. rank_t dcellr = rank_cell(std::ssize(dimv), dspec);
  49. rank_t dframer = this->rank();
  50. RA_CHECK(0<=dframer && 0<=dcellr, "Bad cell rank ", dcellr, " for array rank ", ssize(dimv), ").");
  51. resize(c.dimv, dcellr);
  52. for (int k=0; k<dcellr; ++k) {
  53. c.dimv[k] = dimv[dframer+k];
  54. }
  55. }
  56. RA_ASSIGNOPS_SELF(CellBig)
  57. RA_ASSIGNOPS_DEFAULT_SET
  58. constexpr decltype(auto)
  59. at(auto const & i) const
  60. {
  61. auto d = longer(*this, i);
  62. if constexpr (0==cellr) {
  63. return c.cp[d];
  64. } else {
  65. ctype cc(c); cc.cp += d;
  66. return cc;
  67. }
  68. }
  69. constexpr decltype(auto) operator*() const requires (0==cellr) { return *(c.cp); }
  70. constexpr ctype const & operator*() const requires (0!=cellr) { return c; }
  71. constexpr auto save() const{ return c.cp; }
  72. constexpr void load(decltype(c.cp) cp) { c.cp = cp; }
  73. constexpr void mov(dim_t d) { c.cp += d; }
  74. };
  75. // --------------------
  76. // nested braces for Container initializers, cf small_args in small.hh. FIXME Let any expr = braces.
  77. // --------------------
  78. template <class T, rank_t rank>
  79. struct braces_def { using type = noarg; };
  80. template <class T, rank_t rank>
  81. using braces = braces_def<T, rank>::type;
  82. template <class T, rank_t rank> requires (rank==1)
  83. struct braces_def<T, rank> { using type = std::initializer_list<T>; };
  84. template <class T, rank_t rank> requires (rank>1)
  85. struct braces_def<T, rank> { using type = std::initializer_list<braces<T, rank-1>>; };
  86. template <int i, class T, rank_t rank>
  87. constexpr dim_t
  88. braces_len(braces<T, rank> const & l)
  89. {
  90. if constexpr (i>=rank) {
  91. return 0;
  92. } else if constexpr (i==0) {
  93. return l.size();
  94. } else {
  95. return braces_len<i-1, T, rank-1>(*(l.begin()));
  96. }
  97. }
  98. template <class T, rank_t rank, class S = std::array<dim_t, rank>>
  99. constexpr S
  100. braces_shape(braces<T, rank> const & l)
  101. {
  102. return std::apply([&l](auto ... i) { return S { braces_len<decltype(i)::value, T, rank>(l) ... }; },
  103. mp::iota<rank> {});
  104. }
  105. // --------------------
  106. // View
  107. // --------------------
  108. // FIXME size is immutable so that it can be kept together with rank.
  109. // TODO Parameterize on Child having .data() so that there's only one pointer.
  110. // TODO Parameterize on iterator type not on value type.
  111. // TODO Constructor checks (nonnegative lens, steps inside, etc.).
  112. template <class T, rank_t RANK>
  113. struct View
  114. {
  115. using Dimv = std::conditional_t<ANY==RANK, vector_default_init<Dim>, Small<Dim, ANY==RANK ? 0 : RANK>>;
  116. Dimv dimv;
  117. T * cp;
  118. consteval static rank_t rank() requires (RANK!=ANY) { return RANK; }
  119. constexpr rank_t rank() const requires (RANK==ANY) { return rank_t(dimv.size()); }
  120. constexpr static dim_t len_s(int k) { return ANY; }
  121. constexpr dim_t len(int k) const { return dimv[k].len; }
  122. constexpr dim_t step(int k) const { return dimv[k].step; }
  123. constexpr auto data() const { return cp; }
  124. constexpr dim_t size() const { return prod(map(&Dim::len, dimv)); }
  125. constexpr bool empty() const { return any(0==map(&Dim::len, dimv)); }
  126. constexpr View(): cp() {} // FIXME used by Container constructors
  127. constexpr View(Dimv const & dimv_, T * cp_): dimv(dimv_), cp(cp_) {} // [ra36]
  128. constexpr View(auto && s, T * cp_): cp(cp_)
  129. {
  130. ra::resize(dimv, ra::size(s)); // [ra37]
  131. if constexpr (std::is_convertible_v<value_t<decltype(s)>, Dim>) {
  132. start(dimv) = s;
  133. } else {
  134. filldim(dimv, s);
  135. }
  136. }
  137. constexpr View(std::initializer_list<dim_t> s, T * cp_): View(start(s), cp_) {}
  138. // cf RA_ASSIGNOPS_SELF [ra38] [ra34]
  139. View const & operator=(View && x) const { start(*this) = x; return *this; }
  140. View const & operator=(View const & x) const { start(*this) = x; return *this; }
  141. constexpr View(View &&) = default;
  142. constexpr View(View const &) = default;
  143. #define ASSIGNOPS(OP) \
  144. View const & operator OP (auto && x) const { start(*this) OP x; return *this; }
  145. FOR_EACH(ASSIGNOPS, =, *=, +=, -=, /=)
  146. #undef ASSIGNOPS
  147. // braces row-major ravel for rank!=1. See Container::fill1
  148. using ravel_arg = std::conditional_t<RANK==1, noarg, std::initializer_list<T>>;
  149. View const & operator=(ravel_arg const x) const
  150. {
  151. auto xsize = ssize(x);
  152. RA_CHECK(size()==xsize, "Mismatched sizes ", View::size(), " ", xsize, ".");
  153. std::copy_n(x.begin(), xsize, begin());
  154. return *this;
  155. }
  156. constexpr View const & operator=(braces<T, RANK> x) const requires (RANK!=ANY) { ra::iter<-1>(*this) = x; return *this; }
  157. #define RA_BRACES_ANY(N) \
  158. constexpr View const & operator=(braces<T, N> x) const requires (RANK==ANY) { ra::iter<-1>(*this) = x; return *this; }
  159. FOR_EACH(RA_BRACES_ANY, 2, 3, 4);
  160. #undef RA_BRACES_ANY
  161. template <rank_t c=0> constexpr auto iter() const && { return CellBig<T, Dimv, ic_t<c>>(cp, std::move(dimv)); }
  162. template <rank_t c=0> constexpr auto iter() const & { return CellBig<T, Dimv const &, ic_t<c>>(cp, dimv); }
  163. constexpr auto iter(rank_t c) const && { return CellBig<T, Dimv, dim_t>(cp, std::move(dimv), c); }
  164. constexpr auto iter(rank_t c) const & { return CellBig<T, Dimv const &, dim_t>(cp, dimv, c); }
  165. constexpr auto begin() const { return STLIterator(iter<0>()); }
  166. constexpr decltype(auto) static end() { return std::default_sentinel; }
  167. constexpr dim_t
  168. select(Dim * dim, int k, dim_t i) const
  169. {
  170. RA_CHECK(inside(i, len(k)), "Bad index in len[", k, "]=", len(k), ": ", i, ".");
  171. return step(k)*i;
  172. }
  173. constexpr dim_t
  174. select(Dim * dim, int k, is_iota auto i) const
  175. {
  176. RA_CHECK(inside(i, len(k)), "Bad index in len[", k, "]=", len(k), ": iota [", i.n, " ", i.i, " ", i.s, "].");
  177. *dim = { .len = i.n, .step = step(k) * i.s };
  178. return step(k)*i.i;
  179. }
  180. template <class I0, class ... I>
  181. constexpr dim_t
  182. select_loop(Dim * dim, int k, I0 && i0, I && ... i) const
  183. {
  184. return select(dim, k, with_len(len(k), RA_FWD(i0)))
  185. + select_loop(dim + beatable<I0>.dst, k + beatable<I0>.src, RA_FWD(i) ...);
  186. }
  187. template <int n, class ... I>
  188. constexpr dim_t
  189. select_loop(Dim * dim, int k, dots_t<n> i0, I && ... i) const
  190. {
  191. int nn = (BAD==n) ? (rank() - k - (0 + ... + beatable<I>.src)) : n;
  192. for (Dim * end = dim+nn; dim!=end; ++dim, ++k) {
  193. *dim = dimv[k];
  194. }
  195. return select_loop(dim, k, RA_FWD(i) ...);
  196. }
  197. template <int n, class ... I>
  198. constexpr dim_t
  199. select_loop(Dim * dim, int k, insert_t<n> i0, I && ... i) const
  200. {
  201. for (Dim * end = dim+n; dim!=end; ++dim) {
  202. *dim = { .len = BAD, .step = 0 };
  203. }
  204. return select_loop(dim, k, RA_FWD(i) ...);
  205. }
  206. constexpr static dim_t
  207. select_loop(Dim * dim, int k)
  208. {
  209. return 0;
  210. }
  211. // Specialize for rank() integer-args -> scalar, same in ra::SmallBase in small.hh.
  212. template <class ... I>
  213. constexpr decltype(auto)
  214. operator()(I && ... i) const
  215. {
  216. constexpr int stretch = (0 + ... + (beatable<I>.dst==BAD));
  217. static_assert(stretch<=1, "Cannot repeat stretch index.");
  218. if constexpr ((0 + ... + is_scalar_index<I>)==RANK) {
  219. return data()[select_loop(nullptr, 0, i ...)];
  220. } else if constexpr ((beatable<I>.rt && ...)) {
  221. constexpr rank_t extended = (0 + ... + beatable<I>.add);
  222. View<T, rank_sum(RANK, extended)> sub;
  223. rank_t subrank = rank()+extended;
  224. if constexpr (RANK==ANY) {
  225. RA_CHECK(subrank>=0, "Bad rank.");
  226. sub.dimv.resize(subrank);
  227. }
  228. sub.cp = data() + select_loop(sub.dimv.data(), 0, i ...);
  229. // fill the rest of dim, skipping over beatable subscripts.
  230. for (int k = (0==stretch ? (0 + ... + beatable<I>.dst) : subrank); k<subrank; ++k) {
  231. sub.dimv[k] = dimv[k-extended];
  232. }
  233. // if RANK==ANY then rank may be 0
  234. return sub;
  235. // TODO partial beating. FIXME forward this? cf unbeat
  236. } else {
  237. return unbeat<sizeof...(I)>::op(*this, RA_FWD(i) ...);
  238. }
  239. }
  240. constexpr decltype(auto)
  241. operator[](auto && ... i) const { return (*this)(RA_FWD(i) ...); }
  242. template <class I>
  243. constexpr decltype(auto)
  244. at(I && i) const
  245. {
  246. // FIXME can't say 'frame rank 0' so -size wouldn't work.
  247. constexpr rank_t crank = rank_diff(RANK, ra::size_s<I>());
  248. if constexpr (ANY==crank) {
  249. return iter(rank()-ra::size(i)).at(RA_FWD(i));
  250. } else {
  251. return iter<crank>().at(RA_FWD(i));
  252. }
  253. }
  254. constexpr
  255. operator T & () const
  256. {
  257. static_assert(ANY==RANK || 0==RANK, "Bad rank for conversion to scalar.");
  258. if constexpr (ANY==RANK) {
  259. RA_CHECK(0==rank(), "Error converting rank ", rank(), " to scalar.");
  260. }
  261. return data()[0];
  262. }
  263. // necessary here per [ra15] (?)
  264. constexpr operator T & () { return std::as_const(*this); }
  265. // conversions from var rank to fixed rank
  266. template <rank_t R> requires (R==ANY && R!=RANK)
  267. constexpr View(View<T, R> const & x): dimv(x.dimv), cp(x.cp) {}
  268. template <rank_t R> requires (R==ANY && R!=RANK && std::is_const_v<T>)
  269. constexpr View(View<std::remove_const_t<T>, R> const & x): dimv(x.dimv), cp(x.cp) {}
  270. // conversion from fixed rank to var rank
  271. template <rank_t R> requires (R!=ANY && RANK==ANY)
  272. constexpr View(View<T, R> const & x): dimv(x.dimv.begin(), x.dimv.end()), cp(x.cp) {}
  273. template <rank_t R> requires (R!=ANY && RANK==ANY && std::is_const_v<T>)
  274. constexpr View(View<std::remove_const_t<T>, R> const & x): dimv(x.dimv.begin(), x.dimv.end()), cp(x.cp) {}
  275. // conversion to const. We rely on it for Container::view(). FIXME iffy? not constexpr, and doesn't work for Small.
  276. constexpr
  277. operator View<T const, RANK> const & () const requires (!std::is_const_v<T>)
  278. {
  279. return *reinterpret_cast<View<T const, RANK> const *>(this);
  280. }
  281. };
  282. // --------------------
  283. // Container types
  284. // --------------------
  285. template <class V>
  286. struct storage_traits
  287. {
  288. using T = V::value_type;
  289. static_assert(!std::is_same_v<std::remove_const_t<T>, bool>, "No pointers to bool in std::vector<bool>.");
  290. constexpr static auto create(dim_t n) { RA_CHECK(n>=0); return V(n); }
  291. template <class VV> constexpr static auto data(VV & v) { return v.data(); }
  292. };
  293. template <class P>
  294. struct storage_traits<std::unique_ptr<P>>
  295. {
  296. using V = std::unique_ptr<P>;
  297. using T = std::decay_t<decltype(*std::declval<V>().get())>;
  298. constexpr static auto create(dim_t n) { RA_CHECK(n>=0); return V(new T[n]); }
  299. template <class VV> constexpr static auto data(VV & v) { return v.get(); }
  300. };
  301. template <class P>
  302. struct storage_traits<std::shared_ptr<P>>
  303. {
  304. using V = std::shared_ptr<P>;
  305. using T = std::decay_t<decltype(*std::declval<V>().get())>;
  306. constexpr static auto create(dim_t n) { RA_CHECK(n>=0); return V(new T[n], std::default_delete<T[]>()); }
  307. template <class VV> constexpr static auto data(VV & v) { return v.get(); }
  308. };
  309. // FIXME avoid duplicating View::p. Avoid overhead with rank 1.
  310. template <class Store, rank_t RANK>
  311. struct Container: public View<typename storage_traits<Store>::T, RANK>
  312. {
  313. Store store;
  314. using T = typename storage_traits<Store>::T;
  315. using View = ra::View<T, RANK>;
  316. using ViewConst = ra::View<T const, RANK>;
  317. using View::size, View::rank;
  318. using shape_arg = decltype(shape(std::declval<View>().iter()));
  319. constexpr ViewConst const & view() const { return static_cast<View const &>(*this); }
  320. constexpr View & view() { return *this; }
  321. // Needed to set View::cp. FIXME Remove duplication as in SmallBase/SmallArray.
  322. Container(Container && w): store(std::move(w.store))
  323. {
  324. View::dimv = std::move(w.dimv);
  325. View::cp = storage_traits<Store>::data(store);
  326. }
  327. Container(Container const & w): store(w.store)
  328. {
  329. View::dimv = w.dimv;
  330. View::cp = storage_traits<Store>::data(store);
  331. }
  332. Container(Container & w): Container(std::as_const(w)) {}
  333. // A(shape 2 3) = A-type [1 2 3] initializes, so it doesn't behave as A(shape 2 3) = not-A-type [1 2 3] which uses View::operator=. This is used by operator>>(std::istream &, Container &). See test/ownership.cc [ra20].
  334. // TODO don't require copyable T in constructors, see fill1. That requires operator= to initialize, not update.
  335. Container & operator=(Container && w)
  336. {
  337. store = std::move(w.store);
  338. View::dimv = std::move(w.dimv);
  339. View::cp = storage_traits<Store>::data(store);
  340. return *this;
  341. }
  342. Container & operator=(Container const & w)
  343. {
  344. store = w.store;
  345. View::dimv = w.dimv;
  346. View::cp = storage_traits<Store>::data(store);
  347. return *this;
  348. }
  349. Container & operator=(Container & w) { return *this = std::as_const(w); }
  350. // const/nonconst shims over View's methods. FIXME > gcc13 ? __cpp_explicit_this_parameter
  351. #define RA_CONST_OR_NOT(CONST) \
  352. constexpr T CONST & back() CONST { RA_CHECK(1==rank() && size()>0); return store[size()-1]; } \
  353. constexpr auto data() CONST { return view().data(); } \
  354. constexpr decltype(auto) operator()(auto && ... a) CONST { return view()(RA_FWD(a) ...); } \
  355. constexpr decltype(auto) operator[](auto && ... a) CONST { return view()(RA_FWD(a) ...); } \
  356. constexpr decltype(auto) at(auto && i) CONST { return view().at(RA_FWD(i)); } \
  357. /* always compact/row-major, so STL-like iterators can be raw pointers. */ \
  358. constexpr auto begin() CONST { assert(is_c_order(view())); return view().data(); } \
  359. constexpr auto end() CONST { return view().data()+size(); } \
  360. /* FIXME cannot use this with braces<> yet. FIXME optimization (1==RANK && 0==c) opt breaks test/io.cc (?) */ \
  361. template <rank_t c=0> constexpr auto iter() CONST { return view().template iter<c>(); } \
  362. constexpr operator T CONST & () CONST { return view(); }
  363. FOR_EACH(RA_CONST_OR_NOT, /*not const*/, const)
  364. #undef RA_CONST_OR_NOT
  365. // non-copy assignment operators follow View, but cannot be just using'd because of constness.
  366. #define ASSIGNOPS(OP) \
  367. Container & operator OP (auto && x) { view() OP x; return *this; }
  368. FOR_EACH(ASSIGNOPS, =, *=, +=, -=, /=)
  369. #undef ASSIGNOPS
  370. using ravel_arg = std::conditional_t<RANK==1, noarg, std::initializer_list<T>>;
  371. Container & operator=(ravel_arg const x) { view() = x; return *this; }
  372. constexpr Container & operator=(braces<T, RANK> x) requires (RANK!=ANY) { view() = x; return *this; }
  373. #define RA_BRACES_ANY(N) \
  374. constexpr Container & operator=(braces<T, N> x) requires (RANK==ANY) { view() = x; return *this; }
  375. FOR_EACH(RA_BRACES_ANY, 2, 3, 4);
  376. #undef RA_BRACES_ANY
  377. template <class S> requires (1==rank_s<S>() || ANY==rank_s<S>())
  378. void
  379. init(S && s)
  380. {
  381. static_assert(!std::is_convertible_v<value_t<S>, Dim>);
  382. RA_CHECK(1==ra::rank(s), "Rank mismatch for init shape.");
  383. static_assert(ANY==RANK || ANY==size_s<S>() || RANK==size_s<S>() || BAD==size_s<S>(), "Bad shape for rank.");
  384. ra::resize(View::dimv, ra::size(s)); // [ra37]
  385. store = storage_traits<Store>::create(filldim(View::dimv, s));
  386. View::cp = storage_traits<Store>::data(store);
  387. }
  388. void init(dim_t s) { init(std::array {s}); } // scalar allowed as shape if rank is 1.
  389. // provided so that {} calls shape_arg constructor below.
  390. Container() requires (ANY==RANK): View({ Dim {0, 1} }, nullptr) {}
  391. Container() requires (ANY!=RANK && 0!=RANK): View(typename View::Dimv(Dim {0, 1}), nullptr) {}
  392. Container() requires (0==RANK): Container({}, none) {}
  393. // shape_arg overloads handle {...} arguments. Size check is at conversion (if shape_arg is Small) or init().
  394. Container(shape_arg const & s, none_t) { init(s); }
  395. Container(shape_arg const & s, auto && x): Container(s, none) { iter() = x; }
  396. Container(shape_arg const & s, braces<T, RANK> x) requires (RANK==1) : Container(s, none) { view() = x; }
  397. Container(auto && x): Container(ra::shape(x), none) { iter() = x; }
  398. Container(braces<T, RANK> x) requires (RANK!=ANY)
  399. : Container(braces_shape<T, RANK>(x), none) { view() = x; }
  400. #define RA_BRACES_ANY(N) \
  401. Container(braces<T, N> x) requires (RANK==ANY) \
  402. : Container(braces_shape<T, N>(x), none) { view() = x; }
  403. FOR_EACH(RA_BRACES_ANY, 1, 2, 3, 4)
  404. #undef RA_BRACES_ANY
  405. // FIXME requires T to be copiable, which conflicts with the semantics of view_.operator=. store(x) avoids it for Big, but doesn't work for Unique. Should construct in place like std::vector does.
  406. constexpr void
  407. fill1(auto xbegin, dim_t xsize)
  408. {
  409. RA_CHECK(size()==xsize, "Mismatched sizes ", size(), " ", xsize, ".");
  410. std::ranges::copy_n(xbegin, xsize, begin());
  411. }
  412. // shape + row-major ravel.
  413. // FIXME explicit it-is-ravel mark. Also iter<n> initializers.
  414. // FIXME regular (no need for fill1) for ANY if rank is 1.
  415. Container(shape_arg const & s, typename View::ravel_arg x)
  416. : Container(s, none) { fill1(x.begin(), x.size()); }
  417. // FIXME remove
  418. Container(shape_arg const & s, auto * p)
  419. : Container(s, none) { fill1(p, size()); } // FIXME fake check
  420. // FIXME remove
  421. Container(shape_arg const & s, auto pbegin, dim_t psize)
  422. : Container(s, none) { fill1(pbegin, psize); }
  423. // for shape arguments that doesn't convert implicitly to shape_arg
  424. Container(auto && s, none_t)
  425. { init(RA_FWD(s)); }
  426. Container(auto && s, auto && x)
  427. : Container(RA_FWD(s), none) { iter() = x; }
  428. Container(auto && s, std::initializer_list<T> x)
  429. : Container(RA_FWD(s), none) { fill1(x.begin(), x.size()); }
  430. // resize first axis or full shape. Only for some kinds of store.
  431. void resize(dim_t const s)
  432. {
  433. static_assert(RANK==ANY || RANK>0); RA_CHECK(0<rank());
  434. View::dimv[0].len = s;
  435. store.resize(size());
  436. View::cp = store.data();
  437. }
  438. void resize(dim_t const s, T const & t)
  439. {
  440. static_assert(RANK==ANY || RANK>0); RA_CHECK(0<rank());
  441. View::dimv[0].len = s;
  442. store.resize(size(), t);
  443. View::cp = store.data();
  444. }
  445. template <class S> requires (rank_s<S>() > 0)
  446. void resize(S const & s)
  447. {
  448. ra::resize(View::dimv, start(s).len(0)); // [ra37] FIXME is View constructor
  449. store.resize(filldim(View::dimv, s));
  450. View::cp = store.data();
  451. }
  452. // lets us move. A template + RA_FWD wouldn't work for push_back(brace-enclosed-list).
  453. void push_back(T && t)
  454. {
  455. static_assert(RANK==1 || RANK==ANY); RA_CHECK(1==rank());
  456. store.push_back(std::move(t));
  457. ++View::dimv[0].len;
  458. View::cp = store.data();
  459. }
  460. void push_back(T const & t)
  461. {
  462. static_assert(RANK==1 || RANK==ANY); RA_CHECK(1==rank());
  463. store.push_back(t);
  464. ++View::dimv[0].len;
  465. View::cp = store.data();
  466. }
  467. void emplace_back(auto && ... a)
  468. {
  469. static_assert(RANK==1 || RANK==ANY); RA_CHECK(1==rank());
  470. store.emplace_back(RA_FWD(a) ...);
  471. ++View::dimv[0].len;
  472. View::cp = store.data();
  473. }
  474. void pop_back()
  475. {
  476. static_assert(RANK==1 || RANK==ANY); RA_CHECK(1==rank());
  477. RA_CHECK(View::dimv[0].len>0);
  478. store.pop_back();
  479. --View::dimv[0].len;
  480. }
  481. };
  482. // rely on std::swap; else ambiguous
  483. template <class Store, rank_t RANKA, rank_t RANKB> requires (RANKA!=RANKB)
  484. void
  485. swap(Container<Store, RANKA> & a, Container<Store, RANKB> & b)
  486. {
  487. if constexpr (ANY==RANKA) {
  488. RA_CHECK(rank(a)==rank(b));
  489. decltype(b.dimv) c = a.dimv;
  490. start(a.dimv) = b.dimv;
  491. std::swap(b.dimv, c);
  492. } else if constexpr (ANY==RANKB) {
  493. RA_CHECK(rank(a)==rank(b));
  494. decltype(a.dimv) c = b.dimv;
  495. start(b.dimv) = a.dimv;
  496. std::swap(a.dimv, c);
  497. } else {
  498. static_assert(RANKA==RANKB);
  499. std::swap(a.dimv, b.dimv);
  500. }
  501. std::swap(a.store, b.store);
  502. std::swap(a.cp, b.cp);
  503. }
  504. template <class T, rank_t RANK=ANY> using Big = Container<vector_default_init<T>, RANK>;
  505. template <class T, rank_t RANK=ANY> using Unique = Container<std::unique_ptr<T []>, RANK>;
  506. template <class T, rank_t RANK=ANY> using Shared = Container<std::shared_ptr<T>, RANK>;
  507. // -------------
  508. // Used in Guile wrappers to let array parameter either borrow from Guile storage or convert into new array (eg 'f32 into 'f64).
  509. // TODO Can use unique_ptr's deleter for this?
  510. // TODO Shared/Unique should maybe have constructors with unique_ptr/shared_ptr args
  511. // -------------
  512. template <rank_t RANK, class T>
  513. Shared<T, RANK>
  514. shared_borrowing(View<T, RANK> & raw)
  515. {
  516. Shared<T, RANK> a;
  517. a.dimv = raw.dimv;
  518. a.cp = raw.cp;
  519. a.store = std::shared_ptr<T>(raw.data(), [](T *) {});
  520. return a;
  521. }
  522. // --------------------
  523. // Concrete (container) type from array expression.
  524. // --------------------
  525. template <class E>
  526. struct concrete_type_def
  527. {
  528. using type = void;
  529. };
  530. template <class E> requires (size_s<E>()==ANY)
  531. struct concrete_type_def<E>
  532. {
  533. using type = Big<value_t<E>, rank_s<E>()>;
  534. };
  535. template <class E> requires (size_s<E>()!=ANY)
  536. struct concrete_type_def<E>
  537. {
  538. using type = decltype(std::apply([](auto ... i) { return Small<value_t<E>, E::len_s(i) ...> {}; },
  539. mp::iota<rank_s<E>()> {}));
  540. };
  541. // Scalars are their own concrete_type. Treat unregistered types as scalars.
  542. template <class E>
  543. using concrete_type = std::decay_t<
  544. std::conditional_t<(0==rank_s<E>() && !is_ra<E>) || is_scalar<E>,
  545. std::decay_t<E>,
  546. typename concrete_type_def<std::decay_t<decltype(start(std::declval<E>()))>>::type>>;
  547. template <class E>
  548. constexpr auto
  549. concrete(E && e)
  550. {
  551. // FIXME gcc 11.3 on GH workflows (?)
  552. #pragma GCC diagnostic push
  553. #pragma GCC diagnostic warning "-Wmaybe-uninitialized"
  554. return concrete_type<E>(RA_FWD(e));
  555. #pragma GCC diagnostic pop
  556. }
  557. template <class E>
  558. constexpr auto
  559. with_same_shape(E && e)
  560. {
  561. if constexpr (ANY!=size_s<concrete_type<E>>()) {
  562. return concrete_type<E>();
  563. } else {
  564. return concrete_type<E>(ra::shape(e), none);
  565. }
  566. }
  567. template <class E, class X>
  568. constexpr auto
  569. with_same_shape(E && e, X && x)
  570. {
  571. // FIXME gcc 11.3 on GH workflows (?)
  572. #pragma GCC diagnostic push
  573. #pragma GCC diagnostic warning "-Wmaybe-uninitialized"
  574. if constexpr (ANY!=size_s<concrete_type<E>>()) {
  575. return concrete_type<E>(RA_FWD(x));
  576. } else {
  577. return concrete_type<E>(ra::shape(e), RA_FWD(x));
  578. }
  579. #pragma GCC diagnostic pop
  580. }
  581. template <class E, class S, class X>
  582. constexpr auto
  583. with_shape(S && s, X && x)
  584. {
  585. if constexpr (ANY!=size_s<concrete_type<E>>()) {
  586. return concrete_type<E>(RA_FWD(x));
  587. } else {
  588. return concrete_type<E>(RA_FWD(s), RA_FWD(x));
  589. }
  590. }
  591. template <class E, class S, class X>
  592. constexpr auto
  593. with_shape(std::initializer_list<S> && s, X && x)
  594. {
  595. if constexpr (ANY!=size_s<concrete_type<E>>()) {
  596. return concrete_type<E>(RA_FWD(x));
  597. } else {
  598. return concrete_type<E>(s, RA_FWD(x));
  599. }
  600. }
  601. // --------------------
  602. // Operations specific to View.
  603. // --------------------
  604. template <class T, rank_t RANK>
  605. inline View<T, RANK>
  606. reverse(View<T, RANK> const & view, int k=0)
  607. {
  608. RA_CHECK(inside(k, view.rank()), "Bad reverse axis ", k, " for view of rank ", view.rank(), ".");
  609. View<T, RANK> r = view;
  610. if (auto & dim=r.dimv[k]; dim.len!=0) {
  611. r.cp += dim.step*(dim.len-1);
  612. dim.step *= -1;
  613. }
  614. return r;
  615. }
  616. // dynamic transposed axes list.
  617. template <class T, rank_t RANK, class S>
  618. inline View<T, ANY>
  619. transpose_(S && s, View<T, RANK> const & view)
  620. {
  621. RA_CHECK(view.rank()==ra::size(s));
  622. auto rp = std::max_element(s.begin(), s.end());
  623. rank_t dstrank = (rp==s.end() ? 0 : *rp+1);
  624. View<T, ANY> r { decltype(r.dimv)(dstrank, Dim { BAD, 0 }), view.data() };
  625. for (int k=0; int sk: s) {
  626. Dim & dest = r.dimv[sk];
  627. dest.step += view.step(k);
  628. dest.len = dest.len>=0 ? std::min(dest.len, view.len(k)) : view.len(k);
  629. ++k;
  630. }
  631. return r;
  632. }
  633. template <class T, rank_t RANK, class S>
  634. inline View<T, ANY>
  635. transpose(S && s, View<T, RANK> const & view)
  636. {
  637. return transpose_(RA_FWD(s), view);
  638. }
  639. // Need compile time values and not sizes to deduce the output rank, so a builtin array shim (as for reshape()) would be useless.
  640. template <class T, rank_t RANK>
  641. inline View<T, ANY>
  642. transpose(std::initializer_list<ra::rank_t> s, View<T, RANK> const & view)
  643. {
  644. return transpose_(s, view);
  645. }
  646. // Static transposed axes list.
  647. template <int ... Iarg, class T, rank_t RANK>
  648. inline auto
  649. transpose(View<T, RANK> const & view)
  650. {
  651. static_assert(RANK==ANY || RANK==sizeof...(Iarg), "Bad output rank.");
  652. RA_CHECK(view.rank()==sizeof...(Iarg), "Bad output rank: ", view.rank(), "should be ", (sizeof...(Iarg)), ".");
  653. using dummy_s = mp::makelist<sizeof...(Iarg), ic_t<0>>;
  654. using ti = axes_list_indices<mp::int_list<Iarg ...>, dummy_s, dummy_s>;
  655. constexpr rank_t DSTRANK = mp::len<typename ti::dst>;
  656. View<T, DSTRANK> r { decltype(r.dimv)(Dim { BAD, 0 }), view.data() };
  657. std::array<int, sizeof...(Iarg)> s {{ Iarg ... }};
  658. for (int k=0; int sk: s) {
  659. Dim & dest = r.dimv[sk];
  660. dest.step += view.step(k);
  661. dest.len = dest.len>=0 ? std::min(dest.len, view.len(k)) : view.len(k);
  662. ++k;
  663. }
  664. return r;
  665. }
  666. template <class T, rank_t RANK>
  667. inline auto
  668. diag(View<T, RANK> const & view)
  669. {
  670. return transpose<0, 0>(view);
  671. }
  672. template <class T, rank_t RANK>
  673. inline View<T, 1>
  674. ravel_free(View<T, RANK> const & a)
  675. {
  676. RA_CHECK(is_c_order(a, false));
  677. int r = a.rank()-1;
  678. for (; r>=0 && a.len(r)==1; --r) {}
  679. ra::dim_t s = r<0 ? 1 : a.step(r);
  680. return ra::View<T, 1>({{size(a), s}}, a.cp);
  681. }
  682. template <class T, rank_t RANK, class S>
  683. inline auto
  684. reshape_(View<T, RANK> const & a, S && sb_)
  685. {
  686. auto sb = concrete(RA_FWD(sb_));
  687. // FIXME when we need to copy, accept/return Shared
  688. dim_t la = ra::size(a);
  689. dim_t lb = 1;
  690. for (int i=0; i<ra::size(sb); ++i) {
  691. if (sb[i]==-1) {
  692. dim_t quot = lb;
  693. for (int j=i+1; j<ra::size(sb); ++j) {
  694. quot *= sb[j];
  695. RA_CHECK(quot>0, "Cannot deduce dimensions.");
  696. }
  697. auto pv = la/quot;
  698. RA_CHECK((la%quot==0 && pv>=0), "Bad placeholder.");
  699. sb[i] = pv;
  700. lb = la;
  701. break;
  702. } else {
  703. lb *= sb[i];
  704. }
  705. }
  706. auto sa = shape(a);
  707. // FIXME should be able to reshape Scalar etc.
  708. View<T, ra::size_s(sb)> b(map([](auto i) { return Dim { BAD, 0 }; }, ra::iota(ra::size(sb))), a.data());
  709. rank_t i = 0;
  710. for (; i<a.rank() && i<b.rank(); ++i) {
  711. if (sa[a.rank()-i-1]!=sb[b.rank()-i-1]) {
  712. assert(is_c_order(a, false) && "reshape w/copy not implemented"); // FIXME bad abort [ra17]
  713. if (la>=lb) {
  714. // FIXME View(SS const & s, T * p). Cf [ra37].
  715. filldim(b.dimv, sb);
  716. for (int j=0; j!=b.rank(); ++j) {
  717. b.dimv[j].step *= a.step(a.rank()-1);
  718. }
  719. return b;
  720. } else {
  721. assert(0 && "reshape case not implemented"); // FIXME bad abort [ra17]
  722. }
  723. } else {
  724. // select
  725. b.dimv[b.rank()-i-1] = a.dimv[a.rank()-i-1];
  726. }
  727. }
  728. if (i==a.rank()) {
  729. // tile & return
  730. for (rank_t j=i; j<b.rank(); ++j) {
  731. b.dimv[b.rank()-j-1] = { sb[b.rank()-j-1], 0 };
  732. }
  733. }
  734. return b;
  735. }
  736. template <class T, rank_t RANK, class S>
  737. inline auto
  738. reshape(View<T, RANK> const & a, S && sb_)
  739. {
  740. return reshape_(a, RA_FWD(sb_));
  741. }
  742. // We need dimtype bc {1, ...} deduces to int and that fails to match ra::dim_t. initializer_list could handle the general case, but the result would have var rank and would override this one (?).
  743. template <class T, rank_t RANK, class dimtype, int N>
  744. inline auto
  745. reshape(View<T, RANK> const & a, dimtype const (&sb_)[N])
  746. {
  747. return reshape_(a, sb_);
  748. }
  749. // lo: lower bounds, hi: upper bounds. The stencil indices are in [0 lo+1+hi] = [-lo +hi].
  750. template <class LO, class HI, class T, rank_t N>
  751. inline View<T, rank_sum(N, N)>
  752. stencil(View<T, N> const & a, LO && lo, HI && hi)
  753. {
  754. View<T, rank_sum(N, N)> s;
  755. s.cp = a.data();
  756. ra::resize(s.dimv, 2*a.rank());
  757. RA_CHECK(every(lo>=0) && every(hi>=0), "Bad stencil bounds lo ", noshape, lo, " hi ", noshape, hi, ".");
  758. for_each([](auto & dims, auto && dima, auto && lo, auto && hi)
  759. {
  760. RA_CHECK(dima.len>=lo+hi, "Stencil is too large for array.");
  761. dims = {dima.len-lo-hi, dima.step};
  762. },
  763. ptr(s.dimv.data()), a.dimv, lo, hi);
  764. for_each([](auto & dims, auto && dima, auto && lo, auto && hi)
  765. { dims = {lo+hi+1, dima.step}; },
  766. ptr(s.dimv.data()+a.rank()), a.dimv, lo, hi);
  767. return s;
  768. }
  769. // Make last sizes of View<> be compile-time constants.
  770. template <class super_t, rank_t SUPERR, class T, rank_t RANK>
  771. inline auto
  772. explode_(View<T, RANK> const & a)
  773. {
  774. // TODO Reduce to single check, either the first or the second.
  775. static_assert(RANK>=SUPERR || RANK==ANY, "rank of a is too low");
  776. RA_CHECK(a.rank()>=SUPERR, "Rank of a ", a.rank(), " should be at least ", SUPERR, ".");
  777. View<super_t, rank_sum(RANK, -SUPERR)> b;
  778. ra::resize(b.dimv, a.rank()-SUPERR);
  779. dim_t r = 1;
  780. for (int i=0; i<SUPERR; ++i) {
  781. r *= a.len(i+b.rank());
  782. }
  783. RA_CHECK(r*sizeof(T)==sizeof(super_t), "Length of axes ", r*sizeof(T), " doesn't match type ", sizeof(super_t), ".");
  784. for (int i=0; i<b.rank(); ++i) {
  785. RA_CHECK(a.step(i) % r==0, "Step of axes ", a.step(i), " doesn't match type ", r, " on axis ", i, ".");
  786. b.dimv[i] = { .len = a.len(i), .step = a.step(i) / r };
  787. }
  788. RA_CHECK((b.rank()==0 || a.step(b.rank()-1)==r), "Super type is not compact in array.");
  789. b.cp = reinterpret_cast<super_t *>(a.data());
  790. return b;
  791. }
  792. template <class super_t, class T, rank_t RANK>
  793. inline auto
  794. explode(View<T, RANK> const & a)
  795. {
  796. return explode_<super_t, (std::is_same_v<super_t, std::complex<T>> ? 1 : rank_s<super_t>())>(a);
  797. }
  798. // FIXME Maybe namespace level generics
  799. template <class T> inline int gstep(int i) { if constexpr (is_scalar<T>) return 1; else return T::step(i); }
  800. template <class T> inline int glen(int i) { if constexpr (is_scalar<T>) return 1; else return T::len(i); }
  801. // TODO The ranks below SUBR must be compact, which is not checked.
  802. template <class sub_t, class super_t, rank_t RANK>
  803. inline auto
  804. collapse(View<super_t, RANK> const & a)
  805. {
  806. using super_v = value_t<super_t>;
  807. using sub_v = value_t<sub_t>;
  808. constexpr int subtype = sizeof(super_v)/sizeof(sub_t);
  809. constexpr int SUBR = rank_s<super_t>() - rank_s<sub_t>();
  810. View<sub_t, rank_sum(RANK, SUBR+int(subtype>1))> b;
  811. resize(b.dimv, a.rank()+SUBR+int(subtype>1));
  812. constexpr dim_t r = sizeof(super_t)/sizeof(sub_t);
  813. static_assert(sizeof(super_t)==r*sizeof(sub_t), "Cannot make axis of super_t from sub_t.");
  814. for (int i=0; i<a.rank(); ++i) {
  815. b.dimv[i] = { .len = a.len(i), .step = a.step(i) * r };
  816. }
  817. constexpr int t = sizeof(super_v)/sizeof(sub_v);
  818. constexpr int s = sizeof(sub_t)/sizeof(sub_v);
  819. static_assert(t*sizeof(sub_v)>=1, "Bad subtype.");
  820. for (int i=0; i<SUBR; ++i) {
  821. RA_CHECK(((gstep<super_t>(i)/s)*s==gstep<super_t>(i)), "Bad steps."); // TODO actually static
  822. b.dimv[a.rank()+i] = { .len = glen<super_t>(i), .step = gstep<super_t>(i) / s * t };
  823. }
  824. if (subtype>1) {
  825. b.dimv[a.rank()+SUBR] = { .len = t, .step = 1 };
  826. }
  827. b.cp = reinterpret_cast<sub_t *>(a.data());
  828. return b;
  829. }
  830. } // namespace ra