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