#include <unsupported/Eigen/FFT>

#include <complex>
#include <ctime>
#include <iostream>
#include <iterator>
#include <vector>
#include <map>

using namespace std;

void test1();
void test2();
void test3();

int main() {

  const int nnode = 1000;

  std::map<int, float> m_nodes_timer;
  for (int i = 0; i < nnode; ++i) {
    m_nodes_timer[i] = 0;
  }

  // std::vector<float> times = {1E4, 1E5, 1E6, 1E7};
  std::vector<float> times;
  for (int i = 0; i < 100; ++i)
    times.push_back(1E6);

  for (auto time : times) {
    std::clock_t start_init = std::clock();
    double duration_clock, duration_all;

    std::clock_t start;
    double duration = 0;

    // clock only
    for (int i = 0; i < time; ++i) {
      start = std::clock();
      duration = (std::clock() - start) / (double)CLOCKS_PER_SEC;
    }

    duration_clock = (std::clock() - start_init) / (double)CLOCKS_PER_SEC;

    start_init = std::clock();
    // all
    for (int i = 0; i < time; ++i) {
      int inode = i % nnode;

      start = std::clock();
      duration = (std::clock() - start) / (double)CLOCKS_PER_SEC;

      if (m_nodes_timer.find(inode) != m_nodes_timer.end()) {
        m_nodes_timer[inode] += duration;
      } else {
        m_nodes_timer[inode] = duration;
      }

      // m_nodes_timer[inode] += duration;
    }

    duration_all = (std::clock() - start_init) / (double)CLOCKS_PER_SEC;
    std::cout << duration_clock << ", " << duration_all << std::endl;
  }

  return 0;
}

void test3() {
  int nrow = 800;
  int ncol = 6000;

  Eigen::ArrayXXcf d1 = Eigen::ArrayXXcf::Random(nrow, ncol);
  Eigen::ArrayXf d2 = Eigen::ArrayXf::Random(ncol);
  Eigen::ArrayXXcf d = Eigen::ArrayXXcf::Zero(nrow, ncol);

  typedef std::vector<float> realseq_t;
  typedef std::vector<std::vector<std::complex<float>>> mat_t;

  mat_t d1_t(nrow);

  for (int i = 0; i < nrow; ++i) {
    d1_t[i].resize(ncol);
    for (int j = 0; j < ncol; ++j) {
      d1_t[i][j] = d1(i, j);
    }
  }

  realseq_t d2_t(ncol);
  for (int i = 0; i < ncol; ++i) {
    d2_t[i] = d2(i);
  }

  std::clock_t start;
  double duration_total;

  // array-wise - Eigen
  {
    d = Eigen::ArrayXXf::Zero(nrow, ncol);
    start = std::clock();

    for (int irow = 0; irow < nrow; ++irow) {
      d.row(irow) = d1.row(irow) + d2.transpose();
    }

    duration_total = (std::clock() - start) / (double)CLOCKS_PER_SEC;
    std::cout << "array-wise - Eigen: \t\t" << duration_total << '\n';
    if (nrow * ncol < 100)
      cout << d << endl;
  }

  // element-wise- Eigen
  {
    d = Eigen::ArrayXXf::Zero(nrow, ncol);
    start = std::clock();

    for (int i = 0; i < nrow; ++i) {
      for (int j = 0; j < ncol; ++j) {
        d(i, j) = d1(i, j) + d2(j);
      }
    }

    duration_total = (std::clock() - start) / (double)CLOCKS_PER_SEC;
    std::cout << "element-wise- Eigen: \t\t" << duration_total << '\n';
    if (nrow * ncol < 100)
      cout << d << endl;
  }

  // element-wise - Mix
  {
    d = Eigen::ArrayXXf::Zero(nrow, ncol);
    start = std::clock();

    for (int i = 0; i < nrow; ++i) {
      for (int j = 0; j < ncol; ++j) {
        d(i, j) = d1(i, j) + d2_t[j];
      }
    }

    duration_total = (std::clock() - start) / (double)CLOCKS_PER_SEC;
    std::cout << "element-wise - Mix: \t\t" << duration_total << '\n';
    if (nrow * ncol < 100)
      cout << d << endl;
  }

  // element-wise - std::vector
  {
    d = Eigen::ArrayXXf::Zero(nrow, ncol);
    start = std::clock();

    for (int i = 0; i < nrow; ++i) {
      for (int j = 0; j < ncol; ++j) {
        d(i, j) = d1_t[i][j] + d2(j);
      }
    }

    duration_total = (std::clock() - start) / (double)CLOCKS_PER_SEC;
    std::cout << "element-wise - std::vector: \t" << duration_total << '\n';
    if (nrow * ncol < 100)
      cout << d << endl;
  }
}