Libft
Custom implementation of core libc functions with additional utility helpers.
Loading...
Searching...
No Matches
priv_hashmap.h File Reference
#include "libft.h"
Include dependency graph for priv_hashmap.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

size_t hash_string (const char *key)
 Default string hash function (djb2).
 
bool hashmap_need_resize (t_hashmap *map, const char *key)
 
bool hashmap_resize (t_hashmap *map)
 Doubles the bucket capacity and rehashes every stored pair.
 
bool hashmap_insert (t_hashmap *map, t_key_value *new)
 Inserts a pair into the map, or replaces an existing one.
 

Function Documentation

◆ hash_string()

size_t hash_string ( const char *  key)

Default string hash function (djb2).

Computes a hash over the bytes of a NUL-terminated key. The returned value is reduced modulo the bucket count by the caller.

Parameters
keyNUL-terminated key to hash (borrowed).
Returns
The computed hash value.
Here is the caller graph for this function:

◆ hashmap_insert()

bool hashmap_insert ( t_hashmap map,
t_key_value new 
)

Inserts a pair into the map, or replaces an existing one.

Appends new to its target bucket chain. If a pair with the same key already exists in that bucket, its value is released through the map's del callback and the existing entry adopts new (see bucket_replace), in which case size is left unchanged.

Note
On success the map takes ownership of new. On failure new is left untouched and ownership remains with the caller.
Parameters
mapPointer to an initialized map (borrowed).
newPair to insert (ownership transferred on success).
Returns
true on success, false on memory allocation failure.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ hashmap_need_resize()

bool hashmap_need_resize ( t_hashmap map,
const char *  key 
)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ hashmap_resize()

bool hashmap_resize ( t_hashmap map)

Doubles the bucket capacity and rehashes every stored pair.

Allocates a new bucket array of twice the current capacity, swaps it in and redistributes the existing pairs into it. The pairs themselves are moved, not reallocated. On failure the previous bucket array is restored, leaving the map unchanged.

Note
Pairs are transferred between bucket arrays; their keys and values are never freed by a successful resize.
Parameters
mapPointer to an initialized map (borrowed).
Returns
true on success, false on memory allocation failure.
Here is the call graph for this function:
Here is the caller graph for this function: