end.C 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file end.C
  3. /// @brief Check use of special object ra::end
  4. // (c) Daniel Llorens - 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 "ra/operators.H"
  10. #include "ra/io.H"
  11. #include "ra/test.H"
  12. using std::cout, std::endl;
  13. namespace ra {
  14. struct End
  15. {
  16. using value_type = dim_t;
  17. constexpr static rank_t rank_s() { return RANK_BAD; }
  18. constexpr static rank_t rank() { return RANK_BAD; }
  19. constexpr static dim_t size_s() { return DIM_BAD; }
  20. constexpr void adv(rank_t const k, dim_t const d) {}
  21. template <class I> constexpr value_type at(I const & i) const { assert(0); return 0; }
  22. constexpr static dim_t size(int const k) { return DIM_BAD; }
  23. constexpr dim_t stride(int const i) const { assert(0); }
  24. constexpr value_type * flat() const { assert(0); }
  25. };
  26. constexpr End end {};
  27. template <class E>
  28. constexpr decltype(auto) replace_end(dim_t end, E && e)
  29. {
  30. return std::forward<E>(e);
  31. }
  32. constexpr auto replace_end(dim_t end, End && e)
  33. {
  34. return scalar(end);
  35. }
  36. template <class Op, class P ...>
  37. constexpr auto replace_end(dim_t end, Expr<Op, std::tuple<P ...> > && e)
  38. {
  39. return expr(std::forward<decltype(e.op)>(e.op),
  40. replace_end(end, std::forward<P>(std::get<I>(e.t))) ...);
  41. }
  42. } // ra
  43. int main()
  44. {
  45. // End is usable in exprs by virtue of having flat() (TODO will fix this).
  46. ra::Small<int, 3> a {1, 2, 3};
  47. cout << replace_end(10, a + ra::end) << endl;
  48. return 0;
  49. }