heap start

This commit is contained in:
bronku 2025-10-22 00:12:26 +02:00
parent b3b532bb14
commit 7916f181c5
5 changed files with 63 additions and 9 deletions

17
TODO.md
View file

@ -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

28
heap.c Normal file
View file

@ -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;
}

19
heap.h
View file

@ -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);

View file

@ -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

View file

@ -3,3 +3,4 @@
#define END_PROGRAM -1
#define SUCCESS 0
#define ERROR -2
#define NO_SPACE -3