data_structures
min_pq_add.c
Go to the documentation of this file.
1 /* ************************************************************************** */
3 /* */
4 /* ::: :::::::: */
5 /* min_pq_add.c :+: :+: :+: */
6 /* +:+ +:+ +:+ */
7 /* By: unite <marvin@42.fr> +#+ +:+ +#+ */
8 /* +#+#+#+#+#+ +#+ */
9 /* Created: 2020/07/17 14:05:12 by unite #+# #+# */
10 /* Updated: 2020/09/07 22:41:15 by unite ### ########.fr */
11 /* */
12 /* ************************************************************************** */
13 
14 #include "min_pq.h"
15 
16 static void min_pq_swim(t_min_pq *pq, size_t k)
17 {
18  while (k > 1)
19  {
20  if (pq->type->cmp(
21  array_get(pq, (k / 2) - 1),
22  array_get(pq, k - 1)) > 0)
23  array_swap(pq, (k / 2) - 1, k - 1);
24  else
25  break ;
26  k = k / 2;
27  }
28 }
29 
30 void min_pq_add(t_min_pq *pq, const void *data)
31 {
32  array_append(pq, data);
33  min_pq_swim(pq, min_pq_size(pq));
34 }
array_get
void * array_get(const t_array *array, size_t index)
Returns the item at the specified position in this list.
Definition: array_get.c:16
min_pq_add
void min_pq_add(t_min_pq *pq, const void *data)
Adds a copy of the specified element to the queue.
Definition: min_pq_add.c:30
s_type::cmp
int(* cmp)(const void *, const void *)
(optional) A function ponter used to compare members of this data type
Definition: types.h:52
array_swap
void array_swap(t_array *array, size_t ind1, size_t ind2)
Swaps elements at the two specified positions in the array.
Definition: array_swap.c:16
min_pq.h
s_array::type
const t_type * type
The type of items in this array.
Definition: array.h:52
s_array
A resizable array.
Definition: array.h:47
min_pq_size
size_t min_pq_size(const t_min_pq *pq)
Returns the number of keys in this queue.
Definition: min_pq_size.c:16
array_append
void array_append(t_array *array, const void *content)
Appends a copy the specified element to the end of this array.
Definition: array_append.c:17