This commit is contained in:
bronku 2025-12-13 22:11:52 +01:00
parent fddaf6b9d0
commit 4becf40f06
5 changed files with 19 additions and 100 deletions

View file

@ -1 +0,0 @@
BasedOnStyle: WebKit

View file

@ -1,5 +0,0 @@
CompileFlags:
Add:
- "-std=c++20"
- "-I./include"
CompilationDatabase: build

37
TODO.md
View file

@ -1,37 +0,0 @@
- [x] file operations
- [x] write to file (at index)
- [x] read file
- [x] a tape type - representing a file?
- [x] a record type - x1,x2,x3,x4,x5
- [x] comparing records
- [x] buffer quick sort
- [x] configuration
- [x] cmd args - man getopts
- [x] file input
- [ ] keyboard input
- [x] stage 1
- [x] reading chunks
- [x] sorting
- [x] writing runs
- [ ] stage 2
- [ ] implement a min heap with following operations
- [ ] pop
- [ ] push
- [ ] step 4
- [ ] split the buffer into chunks of size n, and (b-1)n
- [ ] create a min heap of size (b-1)n
- [ ] populate the heap with (b-1) runs of n elements (each from a different file)
- [ ] find a way to mark if a last element from a certain file has been popped
- [ ] when a merged buffer is full, write it to file
- [ ] pop off the top element until there are no more elements from a certain array
- [ ] load another run from that file, and rehapify
- [ ] repeat untill all files empty
- [ ] the resulting file should get the new smallest filename in tmp dir
- [ ] multiway merge
- [ ] writing larger runs
- [ ] repeat untill done
- [ ] counting read, and write operations
- [ ] testing
- [ ] report

View file

@ -6,24 +6,27 @@
class MergeSorter {
private:
std::vector<Record> buffer_;
const std::string& tmp_dir_;
size_t buffer_rows_ = 0;
size_t disk_reads_ = 0;
size_t disk_writes_ = 0;
size_t phases_ = 0;
std::vector<Record> buffer_;
const std::string &tmp_dir_;
size_t buffer_rows_ = 0;
size_t disk_reads_ = 0;
size_t disk_writes_ = 0;
size_t phases_ = 0;
std::span<Record> get_chunk_span(size_t chunk_index);
size_t create_initial_runs(const std::string& input_file, const std::string& next_dir);
size_t merge_pass(const std::string& current_dir, const std::string& next_dir, size_t run_count);
void perform_k_way_merge(std::vector<std::unique_ptr<FileReader>>& readers,
FileWriter& writer);
std::span<Record> get_chunk_span(size_t chunk_index);
size_t create_initial_runs(const std::string &input_file,
const std::string &next_dir);
size_t merge_pass(const std::string &current_dir, const std::string &next_dir,
size_t run_count);
void perform_k_way_merge(std::vector<std::unique_ptr<FileReader>> &readers,
FileWriter &writer);
public:
MergeSorter(size_t buffer_rows, size_t buffer_cols, const std::string& tmp_dir);
void sort_file(const std::string& input_file, const std::string& output_file);
MergeSorter(size_t buffer_rows, size_t buffer_cols,
const std::string &tmp_dir);
void sort_file(const std::string &input_file, const std::string &output_file);
size_t disk_reads();
size_t disk_writes();
size_t phases();
size_t disk_reads();
size_t disk_writes();
size_t phases();
};

View file

@ -1,41 +0,0 @@
# Generate 5 significantly different file sizes
echo "./build/main -g 1000 -o build/run/tests/test_1k.in"
./build/main -g 1000 -o build/run/tests/test_1k.in -d build/run/tmp
echo "./build/main -g 5000 -o build/run/tests/test_5k.in"
./build/main -g 5000 -o build/run/tests/test_5k.in -d build/run/tmp
echo "./build/main -g 10000 -o build/run/tests/test_10k.in"
./build/main -g 10000 -o build/run/tests/test_10k.in -d build/run/tmp
echo "./build/main -g 50000 -o build/run/tests/test_50k.in"
./build/main -g 50000 -o build/run/tests/test_50k.in -d build/run/tmp
echo "./build/main -g 100000 -o build/run/tests/test_100k.in"
./build/main -g 100000 -o build/run/tests/test_100k.in -d build/run/tmp
# Sort each test file and capture statistics
echo "./build/main -i build/run/tests/test_1k.in -o build/run/tests/test_1k.out -n 4 -b 100"
./build/main -i build/run/tests/test_1k.in -o build/run/tests/test_1k.out -n 4 -b 100 -d build/run/tmp
echo "./build/main -i build/run/tests/test_5k.in -o build/run/tests/test_5k.out -n 4 -b 100"
./build/main -i build/run/tests/test_5k.in -o build/run/tests/test_5k.out -n 4 -b 100 -d build/run/tmp
echo "./build/main -i build/run/tests/test_10k.in -o build/run/tests/test_10k.out -n 4 -b 100"
./build/main -i build/run/tests/test_10k.in -o build/run/tests/test_10k.out -n 4 -b 100 -d build/run/tmp
echo "./build/main -i build/run/tests/test_50k.in -o build/run/tests/test_50k.out -n 4 -b 100"
./build/main -i build/run/tests/test_50k.in -o build/run/tests/test_50k.out -n 4 -b 100 -d build/run/tmp
echo "./build/main -i build/run/tests/test_100k.in -o build/run/tests/test_100k.out -n 4 -b 100"
./build/main -i build/run/tests/test_100k.in -o build/run/tests/test_100k.out -n 4 -b 100 -d build/run/tmp
# Display first/last records to verify sorting
echo "./build/main -e -i build/run/tests/test_1k.out | less"
./build/main -e -i build/run/tests/test_1k.out | less -d build/run/tmp
echo "./build/main -e -i build/run/tests/test_100k.out | less"
./build/main -e -i build/run/tests/test_100k.out | less -d build/run/tmp
# Test with different K values (buffer rows)
echo "./build/main -i build/run/tests/test_10k.in -o build/run/tests/test_10k_k2.out -n 3 -b 100 # K=2"
./build/main -i build/run/tests/test_10k.in -o build/run/tests/test_10k_k2.out -n 3 -b 100 # K=2 -d build/run/tmp
echo "./build/main -i build/run/tests/test_10k.in -o build/run/tests/test_10k_k4.out -n 5 -b 100 # K=4"
./build/main -i build/run/tests/test_10k.in -o build/run/tests/test_10k_k4.out -n 5 -b 100 # K=4 -d build/run/tmp
echo "./build/main -i build/run/tests/test_10k.in -o build/run/tests/test_10k_k8.out -n 9 -b 100 # K=8"
./build/main -i build/run/tests/test_10k.in -o build/run/tests/test_10k_k8.out -n 9 -b 100 # K=8 -d build/run/tmp