added a generic min heap with a custom comparator
This commit is contained in:
parent
6772407a3c
commit
b1b32525a5
4 changed files with 119 additions and 14 deletions
21
Makefile
21
Makefile
|
|
@ -6,18 +6,8 @@ INCLUDES = coders/codexion.h
|
||||||
OBJS = $(SRCS:.c=.o)
|
OBJS = $(SRCS:.c=.o)
|
||||||
|
|
||||||
CC = cc
|
CC = cc
|
||||||
# CFLAGS = -Wall -Wextra -Werror -pthread -g
|
|
||||||
CFLAGS = -Wall -Wextra -Werror -pthread -g -fsanitize=thread
|
CFLAGS = -Wall -Wextra -Werror -pthread -g -fsanitize=thread
|
||||||
ARGS = 2 800 200 100 10 3 0 edf
|
# CFLAGS = -Wall -Wextra -Werror
|
||||||
|
|
||||||
all: $(NAME)
|
|
||||||
|
|
||||||
$(NAME): $(OBJS)
|
|
||||||
$(CC) $(CFLAGS) -o $@ $^
|
|
||||||
|
|
||||||
%.o: %.c
|
|
||||||
$(CC) $(CFLAGS) -c -o $@ $<
|
|
||||||
|
|
||||||
# number_of_coders
|
# number_of_coders
|
||||||
# time_to_burnout
|
# time_to_burnout
|
||||||
# time_to_compile
|
# time_to_compile
|
||||||
|
|
@ -26,6 +16,15 @@ $(NAME): $(OBJS)
|
||||||
# number_of_compiles_required
|
# number_of_compiles_required
|
||||||
# dongle_cooldown
|
# dongle_cooldown
|
||||||
# scheduler
|
# 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)
|
test: $(NAME)
|
||||||
./$(NAME) $(ARGS)
|
./$(NAME) $(ARGS)
|
||||||
|
|
|
||||||
|
|
@ -66,4 +66,21 @@ typedef struct s_state
|
||||||
|
|
||||||
t_args *parse_arguments(int count, char **args);
|
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
|
#endif
|
||||||
|
|
|
||||||
91
coders/heap.c
Normal file
91
coders/heap.c
Normal file
|
|
@ -0,0 +1,91 @@
|
||||||
|
#include "codexion.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
@ -129,6 +129,7 @@ void *monitor(void *param)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&state->coders[i].info_mutex);
|
pthread_mutex_lock(&state->coders[i].info_mutex);
|
||||||
last = state->coders[i].last_compile;
|
last = state->coders[i].last_compile;
|
||||||
|
compiles_done = state->coders[i].compiles_done;
|
||||||
pthread_mutex_unlock(&state->coders[i].info_mutex);
|
pthread_mutex_unlock(&state->coders[i].info_mutex);
|
||||||
if (now() - last >= state->args.time_to_burnout)
|
if (now() - last >= state->args.time_to_burnout)
|
||||||
{
|
{
|
||||||
|
|
@ -139,9 +140,6 @@ void *monitor(void *param)
|
||||||
return (NULL);
|
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);
|
return (NULL);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue