nested-0.cc 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/test - Using nested arrays as if they were arrays if higher rank.
  3. // (c) Daniel Llorens - 2014
  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. // TODO Make more things work and work efficiently.
  9. #include <sstream>
  10. #include <iostream>
  11. #include "ra/test.hh"
  12. using std::cout, std::endl, std::flush, std::cerr, ra::TestRecorder;
  13. template <class T, int N> using Array = ra::Big<T, N>;
  14. template <class T> using Vec = Array<T, 1>;
  15. int main()
  16. {
  17. TestRecorder tr;
  18. tr.section("operators on nested arrays");
  19. {
  20. // default init is required to make vector-of-vector, but I still want Vec {} to mean 'an empty vector' and not a default-init vector.
  21. {
  22. auto c = Vec<int> {};
  23. tr.test_eq(0, c.len(0));
  24. tr.test_eq(0, c.size());
  25. }
  26. {
  27. auto c = Vec<Vec<int>> { {} , {1}, {1, 2} };
  28. std::ostringstream os;
  29. os << c;
  30. std::istringstream is(os.str());
  31. Vec<Vec<int>> d;
  32. is >> d;
  33. tr.test_eq(3, d.size());
  34. tr.test_eq(d[0], Vec<int>{});
  35. tr.test_eq(d[1], Vec<int>{1});
  36. tr.test_eq(d[2], Vec<int>{1, 2});
  37. // TODO Actually nested 'as if higher rank' should allow just (every(c==d)). This is explicit nesting.
  38. tr.test(every(ra::expr([](auto & c, auto & d) { return every(c==d); }, c.iter(), d.iter())));
  39. }
  40. }
  41. tr.section("selector experiments");
  42. {
  43. // These is an investigation of how to make a(ra::all, i) or a(i, ra::all) work.
  44. // The problem with a(ra::all, i) here is that we probably want to leave the iteration on ra::all for last. Otherwise the indexing is redone for each rank-1 cell.
  45. Vec<int> i = {0, 3, 1, 2};
  46. Array<double, 2> a({4, 4}, ra::_0-ra::_1);
  47. Array<double, 2> b = from([](auto && a, auto && i) -> decltype(auto) { return a(i); }, a.iter<1>(), start(i));
  48. tr.test_eq(a(0, i), b(0));
  49. tr.test_eq(a(1, i), b(1));
  50. tr.test_eq(a(2, i), b(2));
  51. tr.test_eq(a(3, i), b(3));
  52. // The problem with a(i) = a(i, ra::all) is that a(i) returns a nested expression, so it isn't equivalent to a(i, [0 1 ...]), and if we want to write it as a rank 2 expression, we can't use from() as above because the iterator we want is a(i).iter(), it depends on i.
  53. // So ...
  54. }
  55. tr.section("copying btw arrays nested in the same way");
  56. {
  57. Vec<ra::Small<int, 2>> a {{1, 2}, {3, 4}, {5, 6}};
  58. ra::Small<ra::Small<int, 2>, 3> b = a;
  59. tr.test_eq(ra::Small<int, 2> {1, 2}, b(0));
  60. tr.test_eq(ra::Small<int, 2> {3, 4}, b(1));
  61. tr.test_eq(ra::Small<int, 2> {5, 6}, b(2));
  62. b(0) = ra::Small<int, 2> {7, 9};
  63. b(1) = ra::Small<int, 2> {3, 4};
  64. b(2) = ra::Small<int, 2> {1, 6};
  65. a = b;
  66. tr.test_eq(ra::Small<int, 2> {7, 9}, a(0));
  67. tr.test_eq(ra::Small<int, 2> {3, 4}, a(1));
  68. tr.test_eq(ra::Small<int, 2> {1, 6}, a(2));
  69. }
  70. tr.section("TODO copying btw arrays nested in different ways");
  71. {
  72. Vec<ra::Small<int, 2>> a {{1, 2}, {3, 4}, {5, 6}};
  73. Array<int, 2> b({3, 2}, {1, 2, 3, 4, 5, 6});
  74. // there's one level of matching so a(0) matches to b(0, 0) and b(0, 1), a(1) to b(1, 0) and b(1, 1), etc. So this results in a(0) = 1 overwritten with a(0) = 2, etc. finally a = [[2, 2], [4, 4], [6, 6]]. Probably not what we want.
  75. a = b;
  76. // b = a; // dnc bc [x, y] ← z is ok but z ← [x, y] is not.
  77. cout << a << endl;
  78. }
  79. return tr.summary();
  80. }