Added env hashmap with build_env function to build envp
This commit is contained in:
parent
f35e986145
commit
f86947a852
54 changed files with 2010 additions and 105 deletions
82
output/include/me/hashmap/hashmap_env.h
Normal file
82
output/include/me/hashmap/hashmap_env.h
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* hashmap_env.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/06 11:00:22 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/11 15:24:44 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef HASHMAP_ENV_H
|
||||
#define HASHMAP_ENV_H
|
||||
|
||||
#define DEFAULT_BUCKETS 750
|
||||
|
||||
|
||||
|
||||
#include "me/hash/hasher.h"
|
||||
#include "me/types.h"
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct s_kv_env
|
||||
{
|
||||
t_str key;
|
||||
t_str val;
|
||||
} t_kv_env;
|
||||
|
||||
typedef void (*t_hash_env_fn)(t_hasher *hasher, t_str *key);
|
||||
typedef void (*t_drop_env_fn)(t_kv_env val);
|
||||
typedef bool (*t_eq_env_fn)(t_str *lhs, t_str *rhs);
|
||||
|
||||
typedef struct s_entry_env
|
||||
{
|
||||
t_usize hash_id;
|
||||
t_kv_env kv;
|
||||
struct s_entry_env *next;
|
||||
} t_entry_env;
|
||||
|
||||
typedef struct s_hashmap_env
|
||||
{
|
||||
t_entry_env **buckets;
|
||||
t_usize num_buckets;
|
||||
t_hasher hasher;
|
||||
t_hash_env_fn hfunc;
|
||||
t_eq_env_fn cfunc;
|
||||
t_drop_env_fn drop;
|
||||
} t_hashmap_env;
|
||||
|
||||
t_hashmap_env *new_hashmap_env(t_hash_env_fn hash,
|
||||
t_eq_env_fn cmp,
|
||||
t_drop_env_fn drop);
|
||||
t_hashmap_env *new_hashmap_with_buckets_env(
|
||||
t_hash_env_fn hash, t_eq_env_fn cmp,
|
||||
t_drop_env_fn drop, size_t cap);
|
||||
|
||||
void drop_hashmap_env(t_hashmap_env *hmap);
|
||||
|
||||
void insert_hashmap_env(t_hashmap_env *hmap, t_str key,
|
||||
t_str value);
|
||||
|
||||
t_str *get_hashmap_env(t_hashmap_env *hmap,
|
||||
t_str *key);
|
||||
void remove_hashmap_env(t_hashmap_env *hmap, t_str *key);
|
||||
|
||||
t_entry_env *hashmap_get_entry_env(t_hashmap_env *hmap,
|
||||
t_usize hash,
|
||||
t_str *key,
|
||||
t_entry_env **prev);
|
||||
|
||||
t_error hashmap_env_iter(t_hashmap_env *self,
|
||||
t_error (*func)(t_usize idx,
|
||||
const t_str *key,
|
||||
t_str *val, void *ctx),
|
||||
void *ctx);
|
||||
|
||||
#endif
|
||||
58
output/include/me/vec/vec_str.h
Normal file
58
output/include/me/vec/vec_str.h
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* vec_str.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/04 18:46:53 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 17:53:00 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef VEC_STR_H
|
||||
#define VEC_STR_H
|
||||
|
||||
|
||||
#include "me/types.h"
|
||||
|
||||
typedef bool (*t_vec_str_sort_fn)(t_str *, t_str *);
|
||||
typedef void (*t_free_str_item)(t_str);
|
||||
|
||||
typedef struct s_vec_str
|
||||
{
|
||||
t_free_str_item free_func;
|
||||
t_usize len;
|
||||
t_usize capacity;
|
||||
t_str *buffer;
|
||||
} t_vec_str;
|
||||
|
||||
t_vec_str vec_str_new(t_usize capacity,
|
||||
t_free_str_item free_function);
|
||||
t_error vec_str_push(t_vec_str *vec, t_str element);
|
||||
t_error vec_str_push_front(t_vec_str *vec,
|
||||
t_str element);
|
||||
t_error vec_str_pop(t_vec_str *vec, t_str *value);
|
||||
t_error vec_str_pop_front(t_vec_str *vec, t_str *value);
|
||||
void vec_str_free(t_vec_str vec);
|
||||
t_error vec_str_reserve(t_vec_str *vec,
|
||||
t_usize wanted_capacity);
|
||||
t_error vec_str_find(t_vec_str *vec,
|
||||
bool (*fn)(const t_str *), 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);
|
||||
t_error vec_str_all(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);
|
||||
void vec_str_iter(t_vec_str *vec,
|
||||
void (*fn)(t_usize index, t_str *value,
|
||||
void *state),
|
||||
void *state);
|
||||
void vec_str_reverse(t_vec_str *vec);
|
||||
void vec_str_sort(t_vec_str *vec,
|
||||
t_vec_str_sort_fn is_sorted);
|
||||
t_error vec_str_back(t_vec_str *vec, t_str **out);
|
||||
|
||||
#endif
|
||||
116
output/src/hashmap/env/env.c
vendored
Normal file
116
output/src/hashmap/env/env.c
vendored
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* hashmap_env.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/06 10:58:20 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/11 15:32:51 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/hash/hasher.h"
|
||||
#include "me/hash/sip.h"
|
||||
#include "me/hashmap/hashmap_env.h"
|
||||
#include "me/mem/mem_alloc.h"
|
||||
#include "me/mem/mem_alloc_array.h"
|
||||
#include "me/mem/mem_copy.h"
|
||||
#include "me/types.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
t_hashmap_env *new_hashmap_env(t_hash_env_fn hfunc,
|
||||
t_eq_env_fn cfunc,
|
||||
t_drop_env_fn drop)
|
||||
{
|
||||
return (new_hashmap_with_buckets_env(hfunc, cfunc, drop,
|
||||
DEFAULT_BUCKETS));
|
||||
}
|
||||
|
||||
t_hashmap_env *new_hashmap_with_buckets_env(
|
||||
t_hash_env_fn hfunc, t_eq_env_fn cfunc,
|
||||
t_drop_env_fn drop, t_usize buckets)
|
||||
{
|
||||
t_hashmap_env *hmap;
|
||||
|
||||
hmap = mem_alloc(sizeof(*hmap));
|
||||
if (hmap == NULL)
|
||||
return (NULL);
|
||||
hmap->buckets = mem_alloc_array(buckets, sizeof(t_entry_env *));
|
||||
hmap->num_buckets = buckets;
|
||||
hmap->hasher = hasher_sip13_new();
|
||||
hmap->hfunc = hfunc;
|
||||
hmap->cfunc = cfunc;
|
||||
hmap->drop = drop;
|
||||
if (hmap->buckets == NULL)
|
||||
return ((void)free(hmap), NULL);
|
||||
return (hmap);
|
||||
}
|
||||
|
||||
void drop_hashmap_env(t_hashmap_env *hmap)
|
||||
{
|
||||
t_usize index;
|
||||
|
||||
index = 0;
|
||||
while (index < hmap->num_buckets)
|
||||
{
|
||||
if (hmap->buckets[index])
|
||||
{
|
||||
hmap->drop(hmap->buckets[index]->kv);
|
||||
free(hmap->buckets[index]);
|
||||
}
|
||||
index++;
|
||||
}
|
||||
hasher_finish(&hmap->hasher);
|
||||
free(hmap->buckets);
|
||||
free(hmap);
|
||||
}
|
||||
|
||||
t_entry_env *hashmap_get_entry_env(t_hashmap_env *hmap,
|
||||
t_usize hashed_key,
|
||||
t_str *key,
|
||||
t_entry_env **prev)
|
||||
{
|
||||
t_entry_env *entry;
|
||||
|
||||
entry = hmap->buckets[hashed_key % hmap->num_buckets];
|
||||
while (entry != NULL)
|
||||
{
|
||||
if (!hmap->cfunc(&entry->kv.key, key))
|
||||
{
|
||||
*prev = entry;
|
||||
entry = entry->next;
|
||||
}
|
||||
else
|
||||
{
|
||||
return (entry);
|
||||
}
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
void insert_hashmap_env(t_hashmap_env *hmap, t_str key,
|
||||
t_str value)
|
||||
{
|
||||
t_usize hashed_key;
|
||||
t_entry_env *prev;
|
||||
t_entry_env *entry;
|
||||
|
||||
hmap->hfunc(&hmap->hasher, &key);
|
||||
hashed_key = hasher_reset_and_finish(&hmap->hasher);
|
||||
prev = NULL;
|
||||
entry = hashmap_get_entry_env(hmap, hashed_key, &key, &prev);
|
||||
if (entry == NULL)
|
||||
{
|
||||
entry = mem_alloc(sizeof(t_entry_env));
|
||||
entry->hash_id = hashed_key;
|
||||
entry->kv = (t_kv_env){.key = key, .val = value};
|
||||
entry->next = NULL;
|
||||
if (prev == NULL)
|
||||
hmap->buckets[hashed_key % hmap->num_buckets] = entry;
|
||||
else
|
||||
prev->next = entry;
|
||||
}
|
||||
else
|
||||
entry->kv.val = value;
|
||||
}
|
||||
39
output/src/hashmap/env/env_iter.c
vendored
Normal file
39
output/src/hashmap/env/env_iter.c
vendored
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* hashmap_env.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/06 11:00:22 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/11 15:24:44 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/hashmap/hashmap_env.h"
|
||||
|
||||
t_error hashmap_env_iter(t_hashmap_env *self,
|
||||
t_error (*func)(t_usize idx,
|
||||
const t_str *key,
|
||||
t_str *val, void *ctx),
|
||||
void *ctx)
|
||||
{
|
||||
t_usize bucket_id;
|
||||
t_usize all_id;
|
||||
t_entry_env *cur;
|
||||
|
||||
bucket_id = 0;
|
||||
all_id = 0;
|
||||
while (bucket_id < self->num_buckets)
|
||||
{
|
||||
cur = self->buckets[bucket_id];
|
||||
while (cur != NULL)
|
||||
{
|
||||
if (func(all_id++, &cur->kv.key, &cur->kv.val, ctx))
|
||||
return (ERROR);
|
||||
cur = cur->next;
|
||||
}
|
||||
bucket_id++;
|
||||
}
|
||||
return (NO_ERROR);
|
||||
}
|
||||
56
output/src/hashmap/env/env_utils.c
vendored
Normal file
56
output/src/hashmap/env/env_utils.c
vendored
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* hashmap_env_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/06 10:58:20 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/11 15:35:37 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/hash/sip.h"
|
||||
#include "me/hashmap/hashmap_env.h"
|
||||
#include "me/mem/mem_alloc.h"
|
||||
#include "me/mem/mem_alloc_array.h"
|
||||
#include "me/mem/mem_copy.h"
|
||||
#include "me/types.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
t_str *get_hashmap_env(t_hashmap_env *hmap,
|
||||
t_str *key)
|
||||
{
|
||||
t_usize hashed_key;
|
||||
t_entry_env *entry;
|
||||
t_entry_env *prev;
|
||||
|
||||
hmap->hfunc(&hmap->hasher, key);
|
||||
hashed_key = hasher_reset_and_finish(&hmap->hasher);
|
||||
entry = hashmap_get_entry_env(hmap, hashed_key, key, &prev);
|
||||
if (entry == NULL)
|
||||
return (NULL);
|
||||
return (&entry->kv.val);
|
||||
}
|
||||
|
||||
void remove_hashmap_env(t_hashmap_env *hmap, t_str *key)
|
||||
{
|
||||
t_usize hashed_key;
|
||||
t_entry_env *prev;
|
||||
t_entry_env *entry;
|
||||
|
||||
hmap->hfunc(&hmap->hasher, key);
|
||||
hashed_key = hasher_reset_and_finish(&hmap->hasher);
|
||||
hmap->hasher = hasher_sip13_new();
|
||||
prev = NULL;
|
||||
entry = hashmap_get_entry_env(hmap, hashed_key, key, &prev);
|
||||
if (entry == NULL)
|
||||
return;
|
||||
if (prev == NULL)
|
||||
hmap->buckets[hashed_key % hmap->num_buckets] = entry->next;
|
||||
else
|
||||
prev->next = entry->next;
|
||||
hmap->drop(entry->kv);
|
||||
free(entry);
|
||||
hmap->buckets[hashed_key % hmap->num_buckets] = NULL;
|
||||
}
|
||||
115
output/src/vec/str/str.c
Normal file
115
output/src/vec/str/str.c
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* vec_str.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/05 18:46:28 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 17:54:11 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem_alloc_array.h"
|
||||
#include "me/mem/mem_copy.h"
|
||||
#include "me/mem/mem_set_zero.h"
|
||||
#include "me/types.h"
|
||||
#include "me/vec/vec_str.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
t_vec_str vec_str_new(t_usize capacity,
|
||||
t_free_str_item free_function)
|
||||
{
|
||||
t_vec_str out;
|
||||
|
||||
out = (t_vec_str){0};
|
||||
out.free_func = free_function;
|
||||
out.buffer = mem_alloc_array(capacity, sizeof(t_str));
|
||||
if (out.buffer)
|
||||
out.capacity = capacity;
|
||||
return (out);
|
||||
}
|
||||
|
||||
/// Return true in case of an error
|
||||
t_error vec_str_push(t_vec_str *vec, t_str element)
|
||||
{
|
||||
t_str *temp_buffer;
|
||||
size_t new_capacity;
|
||||
|
||||
if (vec == NULL)
|
||||
return (ERROR);
|
||||
if (vec->len + 1 > vec->capacity)
|
||||
{
|
||||
new_capacity = (vec->capacity * 3) / 2 + 1;
|
||||
while (vec->len + 1 > new_capacity)
|
||||
new_capacity = (new_capacity * 3) / 2 + 1;
|
||||
temp_buffer = mem_alloc_array(new_capacity, sizeof(t_str));
|
||||
if (temp_buffer == NULL)
|
||||
return (ERROR);
|
||||
mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_str));
|
||||
free(vec->buffer);
|
||||
vec->buffer = temp_buffer;
|
||||
vec->capacity = new_capacity;
|
||||
}
|
||||
vec->buffer[vec->len] = element;
|
||||
vec->len += 1;
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
/// Return true in case of an error
|
||||
t_error vec_str_reserve(t_vec_str *vec, t_usize wanted_capacity)
|
||||
{
|
||||
t_str *temp_buffer;
|
||||
size_t new_capacity;
|
||||
|
||||
if (vec == NULL)
|
||||
return (ERROR);
|
||||
if (wanted_capacity > vec->capacity)
|
||||
{
|
||||
new_capacity = (vec->capacity * 3) / 2 + 1;
|
||||
while (wanted_capacity > new_capacity)
|
||||
new_capacity = (new_capacity * 3) / 2 + 1;
|
||||
temp_buffer = mem_alloc_array(new_capacity, sizeof(t_str));
|
||||
if (temp_buffer == NULL)
|
||||
return (ERROR);
|
||||
mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_str));
|
||||
free(vec->buffer);
|
||||
vec->buffer = temp_buffer;
|
||||
vec->capacity = new_capacity;
|
||||
}
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
/// Return true if the vector is empty
|
||||
/// This function is safe to call with value being NULL
|
||||
t_error vec_str_pop(t_vec_str *vec, t_str *value)
|
||||
{
|
||||
t_str temp_value;
|
||||
t_str *ptr;
|
||||
|
||||
if (vec == NULL)
|
||||
return (ERROR);
|
||||
ptr = value;
|
||||
if (vec->len == 0)
|
||||
return (ERROR);
|
||||
if (value == NULL)
|
||||
ptr = &temp_value;
|
||||
vec->len--;
|
||||
*ptr = vec->buffer[vec->len];
|
||||
mem_set_zero(&vec->buffer[vec->len], sizeof(t_str));
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
/// This function is safe to call with `free_elem` being NULL
|
||||
void vec_str_free(t_vec_str vec)
|
||||
{
|
||||
if (vec.free_func)
|
||||
{
|
||||
while (vec.len)
|
||||
{
|
||||
vec.free_func(vec.buffer[vec.len - 1]);
|
||||
vec.len--;
|
||||
}
|
||||
}
|
||||
free(vec.buffer);
|
||||
}
|
||||
112
output/src/vec/str/str_functions2.c
Normal file
112
output/src/vec/str/str_functions2.c
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* vec_str.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem_alloc_array.h"
|
||||
#include "me/mem/mem_copy.h"
|
||||
#include "me/mem/mem_set_zero.h"
|
||||
#include "me/types.h"
|
||||
#include "me/vec/vec_str.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
t_error vec_str_find(t_vec_str *vec,
|
||||
bool (*fn)(const t_str *), t_usize *index)
|
||||
{
|
||||
t_usize idx;
|
||||
|
||||
if (vec == NULL || fn == NULL || index == NULL)
|
||||
return (ERROR);
|
||||
idx = 0;
|
||||
while (idx < vec->len)
|
||||
{
|
||||
if (fn(&vec->buffer[idx]))
|
||||
{
|
||||
*index = idx;
|
||||
return (NO_ERROR);
|
||||
}
|
||||
idx++;
|
||||
}
|
||||
return (ERROR);
|
||||
}
|
||||
|
||||
t_error vec_str_find_starting(t_vec_str *vec,
|
||||
bool (*fn)(const t_str *),
|
||||
t_usize starting_index, t_usize *index)
|
||||
{
|
||||
t_usize idx;
|
||||
|
||||
if (vec == NULL || fn == NULL || index == NULL)
|
||||
return (ERROR);
|
||||
idx = starting_index;
|
||||
while (idx < vec->len)
|
||||
{
|
||||
if (fn(&vec->buffer[idx]))
|
||||
{
|
||||
*index = idx;
|
||||
return (NO_ERROR);
|
||||
}
|
||||
idx++;
|
||||
}
|
||||
return (ERROR);
|
||||
}
|
||||
|
||||
t_error vec_str_all(t_vec_str *vec,
|
||||
bool (*fn)(const t_str *), bool *result)
|
||||
{
|
||||
t_usize idx;
|
||||
|
||||
if (vec == NULL || fn == NULL || result == NULL)
|
||||
return (ERROR);
|
||||
idx = 0;
|
||||
*result = true;
|
||||
while (*result && idx < vec->len)
|
||||
{
|
||||
if (!fn(&vec->buffer[idx]))
|
||||
*result = false;
|
||||
idx++;
|
||||
}
|
||||
return (ERROR);
|
||||
}
|
||||
|
||||
t_error vec_str_any(t_vec_str *vec,
|
||||
bool (*fn)(const t_str *), bool *result)
|
||||
{
|
||||
t_usize idx;
|
||||
|
||||
if (vec == NULL || fn == NULL || result == NULL)
|
||||
return (ERROR);
|
||||
idx = 0;
|
||||
*result = false;
|
||||
while (*result && idx < vec->len)
|
||||
{
|
||||
if (fn(&vec->buffer[idx]))
|
||||
*result = true;
|
||||
idx++;
|
||||
}
|
||||
return (ERROR);
|
||||
}
|
||||
|
||||
void vec_str_iter(t_vec_str *vec,
|
||||
void (*fn)(t_usize index, t_str *value,
|
||||
void *state),
|
||||
void *state)
|
||||
{
|
||||
t_usize idx;
|
||||
|
||||
if (vec == NULL || fn == NULL)
|
||||
return;
|
||||
idx = 0;
|
||||
while (idx < vec->len)
|
||||
{
|
||||
fn(idx, &vec->buffer[idx], state);
|
||||
idx++;
|
||||
}
|
||||
}
|
||||
84
output/src/vec/str/str_functions3.c
Normal file
84
output/src/vec/str/str_functions3.c
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* vec_str.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem_alloc_array.h"
|
||||
#include "me/mem/mem_copy.h"
|
||||
#include "me/mem/mem_set_zero.h"
|
||||
#include "me/types.h"
|
||||
#include "me/vec/vec_str.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
t_error vec_str_push_front(t_vec_str *vec,
|
||||
t_str element)
|
||||
{
|
||||
t_usize i;
|
||||
|
||||
if (vec->len == 0)
|
||||
return (vec_str_push(vec, element));
|
||||
i = vec->len - 1;
|
||||
if (vec->capacity < vec->len + 1 &&
|
||||
vec_str_reserve(vec, 3 * vec->len / 2 + 1))
|
||||
return (ERROR);
|
||||
while (i > 0)
|
||||
{
|
||||
vec->buffer[i + 1] = vec->buffer[i];
|
||||
i--;
|
||||
}
|
||||
vec->buffer[1] = vec->buffer[0];
|
||||
vec->buffer[0] = element;
|
||||
vec->len++;
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
t_error vec_str_pop_front(t_vec_str *vec, t_str *value)
|
||||
{
|
||||
t_usize i;
|
||||
|
||||
if (vec->len <= 1)
|
||||
return (vec_str_pop(vec, value));
|
||||
i = 0;
|
||||
*value = vec->buffer[0];
|
||||
vec->len--;
|
||||
while (i < vec->len)
|
||||
{
|
||||
vec->buffer[i] = vec->buffer[i + 1];
|
||||
i++;
|
||||
}
|
||||
mem_set_zero(&vec->buffer[i], sizeof(*vec->buffer));
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
void vec_str_reverse(t_vec_str *vec)
|
||||
{
|
||||
t_str temporary;
|
||||
t_usize i;
|
||||
|
||||
i = 0;
|
||||
while (i < vec->len / 2)
|
||||
{
|
||||
temporary = vec->buffer[vec->len - 1 - i];
|
||||
vec->buffer[vec->len - 1 - i] = vec->buffer[i];
|
||||
vec->buffer[i] = temporary;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
t_error vec_str_back(t_vec_str *vec, t_str **out)
|
||||
{
|
||||
t_str *temporary;
|
||||
|
||||
if (out == NULL)
|
||||
out = &temporary;
|
||||
if (vec->len != 0)
|
||||
return (*out = &vec->buffer[vec->len - 1], true);
|
||||
return (false);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue