-
Notifications
You must be signed in to change notification settings - Fork 0
/
naive_implementation.hpp
115 lines (85 loc) · 2.09 KB
/
naive_implementation.hpp
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
#ifndef NAIVE_IMPLEMENTATION_HPP_INCLUDED
#define NAIVE_IMPLEMENTATION_HPP_INCLUDED
#include <cassert>
#include <cstddef>
#include <cmath>
namespace naive_implementation
{
template <typename Real>
class valarray
{
public:
INLINE valarray(std::size_t size)
: _size(size)
, _values(new Real[size])
{ }
INLINE valarray(std::size_t size, Real value)
: _size(size)
, _values(new Real[size])
{
for (std::size_t i = 0; i < _size; ++i)
_values[i] = value;
}
INLINE ~valarray()
{
delete[] _values;
}
INLINE valarray(const valarray& copy)
: _size(copy._size)
, _values(new Real[copy._size])
{
for (std::size_t i = 0; i < _size; ++i)
_values[i] = copy._values[i];
}
INLINE valarray& operator= (const valarray& copy)
{
assert(_size == copy._size);
for (std::size_t i = 0; i < _size; ++i)
_values[i] = copy._values[i];
return *this;
}
INLINE Real operator[] (std::size_t i) const
{
assert(i < _size);
return _values[i];
}
INLINE Real& operator[] (std::size_t i)
{
assert(i < _size);
return _values[i];
}
INLINE std::size_t size() const
{
return _size;
}
INLINE valarray operator+ (const valarray& rhs) const
{
assert(_size == rhs._size);
valarray result(_size);
for (std::size_t i = 0; i < _size; ++i)
result._values[i] = _values[i] + rhs._values[i];
return result;
}
INLINE valarray operator* (const valarray& rhs) const
{
assert(_size == rhs._size);
valarray result(_size);
for (std::size_t i = 0; i < _size; ++i)
result._values[i] = _values[i] * rhs._values[i];
return result;
}
private:
std::size_t _size;
Real* _values;
} ; // end class valarray<Real>
template <typename Real>
INLINE valarray<Real> sqrt(const valarray<Real>& value)
{
const std::size_t size = value.size();
valarray<Real> result(size);
for (std::size_t i = 0; i < size; ++i)
result[i] = std::sqrt(value[i]);
return result;
}
} // end namespace naive_implementation
#endif // end NAIVE_IMPLEMENTATION_HPP_INCLUDED