stdme is fully normed !

This commit is contained in:
Maieul BOYER 2024-08-01 07:34:51 +02:00
parent 2c9a3ee834
commit 4e72b48b4a
No known key found for this signature in database
21 changed files with 281 additions and 278 deletions

View file

@ -1,123 +1,140 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* vec_buf_str.h :+: :+: :+: */ /* vec_buf_str.h :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/04 18:46:53 by maiboyer #+# #+# */ /* Created: 2023/12/04 18:46:53 by maiboyer #+# #+# */
/* Updated: 2023/12/09 17:53:00 by maiboyer ### ########.fr */ /* Updated: 2024/08/01 07:33:44 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef VEC_BUF_STR_H #ifndef VEC_BUF_STR_H
#define VEC_BUF_STR_H # define VEC_BUF_STR_H
#include "me/string/string.h" # include "me/string/string.h"
#include "me/types.h" # include "me/types.h"
/// @brief A function that takes two t_string and compare them /// @brief A function that takes two t_string and compare them
typedef bool (*t_vec_buf_str_sort_fn)(t_string *, t_string *); typedef bool (*t_vec_buf_str_sort_fn)(t_string *, t_string *);
/// @brief A function that free an t_string /// @brief A function that free an t_string
typedef void (*t_free_buf_str_item)(t_string); typedef void (*t_free_buf_str_item)(t_string);
/// @brief A dynamic array of t_string /// @brief A dynamic array of t_string
typedef struct s_vec_buf_str typedef struct s_vec_buf_str
{ {
t_free_buf_str_item free_func; t_free_buf_str_item free_func;
t_usize len; t_usize len;
t_usize capacity; t_usize capacity;
t_string *buffer; t_string *buffer;
} t_vec_buf_str; } t_vec_buf_str;
/// @brief Create a new vec_buf_str with a given capacity /// @brief Create a new vec_buf_str with a given capacity
/// @param capacity The capacity of the new vec_buf_str (in terms of elements) /// @param capacity The capacity of the new vec_buf_str (in terms of elements)
/// @param free_function The function that will be used to free the elements of the vec_buf_str /// @param free_function The function that will be used to free the elements of
t_vec_buf_str vec_buf_str_new(t_usize capacity, t_free_buf_str_item free_function); /// the vec_buf_str
t_vec_buf_str vec_buf_str_new(t_usize capacity,
t_free_buf_str_item free_function);
/// @brief Push an element to the last position of the vec_buf_str /// @brief Push an element to the last position of the vec_buf_str
/// @param vec The vec_buf_str to push the element to /// @param vec The vec_buf_str to push the element to
/// @param element The element to push /// @param element The element to push
t_error vec_buf_str_push(t_vec_buf_str *vec, t_string element); t_error vec_buf_str_push(t_vec_buf_str *vec, t_string element);
/// @brief Push an element to the first position of the vec_buf_str /// @brief Push an element to the first position of the vec_buf_str
/// @param vec The vec_buf_str to push the element to /// @param vec The vec_buf_str to push the element to
/// @param element The element to push /// @param element The element to push
/// @note This operation is O(n) /// @note This operation is O(n)
t_error vec_buf_str_push_front(t_vec_buf_str *vec, t_string element); t_error vec_buf_str_push_front(t_vec_buf_str *vec,
t_string element);
/// @brief Get the last element from the vec_buf_str, and remove it from the vec_buf_str /// @brief Get the last element from the vec_buf_str,
/// and remove it from the vec_buf_str
/// @param vec The vec_buf_str to get the element from /// @param vec The vec_buf_str to get the element from
/// @param[out] out The last element of the vec_buf_str /// @param[out] out The last element of the vec_buf_str
/// @return true if the operation failed, false otherwise /// @return true if the operation failed, false otherwise
t_error vec_buf_str_pop(t_vec_buf_str *vec, t_string *value); t_error vec_buf_str_pop(t_vec_buf_str *vec, t_string *value);
/// @brief Get the first element from the vec_buf_str,
/// @brief Get the first element from the vec_buf_str, and remove it from the vec_buf_str /// and remove it from the vec_buf_str
/// @param vec The vec_buf_str to get the element from /// @param vec The vec_buf_str to get the element from
/// @param[out] out The first element of the vec_buf_str /// @param[out] out The first element of the vec_buf_str
/// @return true if the operation failed, false otherwise /// @return true if the operation failed, false otherwise
/// @note This operation is O(n) /// @note This operation is O(n)
t_error vec_buf_str_pop_front(t_vec_buf_str *vec, t_string *value); t_error vec_buf_str_pop_front(t_vec_buf_str *vec,
t_string *value);
/// @brief Free the vector and all its elements /// @brief Free the vector and all its elements
/// @param vec The vec_buf_str to free /// @param vec The vec_buf_str to free
void vec_buf_str_free(t_vec_buf_str vec); void vec_buf_str_free(t_vec_buf_str vec);
/// @brief Make the vec_buf_str at least the given capacity /// @brief Make the vec_buf_str at least the given capacity
/// @param vec The vec_buf_str to reserve /// @param vec The vec_buf_str to reserve
/// @param wanted_capacity The minimum capacity to reserve /// @param wanted_capacity The minimum capacity to reserve
/// @return true if the operation failed, false otherwise /// @return true if the operation failed, false otherwise
t_error vec_buf_str_reserve(t_vec_buf_str *vec, t_usize wanted_capacity); t_error vec_buf_str_reserve(t_vec_buf_str *vec,
t_usize wanted_capacity);
/// @brief Run the function and returns the index of the first element that returns true /// @brief Run the function and returns the index of the first element that
/// returns true
/// @param vec The vec_buf_str to search in /// @param vec The vec_buf_str to search in
/// @param fn The function to run on each element /// @param fn The function to run on each element
/// @param[out] index The index of the first element that returns true /// @param[out] index The index of the first element that returns true
t_error vec_buf_str_find(t_vec_buf_str *vec, bool (*fn)(const t_string *), t_usize *index); t_error vec_buf_str_find(t_vec_buf_str *vec,
bool (*fn)(const t_string *), t_usize *index);
/// @brief Run the function and returns the index of the first element that
/// @brief Run the function and returns the index of the first element that returns true, but starting at index starting_index /// returns true, but starting at index starting_index
/// @param vec The vec_buf_str to search in /// @param vec The vec_buf_str to search in
/// @param fn The function to run on each element /// @param fn The function to run on each element
/// @param starting_index The index to start the search from /// @param starting_index The index to start the search from
/// @param[out] index The index of the first element that returns true /// @param[out] index The index of the first element that returns true
t_error vec_buf_str_find_starting(t_vec_buf_str *vec, bool (*fn)(const t_string *), t_usize starting_index, t_usize *index); t_error vec_buf_str_find_starting(t_vec_buf_str *vec,
bool (*fn)(const t_string *),
t_usize starting_index, t_usize *index);
/// @brief Run the function on every element of the vec_buf_str and returns if all elements returned true /// @brief Run the function on every element of the vec_buf_str and returns if
/// all elements returned true
/// @param vec The vec_buf_str to search in /// @param vec The vec_buf_str to search in
/// @param fn The function to run on each element /// @param fn The function to run on each element
/// @param[out] result The result of the operation /// @param[out] result The result of the operation
/// @return true if the operation failed, false otherwise /// @return true if the operation failed, false otherwise
/// @note If the vec_buf_str is empty, result will be true /// @note If the vec_buf_str is empty, result will be true
t_error vec_buf_str_all(t_vec_buf_str *vec, bool (*fn)(const t_string *), bool *result); t_error vec_buf_str_all(t_vec_buf_str *vec,
bool (*fn)(const t_string *), bool *result);
/// @brief Run the function on every element of the vec_buf_str and returns if any element returned true /// @brief Run the function on every element of the vec_buf_str and returns if
/// any element returned true
/// @param vec The vec_buf_str to search in /// @param vec The vec_buf_str to search in
/// @param fn The function to run on each element /// @param fn The function to run on each element
/// @param[out] result The result of the operation /// @param[out] result The result of the operation
/// @return true if the operation failed, false otherwise /// @return true if the operation failed, false otherwise
/// @note If the vec_buf_str is empty, result will be false /// @note If the vec_buf_str is empty, result will be false
t_error vec_buf_str_any(t_vec_buf_str *vec, bool (*fn)(const t_string *), bool *result); t_error vec_buf_str_any(t_vec_buf_str *vec,
bool (*fn)(const t_string *), bool *result);
/// @brief Run the function on every element of the vec_buf_str /// @brief Run the function on every element of the vec_buf_str
/// @param vec The vec_buf_str to iterate over /// @param vec The vec_buf_str to iterate over
/// @param fn The function to run on each element /// @param fn The function to run on each element
/// @param state The state to pass to the function /// @param state The state to pass to the function
void vec_buf_str_iter(t_vec_buf_str *vec, void (*fn)(t_usize index, t_string *value, void *state), void *state); void vec_buf_str_iter(t_vec_buf_str *vec,
void (*fn)(t_usize index, t_string *value,
void *state), void *state);
/// @brief Reverse the order of the elements in the vec_buf_str /// @brief Reverse the order of the elements in the vec_buf_str
/// @param vec The vec_buf_str to reverse /// @param vec The vec_buf_str to reverse
void vec_buf_str_reverse(t_vec_buf_str *vec); void vec_buf_str_reverse(t_vec_buf_str *vec);
/// @brief Sort the elements of the vec_buf_str /// @brief Sort the elements of the vec_buf_str
/// @param vec The vec_buf_str to sort /// @param vec The vec_buf_str to sort
/// @param is_sorted The function to use to compare the elements /// @param is_sorted The function to use to compare the elements
void vec_buf_str_sort(t_vec_buf_str *vec, t_vec_buf_str_sort_fn is_sorted); void vec_buf_str_sort(t_vec_buf_str *vec,
t_vec_buf_str_sort_fn is_sorted);
/// @brief Get a pointer to the last element of the vec_buf_str /// @brief Get a pointer to the last element of the vec_buf_str
/// @param vec The vec_buf_str to get the element from /// @param vec The vec_buf_str to get the element from
/// @param[out] out A pointer to the last element of the vec_buf_str /// @param[out] out A pointer to the last element of the vec_buf_str
/// @return true if the operation failed, false otherwise /// @return true if the operation failed, false otherwise
t_error vec_buf_str_back(t_vec_buf_str *vec, t_string **out); t_error vec_buf_str_back(t_vec_buf_str *vec, t_string **out);
#endif #endif

View file

@ -1,123 +1,133 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* vec_str.h :+: :+: :+: */ /* vec_str.h :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/04 18:46:53 by maiboyer #+# #+# */ /* Created: 2023/12/04 18:46:53 by maiboyer #+# #+# */
/* Updated: 2023/12/09 17:53:00 by maiboyer ### ########.fr */ /* Updated: 2024/08/01 07:34:19 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef VEC_STR_H #ifndef VEC_STR_H
#define VEC_STR_H # define VEC_STR_H
# include "me/types.h"
#include "me/types.h"
/// @brief A function that takes two t_str and compare them /// @brief A function that takes two t_str and compare them
typedef bool (*t_vec_str_sort_fn)(t_str *, t_str *); typedef bool (*t_vec_str_sort_fn)(t_str *, t_str *);
/// @brief A function that free an t_str /// @brief A function that free an t_str
typedef void (*t_free_str_item)(t_str); typedef void (*t_free_str_item)(t_str);
/// @brief A dynamic array of t_str /// @brief A dynamic array of t_str
typedef struct s_vec_str typedef struct s_vec_str
{ {
t_free_str_item free_func; t_free_str_item free_func;
t_usize len; t_usize len;
t_usize capacity; t_usize capacity;
t_str *buffer; t_str *buffer;
} t_vec_str; } t_vec_str;
/// @brief Create a new vec_str with a given capacity /// @brief Create a new vec_str with a given capacity
/// @param capacity The capacity of the new vec_str (in terms of elements) /// @param capacity The capacity of the new vec_str (in terms of elements)
/// @param free_function The function that will be used to free the elements of the vec_str /// @param free_function The function that will be used to free the elements of
t_vec_str vec_str_new(t_usize capacity, t_free_str_item free_function); /// the vec_str
t_vec_str vec_str_new(t_usize capacity,
t_free_str_item free_function);
/// @brief Push an element to the last position of the vec_str /// @brief Push an element to the last position of the vec_str
/// @param vec The vec_str to push the element to /// @param vec The vec_str to push the element to
/// @param element The element to push /// @param element The element to push
t_error vec_str_push(t_vec_str *vec, t_str element); t_error vec_str_push(t_vec_str *vec, t_str element);
/// @brief Push an element to the first position of the vec_str /// @brief Push an element to the first position of the vec_str
/// @param vec The vec_str to push the element to /// @param vec The vec_str to push the element to
/// @param element The element to push /// @param element The element to push
/// @note This operation is O(n) /// @note This operation is O(n)
t_error vec_str_push_front(t_vec_str *vec, t_str element); t_error vec_str_push_front(t_vec_str *vec, t_str element);
/// @brief Get the last element from the vec_str, and remove it from the vec_str /// @brief Get the last element from the vec_str, and remove it from the vec_str
/// @param vec The vec_str to get the element from /// @param vec The vec_str to get the element from
/// @param[out] out The last element of the vec_str /// @param[out] out The last element of the vec_str
/// @return true if the operation failed, false otherwise /// @return true if the operation failed, false otherwise
t_error vec_str_pop(t_vec_str *vec, t_str *value); t_error vec_str_pop(t_vec_str *vec, t_str *value);
/// @brief Get the first element from the vec_str,
/// @brief Get the first element from the vec_str, and remove it from the vec_str /// and remove it from the vec_str
/// @param vec The vec_str to get the element from /// @param vec The vec_str to get the element from
/// @param[out] out The first element of the vec_str /// @param[out] out The first element of the vec_str
/// @return true if the operation failed, false otherwise /// @return true if the operation failed, false otherwise
/// @note This operation is O(n) /// @note This operation is O(n)
t_error vec_str_pop_front(t_vec_str *vec, t_str *value); t_error vec_str_pop_front(t_vec_str *vec, t_str *value);
/// @brief Free the vector and all its elements /// @brief Free the vector and all its elements
/// @param vec The vec_str to free /// @param vec The vec_str to free
void vec_str_free(t_vec_str vec); void vec_str_free(t_vec_str vec);
/// @brief Make the vec_str at least the given capacity /// @brief Make the vec_str at least the given capacity
/// @param vec The vec_str to reserve /// @param vec The vec_str to reserve
/// @param wanted_capacity The minimum capacity to reserve /// @param wanted_capacity The minimum capacity to reserve
/// @return true if the operation failed, false otherwise /// @return true if the operation failed, false otherwise
t_error vec_str_reserve(t_vec_str *vec, t_usize wanted_capacity); t_error vec_str_reserve(t_vec_str *vec, t_usize wanted_capacity);
/// @brief Run the function and returns the index of the first element that returns true /// @brief Run the function and returns the index of the first element that
/// returns true
/// @param vec The vec_str to search in /// @param vec The vec_str to search in
/// @param fn The function to run on each element /// @param fn The function to run on each element
/// @param[out] index The index of the first element that returns true /// @param[out] index The index of the first element that returns true
t_error vec_str_find(t_vec_str *vec, bool (*fn)(const t_str *), t_usize *index); t_error vec_str_find(t_vec_str *vec, bool (*fn)(const t_str *),
t_usize *index);
/// @brief Run the function and returns the index of the first element that
/// @brief Run the function and returns the index of the first element that returns true, but starting at index starting_index /// returns true, but starting at index starting_index
/// @param vec The vec_str to search in /// @param vec The vec_str to search in
/// @param fn The function to run on each element /// @param fn The function to run on each element
/// @param starting_index The index to start the search from /// @param starting_index The index to start the search from
/// @param[out] index The index of the first element that returns true /// @param[out] index The index of the first element that returns true
t_error vec_str_find_starting(t_vec_str *vec, bool (*fn)(const t_str *), t_usize starting_index, t_usize *index); t_error vec_str_find_starting(t_vec_str *vec,
bool (*fn)(const t_str *), t_usize starting_index,
t_usize *index);
/// @brief Run the function on every element of the vec_str and returns if all elements returned true /// @brief Run the function on every element of the vec_str and returns if all
/// elements returned true
/// @param vec The vec_str to search in /// @param vec The vec_str to search in
/// @param fn The function to run on each element /// @param fn The function to run on each element
/// @param[out] result The result of the operation /// @param[out] result The result of the operation
/// @return true if the operation failed, false otherwise /// @return true if the operation failed, false otherwise
/// @note If the vec_str is empty, result will be true /// @note If the vec_str is empty, result will be true
t_error vec_str_all(t_vec_str *vec, bool (*fn)(const t_str *), bool *result); t_error vec_str_all(t_vec_str *vec, bool (*fn)(const t_str *),
bool *result);
/// @brief Run the function on every element of the vec_str and returns if any element returned true /// @brief Run the function on every element of the vec_str and returns if any
/// element returned true
/// @param vec The vec_str to search in /// @param vec The vec_str to search in
/// @param fn The function to run on each element /// @param fn The function to run on each element
/// @param[out] result The result of the operation /// @param[out] result The result of the operation
/// @return true if the operation failed, false otherwise /// @return true if the operation failed, false otherwise
/// @note If the vec_str is empty, result will be false /// @note If the vec_str is empty, result will be false
t_error vec_str_any(t_vec_str *vec, bool (*fn)(const t_str *), bool *result); t_error vec_str_any(t_vec_str *vec, bool (*fn)(const t_str *),
bool *result);
/// @brief Run the function on every element of the vec_str /// @brief Run the function on every element of the vec_str
/// @param vec The vec_str to iterate over /// @param vec The vec_str to iterate over
/// @param fn The function to run on each element /// @param fn The function to run on each element
/// @param state The state to pass to the function /// @param state The state to pass to the function
void vec_str_iter(t_vec_str *vec, void (*fn)(t_usize index, t_str *value, void *state), void *state); void vec_str_iter(t_vec_str *vec, void (*fn)(t_usize index,
t_str *value, void *state), void *state);
/// @brief Reverse the order of the elements in the vec_str /// @brief Reverse the order of the elements in the vec_str
/// @param vec The vec_str to reverse /// @param vec The vec_str to reverse
void vec_str_reverse(t_vec_str *vec); void vec_str_reverse(t_vec_str *vec);
/// @brief Sort the elements of the vec_str /// @brief Sort the elements of the vec_str
/// @param vec The vec_str to sort /// @param vec The vec_str to sort
/// @param is_sorted The function to use to compare the elements /// @param is_sorted The function to use to compare the elements
void vec_str_sort(t_vec_str *vec, t_vec_str_sort_fn is_sorted); void vec_str_sort(t_vec_str *vec, t_vec_str_sort_fn is_sorted);
/// @brief Get a pointer to the last element of the vec_str /// @brief Get a pointer to the last element of the vec_str
/// @param vec The vec_str to get the element from /// @param vec The vec_str to get the element from
/// @param[out] out A pointer to the last element of the vec_str /// @param[out] out A pointer to the last element of the vec_str
/// @return true if the operation failed, false otherwise /// @return true if the operation failed, false otherwise
t_error vec_str_back(t_vec_str *vec, t_str **out); t_error vec_str_back(t_vec_str *vec, t_str **out);
#endif #endif

View file

@ -1,123 +1,131 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* vec_u8.h :+: :+: :+: */ /* vec_u8.h :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/04 18:46:53 by maiboyer #+# #+# */ /* Created: 2023/12/04 18:46:53 by maiboyer #+# #+# */
/* Updated: 2023/12/09 17:53:00 by maiboyer ### ########.fr */ /* Updated: 2024/08/01 07:33:13 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef VEC_U8_H #ifndef VEC_U8_H
#define VEC_U8_H # define VEC_U8_H
# include "me/types.h"
#include "me/types.h"
/// @brief A function that takes two t_u8 and compare them /// @brief A function that takes two t_u8 and compare them
typedef bool (*t_vec_u8_sort_fn)(t_u8 *, t_u8 *); typedef bool (*t_vec_u8_sort_fn)(t_u8 *, t_u8 *);
/// @brief A function that free an t_u8 /// @brief A function that free an t_u8
typedef void (*t_free_u8_item)(t_u8); typedef void (*t_free_u8_item)(t_u8);
/// @brief A dynamic array of t_u8 /// @brief A dynamic array of t_u8
typedef struct s_vec_u8 typedef struct s_vec_u8
{ {
t_free_u8_item free_func; t_free_u8_item free_func;
t_usize len; t_usize len;
t_usize capacity; t_usize capacity;
t_u8 *buffer; t_u8 *buffer;
} t_vec_u8; } t_vec_u8;
/// @brief Create a new vec_u8 with a given capacity /// @brief Create a new vec_u8 with a given capacity
/// @param capacity The capacity of the new vec_u8 (in terms of elements) /// @param capacity The capacity of the new vec_u8 (in terms of elements)
/// @param free_function The function that will be used to free the elements of the vec_u8 /// @param free_function The function that will be used to free the elements of
t_vec_u8 vec_u8_new(t_usize capacity, t_free_u8_item free_function); /// the vec_u8
t_vec_u8 vec_u8_new(t_usize capacity, t_free_u8_item free_function);
/// @brief Push an element to the last position of the vec_u8 /// @brief Push an element to the last position of the vec_u8
/// @param vec The vec_u8 to push the element to /// @param vec The vec_u8 to push the element to
/// @param element The element to push /// @param element The element to push
t_error vec_u8_push(t_vec_u8 *vec, t_u8 element); t_error vec_u8_push(t_vec_u8 *vec, t_u8 element);
/// @brief Push an element to the first position of the vec_u8 /// @brief Push an element to the first position of the vec_u8
/// @param vec The vec_u8 to push the element to /// @param vec The vec_u8 to push the element to
/// @param element The element to push /// @param element The element to push
/// @note This operation is O(n) /// @note This operation is O(n)
t_error vec_u8_push_front(t_vec_u8 *vec, t_u8 element); t_error vec_u8_push_front(t_vec_u8 *vec, t_u8 element);
/// @brief Get the last element from the vec_u8, and remove it from the vec_u8 /// @brief Get the last element from the vec_u8, and remove it from the vec_u8
/// @param vec The vec_u8 to get the element from /// @param vec The vec_u8 to get the element from
/// @param[out] out The last element of the vec_u8 /// @param[out] out The last element of the vec_u8
/// @return true if the operation failed, false otherwise /// @return true if the operation failed, false otherwise
t_error vec_u8_pop(t_vec_u8 *vec, t_u8 *value); t_error vec_u8_pop(t_vec_u8 *vec, t_u8 *value);
/// @brief Get the first element from the vec_u8, and remove it from the vec_u8 /// @brief Get the first element from the vec_u8, and remove it from the vec_u8
/// @param vec The vec_u8 to get the element from /// @param vec The vec_u8 to get the element from
/// @param[out] out The first element of the vec_u8 /// @param[out] out The first element of the vec_u8
/// @return true if the operation failed, false otherwise /// @return true if the operation failed, false otherwise
/// @note This operation is O(n) /// @note This operation is O(n)
t_error vec_u8_pop_front(t_vec_u8 *vec, t_u8 *value); t_error vec_u8_pop_front(t_vec_u8 *vec, t_u8 *value);
/// @brief Free the vector and all its elements /// @brief Free the vector and all its elements
/// @param vec The vec_u8 to free /// @param vec The vec_u8 to free
void vec_u8_free(t_vec_u8 vec); void vec_u8_free(t_vec_u8 vec);
/// @brief Make the vec_u8 at least the given capacity /// @brief Make the vec_u8 at least the given capacity
/// @param vec The vec_u8 to reserve /// @param vec The vec_u8 to reserve
/// @param wanted_capacity The minimum capacity to reserve /// @param wanted_capacity The minimum capacity to reserve
/// @return true if the operation failed, false otherwise /// @return true if the operation failed, false otherwise
t_error vec_u8_reserve(t_vec_u8 *vec, t_usize wanted_capacity); t_error vec_u8_reserve(t_vec_u8 *vec, t_usize wanted_capacity);
/// @brief Run the function and returns the index of the first element that returns true /// @brief Run the function and returns the index of the first element that
/// returns true
/// @param vec The vec_u8 to search in /// @param vec The vec_u8 to search in
/// @param fn The function to run on each element /// @param fn The function to run on each element
/// @param[out] index The index of the first element that returns true /// @param[out] index The index of the first element that returns true
t_error vec_u8_find(t_vec_u8 *vec, bool (*fn)(const t_u8 *), t_usize *index); t_error vec_u8_find(t_vec_u8 *vec, bool (*fn)(const t_u8 *),
t_usize *index);
/// @brief Run the function and returns the index of the first element that
/// @brief Run the function and returns the index of the first element that returns true, but starting at index starting_index /// returns true, but starting at index starting_index
/// @param vec The vec_u8 to search in /// @param vec The vec_u8 to search in
/// @param fn The function to run on each element /// @param fn The function to run on each element
/// @param starting_index The index to start the search from /// @param starting_index The index to start the search from
/// @param[out] index The index of the first element that returns true /// @param[out] index The index of the first element that returns true
t_error vec_u8_find_starting(t_vec_u8 *vec, bool (*fn)(const t_u8 *), t_usize starting_index, t_usize *index); t_error vec_u8_find_starting(t_vec_u8 *vec,
bool (*fn)(const t_u8 *), t_usize starting_index,
t_usize *index);
/// @brief Run the function on every element of the vec_u8 and returns if all elements returned true /// @brief Run the function on every element of the vec_u8 and returns if all
/// elements returned true
/// @param vec The vec_u8 to search in /// @param vec The vec_u8 to search in
/// @param fn The function to run on each element /// @param fn The function to run on each element
/// @param[out] result The result of the operation /// @param[out] result The result of the operation
/// @return true if the operation failed, false otherwise /// @return true if the operation failed, false otherwise
/// @note If the vec_u8 is empty, result will be true /// @note If the vec_u8 is empty, result will be true
t_error vec_u8_all(t_vec_u8 *vec, bool (*fn)(const t_u8 *), bool *result); t_error vec_u8_all(t_vec_u8 *vec, bool (*fn)(const t_u8 *),
bool *result);
/// @brief Run the function on every element of the vec_u8 and returns if any element returned true /// @brief Run the function on every element of the vec_u8 and returns if any
/// element returned true
/// @param vec The vec_u8 to search in /// @param vec The vec_u8 to search in
/// @param fn The function to run on each element /// @param fn The function to run on each element
/// @param[out] result The result of the operation /// @param[out] result The result of the operation
/// @return true if the operation failed, false otherwise /// @return true if the operation failed, false otherwise
/// @note If the vec_u8 is empty, result will be false /// @note If the vec_u8 is empty, result will be false
t_error vec_u8_any(t_vec_u8 *vec, bool (*fn)(const t_u8 *), bool *result); t_error vec_u8_any(t_vec_u8 *vec, bool (*fn)(const t_u8 *),
bool *result);
/// @brief Run the function on every element of the vec_u8 /// @brief Run the function on every element of the vec_u8
/// @param vec The vec_u8 to iterate over /// @param vec The vec_u8 to iterate over
/// @param fn The function to run on each element /// @param fn The function to run on each element
/// @param state The state to pass to the function /// @param state The state to pass to the function
void vec_u8_iter(t_vec_u8 *vec, void (*fn)(t_usize index, t_u8 *value, void *state), void *state); void vec_u8_iter(t_vec_u8 *vec, void (*fn)(t_usize index,
t_u8 *value, void *state), void *state);
/// @brief Reverse the order of the elements in the vec_u8 /// @brief Reverse the order of the elements in the vec_u8
/// @param vec The vec_u8 to reverse /// @param vec The vec_u8 to reverse
void vec_u8_reverse(t_vec_u8 *vec); void vec_u8_reverse(t_vec_u8 *vec);
/// @brief Sort the elements of the vec_u8 /// @brief Sort the elements of the vec_u8
/// @param vec The vec_u8 to sort /// @param vec The vec_u8 to sort
/// @param is_sorted The function to use to compare the elements /// @param is_sorted The function to use to compare the elements
void vec_u8_sort(t_vec_u8 *vec, t_vec_u8_sort_fn is_sorted); void vec_u8_sort(t_vec_u8 *vec, t_vec_u8_sort_fn is_sorted);
/// @brief Get a pointer to the last element of the vec_u8 /// @brief Get a pointer to the last element of the vec_u8
/// @param vec The vec_u8 to get the element from /// @param vec The vec_u8 to get the element from
/// @param[out] out A pointer to the last element of the vec_u8 /// @param[out] out A pointer to the last element of the vec_u8
/// @return true if the operation failed, false otherwise /// @return true if the operation failed, false otherwise
t_error vec_u8_back(t_vec_u8 *vec, t_u8 **out); t_error vec_u8_back(t_vec_u8 *vec, t_u8 **out);
#endif #endif

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ /* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */
/* Updated: 2024/07/30 16:32:22 by rparodi ### ########.fr */ /* Updated: 2024/08/01 07:30:38 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* str_to_i64.c :+: :+: :+: */ /* str_to_i16_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ /* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */
/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ /* Updated: 2024/08/01 07:28:38 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,7 +26,7 @@ t_error checked_add_i16(t_i16 lhs, t_i16 rhs, t_i16 *out)
t_error checked_sub_i16(t_i16 lhs, t_i16 rhs, t_i16 *out) t_error checked_sub_i16(t_i16 lhs, t_i16 rhs, t_i16 *out)
{ {
if ((((rhs & (1 << (sizeof(t_i16) - 1)) || rhs == 0) || !true) && (lhs < if ((((rhs & (1 << (sizeof(t_i16) - 1)) || rhs == 0) || !true) && (lhs < \
-32768 + rhs))) -32768 + rhs)))
return (ERROR); return (ERROR);
*out = (t_i16)(lhs - rhs); *out = (t_i16)(lhs - rhs);

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* str_to_i64.c :+: :+: :+: */ /* str_to_i32_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ /* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */
/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ /* Updated: 2024/08/01 07:28:53 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,7 +26,7 @@ t_error checked_add_i32(t_i32 lhs, t_i32 rhs, t_i32 *out)
t_error checked_sub_i32(t_i32 lhs, t_i32 rhs, t_i32 *out) t_error checked_sub_i32(t_i32 lhs, t_i32 rhs, t_i32 *out)
{ {
if ((((rhs & (1 << (sizeof(t_i32) - 1)) || rhs == 0) || !true) && (lhs < if ((((rhs & (1 << (sizeof(t_i32) - 1)) || rhs == 0) || !true) && (lhs < \
-2147483648 + rhs))) -2147483648 + rhs)))
return (ERROR); return (ERROR);
*out = (t_i32)(lhs - rhs); *out = (t_i32)(lhs - rhs);

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* str_to_i64.c :+: :+: :+: */ /* str_to_i64_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ /* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */
/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ /* Updated: 2024/08/01 07:29:00 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,7 +26,7 @@ t_error checked_add_i64(t_i64 lhs, t_i64 rhs, t_i64 *out)
t_error checked_sub_i64(t_i64 lhs, t_i64 rhs, t_i64 *out) t_error checked_sub_i64(t_i64 lhs, t_i64 rhs, t_i64 *out)
{ {
if ((((rhs & (1 << (sizeof(t_i64) - 1)) || rhs == 0) || !true) && (lhs < if ((((rhs & (1 << (sizeof(t_i64) - 1)) || rhs == 0) || !true) && (lhs < \
-(~9223372036854775807ll + 1) + rhs))) -(~9223372036854775807ll + 1) + rhs)))
return (ERROR); return (ERROR);
*out = (t_i64)(lhs - rhs); *out = (t_i64)(lhs - rhs);

View file

@ -1,7 +1,7 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* str_to_i64.c :+: :+: :+: */ /* str_to_i8_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* str_to_i64.c :+: :+: :+: */ /* u16_to_str.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ /* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */
/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ /* Updated: 2024/08/01 07:30:13 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -29,7 +29,7 @@ t_error u16_to_str_base_prefix(t_u16 val, t_str base, t_str prefix, t_str *out)
if (is_nonnegative) if (is_nonnegative)
value.u16 = ~value.u16 + 1; value.u16 = ~value.u16 + 1;
return (_format_u64((t_num_str){.value = value.u64, return (_format_u64((t_num_str){.value = value.u64,
.is_nonnegative = is_nonnegative, .base = base, .prefix = prefix}, .is_nonnegative = is_nonnegative, .base = base, .prefix = prefix}, \
out)); out));
} }

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* str_to_i64.c :+: :+: :+: */ /* u32_to_str.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ /* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */
/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ /* Updated: 2024/08/01 07:30:18 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -29,7 +29,7 @@ t_error u32_to_str_base_prefix(t_u32 val, t_str base, t_str prefix, t_str *out)
if (is_nonnegative) if (is_nonnegative)
value.u32 = ~value.u32 + 1; value.u32 = ~value.u32 + 1;
return (_format_u64((t_num_str){.value = value.u64, return (_format_u64((t_num_str){.value = value.u64,
.is_nonnegative = is_nonnegative, .base = base, .prefix = prefix}, .is_nonnegative = is_nonnegative, .base = base, .prefix = prefix}, \
out)); out));
} }

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* str_to_i64.c :+: :+: :+: */ /* u64_to_str.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ /* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */
/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ /* Updated: 2024/08/01 07:30:22 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -29,7 +29,7 @@ t_error u64_to_str_base_prefix(t_u64 val, t_str base, t_str prefix, t_str *out)
if (is_nonnegative) if (is_nonnegative)
value.u64 = ~value.u64 + 1; value.u64 = ~value.u64 + 1;
return (_format_u64((t_num_str){.value = value.u64, return (_format_u64((t_num_str){.value = value.u64,
.is_nonnegative = is_nonnegative, .base = base, .prefix = prefix}, .is_nonnegative = is_nonnegative, .base = base, .prefix = prefix}, \
out)); out));
} }

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* str_to_i64.c :+: :+: :+: */ /* u8_to_str.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ /* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */
/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ /* Updated: 2024/08/01 07:30:25 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -29,7 +29,7 @@ t_error u8_to_str_base_prefix(t_u8 val, t_str base, t_str prefix, t_str *out)
if (is_nonnegative) if (is_nonnegative)
value.u8 = ~value.u8 + 1; value.u8 = ~value.u8 + 1;
return (_format_u64((t_num_str){.value = value.u64, return (_format_u64((t_num_str){.value = value.u64,
.is_nonnegative = is_nonnegative, .base = base, .prefix = prefix}, .is_nonnegative = is_nonnegative, .base = base, .prefix = prefix}, \
out)); out));
} }

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* vec_buf_str.c :+: :+: :+: */ /* buf_str.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/05 18:46:28 by maiboyer #+# #+# */ /* Created: 2023/12/05 18:46:28 by maiboyer #+# #+# */
/* Updated: 2023/12/09 17:54:11 by maiboyer ### ########.fr */ /* Updated: 2024/08/01 07:31:27 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,10 +15,10 @@
#include "me/vec/vec_buf_str.h" #include "me/vec/vec_buf_str.h"
#include <stdlib.h> #include <stdlib.h>
t_vec_buf_str vec_buf_str_new(t_usize capacity, t_vec_buf_str vec_buf_str_new(t_usize capacity,
t_free_buf_str_item free_function) t_free_buf_str_item free_function)
{ {
t_vec_buf_str out; t_vec_buf_str out;
out = (t_vec_buf_str){0}; out = (t_vec_buf_str){0};
out.free_func = free_function; out.free_func = free_function;
@ -29,7 +29,7 @@ t_vec_buf_str vec_buf_str_new(t_usize capacity,
} }
/// Return true in case of an error /// Return true in case of an error
t_error vec_buf_str_push(t_vec_buf_str *vec, t_string element) t_error vec_buf_str_push(t_vec_buf_str *vec, t_string element)
{ {
if (vec == NULL) if (vec == NULL)
return (ERROR); return (ERROR);
@ -40,9 +40,9 @@ t_error vec_buf_str_push(t_vec_buf_str *vec, t_string element)
} }
/// Return true in case of an error /// Return true in case of an error
t_error vec_buf_str_reserve(t_vec_buf_str *vec, t_usize wanted_capacity) t_error vec_buf_str_reserve(t_vec_buf_str *vec, t_usize wanted_capacity)
{ {
size_t new_capacity; size_t new_capacity;
if (vec == NULL) if (vec == NULL)
return (ERROR); return (ERROR);
@ -51,8 +51,8 @@ t_error vec_buf_str_reserve(t_vec_buf_str *vec, t_usize wanted_capacity)
new_capacity = (vec->capacity * 3) / 2 + 1; new_capacity = (vec->capacity * 3) / 2 + 1;
while (wanted_capacity > new_capacity) while (wanted_capacity > new_capacity)
new_capacity = (new_capacity * 3) / 2 + 1; new_capacity = (new_capacity * 3) / 2 + 1;
vec->buffer = vec->buffer = mem_realloc_array(vec->buffer, new_capacity, \
mem_realloc_array(vec->buffer, new_capacity, sizeof(t_string)); sizeof(t_string));
vec->capacity = new_capacity; vec->capacity = new_capacity;
} }
return (NO_ERROR); return (NO_ERROR);
@ -60,10 +60,10 @@ t_error vec_buf_str_reserve(t_vec_buf_str *vec, t_usize wanted_capacity)
/// Return true if the vector is empty /// Return true if the vector is empty
/// This function is safe to call with value being NULL /// This function is safe to call with value being NULL
t_error vec_buf_str_pop(t_vec_buf_str *vec, t_string *value) t_error vec_buf_str_pop(t_vec_buf_str *vec, t_string *value)
{ {
t_string temp_value; t_string temp_value;
t_string *ptr; t_string *ptr;
if (vec == NULL) if (vec == NULL)
return (ERROR); return (ERROR);
@ -79,10 +79,10 @@ t_error vec_buf_str_pop(t_vec_buf_str *vec, t_string *value)
} }
/// This function is safe to call with `free_elem` being NULL /// This function is safe to call with `free_elem` being NULL
void vec_buf_str_free(t_vec_buf_str vec) void vec_buf_str_free(t_vec_buf_str vec)
{ {
if (vec.buffer == NULL) if (vec.buffer == NULL)
return; return ;
if (vec.free_func) if (vec.free_func)
{ {
while (vec.len) while (vec.len)

View file

@ -10,17 +10,15 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "me/mem/mem.h"
#include "me/mem/mem.h"
#include "me/mem/mem.h" #include "me/mem/mem.h"
#include "me/types.h" #include "me/types.h"
#include "me/vec/vec_buf_str.h" #include "me/vec/vec_buf_str.h"
#include <stdlib.h> #include <stdlib.h>
t_error vec_buf_str_find(t_vec_buf_str *vec, t_error vec_buf_str_find(t_vec_buf_str *vec, bool (*fn)(const t_string *),
bool (*fn)(const t_string *), t_usize *index) t_usize *index)
{ {
t_usize idx; t_usize idx;
if (vec == NULL || fn == NULL || index == NULL) if (vec == NULL || fn == NULL || index == NULL)
return (ERROR); return (ERROR);
@ -37,11 +35,10 @@ t_error vec_buf_str_find(t_vec_buf_str *vec,
return (ERROR); return (ERROR);
} }
t_error vec_buf_str_find_starting(t_vec_buf_str *vec, t_error vec_buf_str_find_starting(t_vec_buf_str *vec,
bool (*fn)(const t_string *), bool (*fn)(const t_string *), t_usize starting_index, t_usize *index)
t_usize starting_index, t_usize *index)
{ {
t_usize idx; t_usize idx;
if (vec == NULL || fn == NULL || index == NULL) if (vec == NULL || fn == NULL || index == NULL)
return (ERROR); return (ERROR);
@ -58,10 +55,10 @@ t_error vec_buf_str_find_starting(t_vec_buf_str *vec,
return (ERROR); return (ERROR);
} }
t_error vec_buf_str_all(t_vec_buf_str *vec, t_error vec_buf_str_all(t_vec_buf_str *vec, bool (*fn)(const t_string *),
bool (*fn)(const t_string *), bool *result) bool *result)
{ {
t_usize idx; t_usize idx;
if (vec == NULL || fn == NULL || result == NULL) if (vec == NULL || fn == NULL || result == NULL)
return (ERROR); return (ERROR);
@ -76,10 +73,10 @@ t_error vec_buf_str_all(t_vec_buf_str *vec,
return (ERROR); return (ERROR);
} }
t_error vec_buf_str_any(t_vec_buf_str *vec, t_error vec_buf_str_any(t_vec_buf_str *vec, bool (*fn)(const t_string *),
bool (*fn)(const t_string *), bool *result) bool *result)
{ {
t_usize idx; t_usize idx;
if (vec == NULL || fn == NULL || result == NULL) if (vec == NULL || fn == NULL || result == NULL)
return (ERROR); return (ERROR);
@ -94,15 +91,13 @@ t_error vec_buf_str_any(t_vec_buf_str *vec,
return (ERROR); return (ERROR);
} }
void vec_buf_str_iter(t_vec_buf_str *vec, void vec_buf_str_iter(t_vec_buf_str *vec, void (*fn)(t_usize index,
void (*fn)(t_usize index, t_string *value, t_string *value, void *state), void *state)
void *state),
void *state)
{ {
t_usize idx; t_usize idx;
if (vec == NULL || fn == NULL) if (vec == NULL || fn == NULL)
return; return ;
idx = 0; idx = 0;
while (idx < vec->len) while (idx < vec->len)
{ {

View file

@ -10,23 +10,20 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "me/mem/mem.h"
#include "me/mem/mem.h"
#include "me/mem/mem.h" #include "me/mem/mem.h"
#include "me/types.h" #include "me/types.h"
#include "me/vec/vec_buf_str.h" #include "me/vec/vec_buf_str.h"
#include <stdlib.h> #include <stdlib.h>
t_error vec_buf_str_push_front(t_vec_buf_str *vec, t_error vec_buf_str_push_front(t_vec_buf_str *vec, t_string element)
t_string element)
{ {
t_usize i; t_usize i;
if (vec->len == 0) if (vec->len == 0)
return (vec_buf_str_push(vec, element)); return (vec_buf_str_push(vec, element));
i = vec->len - 1; i = vec->len - 1;
if (vec->capacity < vec->len + 1 && if (vec->capacity < vec->len + 1 && vec_buf_str_reserve(vec, 3 * vec->len
vec_buf_str_reserve(vec, 3 * vec->len / 2 + 1)) / 2 + 1))
return (ERROR); return (ERROR);
while (i > 0) while (i > 0)
{ {
@ -39,9 +36,9 @@ t_error vec_buf_str_push_front(t_vec_buf_str *vec,
return (NO_ERROR); return (NO_ERROR);
} }
t_error vec_buf_str_pop_front(t_vec_buf_str *vec, t_string *value) t_error vec_buf_str_pop_front(t_vec_buf_str *vec, t_string *value)
{ {
t_usize i; t_usize i;
if (vec->len <= 1) if (vec->len <= 1)
return (vec_buf_str_pop(vec, value)); return (vec_buf_str_pop(vec, value));
@ -57,10 +54,10 @@ t_error vec_buf_str_pop_front(t_vec_buf_str *vec, t_string *value)
return (NO_ERROR); return (NO_ERROR);
} }
void vec_buf_str_reverse(t_vec_buf_str *vec) void vec_buf_str_reverse(t_vec_buf_str *vec)
{ {
t_string temporary; t_string temporary;
t_usize i; t_usize i;
i = 0; i = 0;
while (i < vec->len / 2) while (i < vec->len / 2)
@ -72,9 +69,9 @@ void vec_buf_str_reverse(t_vec_buf_str *vec)
} }
} }
t_error vec_buf_str_back(t_vec_buf_str *vec, t_string **out) t_error vec_buf_str_back(t_vec_buf_str *vec, t_string **out)
{ {
t_string *temporary; t_string *temporary;
if (out == NULL) if (out == NULL)
out = &temporary; out = &temporary;

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* vec_str.c :+: :+: :+: */ /* str.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/05 18:46:28 by maiboyer #+# #+# */ /* Created: 2023/12/05 18:46:28 by maiboyer #+# #+# */
/* Updated: 2023/12/09 17:54:11 by maiboyer ### ########.fr */ /* Updated: 2024/08/01 07:31:20 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,10 +15,9 @@
#include "me/vec/vec_str.h" #include "me/vec/vec_str.h"
#include <stdlib.h> #include <stdlib.h>
t_vec_str vec_str_new(t_usize capacity, t_vec_str vec_str_new(t_usize capacity, t_free_str_item free_function)
t_free_str_item free_function)
{ {
t_vec_str out; t_vec_str out;
out = (t_vec_str){0}; out = (t_vec_str){0};
out.free_func = free_function; out.free_func = free_function;
@ -29,7 +28,7 @@ t_vec_str vec_str_new(t_usize capacity,
} }
/// Return true in case of an error /// Return true in case of an error
t_error vec_str_push(t_vec_str *vec, t_str element) t_error vec_str_push(t_vec_str *vec, t_str element)
{ {
if (vec == NULL) if (vec == NULL)
return (ERROR); return (ERROR);
@ -40,9 +39,9 @@ t_error vec_str_push(t_vec_str *vec, t_str element)
} }
/// Return true in case of an error /// Return true in case of an error
t_error vec_str_reserve(t_vec_str *vec, t_usize wanted_capacity) t_error vec_str_reserve(t_vec_str *vec, t_usize wanted_capacity)
{ {
size_t new_capacity; size_t new_capacity;
if (vec == NULL) if (vec == NULL)
return (ERROR); return (ERROR);
@ -51,8 +50,8 @@ t_error vec_str_reserve(t_vec_str *vec, t_usize wanted_capacity)
new_capacity = (vec->capacity * 3) / 2 + 1; new_capacity = (vec->capacity * 3) / 2 + 1;
while (wanted_capacity > new_capacity) while (wanted_capacity > new_capacity)
new_capacity = (new_capacity * 3) / 2 + 1; new_capacity = (new_capacity * 3) / 2 + 1;
vec->buffer = vec->buffer = mem_realloc_array(vec->buffer, new_capacity, \
mem_realloc_array(vec->buffer, new_capacity, sizeof(t_str)); sizeof(t_str));
vec->capacity = new_capacity; vec->capacity = new_capacity;
} }
return (NO_ERROR); return (NO_ERROR);
@ -60,10 +59,10 @@ t_error vec_str_reserve(t_vec_str *vec, t_usize wanted_capacity)
/// Return true if the vector is empty /// Return true if the vector is empty
/// This function is safe to call with value being NULL /// This function is safe to call with value being NULL
t_error vec_str_pop(t_vec_str *vec, t_str *value) t_error vec_str_pop(t_vec_str *vec, t_str *value)
{ {
t_str temp_value; t_str temp_value;
t_str *ptr; t_str *ptr;
if (vec == NULL) if (vec == NULL)
return (ERROR); return (ERROR);
@ -79,10 +78,10 @@ t_error vec_str_pop(t_vec_str *vec, t_str *value)
} }
/// This function is safe to call with `free_elem` being NULL /// This function is safe to call with `free_elem` being NULL
void vec_str_free(t_vec_str vec) void vec_str_free(t_vec_str vec)
{ {
if (vec.buffer == NULL) if (vec.buffer == NULL)
return; return ;
if (vec.free_func) if (vec.free_func)
{ {
while (vec.len) while (vec.len)

View file

@ -10,17 +10,14 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "me/mem/mem.h"
#include "me/mem/mem.h"
#include "me/mem/mem.h" #include "me/mem/mem.h"
#include "me/types.h" #include "me/types.h"
#include "me/vec/vec_str.h" #include "me/vec/vec_str.h"
#include <stdlib.h> #include <stdlib.h>
t_error vec_str_find(t_vec_str *vec, t_error vec_str_find(t_vec_str *vec, bool (*fn)(const t_str *), t_usize *index)
bool (*fn)(const t_str *), t_usize *index)
{ {
t_usize idx; t_usize idx;
if (vec == NULL || fn == NULL || index == NULL) if (vec == NULL || fn == NULL || index == NULL)
return (ERROR); return (ERROR);
@ -37,11 +34,10 @@ t_error vec_str_find(t_vec_str *vec,
return (ERROR); return (ERROR);
} }
t_error vec_str_find_starting(t_vec_str *vec, t_error vec_str_find_starting(t_vec_str *vec, bool (*fn)(const t_str *),
bool (*fn)(const t_str *), t_usize starting_index, t_usize *index)
t_usize starting_index, t_usize *index)
{ {
t_usize idx; t_usize idx;
if (vec == NULL || fn == NULL || index == NULL) if (vec == NULL || fn == NULL || index == NULL)
return (ERROR); return (ERROR);
@ -58,10 +54,9 @@ t_error vec_str_find_starting(t_vec_str *vec,
return (ERROR); return (ERROR);
} }
t_error vec_str_all(t_vec_str *vec, t_error vec_str_all(t_vec_str *vec, bool (*fn)(const t_str *), bool *result)
bool (*fn)(const t_str *), bool *result)
{ {
t_usize idx; t_usize idx;
if (vec == NULL || fn == NULL || result == NULL) if (vec == NULL || fn == NULL || result == NULL)
return (ERROR); return (ERROR);
@ -76,10 +71,9 @@ t_error vec_str_all(t_vec_str *vec,
return (ERROR); return (ERROR);
} }
t_error vec_str_any(t_vec_str *vec, t_error vec_str_any(t_vec_str *vec, bool (*fn)(const t_str *), bool *result)
bool (*fn)(const t_str *), bool *result)
{ {
t_usize idx; t_usize idx;
if (vec == NULL || fn == NULL || result == NULL) if (vec == NULL || fn == NULL || result == NULL)
return (ERROR); return (ERROR);
@ -94,15 +88,13 @@ t_error vec_str_any(t_vec_str *vec,
return (ERROR); return (ERROR);
} }
void vec_str_iter(t_vec_str *vec, void vec_str_iter(t_vec_str *vec, void (*fn)(t_usize index, t_str *value,
void (*fn)(t_usize index, t_str *value, void *state), void *state)
void *state),
void *state)
{ {
t_usize idx; t_usize idx;
if (vec == NULL || fn == NULL) if (vec == NULL || fn == NULL)
return; return ;
idx = 0; idx = 0;
while (idx < vec->len) while (idx < vec->len)
{ {

View file

@ -10,23 +10,20 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "me/mem/mem.h"
#include "me/mem/mem.h"
#include "me/mem/mem.h" #include "me/mem/mem.h"
#include "me/types.h" #include "me/types.h"
#include "me/vec/vec_str.h" #include "me/vec/vec_str.h"
#include <stdlib.h> #include <stdlib.h>
t_error vec_str_push_front(t_vec_str *vec, t_error vec_str_push_front(t_vec_str *vec, t_str element)
t_str element)
{ {
t_usize i; t_usize i;
if (vec->len == 0) if (vec->len == 0)
return (vec_str_push(vec, element)); return (vec_str_push(vec, element));
i = vec->len - 1; i = vec->len - 1;
if (vec->capacity < vec->len + 1 && if (vec->capacity < vec->len + 1 && vec_str_reserve(vec, 3 * vec->len / 2
vec_str_reserve(vec, 3 * vec->len / 2 + 1)) + 1))
return (ERROR); return (ERROR);
while (i > 0) while (i > 0)
{ {
@ -39,9 +36,9 @@ t_error vec_str_push_front(t_vec_str *vec,
return (NO_ERROR); return (NO_ERROR);
} }
t_error vec_str_pop_front(t_vec_str *vec, t_str *value) t_error vec_str_pop_front(t_vec_str *vec, t_str *value)
{ {
t_usize i; t_usize i;
if (vec->len <= 1) if (vec->len <= 1)
return (vec_str_pop(vec, value)); return (vec_str_pop(vec, value));
@ -57,10 +54,10 @@ t_error vec_str_pop_front(t_vec_str *vec, t_str *value)
return (NO_ERROR); return (NO_ERROR);
} }
void vec_str_reverse(t_vec_str *vec) void vec_str_reverse(t_vec_str *vec)
{ {
t_str temporary; t_str temporary;
t_usize i; t_usize i;
i = 0; i = 0;
while (i < vec->len / 2) while (i < vec->len / 2)
@ -72,9 +69,9 @@ void vec_str_reverse(t_vec_str *vec)
} }
} }
t_error vec_str_back(t_vec_str *vec, t_str **out) t_error vec_str_back(t_vec_str *vec, t_str **out)
{ {
t_str *temporary; t_str *temporary;
if (out == NULL) if (out == NULL)
out = &temporary; out = &temporary;

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* vec_u8.c :+: :+: :+: */ /* u8.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/05 18:46:28 by maiboyer #+# #+# */ /* Created: 2023/12/05 18:46:28 by maiboyer #+# #+# */
/* Updated: 2023/12/09 17:54:11 by maiboyer ### ########.fr */ /* Updated: 2024/08/01 07:31:34 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,10 +15,9 @@
#include "me/vec/vec_u8.h" #include "me/vec/vec_u8.h"
#include <stdlib.h> #include <stdlib.h>
t_vec_u8 vec_u8_new(t_usize capacity, t_vec_u8 vec_u8_new(t_usize capacity, t_free_u8_item free_function)
t_free_u8_item free_function)
{ {
t_vec_u8 out; t_vec_u8 out;
out = (t_vec_u8){0}; out = (t_vec_u8){0};
out.free_func = free_function; out.free_func = free_function;
@ -29,7 +28,7 @@ t_vec_u8 vec_u8_new(t_usize capacity,
} }
/// Return true in case of an error /// Return true in case of an error
t_error vec_u8_push(t_vec_u8 *vec, t_u8 element) t_error vec_u8_push(t_vec_u8 *vec, t_u8 element)
{ {
if (vec == NULL) if (vec == NULL)
return (ERROR); return (ERROR);
@ -40,9 +39,9 @@ t_error vec_u8_push(t_vec_u8 *vec, t_u8 element)
} }
/// Return true in case of an error /// Return true in case of an error
t_error vec_u8_reserve(t_vec_u8 *vec, t_usize wanted_capacity) t_error vec_u8_reserve(t_vec_u8 *vec, t_usize wanted_capacity)
{ {
size_t new_capacity; size_t new_capacity;
if (vec == NULL) if (vec == NULL)
return (ERROR); return (ERROR);
@ -51,8 +50,8 @@ t_error vec_u8_reserve(t_vec_u8 *vec, t_usize wanted_capacity)
new_capacity = (vec->capacity * 3) / 2 + 1; new_capacity = (vec->capacity * 3) / 2 + 1;
while (wanted_capacity > new_capacity) while (wanted_capacity > new_capacity)
new_capacity = (new_capacity * 3) / 2 + 1; new_capacity = (new_capacity * 3) / 2 + 1;
vec->buffer = vec->buffer = mem_realloc_array(vec->buffer, new_capacity, \
mem_realloc_array(vec->buffer, new_capacity, sizeof(t_u8)); sizeof(t_u8));
vec->capacity = new_capacity; vec->capacity = new_capacity;
} }
return (NO_ERROR); return (NO_ERROR);
@ -60,10 +59,10 @@ t_error vec_u8_reserve(t_vec_u8 *vec, t_usize wanted_capacity)
/// Return true if the vector is empty /// Return true if the vector is empty
/// This function is safe to call with value being NULL /// This function is safe to call with value being NULL
t_error vec_u8_pop(t_vec_u8 *vec, t_u8 *value) t_error vec_u8_pop(t_vec_u8 *vec, t_u8 *value)
{ {
t_u8 temp_value; t_u8 temp_value;
t_u8 *ptr; t_u8 *ptr;
if (vec == NULL) if (vec == NULL)
return (ERROR); return (ERROR);
@ -79,10 +78,10 @@ t_error vec_u8_pop(t_vec_u8 *vec, t_u8 *value)
} }
/// This function is safe to call with `free_elem` being NULL /// This function is safe to call with `free_elem` being NULL
void vec_u8_free(t_vec_u8 vec) void vec_u8_free(t_vec_u8 vec)
{ {
if (vec.buffer == NULL) if (vec.buffer == NULL)
return; return ;
if (vec.free_func) if (vec.free_func)
{ {
while (vec.len) while (vec.len)

View file

@ -10,17 +10,14 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "me/mem/mem.h"
#include "me/mem/mem.h"
#include "me/mem/mem.h" #include "me/mem/mem.h"
#include "me/types.h" #include "me/types.h"
#include "me/vec/vec_u8.h" #include "me/vec/vec_u8.h"
#include <stdlib.h> #include <stdlib.h>
t_error vec_u8_find(t_vec_u8 *vec, t_error vec_u8_find(t_vec_u8 *vec, bool (*fn)(const t_u8 *), t_usize *index)
bool (*fn)(const t_u8 *), t_usize *index)
{ {
t_usize idx; t_usize idx;
if (vec == NULL || fn == NULL || index == NULL) if (vec == NULL || fn == NULL || index == NULL)
return (ERROR); return (ERROR);
@ -37,11 +34,10 @@ t_error vec_u8_find(t_vec_u8 *vec,
return (ERROR); return (ERROR);
} }
t_error vec_u8_find_starting(t_vec_u8 *vec, t_error vec_u8_find_starting(t_vec_u8 *vec, bool (*fn)(const t_u8 *),
bool (*fn)(const t_u8 *), t_usize starting_index, t_usize *index)
t_usize starting_index, t_usize *index)
{ {
t_usize idx; t_usize idx;
if (vec == NULL || fn == NULL || index == NULL) if (vec == NULL || fn == NULL || index == NULL)
return (ERROR); return (ERROR);
@ -58,10 +54,9 @@ t_error vec_u8_find_starting(t_vec_u8 *vec,
return (ERROR); return (ERROR);
} }
t_error vec_u8_all(t_vec_u8 *vec, t_error vec_u8_all(t_vec_u8 *vec, bool (*fn)(const t_u8 *), bool *result)
bool (*fn)(const t_u8 *), bool *result)
{ {
t_usize idx; t_usize idx;
if (vec == NULL || fn == NULL || result == NULL) if (vec == NULL || fn == NULL || result == NULL)
return (ERROR); return (ERROR);
@ -76,10 +71,9 @@ t_error vec_u8_all(t_vec_u8 *vec,
return (ERROR); return (ERROR);
} }
t_error vec_u8_any(t_vec_u8 *vec, t_error vec_u8_any(t_vec_u8 *vec, bool (*fn)(const t_u8 *), bool *result)
bool (*fn)(const t_u8 *), bool *result)
{ {
t_usize idx; t_usize idx;
if (vec == NULL || fn == NULL || result == NULL) if (vec == NULL || fn == NULL || result == NULL)
return (ERROR); return (ERROR);
@ -94,15 +88,13 @@ t_error vec_u8_any(t_vec_u8 *vec,
return (ERROR); return (ERROR);
} }
void vec_u8_iter(t_vec_u8 *vec, void vec_u8_iter(t_vec_u8 *vec, void (*fn)(t_usize index, t_u8 *value,
void (*fn)(t_usize index, t_u8 *value, void *state), void *state)
void *state),
void *state)
{ {
t_usize idx; t_usize idx;
if (vec == NULL || fn == NULL) if (vec == NULL || fn == NULL)
return; return ;
idx = 0; idx = 0;
while (idx < vec->len) while (idx < vec->len)
{ {

View file

@ -10,23 +10,20 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "me/mem/mem.h"
#include "me/mem/mem.h"
#include "me/mem/mem.h" #include "me/mem/mem.h"
#include "me/types.h" #include "me/types.h"
#include "me/vec/vec_u8.h" #include "me/vec/vec_u8.h"
#include <stdlib.h> #include <stdlib.h>
t_error vec_u8_push_front(t_vec_u8 *vec, t_error vec_u8_push_front(t_vec_u8 *vec, t_u8 element)
t_u8 element)
{ {
t_usize i; t_usize i;
if (vec->len == 0) if (vec->len == 0)
return (vec_u8_push(vec, element)); return (vec_u8_push(vec, element));
i = vec->len - 1; i = vec->len - 1;
if (vec->capacity < vec->len + 1 && if (vec->capacity < vec->len + 1 && vec_u8_reserve(vec, 3 * vec->len / 2
vec_u8_reserve(vec, 3 * vec->len / 2 + 1)) + 1))
return (ERROR); return (ERROR);
while (i > 0) while (i > 0)
{ {
@ -39,9 +36,9 @@ t_error vec_u8_push_front(t_vec_u8 *vec,
return (NO_ERROR); return (NO_ERROR);
} }
t_error vec_u8_pop_front(t_vec_u8 *vec, t_u8 *value) t_error vec_u8_pop_front(t_vec_u8 *vec, t_u8 *value)
{ {
t_usize i; t_usize i;
if (vec->len <= 1) if (vec->len <= 1)
return (vec_u8_pop(vec, value)); return (vec_u8_pop(vec, value));
@ -57,10 +54,10 @@ t_error vec_u8_pop_front(t_vec_u8 *vec, t_u8 *value)
return (NO_ERROR); return (NO_ERROR);
} }
void vec_u8_reverse(t_vec_u8 *vec) void vec_u8_reverse(t_vec_u8 *vec)
{ {
t_u8 temporary; t_u8 temporary;
t_usize i; t_usize i;
i = 0; i = 0;
while (i < vec->len / 2) while (i < vec->len / 2)
@ -72,9 +69,9 @@ void vec_u8_reverse(t_vec_u8 *vec)
} }
} }
t_error vec_u8_back(t_vec_u8 *vec, t_u8 **out) t_error vec_u8_back(t_vec_u8 *vec, t_u8 **out)
{ {
t_u8 *temporary; t_u8 *temporary;
if (out == NULL) if (out == NULL)
out = &temporary; out = &temporary;