-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrtp_1.cpp
140 lines (107 loc) · 4.16 KB
/
crtp_1.cpp
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
// Guideline 26: Use CRTP to Introduce Static Type Categories
//---- <DenseVector.h> ----------------------------------------------------------------------------
#include <cmath>
#include <numeric>
#include <ostream>
template <typename Derived>
struct DenseVector
{
constexpr Derived &derived() noexcept { return static_cast<Derived &>(*this); }
constexpr Derived const &derived() const noexcept { return static_cast<Derived const &>(*this); }
constexpr size_t size() const noexcept { return derived().size(); }
decltype(auto) operator[](size_t index) noexcept { return derived()[index]; }
decltype(auto) operator[](size_t index) const noexcept { return derived()[index]; }
decltype(auto) begin() noexcept { return derived().begin(); }
decltype(auto) begin() const noexcept { return derived().begin(); }
decltype(auto) end() noexcept { return derived().end(); }
decltype(auto) end() const noexcept { return derived().end(); }
};
template <typename Derived>
std::ostream &operator<<(std::ostream &os, DenseVector<Derived> const &vector)
{
size_t const size(vector.size());
os << "(";
for (size_t i = 0UL; i < size; ++i)
{
os << " " << vector[i];
}
os << " )";
return os;
}
template <typename Derived>
decltype(auto) l2norm(DenseVector<Derived> const &vector)
{
using T = typename Derived::value_type;
return std::sqrt(std::inner_product(std::begin(vector), std::end(vector), std::begin(vector), T{}));
}
//---- <DynamicVector.h> --------------------------------------------------------------------------
// #include <DenseVector.h>
#include <vector>
#include <initializer_list>
template <typename T>
class DynamicVector
: public DenseVector<DynamicVector<T>>
{
public:
using value_type = T;
using iterator = typename std::vector<T>::iterator;
using const_iterator = typename std::vector<T>::const_iterator;
DynamicVector() = default;
DynamicVector(std::initializer_list<T> init)
: values_(std::begin(init), std::end(init))
{
}
size_t size() const noexcept { return values_.size(); }
T &operator[](size_t index) noexcept { return values_[index]; }
T const &operator[](size_t index) const noexcept { return values_[index]; }
iterator begin() noexcept { return values_.begin(); }
const_iterator begin() const noexcept { return values_.begin(); }
iterator end() noexcept { return values_.end(); }
const_iterator end() const noexcept { return values_.end(); }
// ... Many numeric functions
private:
std::vector<T> values_;
};
//---- <StaticVector.h> ---------------------------------------------------------------------------
// #include <DenseVector.h>
#include <array>
#include <initializer_list>
template <typename T, size_t Size>
class StaticVector
: public DenseVector<StaticVector<T, Size>>
{
public:
using value_type = T;
using iterator = typename std::array<T, Size>::iterator;
using const_iterator = typename std::array<T, Size>::const_iterator;
StaticVector() = default;
StaticVector(std::initializer_list<T> init)
{
std::copy(std::begin(init), std::end(init), std::begin(values_));
}
size_t size() const noexcept { return values_.size(); }
T &operator[](size_t index) noexcept { return values_[index]; }
T const &operator[](size_t index) const noexcept { return values_[index]; }
iterator begin() noexcept { return values_.begin(); }
const_iterator begin() const noexcept { return values_.begin(); }
iterator end() noexcept { return values_.end(); }
const_iterator end() const noexcept { return values_.end(); }
// ... Many numeric functions
private:
std::array<T, Size> values_;
};
//---- <Main.cpp> ---------------------------------------------------------------------------------
// #include <DynamicVector.h>
// #include <StaticVector.h>
#include <cstdlib>
#include <iostream>
int main()
{
DynamicVector<int> const a{1, 2, 3};
StaticVector<int, 4UL> const b{4, 5, 6, 7};
std::cout << "\n"
<< " a = " << a << ", L2-norm = " << l2norm(a) << "\n"
<< " b = " << b << ", L2-norm = " << l2norm(b) << "\n"
<< "\n";
return EXIT_SUCCESS;
}