WIP IFS spliiting

This commit is contained in:
Maieul BOYER 2024-07-24 18:54:13 +02:00
parent de9a72e368
commit f56432b372
No known key found for this signature in database
18 changed files with 629 additions and 37 deletions

View file

@ -0,0 +1,95 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_estr.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.h"
#include "me/types.h"
#include "me/vec/vec_estr.h"
#include <stdlib.h>
t_vec_estr vec_estr_new(t_usize capacity,
t_free_estr_item free_function)
{
t_vec_estr out;
out = (t_vec_estr){0};
out.free_func = free_function;
out.buffer = mem_alloc_array(capacity, sizeof(t_expandable_str));
if (out.buffer)
out.capacity = capacity;
return (out);
}
/// Return true in case of an error
t_error vec_estr_push(t_vec_estr *vec, t_expandable_str element)
{
if (vec == NULL)
return (ERROR);
vec_estr_reserve(vec, vec->len + 1);
vec->buffer[vec->len] = element;
vec->len += 1;
return (NO_ERROR);
}
/// Return true in case of an error
t_error vec_estr_reserve(t_vec_estr *vec, t_usize wanted_capacity)
{
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;
vec->buffer =
mem_realloc_array(vec->buffer, new_capacity, sizeof(t_expandable_str));
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_estr_pop(t_vec_estr *vec, t_expandable_str *value)
{
t_expandable_str temp_value;
t_expandable_str *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(t_expandable_str));
return (NO_ERROR);
}
/// This function is safe to call with `free_elem` being NULL
void vec_estr_free(t_vec_estr vec)
{
if (vec.buffer == NULL)
return;
if (vec.free_func)
{
while (vec.len)
{
vec.free_func(vec.buffer[vec.len - 1]);
vec.len--;
}
}
mem_free(vec.buffer);
}

View file

@ -0,0 +1,112 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_estr.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.h"
#include "me/mem/mem.h"
#include "me/mem/mem.h"
#include "me/types.h"
#include "me/vec/vec_estr.h"
#include <stdlib.h>
t_error vec_estr_find(t_vec_estr *vec,
bool (*fn)(const t_expandable_str *), t_usize *index)
{
t_usize idx;
if (vec == NULL || fn == NULL || index == NULL)
return (ERROR);
idx = 0;
while (idx < vec->len)
{
if (fn((const t_expandable_str *)&vec->buffer[idx]))
{
*index = idx;
return (NO_ERROR);
}
idx++;
}
return (ERROR);
}
t_error vec_estr_find_starting(t_vec_estr *vec,
bool (*fn)(const t_expandable_str *),
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((const t_expandable_str *)&vec->buffer[idx]))
{
*index = idx;
return (NO_ERROR);
}
idx++;
}
return (ERROR);
}
t_error vec_estr_all(t_vec_estr *vec,
bool (*fn)(const t_expandable_str *), 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((const t_expandable_str *)&vec->buffer[idx]))
*result = false;
idx++;
}
return (ERROR);
}
t_error vec_estr_any(t_vec_estr *vec,
bool (*fn)(const t_expandable_str *), 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((const t_expandable_str *)&vec->buffer[idx]))
*result = true;
idx++;
}
return (ERROR);
}
void vec_estr_iter(t_vec_estr *vec,
void (*fn)(t_usize index, t_expandable_str *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,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_estr.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.h"
#include "me/mem/mem.h"
#include "me/mem/mem.h"
#include "me/types.h"
#include "me/vec/vec_estr.h"
#include <stdlib.h>
t_error vec_estr_push_front(t_vec_estr *vec,
t_expandable_str element)
{
t_usize i;
if (vec->len == 0)
return (vec_estr_push(vec, element));
i = vec->len - 1;
if (vec->capacity < vec->len + 1 &&
vec_estr_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_estr_pop_front(t_vec_estr *vec, t_expandable_str *value)
{
t_usize i;
if (vec->len <= 1)
return (vec_estr_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_estr_reverse(t_vec_estr *vec)
{
t_expandable_str 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++;
}
}
t_error vec_estr_back(t_vec_estr *vec, t_expandable_str **out)
{
t_expandable_str *temporary;
if (out == NULL)
out = &temporary;
if (vec->len != 0)
return (*out = &vec->buffer[vec->len - 1], true);
return (false);
}