Started from buttom go to the sky

This commit is contained in:
Raphaël 2024-04-28 19:59:01 +02:00
parent 96215449bd
commit f811e55dea
4781 changed files with 10121 additions and 1743 deletions

View file

@ -0,0 +1,76 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hashmap_C__PREFIX__.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/06 11:00:22 by maiboyer #+# #+# */
/* Updated: 2023/12/11 15:24:44 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef HASHMAP_C__PREFIXUP___H
#define HASHMAP_C__PREFIXUP___H
#define DEFAULT_BUCKETS 750
C__TYPEHEADER__
#include "me/hash/hasher.h"
#include "me/types.h"
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct s_kv_C__PREFIX__
{
C__KEYTYPE__ key;
C__VALTYPE__ val;
} t_kv_C__PREFIX__;
typedef void (*t_hash_C__PREFIX___fn)(t_hasher *hasher, C__KEYTYPE__ *key);
typedef void (*t_drop_C__PREFIX___fn)(t_kv_C__PREFIX__ val);
typedef bool (*t_eq_C__PREFIX___fn)(C__KEYTYPE__ *lhs, C__KEYTYPE__ *rhs);
typedef struct s_entry_C__PREFIX__
{
t_usize hash_id;
t_kv_C__PREFIX__ kv;
struct s_entry_C__PREFIX__ *next;
} t_entry_C__PREFIX__;
typedef struct s_hashmap_C__PREFIX__
{
t_entry_C__PREFIX__ **buckets;
t_usize num_buckets;
t_hasher hasher;
t_hash_C__PREFIX___fn hfunc;
t_eq_C__PREFIX___fn cfunc;
t_drop_C__PREFIX___fn drop;
} t_hashmap_C__PREFIX__;
t_hashmap_C__PREFIX__ *new_hashmap_C__PREFIX__(t_hash_C__PREFIX___fn hash,
t_eq_C__PREFIX___fn cmp,
t_drop_C__PREFIX___fn drop);
t_hashmap_C__PREFIX__ *new_hashmap_with_buckets_C__PREFIX__(
t_hash_C__PREFIX___fn hash, t_eq_C__PREFIX___fn cmp,
t_drop_C__PREFIX___fn drop, size_t cap);
void drop_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap);
void insert_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ key,
C__VALTYPE__ value);
C__VALTYPE__ *get_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap,
C__KEYTYPE__ *key);
void remove_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ *key);
t_entry_C__PREFIX__ *hashmap_get_entry_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap,
t_usize hash,
C__KEYTYPE__ *key,
t_entry_C__PREFIX__ **prev);
#endif

View file

@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_C__PREFIX__.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/04 18:46:53 by maiboyer #+# #+# */
/* Updated: 2023/12/09 17:53:00 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef VEC_C__PREFIXUP___H
#define VEC_C__PREFIXUP___H
C__TYPEHEADER__
#include "me/types.h"
typedef bool (*t_vec_C__PREFIX___sort_fn)(C__TYPENAME__ *, C__TYPENAME__ *);
typedef void (*t_free_C__PREFIX___item)(C__TYPENAME__);
typedef struct s_vec_C__PREFIX__
{
t_free_C__PREFIX___item free_func;
t_usize len;
t_usize capacity;
C__TYPENAME__ *buffer;
} t_vec_C__PREFIX__;
t_vec_C__PREFIX__ vec_C__PREFIX___new(t_usize capacity,
t_free_C__PREFIX___item free_function);
t_error vec_C__PREFIX___push(t_vec_C__PREFIX__ *vec, C__TYPENAME__ element);
t_error vec_C__PREFIX___push_front(t_vec_C__PREFIX__ *vec,
C__TYPENAME__ element);
t_error vec_C__PREFIX___pop(t_vec_C__PREFIX__ *vec, C__TYPENAME__ *value);
t_error vec_C__PREFIX___pop_front(t_vec_C__PREFIX__ *vec, C__TYPENAME__ *value);
void vec_C__PREFIX___free(t_vec_C__PREFIX__ vec);
t_error vec_C__PREFIX___reserve(t_vec_C__PREFIX__ *vec,
t_usize wanted_capacity);
t_error vec_C__PREFIX___find(t_vec_C__PREFIX__ *vec,
bool (*fn)(const C__TYPENAME__ *), t_usize *index);
t_error vec_C__PREFIX___find_starting(t_vec_C__PREFIX__ *vec,
bool (*fn)(const C__TYPENAME__ *),
t_usize starting_index, t_usize *index);
t_error vec_C__PREFIX___all(t_vec_C__PREFIX__ *vec,
bool (*fn)(const C__TYPENAME__ *), bool *result);
t_error vec_C__PREFIX___any(t_vec_C__PREFIX__ *vec,
bool (*fn)(const C__TYPENAME__ *), bool *result);
void vec_C__PREFIX___iter(t_vec_C__PREFIX__ *vec,
void (*fn)(t_usize index, C__TYPENAME__ *value,
void *state),
void *state);
void vec_C__PREFIX___reverse(t_vec_C__PREFIX__ *vec);
void vec_C__PREFIX___sort(t_vec_C__PREFIX__ *vec,
t_vec_C__PREFIX___sort_fn is_sorted);
#endif

View file

@ -0,0 +1,116 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hashmap_C__PREFIX__.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/06 10:58:20 by maiboyer #+# #+# */
/* Updated: 2023/12/11 15:32:51 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/hash/hasher.h"
#include "me/hash/sip.h"
#include "me/hashmap/hashmap_C__PREFIX__.h"
#include "me/mem/mem_alloc.h"
#include "me/mem/mem_alloc_array.h"
#include "me/mem/mem_copy.h"
#include "me/types.h"
#include <stdlib.h>
t_hashmap_C__PREFIX__ *new_hashmap_C__PREFIX__(t_hash_C__PREFIX___fn hfunc,
t_eq_C__PREFIX___fn cfunc,
t_drop_C__PREFIX___fn drop)
{
return (new_hashmap_with_buckets_C__PREFIX__(hfunc, cfunc, drop,
DEFAULT_BUCKETS));
}
t_hashmap_C__PREFIX__ *new_hashmap_with_buckets_C__PREFIX__(
t_hash_C__PREFIX___fn hfunc, t_eq_C__PREFIX___fn cfunc,
t_drop_C__PREFIX___fn drop, t_usize buckets)
{
t_hashmap_C__PREFIX__ *hmap;
hmap = mem_alloc(sizeof(*hmap));
if (hmap == NULL)
return (NULL);
hmap->buckets = mem_alloc_array(buckets, sizeof(t_entry_C__PREFIX__ *));
hmap->num_buckets = buckets;
hmap->hasher = hasher_sip13_new();
hmap->hfunc = hfunc;
hmap->cfunc = cfunc;
hmap->drop = drop;
if (hmap->buckets == NULL)
return ((void)free(hmap), NULL);
return (hmap);
}
void drop_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap)
{
t_usize index;
index = 0;
while (index < hmap->num_buckets)
{
if (hmap->buckets[index])
{
hmap->drop(hmap->buckets[index]->kv);
free(hmap->buckets[index]);
}
index++;
}
hasher_finish(&hmap->hasher);
free(hmap->buckets);
free(hmap);
}
t_entry_C__PREFIX__ *hashmap_get_entry_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap,
t_usize hashed_key,
C__KEYTYPE__ *key,
t_entry_C__PREFIX__ **prev)
{
t_entry_C__PREFIX__ *entry;
entry = hmap->buckets[hashed_key % hmap->num_buckets];
while (entry != NULL)
{
if (!hmap->cfunc(&entry->kv.key, key))
{
*prev = entry;
entry = entry->next;
}
else
{
return (entry);
}
}
return (NULL);
}
void insert_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ key,
C__VALTYPE__ value)
{
t_usize hashed_key;
t_entry_C__PREFIX__ *prev;
t_entry_C__PREFIX__ *entry;
hmap->hfunc(&hmap->hasher, &key);
hashed_key = hasher_reset_and_finish(&hmap->hasher);
prev = NULL;
entry = hashmap_get_entry_C__PREFIX__(hmap, hashed_key, &key, &prev);
if (entry == NULL)
{
entry = mem_alloc(sizeof(t_entry_tile));
entry->hash_id = hashed_key;
entry->kv = (t_kv_tile){.key = key, .val = value};
entry->next = NULL;
if (prev == NULL)
hmap->buckets[hashed_key % hmap->num_buckets] = entry;
else
prev->next = entry;
}
else
entry->kv.val = value;
}

View file

@ -0,0 +1,56 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hashmap_C__PREFIX___utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/06 10:58:20 by maiboyer #+# #+# */
/* Updated: 2023/12/11 15:35:37 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/hash/sip.h"
#include "me/hashmap/hashmap_C__PREFIX__.h"
#include "me/mem/mem_alloc.h"
#include "me/mem/mem_alloc_array.h"
#include "me/mem/mem_copy.h"
#include "me/types.h"
#include <stdlib.h>
C__VALTYPE__ *get_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap,
C__KEYTYPE__ *key)
{
t_usize hashed_key;
t_entry_C__PREFIX__ *entry;
t_entry_C__PREFIX__ *prev;
hmap->hfunc(&hmap->hasher, key);
hashed_key = hasher_reset_and_finish(&hmap->hasher);
entry = hashmap_get_entry_tile(hmap, hashed_key, key, &prev);
if (entry == NULL)
return (NULL);
return (&entry->kv.val);
}
void remove_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ *key)
{
t_usize hashed_key;
t_entry_C__PREFIX__ *prev;
t_entry_C__PREFIX__ *entry;
hmap->hfunc(&hmap->hasher, key);
hashed_key = hasher_reset_and_finish(&hmap->hasher);
hmap->hasher = hasher_sip13_new();
prev = NULL;
entry = hashmap_get_entry_C__PREFIX__(hmap, hashed_key, key, &prev);
if (entry == NULL)
return;
if (prev == NULL)
hmap->buckets[hashed_key % hmap->num_buckets] = entry->next;
else
prev->next = entry->next;
hmap->drop(entry->kv);
free(entry);
hmap->buckets[hashed_key % hmap->num_buckets] = NULL;
}

View file

@ -0,0 +1,130 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_to_i64.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */
/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/char/isalpha.h"
#include "me/convert/str_to_numbers.h"
#include "me/string/str_len.h"
#include "me/types.h"
#define OP_ADD 0b0001
#define OP_SUB 0b0010
#define OP_MUL 0b0100
#define OP_CHK 0b1000
t_error checked_add_C__PREFIX__(C__TYPE__ lhs, C__TYPE__ rhs, C__TYPE__ *out);
t_error checked_sub_C__PREFIX__(C__TYPE__ lhs, C__TYPE__ rhs, C__TYPE__ *out);
t_error checked_mul_C__PREFIX__(C__TYPE__ lhs, C__TYPE__ rhs, C__TYPE__ *out);
static inline bool can_not_overflow(t_u32 radix, bool is_signed_type,
t_usize digits_len)
{
return (radix <= 16 &&
digits_len <= sizeof(C__TYPE__) * 2 - (t_usize)is_signed_type);
}
static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out)
{
t_u32 digit;
if (radix < 2 || radix > 36)
return (ERROR);
digit = ascii - '0';
if (radix > 10 && digit >= 10)
{
if (!me_isalpha(ascii))
return (ERROR);
digit = ((ascii | 0b100000) - 'a') + 10;
}
if (digit >= radix)
return (ERROR);
*out = digit;
return (NO_ERROR);
}
static inline t_error do_operation(C__TYPE__ digit, t_u8 op, C__TYPE__ *result)
{
C__TYPE__ rhs;
C__TYPE__ res;
rhs = *result;
res = *result;
if (op & OP_CHK)
{
if (op & OP_MUL && checked_mul_C__PREFIX__(rhs, digit, &res))
return (ERROR);
else if (op & OP_ADD && checked_add_C__PREFIX__(rhs, digit, &res))
return (ERROR);
else if (op & OP_SUB && checked_sub_C__PREFIX__(rhs, digit, &res))
return (ERROR);
}
else
{
if (op & OP_MUL)
res = rhs * digit;
else if (op & OP_ADD)
res = rhs + digit;
else if (op & OP_SUB)
res = rhs - digit;
}
*result = res;
return (NO_ERROR);
}
static inline t_error loop_inner(t_const_str s, t_u32 radix, t_u8 op,
C__TYPE__ *out)
{
t_u32 digit;
C__TYPE__ result;
result = C__ZERO__;
while (*s)
{
if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result))
return (ERROR);
if (to_digit(*s, radix, &digit))
return (ERROR);
if (do_operation(digit, op, &result))
return (ERROR);
s++;
}
if (out)
*out = result;
return (NO_ERROR);
}
t_error str_to_C__PREFIX__(t_const_str s, t_u32 radix, C__TYPE__ *out)
{
t_usize digits_len;
bool is_positive;
t_u8 op;
if (radix < 2 || radix > 36)
return (ERROR);
digits_len = str_len(s);
is_positive = true;
if (digits_len == 0)
return (ERROR);
if ((s[0] == '-' || s[0] == '+') && s[1] == '\0')
return (ERROR);
else if (s[0] == '+')
is_positive = (s++, digits_len--, true);
else if (s[0] == '-' && C__SIGNED_TYPE__)
is_positive = (s++, digits_len--, false);
if (is_positive)
op = OP_ADD;
else
op = OP_SUB;
if (!can_not_overflow(radix, C__SIGNED_TYPE__, digits_len))
op |= OP_CHK;
return (loop_inner(s, radix, op, out));
}

