| 1 |
#include <iostream> |
| 2 |
#include <vector> |
| 3 |
#include <functional> |
| 4 |
|
| 5 |
using namespace std; |
| 6 |
|
| 7 |
enum |
| 8 |
{ |
| 9 |
ADD, |
| 10 |
SUB, |
| 11 |
}; |
| 12 |
|
| 13 |
template <typename LHS, typename RHS, int operation> |
| 14 |
struct Operation; |
| 15 |
|
| 16 |
template <typename LHS, typename RHS> |
| 17 |
struct Operation<LHS, RHS, ADD> |
| 18 |
{ |
| 19 |
static auto cpt(auto const &lhs, auto const &rhs) |
| 20 |
{ |
| 21 |
return lhs + rhs; |
| 22 |
}; |
| 23 |
}; |
| 24 |
|
| 25 |
template <typename LHS, typename RHS> |
| 26 |
struct Operation<LHS, RHS, SUB> |
| 27 |
{ |
| 28 |
static auto cpt(auto const &lhs, auto const &rhs) |
| 29 |
{ |
| 30 |
return lhs - rhs; |
| 31 |
}; |
| 32 |
}; |
| 33 |
|
| 34 |
template<typename LHS, typename RHS, int operation> |
| 35 |
struct BinExpr; |
| 36 |
|
| 37 |
template<typename Type> |
| 38 |
struct BasicType |
| 39 |
{ |
| 40 |
using ObjType = Type; |
| 41 |
}; |
| 42 |
|
| 43 |
template<typename LHS, typename RHS, int operation> |
| 44 |
struct BasicType<BinExpr<LHS, RHS, operation>> |
| 45 |
{ |
| 46 |
using ObjType = typename BinExpr<LHS, RHS, operation>::ObjType; |
| 47 |
}; |
| 48 |
|
| 49 |
template<typename LHS, typename RHS, int operation> |
| 50 |
struct BinExpr |
| 51 |
{ |
| 52 |
using ObjType = typename BasicType<RHS>::ObjType; |
| 53 |
using value_type = typename ObjType::value_type; |
| 54 |
|
| 55 |
LHS const &d_lhs; |
| 56 |
RHS const &d_rhs; |
| 57 |
|
| 58 |
BinExpr(LHS const &lhs, RHS const &rhs) |
| 59 |
: |
| 60 |
d_lhs(lhs), |
| 61 |
d_rhs(rhs) |
| 62 |
{} |
| 63 |
|
| 64 |
size_t size() const |
| 65 |
{ |
| 66 |
return d_lhs.size(); |
| 67 |
} |
| 68 |
|
| 69 |
value_type operator[](size_t ix) const |
| 70 |
{ |
| 71 |
return Operation<LHS, RHS, operation>::cpt(d_lhs[ix], d_rhs[ix]); |
| 72 |
} |
| 73 |
|
| 74 |
operator ObjType() const |
| 75 |
{ |
| 76 |
ObjType retVal; |
| 77 |
|
| 78 |
retVal.resize(size()); |
| 79 |
|
| 80 |
retVal.reserve(size()); |
| 81 |
|
| 82 |
for (size_t ix = 0, end = size(); ix != end; ++ix) |
| 83 |
new(&retVal[ix]) value_type{ (*this)[ix] }; |
| 84 |
|
| 85 |
return retVal; |
| 86 |
} |
| 87 |
}; |
| 88 |
|
| 89 |
|
| 90 |
// define the operators: ADD, SUB, etc. The operation to perform is |
| 91 |
// specified as enum value |
| 92 |
|
| 93 |
template<typename LHS, typename RHS> |
| 94 |
BinExpr<LHS, RHS, ADD> operator+(LHS const &lhs, RHS const &rhs) |
| 95 |
{ |
| 96 |
return BinExpr<LHS, RHS, ADD>(lhs, rhs); |
| 97 |
} |
| 98 |
|
| 99 |
template<typename LHS, typename RHS> |
| 100 |
BinExpr<LHS, RHS, SUB> operator-(LHS const &lhs, RHS const &rhs) |
| 101 |
{ |
| 102 |
return BinExpr<LHS, RHS, SUB>(lhs, rhs); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
struct VI: public vector<int> // define the data structure to |
| 107 |
{ // operate on |
| 108 |
static size_t const s_max = 10000; |
| 109 |
|
| 110 |
VI() |
| 111 |
: |
| 112 |
vector<int>(s_max, 1) |
| 113 |
{} |
| 114 |
}; |
| 115 |
|
| 116 |
int main(int argc, char **argv) |
| 117 |
{ |
| 118 |
if (argc == 1) |
| 119 |
{ |
| 120 |
cout << "arg1: number of iterations\n"; |
| 121 |
return 0; |
| 122 |
} |
| 123 |
|
| 124 |
VI a, b, c, d; |
| 125 |
|
| 126 |
for (size_t idx = 0, count = stoul(argv[1]); idx != count; ++idx) |
| 127 |
d = a + b + c + d + a + b + c + d; |
| 128 |
|
| 129 |
cout << d.front() << ", " << d.back() << '\n'; |
| 130 |
|
| 131 |
for (size_t idx = 0, count = stoul(argv[1]); idx != count; ++idx) |
| 132 |
d = a - b - c - d - a - b - c - d; |
| 133 |
|
| 134 |
cout << d.front() << ", " << d.back() << '\n'; |
| 135 |
} |