updated readme

This commit is contained in:
bronku 2025-10-21 23:27:35 +02:00
parent ece14b11ba
commit b48884a441
2 changed files with 56 additions and 8 deletions

View file

@ -1,3 +1,9 @@
# Introduction
#todo
# Usage
```
// Generates a random.txt file with parameters specified, and exits
// parameters besides N, are only used internally to specify how big are the chunks written
@ -19,3 +25,44 @@
- N = 100000
- b = 10
- n = 101
# Technical stuff
## memory alocated
- in total a b*m*sizeof(record) is alocated to memory for the buffers
- in stage 1 it is used as a continuous block for reading, sorting, and writing
- in stage 2 it is divided into b blocks, 1 for output, and b-1 for input, each block of size n
- an additional (b-1)\*2 bytes is alocated separately to create the heap (saved as a pointer to record + file index)
- if could be carved out of the main memory block, but that would mean that buffers at stage 2 would need to be slightly smaller, and thus not cleanly divisible by n
- the amount of memory for this heap is negligable in the scope of the entire memory usage anyway
## algorithm outline
### stage 1
In stage 1 the file is read in chunks of size b\*n, sorted in-memory, and saved as its own file
The amount of created runs is saved in memory
### stage 2
In stage 2 each buffer is populated by the HEAD of the coresponding file
- the HEAD pointer of each buffer is fed into a min heap, along with the computed g(record) value, and the buffer index
- when a pop is performed from the heap, another record is read from the same buffer, and the popped item written to output buffer
- when the output buffer is full, it is written to a file, subsequent writes are written to the same file
- when an input buffer is empty, more of the file is read into it
- when all input buffers are empty, the steps are repeated for any unprocessed input files
The amount of runs is saved in memory, if more than one run reamain the process is repeated
## required functions
- [ ] split_buffer - splits the buffer into an array of buffers of size n
- [ ] heap_push, heap_pop
- the heap is operating on structs of type g(record), index, buffer_index, and comparing them based on g(record)
- [ ] push operation requires heapify function
- [ ] buffer i/o operations
# Analysis
#todo

17
main.c
View file

@ -25,8 +25,9 @@ int sort_file(Configuration* opts)
{
buffer buff = create_buffer(opts->b * opts->n);
FILE* in = fopen(opts->input_file, "r");
int runs;
// <stage 1>
for (int i = 0; true; i++) {
for (runs = 0; true; runs++) {
// read buffer
int status = read_buffer(&buff, in);
if (status != SUCCESS && status != EOF) {
@ -37,22 +38,22 @@ int sort_file(Configuration* opts)
sort_buffer(&buff);
// write run
char* filename = malloc(256);
sprintf(filename, "%s/%d", opts->directory, i);
sprintf(filename, "%s/%d", opts->directory, runs);
FILE* tmp = fopen(filename, "w");
write_buffer(&buff, tmp);
fclose(tmp);
free(filename);
// write_buffer_debug(&buff, stdout);
if (status != SUCCESS) {
break;
}
}
// <stage 2>
// while tmp:
// merge b-1 runs
//
// close file
// free buffer
// while (runs > 1) {
// // split buffers
// // read buffers
// for (int i = 0; i < opts->b - 1; i++) {
// }
// }
fclose(in);
free(buff.location);
return SUCCESS;