View file

@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_to_i64.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */
/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/char/isalpha.h"
#include "me/convert/str_to_numbers.h"
#include "me/string/str_len.h"
#include "me/types.h"
#include "me/printf/printf.h"
t_error checked_add_C__PREFIX__(C__TYPE__ lhs, C__TYPE__ rhs, C__TYPE__ *out)
{
if (rhs > 0 && (lhs > C__MAX__ - rhs))
return (ERROR);
*out = (C__TYPE__)(lhs + rhs);
return (NO_ERROR);
}
t_error checked_sub_C__PREFIX__(C__TYPE__ lhs, C__TYPE__ rhs, C__TYPE__ *out)
{
if ((((rhs & (1 << (sizeof(C__TYPE__) - 1)) || rhs == 0) || !C__SIGNED_TYPE__) && (lhs < C__MIN__ + rhs)))
return (ERROR);
*out = (C__TYPE__)(lhs - rhs);
return (NO_ERROR);
}
t_error checked_mul_C__PREFIX__(C__TYPE__ lhs, C__TYPE__ rhs, C__TYPE__ *out)
{
C__TYPE__ mul;
mul = lhs * rhs;
if (lhs != 0 && mul / lhs != rhs)
return (ERROR);
*out = mul;
return (NO_ERROR);
}

