diff --git a/buffer.hpp b/buffer.hpp index 8e48592..df2fb52 100644 --- a/buffer.hpp +++ b/buffer.hpp @@ -20,6 +20,10 @@ private: } public: + // so that i can create a vecotr of set size, without the .reserve(), and other stuff + SubBuffer() + { + } // for std::sort support using iterator = Record*; iterator begin() { return data_; } diff --git a/main.cpp b/main.cpp index f621e2c..e833509 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,6 @@ #include "config.hpp" #include "record.hpp" +#include "sort.cpp" #include "writer.hpp" #include #include @@ -17,13 +18,6 @@ void generate_file(int N, const std::string& filename) } } -int sort_file(const Configuration& opts) -{ - // TODO: Implement sorting with new buffer classes - std::cout << "Sorting not yet implemented\n"; - return 0; -} - int main(int argc, char** argv) { try { diff --git a/makefile b/makefile index 6f37da3..0446cba 100644 --- a/makefile +++ b/makefile @@ -1,15 +1,15 @@ CFLAGS = -g -lc++ -std=c++17 -build/main: main.cpp build +build/main: main.cpp sort.cpp buffer.hpp reader.hpp writer.hpp build clang $(CFLAGS) main.cpp -o build/main build: mkdir -p build -build/run: build +build/run: mkdir -p build/run -build/run/tmp: build/run +build/run/tmp: mkdir -p build/run/tmp clean: @@ -17,11 +17,12 @@ clean: generate: build/run/1.in -build/run/1.in: build/run build/main +# actually should depend on build/main and build/run, but that will trigger it every recompile, and i don't need that +build/run/1.in: build/run/tmp build/main -g -o build/run/1.in run: build/main build/run/1.in build/run/tmp - build/main -i build/run/1.in -d build/run/tmp | less + build/main -i build/run/1.in -d build/run/tmp debug: build/main tests/1.in lldb -- build/main diff --git a/reader.hpp b/reader.hpp index c9432fb..2c69b3e 100644 --- a/reader.hpp +++ b/reader.hpp @@ -1,3 +1,4 @@ +// #todo maybe overload >> operators insted of read() #pragma once #include "record.hpp" #include @@ -16,16 +17,21 @@ private: // returns true if at least one record was returned bool refill_buffer() { + std::cout << "refilling buffer\n"; last_element_idx = -1; current_idx = 0; for (size_t i = 0; i < buffer_size; i++) { + std::cout << "buffer refill: i: " << i << '\n'; if (!(input_stream >> read_buffer[i])) { + std::cout << "input stream error\n"; + std::cout << read_buffer[i] << '\n'; break; } - last_element_idx = static_cast(i); + last_element_idx = i; } + std::cout << "buffer refill successfull\n"; return last_element_idx >= 0; } diff --git a/sort.cpp b/sort.cpp new file mode 100644 index 0000000..8e8fdec --- /dev/null +++ b/sort.cpp @@ -0,0 +1,55 @@ +#pragma once +#include "buffer.hpp" +#include "config.hpp" +#include "reader.hpp" +#include "writer.hpp" +#include +// +// returns amount of records read +size_t read_chunk(Reader& reader, SubBuffer& buff) +{ + for (size_t i = 0; i < buff.size(); i++) { + std::optional in = reader.read(); + if (!in.has_value()) { + return i; + } + buff[i] = in.value(); + } + return buff.size(); +} + +void write_to_file(const std::string& filename, SubBuffer& buff, size_t n) +{ + std::ofstream out_stream(filename); + Writer output_writer(out_stream); + for (size_t j = 0; j < n; j++) { + output_writer.write(buff[j]); + } +} + +void create_initial_runs(const std::string& input_filename, const std::string& directory, Buffer& main_buffer) +{ + auto buff = main_buffer.divide(1)[0]; + + std::ifstream in_stream(input_filename, std::ios::binary); + Reader input_reader(in_stream); + for (size_t run_index = 0;; run_index++) { + std::cout << "creating run " << run_index << '\n'; + size_t size = read_chunk(input_reader, buff); + std::cout << "read " << size << " records\n"; + if (size == 0) { + break; + } + std::sort(buff.begin(), buff.begin() + size); + write_to_file(directory + "/" + std::to_string(run_index) + ".run", buff, size); + } +} + +int sort_file(const Configuration& opts) +{ + Buffer main_buffer(opts.n * opts.b); + create_initial_runs(opts.input_file, opts.directory, main_buffer); + // TODO: Implement sorting with new buffer classes + std::cout << "Sorting not yet implemented\n"; + return 0; +} diff --git a/writer.hpp b/writer.hpp index 149b71e..28e089d 100644 --- a/writer.hpp +++ b/writer.hpp @@ -1,3 +1,4 @@ +// #todo maybe overload << operators insted of write() #pragma once #include "record.hpp" #include @@ -17,7 +18,7 @@ private: bool flush_buffer() { for (size_t i = 0; i < current_index; ++i) { - if (!(output_stream << write_buffer[i])) { + if (!(output_stream << write_buffer[i] << " ")) { return false; } }