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
116
stdme/generic_sources/src/hashmap/C__PREFIX__.c__TEMPLATE__
Normal file
116
stdme/generic_sources/src/hashmap/C__PREFIX__.c__TEMPLATE__
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* hashmap_C__PREFIX__.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/06 10:58:20 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/11 15:32:51 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/hash/hasher.h"
|
||||
#include "me/hash/sip.h"
|
||||
#include "me/hashmap/hashmap_C__PREFIX__.h"
|
||||
#include "me/mem/mem_alloc.h"
|
||||
#include "me/mem/mem_alloc_array.h"
|
||||
#include "me/mem/mem_copy.h"
|
||||
#include "me/types.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
t_hashmap_C__PREFIX__ *new_hashmap_C__PREFIX__(t_hash_C__PREFIX___fn hfunc,
|
||||
t_eq_C__PREFIX___fn cfunc,
|
||||
t_drop_C__PREFIX___fn drop)
|
||||
{
|
||||
return (new_hashmap_with_buckets_C__PREFIX__(hfunc, cfunc, drop,
|
||||
DEFAULT_BUCKETS));
|
||||
}
|
||||
|
||||
t_hashmap_C__PREFIX__ *new_hashmap_with_buckets_C__PREFIX__(
|
||||
t_hash_C__PREFIX___fn hfunc, t_eq_C__PREFIX___fn cfunc,
|
||||
t_drop_C__PREFIX___fn drop, t_usize buckets)
|
||||
{
|
||||
t_hashmap_C__PREFIX__ *hmap;
|
||||
|
||||
hmap = mem_alloc(sizeof(*hmap));
|
||||
if (hmap == NULL)
|
||||
return (NULL);
|
||||
hmap->buckets = mem_alloc_array(buckets, sizeof(t_entry_C__PREFIX__ *));
|
||||
hmap->num_buckets = buckets;
|
||||
hmap->hasher = hasher_sip13_new();
|
||||
hmap->hfunc = hfunc;
|
||||
hmap->cfunc = cfunc;
|
||||
hmap->drop = drop;
|
||||
if (hmap->buckets == NULL)
|
||||
return ((void)free(hmap), NULL);
|
||||
return (hmap);
|
||||
}
|
||||
|
||||
void drop_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap)
|
||||
{
|
||||
t_usize index;
|
||||
|
||||
index = 0;
|
||||
while (index < hmap->num_buckets)
|
||||
{
|
||||
if (hmap->buckets[index])
|
||||
{
|
||||
hmap->drop(hmap->buckets[index]->kv);
|
||||
free(hmap->buckets[index]);
|
||||
}
|
||||
index++;
|
||||
}
|
||||
hasher_finish(&hmap->hasher);
|
||||
free(hmap->buckets);
|
||||
free(hmap);
|
||||
}
|
||||
|
||||
t_entry_C__PREFIX__ *hashmap_get_entry_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap,
|
||||
t_usize hashed_key,
|
||||
C__KEYTYPE__ *key,
|
||||
t_entry_C__PREFIX__ **prev)
|
||||
{
|
||||
t_entry_C__PREFIX__ *entry;
|
||||
|
||||
entry = hmap->buckets[hashed_key % hmap->num_buckets];
|
||||
while (entry != NULL)
|
||||
{
|
||||
if (!hmap->cfunc(&entry->kv.key, key))
|
||||
{
|
||||
*prev = entry;
|
||||
entry = entry->next;
|
||||
}
|
||||
else
|
||||
{
|
||||
return (entry);
|
||||
}
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
void insert_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ key,
|
||||
C__VALTYPE__ value)
|
||||
{
|
||||
t_usize hashed_key;
|
||||
t_entry_C__PREFIX__ *prev;
|
||||
t_entry_C__PREFIX__ *entry;
|
||||
|
||||
hmap->hfunc(&hmap->hasher, &key);
|
||||
hashed_key = hasher_reset_and_finish(&hmap->hasher);
|
||||
prev = NULL;
|
||||
entry = hashmap_get_entry_C__PREFIX__(hmap, hashed_key, &key, &prev);
|
||||
if (entry == NULL)
|
||||
{
|
||||
entry = mem_alloc(sizeof(t_entry_C__PREFIX__));
|
||||
entry->hash_id = hashed_key;
|
||||
entry->kv = (t_kv_C__PREFIX__){.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;
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* hashmap_C__PREFIX__.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/06 11:00:22 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/11 15:24:44 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/hashmap/hashmap_C__PREFIX__.h"
|
||||
|
||||
t_error hashmap_C__PREFIX___iter(t_hashmap_C__PREFIX__ *self,
|
||||
t_error (*func)(t_usize idx,
|
||||
const C__KEYTYPE__ *key,
|
||||
C__VALTYPE__ *val, void *ctx),
|
||||
void *ctx)
|
||||
{
|
||||
t_usize bucket_id;
|
||||
t_usize all_id;
|
||||
t_entry_C__PREFIX__ *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);
|
||||
}
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* hashmap_C__PREFIX___utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/06 10:58:20 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/11 15:35:37 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/hash/sip.h"
|
||||
#include "me/hashmap/hashmap_C__PREFIX__.h"
|
||||
#include "me/mem/mem_alloc.h"
|
||||
#include "me/mem/mem_alloc_array.h"
|
||||
#include "me/mem/mem_copy.h"
|
||||
#include "me/types.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
C__VALTYPE__ *get_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap,
|
||||
C__KEYTYPE__ *key)
|
||||
{
|
||||
t_usize hashed_key;
|
||||
t_entry_C__PREFIX__ *entry;
|
||||
t_entry_C__PREFIX__ *prev;
|
||||
|
||||
hmap->hfunc(&hmap->hasher, key);
|
||||
hashed_key = hasher_reset_and_finish(&hmap->hasher);
|
||||
entry = hashmap_get_entry_C__PREFIX__(hmap, hashed_key, key, &prev);
|
||||
if (entry == NULL)
|
||||
return (NULL);
|
||||
return (&entry->kv.val);
|
||||
}
|
||||
|
||||
void remove_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ *key)
|
||||
{
|
||||
t_usize hashed_key;
|
||||
t_entry_C__PREFIX__ *prev;
|
||||
t_entry_C__PREFIX__ *entry;
|
||||
|
||||
hmap->hfunc(&hmap->hasher, key);
|
||||
hashed_key = hasher_reset_and_finish(&hmap->hasher);
|
||||
hmap->hasher = hasher_sip13_new();
|
||||
prev = NULL;
|
||||
entry = hashmap_get_entry_C__PREFIX__(hmap, hashed_key, key, &prev);
|
||||
if (entry == NULL)
|
||||
return;
|
||||
if (prev == NULL)
|
||||
hmap->buckets[hashed_key % hmap->num_buckets] = entry->next;
|
||||
else
|
||||
prev->next = entry->next;
|
||||
hmap->drop(entry->kv);
|
||||
free(entry);
|
||||
hmap->buckets[hashed_key % hmap->num_buckets] = NULL;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue