#include <iostream>
#include <fstream>
#include <sys/time.h>
#include <float.h>
using namespace std;
struct timeval start_time,end_time;
// We shall try results with different N values
static const int N=100;
// We shall try results with different OFFSET values
static const int OFFSET=0;
//--------------------------------------------------------------------------------------------------------------------------------------------
// template <typename Left, typename Right> // Left and Right might be expressions!
// class sum { // Functor to evaluate left[i]+right[i]
// private:
// Left myleft; // operator[](int) is another standard operator
// Right myright;
// public:
// sum(Left l, Right r) : myleft(l), myright(r) {}
// int operator[](int i) {return (myleft[i]+myright[i]);} // Left and right must also define operator[]
// };
//-----------------------------------------------------
// template <typename T>
// T arrayElementAdd(T a, T b) {
// return a+b;
// }
//-----------------------------------------------------
template<typename T>
class array {
private:
T* data;
int N;
public:
array( T *_data, int _N) : data(_data), N(_N) { } // Constructor
// // template <typename Left,typename Right> // Assign the result of sum<left,right>[i] to â??this arrayâ?
// void operator=(T right) { // Argument is a functor embodying left+right
// data[i] = right[i];
// }
void operator=(const T& right) { // Argument is a functor embodying left+right
for(int i=1; i<=N; i++)
data[i] = right[i];
}
array operator+(const array& right) {
array arr = *this;
// T *arrmem = new T(N);
// array<T> arr(arrmem,N);
for(int i=1; i<=N; i++)
arr[i]=arr[i]/*+right[i+OFFSET]*/;
return arr;
}
T& operator[](const int i) {return data[i];}
};
//-------------------------------------------------------------
int main()
{
int *amem = new int(N);
int *bmem = new int(N);
int *cmem = new int(N);
array<int> a(amem, N);
array<int> b(bmem, N);
array<int> c(cmem, N);
//Initialization of vectors with values from 1 to 500
for (int i=1;i<=N;i++)
{
c[i]=i;
b[i]=c[i];
a[i]=b[i];
}
gettimeofday(&start_time,NULL);
// Burada okuduÄ?umuz ilk deÄ?ere bakalım
cout<<"\n\ngettimeofday start_time (in seconds)="<<
start_time.tv_sec;
cout<<"\n\ngettimeofday start_time (in micro secs)="<<
start_time.tv_usec;
a=b+c;
for (int i=1;i<=N-OFFSET;i++)
{
// // a[i]=b[i]+c[i+OFFSET];
// a[i] = arrayElementAdd(b[i], c[i]);
cout << "\n" << (int)a[i];
}
gettimeofday(&end_time,NULL);
// Burada okuduÄ?umuz son deÄ?ere bakalım
cout<<"\n\ngettimeofday end_time (in seconds)="<<
end_time.tv_sec;
cout<<"\n\ngettimeofday end_time (in micro secs)="<<
end_time.tv_usec;
//Print the resultant array.
cout<<"\n\ngettimeofday wall time="<<
end_time.tv_sec - start_time.tv_sec+(end_time.tv_usec-start_time.tv_usec)/1e6;
}