-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctors.h
73 lines (63 loc) · 1.75 KB
/
functors.h
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
// ptr
#include <memory>
// assert
#include <assert.h>
// stdint definitions
#include <stdint.h>
// =============================================================================
// FUNCTORS AND FUNCTOR HELPERS
// =============================================================================
// -----------------------------------------------------------------------------
// Functor for the functions of the kind y = f(x), Double -> Double
// -----------------------------------------------------------------------------
template <class F>
std::vector<double> applyCpuOp1(
const std::vector<double>& x,
F functor
) {
std::vector<double> y(x.size());
for (auto i = 0; i < x.size(); i++) {
y[i] = functor(x[i]);
}
return y;
}
template <class F>
std::vector<float> applyCpuOp1Float(
const std::vector<float>& x,
F functor
) {
std::vector<float> y(x.size());
for (auto i = 0; i < x.size(); i++) {
y[i] = functor(x[i]);
}
return y;
}
// -----------------------------------------------------------------------------
// Functor for the function of the kind z = f(x, y), (Double x Double) -> Double
// -----------------------------------------------------------------------------
template <class F>
std::vector<double> applyCpuOp2(
const std::vector<double>& x,
const std::vector<double>& y,
F functor
) {
assert(x.size() == y.size());
std::vector<double> z(x.size());
for (auto i = 0; i < x.size(); i++) {
z[i] = functor(x[i], y[i]);
}
return z;
}
template <class F>
std::vector<float> applyCpuOp2Float(
const std::vector<float>& x,
const std::vector<float>& y,
F functor
) {
assert(x.size() == y.size());
std::vector<float> z(x.size());
for (auto i = 0; i < x.size(); i++) {
z[i] = functor(x[i], y[i]);
}
return z;
}