vec3.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. //[ vec3
  7. #include <boost/yap/yap.hpp>
  8. #include <array>
  9. #include <iostream>
  10. struct take_nth
  11. {
  12. auto operator() (boost::yap::terminal<boost::yap::expression, std::array<int, 3>> const & expr)
  13. {
  14. int x = boost::yap::value(expr)[n];
  15. // The move forces the terminal to store the value of x, not a
  16. // reference.
  17. return boost::yap::make_terminal(std::move(x));
  18. }
  19. std::size_t n;
  20. };
  21. // Since this example doesn't constrain the operators defined on its
  22. // expressions, we can just use boost::yap::expression<> as the expression
  23. // template.
  24. using vec3_terminal = boost::yap::expression<
  25. boost::yap::expr_kind::terminal,
  26. boost::hana::tuple<std::array<int, 3>>
  27. >;
  28. // Customize the terminal type we use by adding index and assignment
  29. // operations.
  30. struct vec3 : vec3_terminal
  31. {
  32. explicit vec3 (int i = 0, int j = 0, int k = 0)
  33. {
  34. (*this)[0] = i;
  35. (*this)[1] = j;
  36. (*this)[2] = k;
  37. }
  38. explicit vec3 (std::array<int, 3> a)
  39. {
  40. (*this)[0] = a[0];
  41. (*this)[1] = a[1];
  42. (*this)[2] = a[2];
  43. }
  44. int & operator[] (std::ptrdiff_t i)
  45. { return boost::yap::value(*this)[i]; }
  46. int const & operator[] (std::ptrdiff_t i) const
  47. { return boost::yap::value(*this)[i]; }
  48. template <typename T>
  49. vec3 & operator= (T const & t)
  50. {
  51. decltype(auto) expr = boost::yap::as_expr(t);
  52. (*this)[0] = boost::yap::evaluate(boost::yap::transform(expr, take_nth{0}));
  53. (*this)[1] = boost::yap::evaluate(boost::yap::transform(expr, take_nth{1}));
  54. (*this)[2] = boost::yap::evaluate(boost::yap::transform(expr, take_nth{2}));
  55. return *this;
  56. }
  57. void print() const
  58. {
  59. std::cout << '{' << (*this)[0]
  60. << ", " << (*this)[1]
  61. << ", " << (*this)[2]
  62. << '}' << std::endl;
  63. }
  64. };
  65. // This is a stateful transform that keeps a running count of the terminals it
  66. // has seen.
  67. struct count_leaves_impl
  68. {
  69. auto operator() (vec3_terminal const & expr)
  70. {
  71. value += 1;
  72. return expr;
  73. }
  74. int value = 0;
  75. };
  76. template <typename Expr>
  77. int count_leaves (Expr const & expr)
  78. {
  79. count_leaves_impl impl;
  80. boost::yap::transform(boost::yap::as_expr(expr), impl);
  81. return impl.value;
  82. }
  83. int main()
  84. {
  85. vec3 a, b, c;
  86. c = 4;
  87. b[0] = -1;
  88. b[1] = -2;
  89. b[2] = -3;
  90. a = b + c;
  91. a.print();
  92. vec3 d;
  93. auto expr1 = b + c;
  94. d = expr1;
  95. d.print();
  96. int num = count_leaves(expr1);
  97. std::cout << num << std::endl;
  98. num = count_leaves(b + 3 * c);
  99. std::cout << num << std::endl;
  100. num = count_leaves(b + c * d);
  101. std::cout << num << std::endl;
  102. return 0;
  103. }
  104. //]