expr.hh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file expr.hh
  3. /// @brief Operator nodes for expression templates.
  4. // (c) Daniel Llorens - 2011-2014, 2016-2017, 2019
  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. #pragma once
  10. #include "ra/ply.hh"
  11. #include "ra/wrank.hh"
  12. namespace ra {
  13. // Manipulate ET through flat (raw pointer-like) iterators P ...
  14. template <class Op, class T, class I=mp::iota<mp::len<T>>> struct Flat;
  15. template <class Op, class T, int ... I>
  16. struct Flat<Op, T, mp::int_list<I ...>>
  17. {
  18. Op & op;
  19. T t;
  20. template <class S> constexpr void operator+=(S const & s) { ((std::get<I>(t) += std::get<I>(s)), ...); }
  21. constexpr decltype(auto) operator*() { return op(*std::get<I>(t) ...); }
  22. };
  23. template <class Op, class ... P> inline constexpr auto
  24. flat(Op & op, P && ... p)
  25. {
  26. return Flat<Op, std::tuple<P ...>> { op, std::tuple<P ...> { std::forward<P>(p) ... } };
  27. }
  28. // forward decl in atom.hh
  29. template <class Op, class ... P, int ... I>
  30. struct Expr<Op, std::tuple<P ...>, mp::int_list<I ...>>: public Match<std::tuple<P ...>>
  31. {
  32. using Match_ = Match<std::tuple<P ...>>;
  33. Op op;
  34. // test/ra-9.cc [ra1] for forward() here.
  35. constexpr Expr(Op op_, P ... p_): Match_(std::forward<P>(p_) ...), op(std::forward<Op>(op_)) {}
  36. template <class J> constexpr decltype(auto)
  37. at(J const & i)
  38. {
  39. return op(std::get<I>(this->t).at(i) ...);
  40. }
  41. constexpr decltype(auto)
  42. flat()
  43. {
  44. return ra::flat(op, std::get<I>(this->t).flat() ...);
  45. }
  46. // needed for xpr with rank_s()==RANK_ANY, which don't decay to scalar when used as operator arguments.
  47. using scalar = decltype(*(ra::flat(op, std::get<I>(Match_::t).flat() ...)));
  48. operator scalar()
  49. {
  50. if constexpr (this->rank_s()!=1 || size_s(*this)!=1) { // for coord types; so fixed only
  51. if constexpr (this->rank_s()!=0) {
  52. static_assert(this->rank_s()==RANK_ANY);
  53. assert(this->rank()==0);
  54. }
  55. }
  56. return *flat();
  57. }
  58. FOR_EACH(RA_DEF_ASSIGNOPS, =, *=, +=, -=, /=)
  59. };
  60. template <class V, class ... T, int ... i> inline constexpr auto
  61. expr_verb(mp::int_list<i ...>, V && v, T && ... t)
  62. {
  63. using FM = Framematch<V, std::tuple<T ...>>;
  64. return expr(FM::op(std::forward<V>(v)), reframe<mp::ref<typename FM::R, i>>(std::forward<T>(t)) ...);
  65. }
  66. template <class Op, class ... P> inline constexpr auto
  67. expr(Op && op, P && ... p)
  68. {
  69. if constexpr (is_verb<Op>) {
  70. return expr_verb(mp::iota<sizeof...(P)> {}, std::forward<Op>(op), std::forward<P>(p) ...);
  71. } else {
  72. return Expr<Op, std::tuple<P ...>> { std::forward<Op>(op), std::forward<P>(p) ... };
  73. }
  74. }
  75. template <class Op, class ... A> inline constexpr auto
  76. map(Op && op, A && ... a)
  77. {
  78. return expr(std::forward<Op>(op), start(std::forward<A>(a)) ...);
  79. }
  80. template <class Op, class ... A> inline constexpr void
  81. for_each(Op && op, A && ... a)
  82. {
  83. ply(map(std::forward<Op>(op), std::forward<A>(a) ...));
  84. }
  85. } // namespace ra