stl-compat.C 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file stl-compat.C
  3. /// @brief Using ra:: array & iterator types with the STL algos & types.
  4. // (c) Daniel Llorens - 2014
  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. // ra:: iterators are only partially STL compatible, because of copiability,
  10. // lack of random access (which for the STL also means linear, but at least for
  11. // 1D expressions it should be available), etc. Check some cases here.
  12. #include <iostream>
  13. #include <iterator>
  14. #include "ra/complex.H"
  15. #include "ra/test.H"
  16. #include "ra/big.H"
  17. #include "ra/operators.H"
  18. #include "ra/io.H"
  19. using std::cout, std::endl, std::flush;
  20. int main()
  21. {
  22. TestRecorder tr;
  23. tr.section("random access iterators");
  24. {
  25. // TODO rank-0 begin()/end() in ra::Small
  26. // TODO others?
  27. }
  28. tr.section("copyable iterators, but not random access");
  29. {
  30. {
  31. ra::Big<int, 1> a = { 1, 2, 3 };
  32. ra::Big<int, 1> b = { 0, 0, 0 };
  33. std::transform(a.begin(), a.end(), b.begin(), [](int a) { return -a; });
  34. tr.test_eq(a, -b);
  35. }
  36. {
  37. ra::Big<int, 2> a({2, 3}, ra::_0 - 2*ra::_1);
  38. ra::Big<int, 2> b({2, 3}, 99);
  39. std::transform(a.begin(), a.end(), b.begin(), [](int a) { return -a; });
  40. tr.test_eq(a, -b);
  41. }
  42. {
  43. ra::Small<int, 2, 3> a(ra::_0 - 2*ra::_1);
  44. ra::Small<int, 2, 3> b(99);
  45. std::transform(a.begin(), a.end(), b.begin(), [](int a) { return -a; });
  46. tr.test_eq(a, -b);
  47. }
  48. }
  49. tr.section("raw, slippery pointers");
  50. {
  51. ra::Big<int, 1> a = {1, 2, 3};
  52. int b[] = { +1, -1, +1 };
  53. tr.test_eq(ra::Small<int, 3> {2, 1, 4}, a + ra::ptr(b));
  54. ra::ptr(b) = ra::Small<int, 3> {7, 4, 5};
  55. tr.test_eq(ra::Small<int, 3> {7, 4, 5}, ra::ptr(b));
  56. int cp[3] = {1, 2, 3};
  57. // ra::Big<int, 1> c({3}, &cp[0]); // forbidden, confusing for higher rank c (pointer matches as rank 1).
  58. ra::Big<int, 1> c({3}, ra::ptr(cp));
  59. tr.test_eq(ra::Small<int, 3> {1, 2, 3}, c);
  60. }
  61. tr.section("ptr with other iterators");
  62. {
  63. std::vector<int> a = {1, 2, 3};
  64. ra::Small<int, 3> b = ra::ptr(a.begin());
  65. tr.test_eq(ra::Small<int, 3> {1, 2, 3}, b);
  66. }
  67. tr.section("[ra12] check that begin() and end() match for empty views");
  68. {
  69. ra::Big<int, 3> aa({0, 2, 3}, 0.);
  70. auto a = aa(ra::all, 1);
  71. tr.info("begin ", a.begin().ii.c.p, " end ", a.end().ii.c.p).test(a.begin()==a.end());
  72. }
  73. return tr.summary();
  74. }