iterator-small.C 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file iterator-small.C
  3. /// @brief Higher-rank iterator for SmallArray/SmallView.
  4. // (c) Daniel Llorens - 2014, 2016
  5. // This library is free software; you can redistribute it and/or modify it under
  6. // the terms of the GNU Lesser General Public License as published by the Free
  7. // Software Foundation; either version 3 of the License, or (at your option) any
  8. // later version.
  9. #include <iostream>
  10. #include <iterator>
  11. #include "ra/complex.H"
  12. #include "ra/small.H"
  13. #include "ra/operators.H"
  14. #include "ra/io.H"
  15. #include "ra/big.H"
  16. #include "ra/small.H"
  17. #include "ra/format.H"
  18. #include "ra/test.H"
  19. #include "ra/mpdebug.H"
  20. using std::cout, std::endl, std::flush;
  21. int main()
  22. {
  23. TestRecorder tr;
  24. {
  25. tr.section("fiddling");
  26. {
  27. using A = ra::Small<int, 2, 3>;
  28. A a = {{1, 2, 3}, {4, 5, 6}};
  29. cout << a << endl;
  30. using AI0 = typename A::iterator<0>;
  31. cout << "AI0 " << std::is_same_v<int, AI0> << endl;
  32. cout << AI0::rank() << endl;
  33. using AI1 = typename A::iterator<1>;
  34. cout << "AI1 " << std::is_same_v<int, AI1> << endl;
  35. cout << AI1::rank() << endl;
  36. AI0 bi(a.data());
  37. cout << bi.c.p << endl;
  38. }
  39. tr.section("STL style, should be a pointer for default strides");
  40. {
  41. using A = ra::Small<int, 2, 3>;
  42. A a = {{1, 2, 3}, {4, 5, 6}};
  43. tr.test(std::is_same_v<int *, decltype(a.begin())>);
  44. int i = 0;
  45. for (auto && ai: a) {
  46. tr.test_eq(i+1, ai);
  47. ++i;
  48. }
  49. }
  50. tr.section("STL style with non-default strides, which keeps indices (as internal detail)");
  51. {
  52. ra::Small<int, 2, 3> a = {{1, 2, 3}, {4, 5, 6}};
  53. auto b = transpose<1, 0>(a);
  54. tr.test_eq(ra::start({0, 0}), ra::start(b.begin().i));
  55. tr.test_eq(ra::start({0, 1}), ra::start((++b.begin()).i));
  56. tr.test(!std::is_same_v<int *, decltype(b.begin())>);
  57. int bref[6] = {1, 4, 2, 5, 3, 6};
  58. int i = 0;
  59. for (auto && bi: b) {
  60. tr.test_eq(bref[i], bi);
  61. ++i;
  62. }
  63. }
  64. tr.section("rank>0");
  65. {
  66. auto test_over
  67. = [&](auto && a)
  68. {
  69. auto test = [&](std::string ref, auto cr)
  70. {
  71. std::ostringstream o;
  72. for_each([&o](auto && a) { o << a << "|"; }, iter<decltype(cr)::value>(a));
  73. tr.test_eq(ra::scalar(ref), ra::scalar(o.str()));
  74. };
  75. test("1|2|3|4|5|6|", mp::int_t<-2>());
  76. test("1 2 3|4 5 6|", mp::int_t<-1>());
  77. test("1|2|3|4|5|6|", mp::int_t<0>());
  78. test("1 2 3|4 5 6|", mp::int_t<1>());
  79. test("1 2 3\n4 5 6|", mp::int_t<2>());
  80. };
  81. tr.section("default strides");
  82. test_over(ra::Small<int, 2, 3> {{1, 2, 3}, {4, 5, 6}});
  83. tr.section("non-default strides");
  84. test_over(ra::transpose<1, 0>(ra::Small<int, 3, 2> {{1, 4}, {2, 5}, {3, 6}}));
  85. }
  86. }
  87. return tr.summary();
  88. }