changed how to buffer is created
This commit is contained in:
parent
0448d78fc5
commit
f31bf4712c
3 changed files with 48 additions and 18 deletions
27
buffer.c
27
buffer.c
|
|
@ -1,16 +1,33 @@
|
||||||
#include "buffer.h"
|
#include "buffer.h"
|
||||||
#include "record.h"
|
#include "record.h"
|
||||||
#include "status_codes.h"
|
#include "status_codes.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
buffer create_buffer(int capacity)
|
buffer* create_buffer(int capacity)
|
||||||
{
|
{
|
||||||
buffer out;
|
buffer* out = malloc(sizeof(buffer));
|
||||||
out.length = 0;
|
out->length = 0;
|
||||||
out.capacity = capacity;
|
out->capacity = capacity;
|
||||||
out.location = malloc(sizeof(record) * capacity);
|
out->location = malloc(sizeof(record) * capacity);
|
||||||
|
out->original = true;
|
||||||
return out;
|
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 read_buffer(buffer* buff, FILE* in)
|
||||||
{
|
{
|
||||||
int status;
|
int status;
|
||||||
|
|
|
||||||
8
buffer.h
8
buffer.h
|
|
@ -1,13 +1,19 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "record.h"
|
#include "record.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
record* location;
|
record* location;
|
||||||
int length;
|
int length;
|
||||||
int capacity;
|
int capacity;
|
||||||
|
bool original;
|
||||||
} buffer;
|
} 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 read_buffer(buffer* buff, FILE* in);
|
||||||
int write_buffer(buffer* buff, FILE* in);
|
int write_buffer(buffer* buff, FILE* in);
|
||||||
int write_buffer_debug(buffer* buff, FILE* in);
|
int write_buffer_debug(buffer* buff, FILE* in);
|
||||||
|
|
|
||||||
31
main.c
31
main.c
|
|
@ -23,39 +23,46 @@ int generate_file(int N, const char* filename)
|
||||||
|
|
||||||
int sort_file(Configuration* opts)
|
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");
|
FILE* in = fopen(opts->input_file, "r");
|
||||||
int runs;
|
int runs;
|
||||||
// <stage 1>
|
// <stage 1>
|
||||||
for (runs = 0; true; runs++) {
|
for (runs = 0; true; runs++) {
|
||||||
// read buffer
|
// read buffer
|
||||||
int status = read_buffer(&buff, in);
|
int status = read_buffer(buff, in);
|
||||||
if (status != SUCCESS && status != EOF) {
|
if (status != SUCCESS && status != EOF) {
|
||||||
fclose(in);
|
fclose(in);
|
||||||
free(buff.location);
|
destroy_buffer(buff);
|
||||||
}
|
}
|
||||||
// sort buffer
|
// sort buffer
|
||||||
sort_buffer(&buff);
|
sort_buffer(buff);
|
||||||
// write run
|
// write run
|
||||||
char* filename = malloc(256);
|
char* filename = malloc(256);
|
||||||
sprintf(filename, "%s/%d", opts->directory, runs);
|
sprintf(filename, "%s/%d", opts->directory, runs);
|
||||||
FILE* tmp = fopen(filename, "w");
|
FILE* tmp = fopen(filename, "w");
|
||||||
write_buffer(&buff, tmp);
|
write_buffer(buff, tmp);
|
||||||
|
// write_buffer_debug(buff, stdout);
|
||||||
fclose(tmp);
|
fclose(tmp);
|
||||||
free(filename);
|
free(filename);
|
||||||
if (status != SUCCESS) {
|
if (status != SUCCESS) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
printf("created runs: %d\n", runs);
|
||||||
|
|
||||||
// <stage 2>
|
// <stage 2>
|
||||||
// while (runs > 1) {
|
while (runs > 1) {
|
||||||
// // split buffers
|
|
||||||
// // read buffers
|
// split buffers
|
||||||
// for (int i = 0; i < opts->b - 1; i++) {
|
// 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);
|
fclose(in);
|
||||||
free(buff.location);
|
destroy_buffer(buff);
|
||||||
return SUCCESS;
|
return SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue