From 635ad1a2250848acfc618eff664ab4b8ff2b9071 Mon Sep 17 00:00:00 2001 From: bronku Date: Thu, 13 Nov 2025 19:52:34 +0100 Subject: [PATCH] verification --- config.hpp | 11 +++++++++-- main.cpp | 27 +++++++++++++++++++++++++++ makefile | 2 +- sort.cpp | 2 +- 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/config.hpp b/config.hpp index 7bdc3c9..d595410 100644 --- a/config.hpp +++ b/config.hpp @@ -10,6 +10,7 @@ public: std::string output_file = "1.in"; std::string directory = "tmp"; bool generate_data = false; + bool evaluate_file = false; int N = 100000; // number of records in a file int n = 101; // number of buffers int b = 10; // blocking factor @@ -18,14 +19,14 @@ public: { if (argc <= 1) { std::cerr << "Please specify input arguments\n"; - std::cerr << "Usage: " << argv[0] << " [-g] [-N count] [-i input] [-o output] [-d directory] [-n buffers] [-b block_size]\n"; + std::cerr << "Usage: " << argv[0] << " [-g] [-e] [-N count] [-i input] [-o output] [-d directory] [-n buffers] [-b block_size]\n"; throw std::invalid_argument("No input arguments"); } Configuration config; bool input_set = false; int opt; - while ((opt = getopt(argc, argv, "i:o:d:gN:n:b:")) != -1) { + while ((opt = getopt(argc, argv, "i:o:d:geN:n:b:")) != -1) { switch (opt) { case 'i': config.input_file = optarg; @@ -40,6 +41,9 @@ public: case 'g': config.generate_data = true; break; + case 'e': + config.evaluate_file = true; + break; case 'N': config.N = std::stoi(optarg); break; @@ -57,6 +61,9 @@ public: if (config.generate_data && input_set) { throw std::invalid_argument("Cannot specify both -g and -i options"); } + if (config.generate_data && config.evaluate_file) { + throw std::invalid_argument("Cannot specify both -g and -e options"); + } return config; } diff --git a/main.cpp b/main.cpp index e833509..1861b9c 100644 --- a/main.cpp +++ b/main.cpp @@ -18,6 +18,28 @@ void generate_file(int N, const std::string& filename) } } +void read_and_evaluate(std::string filename, int n) +{ + Buffer main_buffer(n); + auto buff = main_buffer.divide(1)[0]; + + std::ifstream in_stream(filename); + Reader input_reader(in_stream); + + while (true) { + size_t records_read = read_chunk(input_reader, buff); + + if (records_read == 0) { + break; + } + + for (size_t i = 0; i < records_read; ++i) { + const Record& rec = buff[i]; + std::cout << "[Evaluate i = " << i << ": " << rec.evaluate() << "] " << rec << "\n"; + } + } +} + int main(int argc, char** argv) { try { @@ -28,6 +50,11 @@ int main(int argc, char** argv) return 0; } + if (opts.evaluate_file) { + read_and_evaluate(opts.input_file, opts.N); + return 0; + } + sort_file(opts); } catch (const std::exception& e) { diff --git a/makefile b/makefile index 0446cba..83535d7 100644 --- a/makefile +++ b/makefile @@ -1,6 +1,6 @@ CFLAGS = -g -lc++ -std=c++17 -build/main: main.cpp sort.cpp buffer.hpp reader.hpp writer.hpp build +build/main: main.cpp sort.cpp config.hpp buffer.hpp reader.hpp writer.hpp build clang $(CFLAGS) main.cpp -o build/main build: diff --git a/sort.cpp b/sort.cpp index 8e8fdec..5697e14 100644 --- a/sort.cpp +++ b/sort.cpp @@ -31,7 +31,7 @@ void create_initial_runs(const std::string& input_filename, const std::string& d { auto buff = main_buffer.divide(1)[0]; - std::ifstream in_stream(input_filename, std::ios::binary); + std::ifstream in_stream(input_filename); Reader input_reader(in_stream); for (size_t run_index = 0;; run_index++) { std::cout << "creating run " << run_index << '\n';