copy on git

This commit is contained in:
Thomas Rieg 2025-11-28 19:50:58 +01:00
commit 42653de246
205 changed files with 7459 additions and 0 deletions

View file

@ -0,0 +1,71 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_priority_queue.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/19 14:51:23 by thrieg #+# #+# */
/* Updated: 2025/02/16 19:07:06 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_priority_queue_private.h"
void ft_insert_pq(t_priority_queue *pq, void *data, size_t priority)
{
t_priority_queue_node new_node;
if (pq->size >= pq->capacity)
ft_resize_pq(pq);
new_node.data = data;
new_node.priority = priority;
pq->nodes[pq->size] = new_node;
pq->size++;
heapify_up(pq, pq->size - 1);
}
t_priority_queue *create_priority_queue(
int capacity,
int (*compare)(void *, void *))
{
t_priority_queue *pq;
pq = malloc(sizeof(t_priority_queue));
if (!pq)
return (NULL);
pq->nodes = malloc(capacity * sizeof(t_priority_queue_node));
if (!pq->nodes)
return (free(pq), NULL);
pq->size = 0;
pq->capacity = capacity;
pq->compare = compare;
return (pq);
}
void *peek(t_priority_queue *pq)
{
if (pq->size == 0)
return (NULL);
return (pq->nodes[0].data);
}
void *dequeue(t_priority_queue *pq)
{
void *ret;
if (pq->size == 0)
return (NULL);
ret = pq->nodes[0].data;
pq->nodes[0] = pq->nodes[pq->size - 1];
pq->size--;
heapify_down(pq, 0);
ft_shrink_pq(pq);
return (ret);
}
void free_pq(t_priority_queue *pq)
{
free(pq->nodes);
free(pq);
}

Binary file not shown.

View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_priority_queue_private.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/19 16:41:11 by thrieg #+# #+# */
/* Updated: 2025/02/16 19:07:08 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_PRIORITY_QUEUE_PRIVATE_H
# define FT_PRIORITY_QUEUE_PRIVATE_H
# include "../ft_priority_queue.h"
void heapify_up(t_priority_queue *pq, size_t index);
void heapify_down(t_priority_queue *pq, size_t index);
void ft_resize_pq(t_priority_queue *pq);
void ft_shrink_pq(t_priority_queue *pq);
#endif

View file

@ -0,0 +1,118 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_priority_queue_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/19 16:00:58 by thrieg #+# #+# */
/* Updated: 2025/02/16 19:07:01 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_priority_queue_private.h"
#include "../ft_printf/ft_printf.h"
static inline void pq_swap(t_priority_queue_node *a, t_priority_queue_node *b)
{
t_priority_queue_node temp;
temp = *a;
*a = *b;
*b = temp;
}
void heapify_up(t_priority_queue *pq, size_t index)
{
size_t parent;
while (index > 0)
{
parent = (index - 1) / 2;
if (pq->nodes[index].priority < pq->nodes[parent].priority)
{
pq_swap(&pq->nodes[index], &pq->nodes[parent]);
index = parent;
}
else if (pq->nodes[index].priority == pq->nodes[parent].priority
&& pq->compare(pq->nodes[index].data, pq->nodes[parent].data) < 0)
{
pq_swap(&pq->nodes[index], &pq->nodes[parent]);
index = parent;
}
else
{
break ;
}
}
}
void heapify_down(t_priority_queue *pq, size_t index)
{
size_t smallest;
size_t left;
size_t right;
smallest = index;
left = 2 * index + 1;
right = 2 * index + 2;
if (left < pq->size
&& ((pq->nodes[left].priority < pq->nodes[smallest].priority)
|| (pq->compare(pq->nodes[left].data,
pq->nodes[smallest].data) < 0)))
smallest = left;
if (right < pq->size
&& ((pq->nodes[right].priority < pq->nodes[smallest].priority)
|| (pq->compare(pq->nodes[right].data,
pq->nodes[smallest].data) < 0)))
smallest = right;
if (smallest != index)
{
pq_swap(&pq->nodes[index], &pq->nodes[smallest]);
heapify_down(pq, smallest);
}
}
void ft_resize_pq(t_priority_queue *pq)
{
size_t new_capacity;
t_priority_queue_node *new_nodes;
size_t i;
new_capacity = pq->capacity * 2;
new_nodes = malloc(new_capacity * sizeof(t_priority_queue_node));
if (!new_nodes)
return ;
i = 0;
while (i < pq->size)
{
new_nodes[i] = pq->nodes[i];
i++;
}
free(pq->nodes);
pq->nodes = new_nodes;
pq->capacity = new_capacity;
}
void ft_shrink_pq(t_priority_queue *pq)
{
size_t new_capacity;
t_priority_queue_node *new_nodes;
size_t i;
if (pq->size >= pq->capacity / 4 || pq->capacity <= 4)
return ;
new_capacity = pq->capacity / 2;
new_nodes = malloc(new_capacity * sizeof(t_priority_queue_node));
if (!new_nodes)
return ;
i = 0;
while (i < pq->size)
{
new_nodes[i] = pq->nodes[i];
i++;
}
free(pq->nodes);
pq->nodes = new_nodes;
pq->capacity = new_capacity;
}

Binary file not shown.