From b1b32525a51c7ab003edcc6a850e68bd56c3c0c5 Mon Sep 17 00:00:00 2001 From: "Semere M. Mebrahtom" Date: Wed, 1 Apr 2026 23:19:17 +0200 Subject: [PATCH] added a generic min heap with a custom comparator --- Makefile | 21 ++++++----- coders/codexion.h | 17 +++++++++ coders/heap.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++ coders/main.c | 4 +-- 4 files changed, 119 insertions(+), 14 deletions(-) create mode 100644 coders/heap.c diff --git a/Makefile b/Makefile index 913c7fc..7575f22 100644 --- a/Makefile +++ b/Makefile @@ -6,18 +6,8 @@ INCLUDES = coders/codexion.h OBJS = $(SRCS:.c=.o) CC = cc -# CFLAGS = -Wall -Wextra -Werror -pthread -g CFLAGS = -Wall -Wextra -Werror -pthread -g -fsanitize=thread -ARGS = 2 800 200 100 10 3 0 edf - -all: $(NAME) - -$(NAME): $(OBJS) - $(CC) $(CFLAGS) -o $@ $^ - -%.o: %.c - $(CC) $(CFLAGS) -c -o $@ $< - +# CFLAGS = -Wall -Wextra -Werror # number_of_coders # time_to_burnout # time_to_compile @@ -26,6 +16,15 @@ $(NAME): $(OBJS) # number_of_compiles_required # dongle_cooldown # scheduler +ARGS = 4 800 200 100 10 3 0 edf + +all: $(NAME) + +$(NAME): $(OBJS) + $(CC) $(CFLAGS) -o $@ $^ + +%.o: %.c + $(CC) $(CFLAGS) -c -o $@ $< test: $(NAME) ./$(NAME) $(ARGS) diff --git a/coders/codexion.h b/coders/codexion.h index 9d29c1a..c872aba 100644 --- a/coders/codexion.h +++ b/coders/codexion.h @@ -66,4 +66,21 @@ typedef struct s_state t_args *parse_arguments(int count, char **args); +// heap.c +# ifndef HEAP_SIZE +# define HEAP_SIZE 25 +# endif + +typedef struct s_minheap +{ + int length; + void *array[HEAP_SIZE]; + int (*cmp)(void *, void *); +} t_minheap; + +t_minheap *init_heap(int (*cmp)(void *, void *)); +void *peek(t_minheap *heap); +void enqueue(t_minheap *heap, void *val); +void *dequeue(t_minheap *heap); + #endif diff --git a/coders/heap.c b/coders/heap.c new file mode 100644 index 0000000..1047322 --- /dev/null +++ b/coders/heap.c @@ -0,0 +1,91 @@ +#include "codexion.h" +#include +#include + +static void sift_up(t_minheap *heap, int i, int (*cmp)(void *, void *)) +{ + int parent; + void *temp; + + while (i > 0) + { + parent = (i - 1) / 2; + if (cmp(heap->array[parent], heap->array[i]) > 0) + { + temp = heap->array[parent]; + heap->array[parent] = heap->array[i]; + heap->array[i] = temp; + i = parent; + } + else + break ; + } +} + +static void sift_down(t_minheap *heap, int i, int (*cmp)(void *, void *)) +{ + int smallest; + int left; + int right; + void *temp; + + while (true) + { + smallest = i; + left = 2 * i + 1; + right = 2 * i + 2; + if (left < heap->length && cmp(heap->array[left], + heap->array[smallest]) < 0) + smallest = left; + if (right < heap->length && cmp(heap->array[right], + heap->array[smallest]) < 0) + smallest = right; + if (smallest == i) + break ; + temp = heap->array[smallest]; + heap->array[smallest] = heap->array[i]; + heap->array[i] = temp; + i = smallest; + } +} + +t_minheap *init_heap(int (*cmp)(void *, void *)) +{ + t_minheap *heap; + + heap = malloc(sizeof(t_minheap)); + if (!heap) + return (NULL); + heap->length = 0; + heap->cmp = cmp; + return (heap); +} + +void *peek(t_minheap *heap) +{ + if (!heap || !heap->length) + return (NULL); + return (heap->array[0]); +} + +void enqueue(t_minheap *heap, void *val) +{ + if (heap->length >= HEAP_SIZE) + return ; + heap->array[heap->length] = val; + heap->length++; + sift_up(heap, heap->length - 1, heap->cmp); +} + +void *dequeue(t_minheap *heap) +{ + void *temp; + + if (!heap || !heap->length) + return (NULL); + temp = heap->array[0]; + heap->array[0] = heap->array[heap->length - 1]; + heap->length--; + sift_down(heap, 0, heap->cmp); + return (temp); +} diff --git a/coders/main.c b/coders/main.c index 5502b84..2c1c211 100644 --- a/coders/main.c +++ b/coders/main.c @@ -129,6 +129,7 @@ void *monitor(void *param) { pthread_mutex_lock(&state->coders[i].info_mutex); last = state->coders[i].last_compile; + compiles_done = state->coders[i].compiles_done; pthread_mutex_unlock(&state->coders[i].info_mutex); if (now() - last >= state->args.time_to_burnout) { @@ -139,9 +140,6 @@ void *monitor(void *param) return (NULL); } } - pthread_mutex_lock(&state->coders[i - 1].info_mutex); - compiles_done = state->coders[i - 1].compiles_done; - pthread_mutex_unlock(&state->coders[i - 1].info_mutex); } return (NULL); }