changed how to buffer is created

This commit is contained in:
bronku 2025-10-24 10:35:56 +02:00
parent 0448d78fc5
commit f31bf4712c
3 changed files with 48 additions and 18 deletions

View file

@ -1,16 +1,33 @@
#include "buffer.h"
#include "record.h"
#include "status_codes.h"
#include <stdbool.h>
#include <string.h>
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;

View file

@ -1,13 +1,19 @@
#pragma once
#include "record.h"
#include <stdbool.h>
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);

31
main.c
View file

@ -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;
// <stage 1>
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);
// <stage 2>
// 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;
}