update: normed the rest output

This commit is contained in:
maix0 2024-09-19 17:52:19 +02:00
parent 4a6ea68d08
commit bec1320c5f
26 changed files with 332 additions and 382 deletions

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/06 10:58:20 by maiboyer #+# #+# */ /* Created: 2023/12/06 10:58:20 by maiboyer #+# #+# */
/* Updated: 2024/09/19 15:20:55 by rparodi ### ########.fr */ /* Updated: 2024/09/19 17:50:02 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,15 +17,14 @@
#include "me/types.h" #include "me/types.h"
#include <stdlib.h> #include <stdlib.h>
t_hashmap_env *hmap_env_new(\ t_hashmap_env *hmap_env_new(t_hash_env_fn hfunc, t_eq_env_fn cfunc,
t_hash_env_fn hfunc, t_eq_env_fn cfunc, t_free_env_fn free) t_free_env_fn free)
{ {
return ( return (hmap_env_new_with_buckets(hfunc, cfunc, free, DEFAULT_BUCKETS));
hmap_env_new_with_buckets(hfunc, cfunc, free, DEFAULT_BUCKETS));
} }
t_hashmap_env *hmap_env_new_with_buckets(\ t_hashmap_env *hmap_env_new_with_buckets(t_hash_env_fn hfunc,
t_hash_env_fn hfunc, t_eq_env_fn cfunc, t_free_env_fn free, t_usize buckets) t_eq_env_fn cfunc, t_free_env_fn free, t_usize buckets)
{ {
t_hashmap_env *hmap; t_hashmap_env *hmap;
@ -67,8 +66,8 @@ void hmap_env_free(t_hashmap_env *hmap)
mem_free(hmap); mem_free(hmap);
} }
t_entry_env *hmap_env_get_entry(\ t_entry_env *hmap_env_get_entry(t_hashmap_env *hmap, t_usize hashed_key,
t_hashmap_env *hmap, t_usize hashed_key, t_str *key, t_entry_env **prev) t_str *key, t_entry_env **prev)
{ {
t_entry_env *entry; t_entry_env *entry;
@ -88,8 +87,7 @@ t_entry_env *hmap_env_get_entry(\
return (NULL); return (NULL);
} }
bool hmap_env_insert(\ bool hmap_env_insert(t_hashmap_env *hmap, t_str key, t_str value)
t_hashmap_env *hmap, t_str key, t_str value)
{ {
t_usize hashed_key; t_usize hashed_key;
t_entry_env *prev; t_entry_env *prev;
@ -114,8 +112,6 @@ bool hmap_env_insert(\
else else
{ {
hmap->free(entry->kv); hmap->free(entry->kv);
entry->kv.key = key; return (entry->kv = (typeof(entry->kv)){key, value}, true);
entry->kv.val = value;
return (true);
} }
} }

View file

@ -15,8 +15,7 @@
#include "me/vec/vec_estr.h" #include "me/vec/vec_estr.h"
#include <stdlib.h> #include <stdlib.h>
t_vec_estr vec_estr_new(t_usize capacity, t_vec_estr vec_estr_new(t_usize capacity, t_free_estr_item free_function)
t_free_estr_item free_function)
{ {
t_vec_estr out; t_vec_estr out;
@ -51,8 +50,8 @@ t_error vec_estr_reserve(t_vec_estr *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_expandable_str)); sizeof(t_expandable_str));
vec->capacity = new_capacity; vec->capacity = new_capacity;
} }
return (NO_ERROR); return (NO_ERROR);

View file

@ -10,15 +10,13 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#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_estr.h" #include "me/vec/vec_estr.h"
#include <stdlib.h> #include <stdlib.h>
t_error vec_estr_find(t_vec_estr *vec, t_error vec_estr_find(t_vec_estr *vec, bool (*fn)(const t_expandable_str *),
bool (*fn)(const t_expandable_str *), t_usize *index) t_usize *index)
{ {
t_usize idx; t_usize idx;
@ -38,8 +36,8 @@ t_error vec_estr_find(t_vec_estr *vec,
} }
t_error vec_estr_find_starting(t_vec_estr *vec, t_error vec_estr_find_starting(t_vec_estr *vec,
bool (*fn)(const t_expandable_str *), bool (*fn)(const t_expandable_str *), t_usize starting_index,
t_usize starting_index, t_usize *index) t_usize *index)
{ {
t_usize idx; t_usize idx;
@ -58,8 +56,8 @@ t_error vec_estr_find_starting(t_vec_estr *vec,
return (ERROR); return (ERROR);
} }
t_error vec_estr_all(t_vec_estr *vec, t_error vec_estr_all(t_vec_estr *vec, bool (*fn)(const t_expandable_str *),
bool (*fn)(const t_expandable_str *), bool *result) bool *result)
{ {
t_usize idx; t_usize idx;
@ -76,8 +74,8 @@ t_error vec_estr_all(t_vec_estr *vec,
return (ERROR); return (ERROR);
} }
t_error vec_estr_any(t_vec_estr *vec, t_error vec_estr_any(t_vec_estr *vec, bool (*fn)(const t_expandable_str *),
bool (*fn)(const t_expandable_str *), bool *result) bool *result)
{ {
t_usize idx; t_usize idx;
@ -94,10 +92,8 @@ t_error vec_estr_any(t_vec_estr *vec,
return (ERROR); return (ERROR);
} }
void vec_estr_iter(t_vec_estr *vec, void vec_estr_iter(t_vec_estr *vec, void (*fn)(t_usize index,
void (*fn)(t_usize index, t_expandable_str *value, t_expandable_str *value, void *state), void *state)
void *state),
void *state)
{ {
t_usize idx; t_usize idx;

View file

@ -15,16 +15,15 @@
#include "me/vec/vec_estr.h" #include "me/vec/vec_estr.h"
#include <stdlib.h> #include <stdlib.h>
t_error vec_estr_push_front(t_vec_estr *vec, t_error vec_estr_push_front(t_vec_estr *vec, t_expandable_str element)
t_expandable_str element)
{ {
t_usize i; t_usize i;
if (vec->len == 0) if (vec->len == 0)
return (vec_estr_push(vec, element)); return (vec_estr_push(vec, element));
i = vec->len - 1; i = vec->len - 1;
if (vec->capacity < vec->len + 1 && if (vec->capacity < vec->len + 1 && vec_estr_reserve(vec, 3 * vec->len / 2
vec_estr_reserve(vec, 3 * vec->len / 2 + 1)) + 1))
return (ERROR); return (ERROR);
while (i > 0) while (i > 0)
{ {

View file

@ -39,12 +39,11 @@ void vec_estr_copy_into(t_vec_estr *vec, t_vec_estr *dest)
mem_copy(dest->buffer, vec->buffer, vec->len * sizeof(t_expandable_str)); mem_copy(dest->buffer, vec->buffer, vec->len * sizeof(t_expandable_str));
} }
struct s_vec_estr_splice_arguments vec_estr_splice_args( struct s_vec_estr_splice_arguments vec_estr_splice_args(t_usize index,
t_usize index, t_usize old_count, t_usize new_count, t_usize old_count, t_usize new_count, const t_expandable_str *elements)
const t_expandable_str *elements)
{ {
return ((struct s_vec_estr_splice_arguments){index, old_count, return ((struct s_vec_estr_splice_arguments){index, old_count, new_count,
new_count, elements}); elements});
} }
void vec_estr_splice(t_vec_estr *self, void vec_estr_splice(t_vec_estr *self,
@ -61,9 +60,8 @@ void vec_estr_splice(t_vec_estr *self,
vec_estr_reserve(self, new_size); vec_estr_reserve(self, new_size);
contents = self->buffer; contents = self->buffer;
if (self->len > old_end) if (self->len > old_end)
mem_move(contents + new_end, mem_move(contents + new_end, contents + old_end, (self->len - old_end)
contents + old_end, * sizeof(t_expandable_str));
(self->len - old_end) * sizeof(t_expandable_str));
if (args.new_count > 0) if (args.new_count > 0)
{ {
if (args.elements) if (args.elements)

View file

@ -13,8 +13,7 @@
#include "me/types.h" #include "me/types.h"
#include "me/vec/vec_estr.h" #include "me/vec/vec_estr.h"
void vec_estr_sort(t_vec_estr *v, void vec_estr_sort(t_vec_estr *v, t_vec_estr_sort_fn is_sorted_fn)
t_vec_estr_sort_fn is_sorted_fn)
{ {
t_usize sorted_part; t_usize sorted_part;
t_usize i; t_usize i;

View file

@ -15,8 +15,7 @@
#include "me/vec/vec_pid.h" #include "me/vec/vec_pid.h"
#include <stdlib.h> #include <stdlib.h>
t_vec_pid vec_pid_new(t_usize capacity, t_vec_pid vec_pid_new(t_usize capacity, t_free_pid_item free_function)
t_free_pid_item free_function)
{ {
t_vec_pid out; t_vec_pid out;
@ -51,8 +50,8 @@ t_error vec_pid_reserve(t_vec_pid *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_pid)); sizeof(t_pid));
vec->capacity = new_capacity; vec->capacity = new_capacity;
} }
return (NO_ERROR); return (NO_ERROR);

View file

@ -10,15 +10,12 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#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_pid.h" #include "me/vec/vec_pid.h"
#include <stdlib.h> #include <stdlib.h>
t_error vec_pid_find(t_vec_pid *vec, t_error vec_pid_find(t_vec_pid *vec, bool (*fn)(const t_pid *), t_usize *index)
bool (*fn)(const t_pid *), t_usize *index)
{ {
t_usize idx; t_usize idx;
@ -37,8 +34,7 @@ t_error vec_pid_find(t_vec_pid *vec,
return (ERROR); return (ERROR);
} }
t_error vec_pid_find_starting(t_vec_pid *vec, t_error vec_pid_find_starting(t_vec_pid *vec, bool (*fn)(const t_pid *),
bool (*fn)(const t_pid *),
t_usize starting_index, t_usize *index) t_usize starting_index, t_usize *index)
{ {
t_usize idx; t_usize idx;
@ -58,8 +54,7 @@ t_error vec_pid_find_starting(t_vec_pid *vec,
return (ERROR); return (ERROR);
} }
t_error vec_pid_all(t_vec_pid *vec, t_error vec_pid_all(t_vec_pid *vec, bool (*fn)(const t_pid *), bool *result)
bool (*fn)(const t_pid *), bool *result)
{ {
t_usize idx; t_usize idx;
@ -76,8 +71,7 @@ t_error vec_pid_all(t_vec_pid *vec,
return (ERROR); return (ERROR);
} }
t_error vec_pid_any(t_vec_pid *vec, t_error vec_pid_any(t_vec_pid *vec, bool (*fn)(const t_pid *), bool *result)
bool (*fn)(const t_pid *), bool *result)
{ {
t_usize idx; t_usize idx;
@ -94,10 +88,8 @@ t_error vec_pid_any(t_vec_pid *vec,
return (ERROR); return (ERROR);
} }
void vec_pid_iter(t_vec_pid *vec, void vec_pid_iter(t_vec_pid *vec, void (*fn)(t_usize index, t_pid *value,
void (*fn)(t_usize index, t_pid *value, void *state), void *state)
void *state),
void *state)
{ {
t_usize idx; t_usize idx;

View file

@ -15,16 +15,15 @@
#include "me/vec/vec_pid.h" #include "me/vec/vec_pid.h"
#include <stdlib.h> #include <stdlib.h>
t_error vec_pid_push_front(t_vec_pid *vec, t_error vec_pid_push_front(t_vec_pid *vec, t_pid element)
t_pid element)
{ {
t_usize i; t_usize i;
if (vec->len == 0) if (vec->len == 0)
return (vec_pid_push(vec, element)); return (vec_pid_push(vec, element));
i = vec->len - 1; i = vec->len - 1;
if (vec->capacity < vec->len + 1 && if (vec->capacity < vec->len + 1 && vec_pid_reserve(vec, 3 * vec->len / 2
vec_pid_reserve(vec, 3 * vec->len / 2 + 1)) + 1))
return (ERROR); return (ERROR);
while (i > 0) while (i > 0)
{ {

View file

@ -39,16 +39,14 @@ void vec_pid_copy_into(t_vec_pid *vec, t_vec_pid *dest)
mem_copy(dest->buffer, vec->buffer, vec->len * sizeof(t_pid)); mem_copy(dest->buffer, vec->buffer, vec->len * sizeof(t_pid));
} }
struct s_vec_pid_splice_arguments vec_pid_splice_args( struct s_vec_pid_splice_arguments vec_pid_splice_args(t_usize index,
t_usize index, t_usize old_count, t_usize new_count, t_usize old_count, t_usize new_count, const t_pid *elements)
const t_pid *elements)
{ {
return ((struct s_vec_pid_splice_arguments){index, old_count, return ((struct s_vec_pid_splice_arguments){index, old_count, new_count,
new_count, elements}); elements});
} }
void vec_pid_splice(t_vec_pid *self, void vec_pid_splice(t_vec_pid *self, struct s_vec_pid_splice_arguments args)
struct s_vec_pid_splice_arguments args)
{ {
t_pid *contents; t_pid *contents;
t_u32 new_size; t_u32 new_size;
@ -61,17 +59,16 @@ void vec_pid_splice(t_vec_pid *self,
vec_pid_reserve(self, new_size); vec_pid_reserve(self, new_size);
contents = self->buffer; contents = self->buffer;
if (self->len > old_end) if (self->len > old_end)
mem_move(contents + new_end, mem_move(contents + new_end, contents + old_end, (self->len - old_end)
contents + old_end, * sizeof(t_pid));
(self->len - old_end) * sizeof(t_pid));
if (args.new_count > 0) if (args.new_count > 0)
{ {
if (args.elements) if (args.elements)
mem_copy((contents + args.index * sizeof(t_pid)), mem_copy((contents + args.index * sizeof(t_pid)), args.elements,
args.elements, args.new_count * sizeof(t_pid));
else
mem_set_zero((contents + args.index * sizeof(t_pid)),
args.new_count * sizeof(t_pid)); args.new_count * sizeof(t_pid));
else
mem_set_zero((contents + args.index * sizeof(t_pid)), args.new_count
* sizeof(t_pid));
} }
self->len += args.new_count - args.old_count; self->len += args.new_count - args.old_count;
} }

View file

@ -13,8 +13,7 @@
#include "me/types.h" #include "me/types.h"
#include "me/vec/vec_pid.h" #include "me/vec/vec_pid.h"
void vec_pid_sort(t_vec_pid *v, void vec_pid_sort(t_vec_pid *v, t_vec_pid_sort_fn is_sorted_fn)
t_vec_pid_sort_fn is_sorted_fn)
{ {
t_usize sorted_part; t_usize sorted_part;
t_usize i; t_usize i;

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* vec_reduce_action.c :+: :+: :+: */ /* reduce_action.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/09/19 17:50:33 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,14 +22,15 @@ t_vec_reduce_action vec_reduce_action_new(t_usize capacity,
out = (t_vec_reduce_action){0}; out = (t_vec_reduce_action){0};
out.free_func = free_function; out.free_func = free_function;
out.buffer = mem_alloc_array(capacity, sizeof(t_reduce_action)); out.buffer = mem_alloc_array(capacity, sizeof(*out.buffer));
if (out.buffer) if (out.buffer)
out.capacity = capacity; out.capacity = capacity;
return (out); return (out);
} }
/// Return true in case of an error /// Return true in case of an error
t_error vec_reduce_action_push(t_vec_reduce_action *vec, t_reduce_action element) t_error vec_reduce_action_push(t_vec_reduce_action *vec,
t_reduce_action element)
{ {
if (vec == NULL) if (vec == NULL)
return (ERROR); return (ERROR);
@ -40,7 +41,8 @@ t_error vec_reduce_action_push(t_vec_reduce_action *vec, t_reduce_action element
} }
/// Return true in case of an error /// Return true in case of an error
t_error vec_reduce_action_reserve(t_vec_reduce_action *vec, t_usize wanted_capacity) t_error vec_reduce_action_reserve(t_vec_reduce_action *vec,
t_usize wanted_capacity)
{ {
size_t new_capacity; size_t new_capacity;
@ -51,8 +53,8 @@ t_error vec_reduce_action_reserve(t_vec_reduce_action *vec, t_usize wanted_capac
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_reduce_action)); sizeof(t_reduce_action));
vec->capacity = new_capacity; vec->capacity = new_capacity;
} }
return (NO_ERROR); return (NO_ERROR);

View file

@ -1,17 +1,15 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* vec_reduce_action.c :+: :+: :+: */ /* reduce_action_functions2.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */ /* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */
/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */ /* Updated: 2024/09/19 17:50:57 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#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_reduce_action.h" #include "me/vec/vec_reduce_action.h"
@ -38,8 +36,8 @@ t_error vec_reduce_action_find(t_vec_reduce_action *vec,
} }
t_error vec_reduce_action_find_starting(t_vec_reduce_action *vec, t_error vec_reduce_action_find_starting(t_vec_reduce_action *vec,
bool (*fn)(const t_reduce_action *), bool (*fn)(const t_reduce_action *), t_usize starting_index,
t_usize starting_index, t_usize *index) t_usize *index)
{ {
t_usize idx; t_usize idx;
@ -95,8 +93,7 @@ t_error vec_reduce_action_any(t_vec_reduce_action *vec,
} }
void vec_reduce_action_iter(t_vec_reduce_action *vec, void vec_reduce_action_iter(t_vec_reduce_action *vec,
void (*fn)(t_usize index, t_reduce_action *value, void (*fn)(t_usize index, t_reduce_action *value, void *state),
void *state),
void *state) void *state)
{ {
t_usize idx; t_usize idx;

View file

@ -1,7 +1,7 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* vec_reduce_action.c :+: :+: :+: */ /* reduce_action_functions3.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
@ -23,8 +23,8 @@ t_error vec_reduce_action_push_front(t_vec_reduce_action *vec,
if (vec->len == 0) if (vec->len == 0)
return (vec_reduce_action_push(vec, element)); return (vec_reduce_action_push(vec, element));
i = vec->len - 1; i = vec->len - 1;
if (vec->capacity < vec->len + 1 && if (vec->capacity < vec->len + 1 && vec_reduce_action_reserve(vec, 3
vec_reduce_action_reserve(vec, 3 * vec->len / 2 + 1)) * vec->len / 2 + 1))
return (ERROR); return (ERROR);
while (i > 0) while (i > 0)
{ {
@ -37,7 +37,8 @@ t_error vec_reduce_action_push_front(t_vec_reduce_action *vec,
return (NO_ERROR); return (NO_ERROR);
} }
t_error vec_reduce_action_pop_front(t_vec_reduce_action *vec, t_reduce_action *value) t_error vec_reduce_action_pop_front(t_vec_reduce_action *vec,
t_reduce_action *value)
{ {
t_usize i; t_usize i;

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* vec_reduce_action.c :+: :+: :+: */ /* reduce_action_functions4.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */ /* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */
/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */ /* Updated: 2024/09/19 17:51:32 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -31,7 +31,8 @@ t_reduce_action *vec_reduce_action_last(t_vec_reduce_action *vec)
return (&vec->buffer[vec->len - 1]); return (&vec->buffer[vec->len - 1]);
} }
void vec_reduce_action_copy_into(t_vec_reduce_action *vec, t_vec_reduce_action *dest) void vec_reduce_action_copy_into(t_vec_reduce_action *vec,
t_vec_reduce_action *dest)
{ {
if (vec == NULL || dest == NULL) if (vec == NULL || dest == NULL)
return ; return ;
@ -39,9 +40,9 @@ void vec_reduce_action_copy_into(t_vec_reduce_action *vec, t_vec_reduce_action *
mem_copy(dest->buffer, vec->buffer, vec->len * sizeof(t_reduce_action)); mem_copy(dest->buffer, vec->buffer, vec->len * sizeof(t_reduce_action));
} }
struct s_vec_reduce_action_splice_arguments vec_reduce_action_splice_args( struct s_vec_reduce_action_splice_arguments vec_reduce_action_splice_args(\
t_usize index, t_usize old_count, t_usize new_count, t_usize index, \
const t_reduce_action *elements) t_usize old_count, t_usize new_count, const t_reduce_action *elements)
{ {
return ((struct s_vec_reduce_action_splice_arguments){index, old_count, return ((struct s_vec_reduce_action_splice_arguments){index, old_count,
new_count, elements}); new_count, elements});
@ -61,9 +62,8 @@ void vec_reduce_action_splice(t_vec_reduce_action *self,
vec_reduce_action_reserve(self, new_size); vec_reduce_action_reserve(self, new_size);
contents = self->buffer; contents = self->buffer;
if (self->len > old_end) if (self->len > old_end)
mem_move(contents + new_end, mem_move(contents + new_end, contents + old_end, (self->len - old_end)
contents + old_end, * sizeof(t_reduce_action));
(self->len - old_end) * sizeof(t_reduce_action));
if (args.new_count > 0) if (args.new_count > 0)
{ {
if (args.elements) if (args.elements)

View file

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

View file

@ -15,8 +15,7 @@
#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;
@ -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);

View file

@ -10,15 +10,12 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#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;
@ -37,8 +34,7 @@ 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;
@ -58,8 +54,7 @@ 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;
@ -76,8 +71,7 @@ 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;
@ -94,10 +88,8 @@ 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;

View file

@ -15,16 +15,15 @@
#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)
{ {

View file

@ -39,16 +39,14 @@ void vec_str_copy_into(t_vec_str *vec, t_vec_str *dest)
mem_copy(dest->buffer, vec->buffer, vec->len * sizeof(t_str)); mem_copy(dest->buffer, vec->buffer, vec->len * sizeof(t_str));
} }
struct s_vec_str_splice_arguments vec_str_splice_args( struct s_vec_str_splice_arguments vec_str_splice_args(t_usize index,
t_usize index, t_usize old_count, t_usize new_count, t_usize old_count, t_usize new_count, const t_str *elements)
const t_str *elements)
{ {
return ((struct s_vec_str_splice_arguments){index, old_count, return ((struct s_vec_str_splice_arguments){index, old_count, new_count,
new_count, elements}); elements});
} }
void vec_str_splice(t_vec_str *self, void vec_str_splice(t_vec_str *self, struct s_vec_str_splice_arguments args)
struct s_vec_str_splice_arguments args)
{ {
t_str *contents; t_str *contents;
t_u32 new_size; t_u32 new_size;
@ -61,17 +59,16 @@ void vec_str_splice(t_vec_str *self,
vec_str_reserve(self, new_size); vec_str_reserve(self, new_size);
contents = self->buffer; contents = self->buffer;
if (self->len > old_end) if (self->len > old_end)
mem_move(contents + new_end, mem_move(contents + new_end, contents + old_end, (self->len - old_end)
contents + old_end, * sizeof(t_str));
(self->len - old_end) * sizeof(t_str));
if (args.new_count > 0) if (args.new_count > 0)
{ {
if (args.elements) if (args.elements)
mem_copy((contents + args.index * sizeof(t_str)), mem_copy((contents + args.index * sizeof(t_str)), args.elements,
args.elements, args.new_count * sizeof(t_str));
else
mem_set_zero((contents + args.index * sizeof(t_str)),
args.new_count * sizeof(t_str)); args.new_count * sizeof(t_str));
else
mem_set_zero((contents + args.index * sizeof(t_str)), args.new_count
* sizeof(t_str));
} }
self->len += args.new_count - args.old_count; self->len += args.new_count - args.old_count;
} }

View file

@ -13,8 +13,7 @@
#include "me/types.h" #include "me/types.h"
#include "me/vec/vec_str.h" #include "me/vec/vec_str.h"
void vec_str_sort(t_vec_str *v, void vec_str_sort(t_vec_str *v, t_vec_str_sort_fn is_sorted_fn)
t_vec_str_sort_fn is_sorted_fn)
{ {
t_usize sorted_part; t_usize sorted_part;
t_usize i; t_usize i;

View file

@ -51,8 +51,8 @@ t_error vec_subtree_reserve(t_vec_subtree *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_subtree)); sizeof(t_subtree));
vec->capacity = new_capacity; vec->capacity = new_capacity;
} }
return (NO_ERROR); return (NO_ERROR);

View file

@ -10,15 +10,13 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#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_subtree.h" #include "me/vec/vec_subtree.h"
#include <stdlib.h> #include <stdlib.h>
t_error vec_subtree_find(t_vec_subtree *vec, t_error vec_subtree_find(t_vec_subtree *vec, bool (*fn)(const t_subtree *),
bool (*fn)(const t_subtree *), t_usize *index) t_usize *index)
{ {
t_usize idx; t_usize idx;
@ -38,8 +36,7 @@ t_error vec_subtree_find(t_vec_subtree *vec,
} }
t_error vec_subtree_find_starting(t_vec_subtree *vec, t_error vec_subtree_find_starting(t_vec_subtree *vec,
bool (*fn)(const t_subtree *), bool (*fn)(const t_subtree *), t_usize starting_index, t_usize *index)
t_usize starting_index, t_usize *index)
{ {
t_usize idx; t_usize idx;
@ -58,8 +55,8 @@ t_error vec_subtree_find_starting(t_vec_subtree *vec,
return (ERROR); return (ERROR);
} }
t_error vec_subtree_all(t_vec_subtree *vec, t_error vec_subtree_all(t_vec_subtree *vec, bool (*fn)(const t_subtree *),
bool (*fn)(const t_subtree *), bool *result) bool *result)
{ {
t_usize idx; t_usize idx;
@ -76,8 +73,8 @@ t_error vec_subtree_all(t_vec_subtree *vec,
return (ERROR); return (ERROR);
} }
t_error vec_subtree_any(t_vec_subtree *vec, t_error vec_subtree_any(t_vec_subtree *vec, bool (*fn)(const t_subtree *),
bool (*fn)(const t_subtree *), bool *result) bool *result)
{ {
t_usize idx; t_usize idx;
@ -94,10 +91,8 @@ t_error vec_subtree_any(t_vec_subtree *vec,
return (ERROR); return (ERROR);
} }
void vec_subtree_iter(t_vec_subtree *vec, void vec_subtree_iter(t_vec_subtree *vec, void (*fn)(t_usize index,
void (*fn)(t_usize index, t_subtree *value, t_subtree *value, void *state), void *state)
void *state),
void *state)
{ {
t_usize idx; t_usize idx;

View file

@ -15,16 +15,15 @@
#include "me/vec/vec_subtree.h" #include "me/vec/vec_subtree.h"
#include <stdlib.h> #include <stdlib.h>
t_error vec_subtree_push_front(t_vec_subtree *vec, t_error vec_subtree_push_front(t_vec_subtree *vec, t_subtree element)
t_subtree element)
{ {
t_usize i; t_usize i;
if (vec->len == 0) if (vec->len == 0)
return (vec_subtree_push(vec, element)); return (vec_subtree_push(vec, element));
i = vec->len - 1; i = vec->len - 1;
if (vec->capacity < vec->len + 1 && if (vec->capacity < vec->len + 1 && vec_subtree_reserve(vec, 3 * vec->len
vec_subtree_reserve(vec, 3 * vec->len / 2 + 1)) / 2 + 1))
return (ERROR); return (ERROR);
while (i > 0) while (i > 0)
{ {

View file

@ -39,12 +39,11 @@ void vec_subtree_copy_into(t_vec_subtree *vec, t_vec_subtree *dest)
mem_copy(dest->buffer, vec->buffer, vec->len * sizeof(t_subtree)); mem_copy(dest->buffer, vec->buffer, vec->len * sizeof(t_subtree));
} }
struct s_vec_subtree_splice_arguments vec_subtree_splice_args( struct s_vec_subtree_splice_arguments vec_subtree_splice_args(t_usize index,
t_usize index, t_usize old_count, t_usize new_count, t_usize old_count, t_usize new_count, const t_subtree *elements)
const t_subtree *elements)
{ {
return ((struct s_vec_subtree_splice_arguments){index, old_count, return ((struct s_vec_subtree_splice_arguments){index, old_count, new_count,
new_count, elements}); elements});
} }
void vec_subtree_splice(t_vec_subtree *self, void vec_subtree_splice(t_vec_subtree *self,
@ -61,14 +60,13 @@ void vec_subtree_splice(t_vec_subtree *self,
vec_subtree_reserve(self, new_size); vec_subtree_reserve(self, new_size);
contents = self->buffer; contents = self->buffer;
if (self->len > old_end) if (self->len > old_end)
mem_move(contents + new_end, mem_move(contents + new_end, contents + old_end, (self->len - old_end)
contents + old_end, * sizeof(t_subtree));
(self->len - old_end) * sizeof(t_subtree));
if (args.new_count > 0) if (args.new_count > 0)
{ {
if (args.elements) if (args.elements)
mem_copy((contents + args.index * sizeof(t_subtree)), mem_copy((contents + args.index * sizeof(t_subtree)), args.elements,
args.elements, args.new_count * sizeof(t_subtree)); args.new_count * sizeof(t_subtree));
else else
mem_set_zero((contents + args.index * sizeof(t_subtree)), mem_set_zero((contents + args.index * sizeof(t_subtree)),
args.new_count * sizeof(t_subtree)); args.new_count * sizeof(t_subtree));

View file

@ -13,8 +13,7 @@
#include "me/types.h" #include "me/types.h"
#include "me/vec/vec_subtree.h" #include "me/vec/vec_subtree.h"
void vec_subtree_sort(t_vec_subtree *v, void vec_subtree_sort(t_vec_subtree *v, t_vec_subtree_sort_fn is_sorted_fn)
t_vec_subtree_sort_fn is_sorted_fn)
{ {
t_usize sorted_part; t_usize sorted_part;
t_usize i; t_usize i;