From d22c0606c13cd7f957da0520948cd219fe2c958b Mon Sep 17 00:00:00 2001 From: bronku Date: Thu, 16 Oct 2025 15:26:56 +0200 Subject: [PATCH] reading, and sorting chunks --- TODO.md | 17 +++++++++++++---- main.c | 36 ++++++++++++++++++++++++++++-------- makefile | 6 +++--- tape.c | 33 +++++++++++++++++++++++++++++++++ tape.h | 1 + 5 files changed, 78 insertions(+), 15 deletions(-) diff --git a/TODO.md b/TODO.md index add9ff1..a5f6fa6 100644 --- a/TODO.md +++ b/TODO.md @@ -1,7 +1,7 @@ -- [ ] file operations - - [ ] write to file (at index) - - [ ] print_file (maybe just calling cat???) -- [ ] a tape type - representing a file? +- [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 @@ -9,5 +9,14 @@ - [x] cmd args - man getopts - [x] file input - [x] keyboard input +- [ ] stage 1 + - [x] reading chunks + - [x] sorting + - [ ] writing chunks +- [ ] stage 2 + - [ ] reading chunks + - [ ] multiway merge + - [ ] writing chunks + - [ ] repeat untill done - [ ] testing - [ ] report diff --git a/main.c b/main.c index 954daf4..f256125 100644 --- a/main.c +++ b/main.c @@ -110,20 +110,40 @@ void write_file(options* opts) { } -void sort_file(options* opts) +int sort_block(tape* t, buffer* buff) +{ + static int counter = 0; + int status = read_buffer(buff, t); + if (status != SUCCESS && status != EOF) { + fprintf(stderr, "Error reading file: %d\n", status); + return status; + } + + sort_buffer(buff); + printf("sorted the buffer %d, the results:\n", counter); + print_buffer(buff); + counter++; + return status; +} + +int sort_file(options* opts) { tape a = { 0, 0, opts->input }; buffer buff; - buff.capacity = 100; + buff.capacity = opts->b * opts->n; buff.location = malloc(sizeof(record) * buff.capacity); - int status = read_buffer(&buff, &a); - if (status != SUCCESS && status != EOF) { - printf("Error reading file: %d\n", status); + int status = 0; + while (status == SUCCESS) { + status = sort_block(&a, &buff); + } + if (status != EOF) { + free(buff.location); + fclose(a.file); + return status; } - fclose(a.file); - a.file = stdout; - sort_buffer(&buff); free(buff.location); + fclose(a.file); + return SUCCESS; } int main(int argc, char** argv) diff --git a/makefile b/makefile index 96b2bee..3a1cb41 100644 --- a/makefile +++ b/makefile @@ -16,9 +16,9 @@ clean: rm -rf build run: build/main - build/main -f tests/1.in + build/main -f tests/2.in | less debug: build/main - lldb -- build/main -f tests/1.in + lldb -- build/main -f tests/2.in -.PHONY: clean run +.PHONY: clean run debug diff --git a/tape.c b/tape.c index 6abcaf7..d9b304a 100644 --- a/tape.c +++ b/tape.c @@ -60,6 +60,39 @@ int read_buffer(buffer* buff, tape* tape) return 0; } +void print_full_buffer(buffer* buff) +{ + for (int i = 0; i < buff->length; i++) { + record* current = buff->location; + current += i; + printf("i: %d\tg: %d\t", i, g(current)); + print_record(stdout, current); + printf("\n"); + } +} + +void print_buffer(buffer* buff) +{ + if (buff->length <= 10) { + print_full_buffer(buff); + return; + } + for (int i = 0; i < 5; i++) { + record* current = buff->location; + current += i; + printf("i: %d\tg: %d\t", i, g(current)); + print_record(stdout, current); + printf("\n"); + } + for (int i = buff->length - 5; i < buff->length; i++) { + record* current = buff->location; + current += i; + printf("i: %d\tg: %d\t", i, g(current)); + print_record(stdout, current); + printf("\n"); + } +} + int write_buffer(buffer* buff, tape* tape) { record* current = buff->location; diff --git a/tape.h b/tape.h index 2a064c7..9421e1b 100644 --- a/tape.h +++ b/tape.h @@ -20,5 +20,6 @@ void sort_buffer(buffer* buff); int read_buffer(buffer* buff, tape* tape); // writes everything int write_buffer(buffer* buff, tape* tape); +void print_buffer(buffer* buff); void clear_tape(tape* tape);