From f31bf4712c85e55d147f2c2a3ba6d5d4fd1564bc Mon Sep 17 00:00:00 2001 From: bronku Date: Fri, 24 Oct 2025 10:35:56 +0200 Subject: [PATCH] changed how to buffer is created --- buffer.c | 27 ++++++++++++++++++++++----- buffer.h | 8 +++++++- main.c | 31 +++++++++++++++++++------------ 3 files changed, 48 insertions(+), 18 deletions(-) diff --git a/buffer.c b/buffer.c index 9a7fffd..6fa2f64 100644 --- a/buffer.c +++ b/buffer.c @@ -1,16 +1,33 @@ #include "buffer.h" #include "record.h" #include "status_codes.h" +#include +#include -buffer create_buffer(int capacity) +buffer* create_buffer(int capacity) { - buffer out; - out.length = 0; - out.capacity = capacity; - out.location = malloc(sizeof(record) * capacity); + buffer* out = malloc(sizeof(buffer)); + out->length = 0; + out->capacity = capacity; + out->location = malloc(sizeof(record) * capacity); + out->original = true; return out; } +void destroy_buffer(buffer* buff) +{ + if (buff->original) { + free(buff->location); + } + free(buff); +} + +// #todo +buffer* split_buffer(buffer* original, int pieces) +{ + return NULL; +} + int read_buffer(buffer* buff, FILE* in) { int status; diff --git a/buffer.h b/buffer.h index c70e39f..e6e3378 100644 --- a/buffer.h +++ b/buffer.h @@ -1,13 +1,19 @@ #pragma once #include "record.h" +#include typedef struct { record* location; int length; int capacity; + bool original; } buffer; -buffer create_buffer(int capacity); +// #todo switch to returning a pointer, and write destroy_buffer method +buffer* create_buffer(int capacity); +void destroy_buffer(buffer* buff); +buffer* split_buffer(buffer* original, int pieces); + int read_buffer(buffer* buff, FILE* in); int write_buffer(buffer* buff, FILE* in); int write_buffer_debug(buffer* buff, FILE* in); diff --git a/main.c b/main.c index 90da7fa..d6e6acf 100644 --- a/main.c +++ b/main.c @@ -23,39 +23,46 @@ int generate_file(int N, const char* filename) int sort_file(Configuration* opts) { - buffer buff = create_buffer(opts->b * opts->n); + buffer* buff = create_buffer(opts->b * opts->n); FILE* in = fopen(opts->input_file, "r"); int runs; // for (runs = 0; true; runs++) { // read buffer - int status = read_buffer(&buff, in); + int status = read_buffer(buff, in); if (status != SUCCESS && status != EOF) { fclose(in); - free(buff.location); + destroy_buffer(buff); } // sort buffer - sort_buffer(&buff); + sort_buffer(buff); // write run char* filename = malloc(256); sprintf(filename, "%s/%d", opts->directory, runs); FILE* tmp = fopen(filename, "w"); - write_buffer(&buff, tmp); + write_buffer(buff, tmp); + // write_buffer_debug(buff, stdout); fclose(tmp); free(filename); if (status != SUCCESS) { break; } } + printf("created runs: %d\n", runs); + // - // while (runs > 1) { - // // split buffers - // // read buffers - // for (int i = 0; i < opts->b - 1; i++) { - // } - // } + while (runs > 1) { + + // split buffers + // read buffers + for (int i = 0; i < opts->b - 1; i++) { + } + + // b-1 runs are turned into one, so the total is reduced by b-2 + runs -= (opts->b - 2); + } fclose(in); - free(buff.location); + destroy_buffer(buff); return SUCCESS; }