// Corre el experimento de duplicar con las seis funciones.
// Uso: run_doubling [archivo.csv]
#include <cstddef>
#include <cstdio>
#include <fstream>

#include "measure.h"

void print_table(const std::vector<Row>& rows) {
    std::printf("%10s %12s %14s %8s %12s %10s\n", "n", "pasos", "T (ns)", "razon", "T-Tant (ns)",
                "T/n (ns)");
    for (std::size_t i = 0; i < rows.size(); i++) {
        const Row& row = rows[i];
        double nanos = row.seconds * 1e9;
        double difference = i == 0 ? 0 : nanos - rows[i - 1].seconds * 1e9;
        std::printf("%10lld %12lld %14.1f %8.2f %12.1f %10.3f\n", row.n, row.steps, nanos, row.ratio,
                    difference, nanos / static_cast<double>(row.n));
    }
}

int main(int argc, char* argv[]) {
    const char* path = argc > 1 ? argv[1] : "doubling.csv";
    std::ofstream out(path);
    if (!out) {
        std::printf("no se pudo abrir %s\n", path);
        return 1;
    }
    out << "function,n,steps,seconds,ratio\n";

    for (const Mystery& mystery : all_mysteries()) {
        std::vector<Row> rows = doubling_experiment(mystery.fn, mystery.first_n, mystery.rows, 5);
        std::printf("\n%s\n", mystery.name.c_str());
        print_table(rows);
        write_csv(out, mystery.name, rows);
    }
    return 0;
}
