diff --git a/TODO.md b/TODO.md index 9996303..f065214 100644 --- a/TODO.md +++ b/TODO.md @@ -14,9 +14,24 @@ - [x] sorting - [x] writing runs - [ ] stage 2 - - [ ] reading runs + - [ ] implement a min heap with following operations + - [ ] pop + - [ ] push + - [ ] step 4 + - [ ] split the buffer into chunks of size n, and (b-1)n + - [ ] create a min heap of size (b-1)n + - [ ] populate the heap with (b-1) runs of n elements (each from a different file) + - [ ] find a way to mark if a last element from a certain file has been popped + - [ ] when a merged buffer is full, write it to file + - [ ] pop off the top element until there are no more elements from a certain array + - [ ] load another run from that file, and rehapify + - [ ] repeat untill all files empty + - [ ] the resulting file should get the new smallest filename in tmp dir + - [ ] multiway merge - [ ] writing larger runs - [ ] repeat untill done + +- [ ] counting read, and write operations - [ ] testing - [ ] report diff --git a/heap.c b/heap.c new file mode 100644 index 0000000..587e7ca --- /dev/null +++ b/heap.c @@ -0,0 +1,28 @@ +#include "heap.h" +#include "status_codes.h" + +int heap_push(heap* h, heap_record* in) +{ + if (h->length >= h->capacity) { + return NO_SPACE; + } + + int index = h->length; + h->length++; + heap_record* last = h->location; + last += index; + *last = *in; + + if (index == 0) { + return SUCCESS; + } + + // check if violated heap property + heap_record* parent = h->location; + parent += ((index - 1) / 2); + while (parent->g > last->g) { + // just swap them, and check again? + } + // swim up + return SUCCESS; +} diff --git a/heap.h b/heap.h index 00f2bb1..4397dae 100644 --- a/heap.h +++ b/heap.h @@ -1,6 +1,7 @@ #pragma once #include "record.h" +#include "stdbool.h" typedef struct { record* location; @@ -9,9 +10,15 @@ typedef struct { bool is_blank; } heap_record; -// moves elements to maintain heap property -int heapify(heap_record* heap); -// returns the first element, and calls heapify -int heap_pop(heap_record* heap, heap_record* out); -// writes to the last elements, and calls heapify -int heap_pus(heap_record* heap, heap_record* in); +// almost the same as typedef buffer, but the usage is different, so I think it can be repeated +typedef struct { + heap_record* location; + int length; + int capacity; +} heap; + +heap new_heap(int size); +// free_heap is just free(h->location), so it's kinda unnecessary + +int heap_pop(heap* h, heap_record* out); +int heap_push(heap* h, heap_record* in); diff --git a/makefile b/makefile index 95df1ee..f72b723 100644 --- a/makefile +++ b/makefile @@ -1,7 +1,7 @@ CFLAGS = -g -build/main: build/main.o build/config.o build/buffer.o - clang $(CFLAGS) build/main.o build/config.o build/buffer.o -o build/main +build/main: build/main.o build/config.o build/buffer.o build/heap.o + clang $(CFLAGS) build/main.o build/config.o build/buffer.o build/heap.o -o build/main build/main.o: main.c build clang $(CFLAGS) -c main.c -o build/main.o @@ -12,6 +12,9 @@ build/config.o: config.c build build/buffer.o: buffer.c build clang $(CFLAGS) -c buffer.c -o build/buffer.o +build/heap.o: heap.c build + clang $(CFLAGS) -c heap.c -o build/heap.o + build: mkdir -p build diff --git a/status_codes.h b/status_codes.h index 2a38140..9866402 100644 --- a/status_codes.h +++ b/status_codes.h @@ -3,3 +3,4 @@ #define END_PROGRAM -1 #define SUCCESS 0 #define ERROR -2 +#define NO_SPACE -3