From 83061c4658cac7744f2e90dc0bd64dc2f0861548 Mon Sep 17 00:00:00 2001 From: bronku Date: Wed, 22 Oct 2025 17:10:51 +0200 Subject: [PATCH] heap push --- heap.c | 31 ++++++++++++++++++++++++------- heap.h | 6 ++---- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/heap.c b/heap.c index 587e7ca..7ba34bb 100644 --- a/heap.c +++ b/heap.c @@ -1,28 +1,45 @@ #include "heap.h" #include "status_codes.h" +// #unteasted int heap_push(heap* h, heap_record* in) { if (h->length >= h->capacity) { return NO_SPACE; } - int index = h->length; + int current_index = h->length; h->length++; heap_record* last = h->location; - last += index; + last += current_index; *last = *in; - if (index == 0) { + if (current_index == 0) { return SUCCESS; } + int parent_index = (current_index - 1) / 2; // 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? - } + parent += parent_index; + heap_record* current = h->location; + current += current_index; // swim up + while (current_index != 0 && h->compare(parent, current) == 1) { + // current, parent = parent, current + heap_record tmp = *current; + *current = *parent; + *parent = tmp; + // current_index = parent_index + current_index = parent_index; + // parent_index = (current_index -1 )/2 + parent_index = (current_index - 1) / 2; + // current = loc[current_index] + current = h->location; + current += current_index; + // parent = loc[parent_index] + parent = h->location; + parent_index += parent_index; + } return SUCCESS; } diff --git a/heap.h b/heap.h index 4397dae..f59116d 100644 --- a/heap.h +++ b/heap.h @@ -1,13 +1,10 @@ #pragma once -#include "record.h" #include "stdbool.h" typedef struct { - record* location; - int g; + int index; int buffer_id; - bool is_blank; } heap_record; // almost the same as typedef buffer, but the usage is different, so I think it can be repeated @@ -15,6 +12,7 @@ typedef struct { heap_record* location; int length; int capacity; + int (*compare)(const void* a, const void* b); } heap; heap new_heap(int size);