From cf29402dc9e0582a9e08ffc50e60c9e94906bdf7 Mon Sep 17 00:00:00 2001 From: bronku Date: Tue, 21 Oct 2025 19:09:33 +0200 Subject: [PATCH] writing first runs --- buffer.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ buffer.h | 2 ++ main.c | 18 ++++++++++++------ makefile | 7 +++++-- 4 files changed, 77 insertions(+), 8 deletions(-) diff --git a/buffer.c b/buffer.c index 4d46105..9a7fffd 100644 --- a/buffer.c +++ b/buffer.c @@ -41,3 +41,61 @@ int write_buffer(buffer* buff, FILE* out) } return SUCCESS; } + +int write_buffer_debug(buffer* buff, FILE* out) +{ + int status; + record* current = buff->location; + for (int i = 0; i < buff->length; i++) { + fprintf(out, "%d\t", g(current)); + status = print_record(out, current); + if (status < 0) { + return status; + } + fprintf(out, "\n"); + current++; + } + return SUCCESS; +} + +int partition(record* arr, int start, int end) +{ + int i = start; + record* pivot = arr; + pivot += end; + for (int j = start; j < end; j++) { + record* current = arr; + current += j; + if (compare_records(current, pivot) > 0) { + continue; + } + record* i_rec = arr; + i_rec += i; + record tmp = *i_rec; + *i_rec = *current; + *current = tmp; + i++; + } + record* i_rec = arr; + i_rec += i; + record tmp = *i_rec; + *i_rec = *pivot; + *pivot = tmp; + return i; +} + +void quicksort(record* arr, int start, int end, int depth) +{ + if (start >= end || start < 0) { + return; + } + + int p = partition(arr, start, end); + quicksort(arr, start, p - 1, depth + 1); + quicksort(arr, p + 1, end, depth + 1); +} + +void sort_buffer(buffer* buff) +{ + quicksort(buff->location, 0, buff->length - 1, 0); +} diff --git a/buffer.h b/buffer.h index d713160..c70e39f 100644 --- a/buffer.h +++ b/buffer.h @@ -10,3 +10,5 @@ typedef struct { buffer create_buffer(int capacity); int read_buffer(buffer* buff, FILE* in); int write_buffer(buffer* buff, FILE* in); +int write_buffer_debug(buffer* buff, FILE* in); +void sort_buffer(buffer* buff); diff --git a/main.c b/main.c index 7cc7059..36989f5 100644 --- a/main.c +++ b/main.c @@ -23,20 +23,26 @@ int generate_file(int N, const char* filename) int sort_file(Configuration* opts) { - // create buffer buffer buff = create_buffer(opts->b * opts->n); - // open file FILE* in = fopen(opts->input_file, "r"); // - // while !eof: for (int i = 0; true; i++) { // read buffer int status = read_buffer(&buff, in); - printf("buffer i:%d, status: %d\n", i, status); + if (status != SUCCESS && status != EOF) { + fclose(in); + free(buff.location); + } // sort buffer - + sort_buffer(&buff); // write run - write_buffer(&buff, stdout); + char* filename = malloc(256); + sprintf(filename, "%s/%d", opts->directory, i); + FILE* tmp = fopen(filename, "w"); + write_buffer(&buff, tmp); + fclose(tmp); + free(filename); + // write_buffer_debug(&buff, stdout); if (status != SUCCESS) { break; } diff --git a/makefile b/makefile index 6196c62..95df1ee 100644 --- a/makefile +++ b/makefile @@ -18,14 +18,17 @@ build: build/run: build mkdir -p build/run +build/run/tmp: build/run + mkdir -p build/run/tmp + clean: rm -rf build build/run/1.in: build/run build/main build/main -g -o build/run/1.in -run: build/main build/run/1.in - build/main -i build/run/1.in | less +run: build/main build/run/1.in build/run/tmp + build/main -i build/run/1.in -d build/run/tmp | less debug: build/main tests/1.in lldb -- build/main