lazy_vector_perf.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. // Defining this allows the assignment below of an expression to a double
  8. // without writing any specific code to do so.
  9. #include <boost/yap/expression.hpp>
  10. #include <algorithm>
  11. #include <cassert>
  12. #include <iostream>
  13. #include <vector>
  14. template<boost::yap::expr_kind Kind, typename Tuple>
  15. struct lazy_vector_expr;
  16. // This transform turns a terminal of std::vector<double> into a terminal
  17. // containing the nth double in that vector. Think of it as turning our
  18. // expression of vectors into an expression of scalars.
  19. struct take_nth
  20. {
  21. boost::yap::terminal<lazy_vector_expr, double> operator()(
  22. boost::yap::terminal<lazy_vector_expr, std::vector<double>> const &
  23. expr);
  24. std::size_t n;
  25. };
  26. // A custom expression template that defines lazy + and - operators that
  27. // produce expressions, and an eager [] operator that returns the nth element
  28. // of the expression.
  29. //[ lazy_vector_decl
  30. template<boost::yap::expr_kind Kind, typename Tuple>
  31. struct lazy_vector_expr
  32. {
  33. static const boost::yap::expr_kind kind = Kind;
  34. Tuple elements;
  35. // Note that this does not return an expression; it is greedily evaluated.
  36. auto operator[](std::size_t n) const;
  37. };
  38. BOOST_YAP_USER_BINARY_OPERATOR(plus, lazy_vector_expr, lazy_vector_expr)
  39. BOOST_YAP_USER_BINARY_OPERATOR(minus, lazy_vector_expr, lazy_vector_expr)
  40. //]
  41. template<boost::yap::expr_kind Kind, typename Tuple>
  42. auto lazy_vector_expr<Kind, Tuple>::operator[](std::size_t n) const
  43. {
  44. return boost::yap::evaluate(boost::yap::transform(*this, take_nth{n}));
  45. }
  46. boost::yap::terminal<lazy_vector_expr, double> take_nth::operator()(
  47. boost::yap::terminal<lazy_vector_expr, std::vector<double>> const & expr)
  48. {
  49. double x = boost::yap::value(expr)[n];
  50. // This move is something of a hack. The move indicates that the terminal
  51. // should keep the value of x (since, being an rvalue, it may be a
  52. // temporary), rather than a reference to x. See the "How Expression
  53. // Operands Are Treated" section of the tutorial for details.
  54. return boost::yap::make_terminal<lazy_vector_expr, double>(std::move(x));
  55. }
  56. // In order to define the += operator with the semantics we want, it's
  57. // convenient to derive a terminal type from a terminal instantiation of
  58. // lazy_vector_expr. note that we could have written a template
  59. // specialization here instead -- either one would work. That would of course
  60. // have required more typing.
  61. struct lazy_vector : lazy_vector_expr<
  62. boost::yap::expr_kind::terminal,
  63. boost::hana::tuple<std::vector<double>>>
  64. {
  65. lazy_vector() {}
  66. explicit lazy_vector(std::vector<double> && vec)
  67. {
  68. elements = boost::hana::tuple<std::vector<double>>(std::move(vec));
  69. }
  70. template<boost::yap::expr_kind Kind, typename Tuple>
  71. lazy_vector & operator+=(lazy_vector_expr<Kind, Tuple> const & rhs)
  72. {
  73. std::vector<double> & this_vec = boost::yap::value(*this);
  74. for (int i = 0, size = (int)this_vec.size(); i < size; ++i) {
  75. this_vec[i] += rhs[i];
  76. }
  77. return *this;
  78. }
  79. };
  80. lazy_vector v1{std::vector<double>(4, 1.0)};
  81. lazy_vector v2{std::vector<double>(4, 2.0)};
  82. lazy_vector v3{std::vector<double>(4, 3.0)};
  83. double get_d1_with_yap()
  84. {
  85. double retval = (v2 + v3)[2];
  86. return retval;
  87. }
  88. double get_d1_by_hand()
  89. {
  90. std::vector<double> & v2_ref = boost::yap::value(v2);
  91. std::vector<double> & v3_ref = boost::yap::value(v3);
  92. double retval = v2_ref[2] + v3_ref[2];
  93. return retval;
  94. }
  95. void update_v1_with_yap() { v1 += v2 - v3; }
  96. void update_v1_by_hand()
  97. {
  98. std::vector<double> & v1_ref = boost::yap::value(v1);
  99. std::vector<double> & v2_ref = boost::yap::value(v2);
  100. std::vector<double> & v3_ref = boost::yap::value(v3);
  101. for (int i = 0, size = (int)v1_ref.size(); i < size; ++i) {
  102. v1_ref[i] += v2_ref[i] - v3_ref[i];
  103. }
  104. }
  105. int main()
  106. {
  107. double d1_1 = get_d1_with_yap();
  108. std::cout << d1_1 << "\n";
  109. double d1_2 = get_d1_by_hand();
  110. std::cout << d1_2 << "\n";
  111. update_v1_with_yap();
  112. std::cout << '{' << v1[0] << ',' << v1[1] << ',' << v1[2] << ',' << v1[3]
  113. << '}' << "\n";
  114. boost::yap::value(v1) = std::vector<double>(4, 1.0);
  115. update_v1_by_hand();
  116. std::cout << '{' << v1[0] << ',' << v1[1] << ',' << v1[2] << ',' << v1[3]
  117. << '}' << "\n";
  118. // This expression is disallowed because it does not conform to the
  119. // implicit grammar. operator+= is only defined on terminals, not
  120. // arbitrary expressions.
  121. // (v2 + v3) += v1;
  122. return 0;
  123. }
  124. //]