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