iterator-small.cc 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file iterator-small.cc
  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.hh"
  12. #include "ra/small.hh"
  13. #include "ra/ra.hh"
  14. #include "ra/format.hh"
  15. #include "ra/test.hh"
  16. #include "ra/mpdebug.hh"
  17. using std::cout, std::endl, std::flush, ra::TestRecorder;
  18. int main()
  19. {
  20. TestRecorder tr;
  21. {
  22. tr.section("fiddling");
  23. {
  24. using A = ra::Small<int, 2, 3>;
  25. A a = {{1, 2, 3}, {4, 5, 6}};
  26. cout << a << endl;
  27. using AI0 = typename A::iterator<0>;
  28. cout << "AI0 " << std::is_same_v<int, AI0> << endl;
  29. cout << AI0::rank() << endl;
  30. using AI1 = typename A::iterator<1>;
  31. cout << "AI1 " << std::is_same_v<int, AI1> << endl;
  32. cout << AI1::rank() << endl;
  33. AI0 bi(a.data());
  34. cout << bi.c.p << endl;
  35. }
  36. tr.section("STL style, should be a pointer for default strides");
  37. {
  38. using A = ra::Small<int, 2, 3>;
  39. A a = {{1, 2, 3}, {4, 5, 6}};
  40. tr.test(std::is_same_v<int *, decltype(a.begin())>);
  41. for (int i=0; auto && ai: a) {
  42. tr.test_eq(i+1, ai);
  43. ++i;
  44. }
  45. }
  46. tr.section("STL style with non-default strides, which keeps indices (as internal detail)");
  47. {
  48. ra::Small<int, 2, 3> a = {{1, 2, 3}, {4, 5, 6}};
  49. auto b = transpose<1, 0>(a);
  50. tr.test_eq(ra::start({0, 0}), ra::start(b.begin().i));
  51. tr.test_eq(ra::start({0, 1}), ra::start((++b.begin()).i));
  52. tr.test(!std::is_same_v<int *, decltype(b.begin())>);
  53. int bref[6] = {1, 4, 2, 5, 3, 6};
  54. for (int i=0; auto && bi: b) {
  55. tr.test_eq(bref[i], bi);
  56. ++i;
  57. }
  58. }
  59. tr.section("rank>0");
  60. {
  61. auto test_over
  62. = [&](auto && a)
  63. {
  64. auto test = [&](std::string ref, auto cr)
  65. {
  66. std::ostringstream o;
  67. for_each([&o](auto && a) { o << a << "|"; }, iter<decltype(cr)::value>(a));
  68. tr.test_eq(ra::scalar(ref), ra::scalar(o.str()));
  69. };
  70. test("1|2|3|4|5|6|", mp::int_t<-2>());
  71. test("1 2 3|4 5 6|", mp::int_t<-1>());
  72. test("1|2|3|4|5|6|", mp::int_t<0>());
  73. test("1 2 3|4 5 6|", mp::int_t<1>());
  74. test("1 2 3\n4 5 6|", mp::int_t<2>());
  75. };
  76. tr.section("default strides");
  77. test_over(ra::Small<int, 2, 3> {{1, 2, 3}, {4, 5, 6}});
  78. tr.section("non-default strides");
  79. test_over(ra::transpose<1, 0>(ra::Small<int, 3, 2> {{1, 4}, {2, 5}, {3, 6}}));
  80. }
  81. }
  82. return tr.summary();
  83. }