View file

@ -0,0 +1,115 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_C__PREFIX__.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/05 18:46:28 by maiboyer #+# #+# */
/* Updated: 2023/12/09 17:54:11 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem_alloc_array.h"
#include "me/mem/mem_copy.h"
#include "me/mem/mem_set_zero.h"
#include "me/types.h"
#include "me/vec/vec_C__PREFIX__.h"
#include <stdlib.h>
t_vec_C__PREFIX__ vec_C__PREFIX___new(t_usize capacity,
t_free_C__PREFIX___item free_function)
{
t_vec_C__PREFIX__ out;
out = (t_vec_C__PREFIX__){0};
out.free_func = free_function;
out.buffer = mem_alloc_array(capacity, sizeof(C__TYPENAME__));
if (out.buffer)
out.capacity = capacity;
return (out);
}
/// Return true in case of an error
t_error vec_C__PREFIX___push(t_vec_C__PREFIX__ *vec, C__TYPENAME__ element)
{
C__TYPENAME__ *temp_buffer;
size_t new_capacity;
if (vec == NULL)
return (ERROR);
if (vec->len + 1 > vec->capacity)
{
new_capacity = (vec->capacity * 3) / 2 + 1;
while (vec->len + 1 > new_capacity)
new_capacity = (new_capacity * 3) / 2 + 1;
temp_buffer = mem_alloc_array(new_capacity, sizeof(C__TYPENAME__));
if (temp_buffer == NULL)
return (ERROR);
mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(C__TYPENAME__));
free(vec->buffer);
vec->buffer = temp_buffer;
vec->capacity = new_capacity;
}
vec->buffer[vec->len] = element;
vec->len += 1;
return (NO_ERROR);
}
/// Return true in case of an error
t_error vec_C__PREFIX___reserve(t_vec_C__PREFIX__ *vec, t_usize wanted_capacity)
{
C__TYPENAME__ *temp_buffer;
size_t new_capacity;
if (vec == NULL)
return (ERROR);
if (wanted_capacity > vec->capacity)
{
new_capacity = (vec->capacity * 3) / 2 + 1;
while (wanted_capacity > new_capacity)
new_capacity = (new_capacity * 3) / 2 + 1;
temp_buffer = mem_alloc_array(new_capacity, sizeof(C__TYPENAME__));
if (temp_buffer == NULL)
return (ERROR);
mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(C__TYPENAME__));
free(vec->buffer);
vec->buffer = temp_buffer;
vec->capacity = new_capacity;
}
return (NO_ERROR);
}
/// Return true if the vector is empty
/// This function is safe to call with value being NULL
t_error vec_C__PREFIX___pop(t_vec_C__PREFIX__ *vec, C__TYPENAME__ *value)
{
C__TYPENAME__ temp_value;
C__TYPENAME__ *ptr;
if (vec == NULL)
return (ERROR);
ptr = value;
if (vec->len == 0)
return (ERROR);
if (value == NULL)
ptr = &temp_value;
vec->len--;
*ptr = vec->buffer[vec->len];
mem_set_zero(&vec->buffer[vec->len], sizeof(C__TYPENAME__));
return (NO_ERROR);
}
/// This function is safe to call with `free_elem` being NULL
void vec_C__PREFIX___free(t_vec_C__PREFIX__ vec)
{
if (vec.free_func)
{
while (vec.len)
{
vec.free_func(vec.buffer[vec.len - 1]);
vec.len--;
}
}
free(vec.buffer);
}

View file

@ -0,0 +1,112 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_C__PREFIX__.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */
/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem_alloc_array.h"
#include "me/mem/mem_copy.h"
#include "me/mem/mem_set_zero.h"
#include "me/types.h"
#include "me/vec/vec_C__PREFIX__.h"
#include <stdlib.h>
t_error vec_C__PREFIX___find(t_vec_C__PREFIX__ *vec,
bool (*fn)(const C__TYPENAME__ *), t_usize *index)
{
t_usize idx;
if (vec == NULL || fn == NULL || index == NULL)
return (ERROR);
idx = 0;
while (idx < vec->len)
{
if (fn(&vec->buffer[idx]))
{
*index = idx;
return (NO_ERROR);
}
idx++;
}
return (ERROR);
}
t_error vec_C__PREFIX___find_starting(t_vec_C__PREFIX__ *vec,
bool (*fn)(const C__TYPENAME__ *),
t_usize starting_index, t_usize *index)
{
t_usize idx;
if (vec == NULL || fn == NULL || index == NULL)
return (ERROR);
idx = starting_index;
while (idx < vec->len)
{
if (fn(&vec->buffer[idx]))
{
*index = idx;
return (NO_ERROR);
}
idx++;
}
return (ERROR);
}
t_error vec_C__PREFIX___all(t_vec_C__PREFIX__ *vec,
bool (*fn)(const C__TYPENAME__ *), bool *result)
{
t_usize idx;
if (vec == NULL || fn == NULL || result == NULL)
return (ERROR);
idx = 0;
*result = true;
while (*result && idx < vec->len)
{
if (!fn(&vec->buffer[idx]))
*result = false;
idx++;
}
return (ERROR);
}
t_error vec_C__PREFIX___any(t_vec_C__PREFIX__ *vec,
bool (*fn)(const C__TYPENAME__ *), bool *result)
{
t_usize idx;
if (vec == NULL || fn == NULL || result == NULL)
return (ERROR);
idx = 0;
*result = false;
while (*result && idx < vec->len)
{
if (fn(&vec->buffer[idx]))
*result = true;
idx++;
}
return (ERROR);
}
void vec_C__PREFIX___iter(t_vec_C__PREFIX__ *vec,
void (*fn)(t_usize index, C__TYPENAME__ *value,
void *state),
void *state)
{
t_usize idx;
if (vec == NULL || fn == NULL)
return;
idx = 0;
while (idx < vec->len)
{
fn(idx, &vec->buffer[idx], state);
idx++;
}
}

