-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathExercises_08_ExpressionTemplates.cpp
More file actions
140 lines (113 loc) · 4.35 KB
/
Copy pathExercises_08_ExpressionTemplates.cpp
File metadata and controls
140 lines (113 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// =====================================================================================
// Exercises_08_ExpressionTemplates.cpp
// =====================================================================================
module modern_cpp_exercises:expression_templates;
import std;
import scoped_timer;
namespace Exercises_ExpressionTemplates {
template <typename T>
static T scalarProduct(const std::vector<T>& a, const std::vector<T>& b)
{
T product{};
for (std::size_t i{}; i != a.size(); ++i) {
product += (a[i]) * (b[i]);
}
return product;
}
template <typename T>
static T scalarProductEx(typename std::vector<T>::iterator&& a, typename std::vector<T>::iterator&& end, typename std::vector<T>::iterator&& b)
{
T product{};
for (; a != end; ++a, ++b) {
product += (*a) * (*b);
}
return product;
}
static void test_01()
{
std::vector<double> a{ 1, 2, 3, 4, 5 };
std::vector<double> b{ 6, 7, 8, 9, 10 };
auto prod1 { scalarProduct<double>(a, b) };
auto prod2 { scalarProduct<double>(a, a) };
std::cout << "scalarProduct<double>(a, b) = " << prod1 << std::endl; // 130
std::cout << "scalarProduct<double>(a, a) = " << prod2 << std::endl; // 55
}
static void test_02()
{
std::vector<double> a{ 1, 2, 3, 4, 5 };
std::vector<double> b{ 6, 7, 8, 9, 10 };
auto prod1{ scalarProductEx<double>(a.begin(), a.end(), b.begin()) };
auto prod2{ scalarProductEx<double>(a.begin(), a.end(), a.begin()) };
std::cout << "scalarProductEx<double>(a, b) = " << prod1 << std::endl; // 130
std::cout << "scalarProductEx<double>(a, a) = " << prod2 << std::endl; // 55
}
// primary template
template <std::size_t N, typename T>
class ScalarProduct {
public:
static inline T result(
const typename std::vector<T>::const_iterator a,
const typename std::vector<T>::const_iterator b) {
return (*a) * (*b) + ScalarProduct<N - 1, T>::result(a + 1, b + 1);
}
};
// partial specialization to terminate recursion
template <typename T>
class ScalarProduct<1, T> {
public:
static inline T result(
const typename std::vector<T>::const_iterator a,
const typename std::vector<T>::const_iterator b) {
return (*a) * (*b);
}
};
static void test_03()
{
std::vector<double> a{ 1, 2, 3, 4, 5 };
std::vector<double> b{ 6, 7, 8, 9, 10 };
auto prod1{ ScalarProduct<5, double>::result(a.cbegin(), b.cbegin()) };
auto prod2{ ScalarProduct<5, double>::result(a.cbegin(), a.cbegin()) };
std::cout << "ScalarProduct<5, double> = " << prod1 << std::endl; // 130
std::cout << "ScalarProduct<5, double> = " << prod2 << std::endl; // 55
}
static void test_04()
{
constexpr auto MaxIterations = 1000000000;
std::vector<double> a{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
{
ScopedTimer watch{};
auto prod{ 0.0 };
for (std::size_t n{}; n != MaxIterations; ++n) {
prod = scalarProduct<double>(a, a);
}
std::cout << "scalarProduct<double>(a, a): " << prod << std::endl;
}
{
ScopedTimer watch{};
auto prod{ 0.0 };
for (std::size_t n{}; n != MaxIterations; ++n) {
prod = scalarProductEx<double>(a.begin(), a.end(), a.begin());
}
std::cout << "scalarProductEx<double>(a.begin(), a.end(), a.begin()): " << prod << std::endl;
}
{
ScopedTimer watch{};
auto prod{ 0.0 };
for (std::size_t n{}; n != MaxIterations; ++n) {
prod = ScalarProduct<10, double>::result(a.cbegin(), a.cbegin());
}
std::cout << "ScalarProduct<10, double>::result(a.cbegin(), a.cbegin()): " << prod << std::endl;
}
}
}
void test_exercices_expression_templates()
{
using namespace Exercises_ExpressionTemplates;
test_01();
test_02();
test_03();
test_04();
}
// =====================================================================================
// End-of-File
// =====================================================================================