/* Compare saved table data between two runs.
 * First run: build every table type & save each one to its own file.
 * Second run: use the files from the first run to rebuild each table type.
 * Then, save each rebuilt table to new files. */

#include <func/func.hpp>
#include <iostream>
#include <string>
#include <fstream>

#include "../sample_functions/gamma.hpp"

#define NUM_SAMPLES 30
#define MIN_STEP_EXP -6

bool file_exists(std::string name) {
  std::ifstream f(name);
  return f.good();
}

//---------------------------------------------------------------------------->>
int main(int argc, char* argv[]) {
  using namespace func;

  LookupTableGenerator<double> gen({FUNC_SET_F(MyFunction, double)}, MIN_ARG, MAX_ARG);

  std::string filename = "linear_interp_table";
  arma::vec stepsizes = arma::logspace(MIN_STEP_EXP, log10((MAX_ARG-MIN_ARG)/2), NUM_SAMPLES);
  for(auto h : stepsizes){
    std::string filenameh = filename + std::to_string( (unsigned) (10*std::abs(std::log(h))) ) + ".json";

    func::Timer t1;
    auto lut1 = gen.generate_by_step("UniformExactInterpTable<1>",h,filenameh);
    t1.stop();

    // just make absolutely sure that the file was actually written to
    if(!file_exists(filenameh)){ std::cerr << filenameh << " does not exist." << std::endl; exit(1); }

    func::Timer t2;
    auto lut2 = gen.generate_by_step("UniformExactInterpTable<1>",h,filenameh);
    t2.stop();

    std::cout << h << " " << t1.duration() << " " << t2.duration() << std::endl;
  }
}