View file

@ -0,0 +1,73 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_C__PREFIX__.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */
/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem_alloc_array.h"
#include "me/mem/mem_copy.h"
#include "me/mem/mem_set_zero.h"
#include "me/types.h"
#include "me/vec/vec_C__PREFIX__.h"
#include <stdlib.h>
t_error vec_C__PREFIX___push_front(t_vec_C__PREFIX__ *vec,
C__TYPENAME__ element)
{
t_usize i;
if (vec->len == 0)
return (vec_C__PREFIX___push(vec, element));
i = vec->len - 1;
if (vec->capacity < vec->len + 1 &&
vec_C__PREFIX___reserve(vec, 3 * vec->len / 2 + 1))
return (ERROR);
while (i > 0)
{
vec->buffer[i + 1] = vec->buffer[i];
i--;
}
vec->buffer[1] = vec->buffer[0];
vec->buffer[0] = element;
vec->len++;
return (NO_ERROR);
}
t_error vec_C__PREFIX___pop_front(t_vec_C__PREFIX__ *vec, C__TYPENAME__ *value)
{
t_usize i;
if (vec->len <= 1)
return (vec_C__PREFIX___pop(vec, value));
i = 0;
*value = vec->buffer[0];
vec->len--;
while (i < vec->len)
{
vec->buffer[i] = vec->buffer[i + 1];
i++;
}
mem_set_zero(&vec->buffer[i], sizeof(*vec->buffer));
return (NO_ERROR);
}
void vec_C__PREFIX___reverse(t_vec_C__PREFIX__ *vec)
{
C__TYPENAME__ temporary;
t_usize i;
i = 0;
while (i < vec->len / 2)
{
temporary = vec->buffer[vec->len - 1 - i];
vec->buffer[vec->len - 1 - i] = vec->buffer[i];
vec->buffer[i] = temporary;
i++;
}
}

View file

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* best_move.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/29 20:04:33 by maiboyer #+# #+# */
/* Updated: 2024/01/31 14:25:00 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/types.h"
#include "me/vec/vec_C__PREFIX__.h"
void vec_C__PREFIX___sort(t_vec_C__PREFIX__ *v,
t_vec_C__PREFIX___sort_fn is_sorted_fn)
{
t_usize sorted_part;
t_usize i;
C__TYPENAME__ tmp;
if (v == NULL)
return;
sorted_part = v->len;
while (sorted_part > 0)
{
i = 0;
while (i < sorted_part - 1)
{
if (!is_sorted_fn(&v->buffer[i], &v->buffer[i + 1]))
{
tmp = v->buffer[i];
v->buffer[i] = v->buffer[i + 1];
v->buffer[i + 1] = tmp;
}
i++;
}
sorted_part--;
}
}