lazy_vector.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright (C) 2016-2018 T. Zachary Laine
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. //[ lazy_vector
  7. #include <boost/yap/expression.hpp>
  8. #include <algorithm>
  9. #include <cassert>
  10. #include <iostream>
  11. #include <vector>
  12. template <boost::yap::expr_kind Kind, typename Tuple>
  13. struct lazy_vector_expr;
  14. // This transform turns a terminal of std::vector<double> into a terminal
  15. // containing the nth double in that vector. Think of it as turning our
  16. // expression of vectors into an expression of scalars.
  17. struct take_nth
  18. {
  19. boost::yap::terminal<lazy_vector_expr, double>
  20. operator() (boost::yap::terminal<lazy_vector_expr, std::vector<double>> const & expr);
  21. std::size_t n;
  22. };
  23. // A custom expression template that defines lazy + and - operators that
  24. // produce expressions, and an eager [] operator that returns the nth element
  25. // of the expression.
  26. //[ lazy_vector_decl
  27. template <boost::yap::expr_kind Kind, typename Tuple>
  28. struct lazy_vector_expr
  29. {
  30. static const boost::yap::expr_kind kind = Kind;
  31. Tuple elements;
  32. // Note that this does not return an expression; it is greedily evaluated.
  33. auto operator[] (std::size_t n) const;
  34. };
  35. BOOST_YAP_USER_BINARY_OPERATOR(plus, lazy_vector_expr, lazy_vector_expr)
  36. BOOST_YAP_USER_BINARY_OPERATOR(minus, lazy_vector_expr, lazy_vector_expr)
  37. //]
  38. template <boost::yap::expr_kind Kind, typename Tuple>
  39. auto lazy_vector_expr<Kind, Tuple>::operator[] (std::size_t n) const
  40. { return boost::yap::evaluate(boost::yap::transform(*this, take_nth{n})); }
  41. boost::yap::terminal<lazy_vector_expr, double>
  42. take_nth::operator() (boost::yap::terminal<lazy_vector_expr, std::vector<double>> const & expr)
  43. {
  44. double x = boost::yap::value(expr)[n];
  45. // This move is something of a hack; we're forcing Yap to take a copy of x
  46. // by using std::move(). The move indicates that the terminal should keep
  47. // the value of x (since, being an rvalue, it may be a temporary), rather
  48. // than a reference to x. See the "How Expression Operands Are Treated"
  49. // section of the tutorial for details.
  50. return boost::yap::make_terminal<lazy_vector_expr, double>(std::move(x));
  51. }
  52. // In order to define the += operator with the semantics we want, it's
  53. // convenient to derive a terminal type from a terminal instantiation of
  54. // lazy_vector_expr. Note that we could have written a template
  55. // specialization here instead -- either one would work. That would of course
  56. // have required more typing.
  57. struct lazy_vector :
  58. lazy_vector_expr<
  59. boost::yap::expr_kind::terminal,
  60. boost::hana::tuple<std::vector<double>>
  61. >
  62. {
  63. lazy_vector () {}
  64. explicit lazy_vector (std::vector<double> && vec)
  65. { elements = boost::hana::tuple<std::vector<double>>(std::move(vec)); }
  66. template <boost::yap::expr_kind Kind, typename Tuple>
  67. lazy_vector & operator+= (lazy_vector_expr<Kind, Tuple> const & rhs)
  68. {
  69. std::vector<double> & this_vec = boost::yap::value(*this);
  70. for (int i = 0, size = (int)this_vec.size(); i < size; ++i) {
  71. this_vec[i] += rhs[i];
  72. }
  73. return *this;
  74. }
  75. };
  76. int main ()
  77. {
  78. lazy_vector v1{std::vector<double>(4, 1.0)};
  79. lazy_vector v2{std::vector<double>(4, 2.0)};
  80. lazy_vector v3{std::vector<double>(4, 3.0)};
  81. double d1 = (v2 + v3)[2];
  82. std::cout << d1 << "\n";
  83. v1 += v2 - v3;
  84. std::cout << '{' << v1[0] << ',' << v1[1]
  85. << ',' << v1[2] << ',' << v1[3] << '}' << "\n";
  86. // This expression is disallowed because it does not conform to the
  87. // implicit grammar. operator+= is only defined on terminals, not
  88. // arbitrary expressions.
  89. // (v2 + v3) += v1;
  90. return 0;
  91. }
  92. //]