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

Go to the source code of this file.

Functions

bool buckets_init (t_vector *buckets, size_t init_cap)
 Initializes the bucket array.
 
void buckets_free (t_vector *buckets, void(*del)(void *))
 Frees the bucket array and every pair it contains.
 
void buckets_detach (t_vector *buckets)
 Detaches every pair from the bucket chains without freeing them.
 
void buckets_swap (t_vector *buckets1, t_vector *buckets2)
 Swaps the contents of two bucket vectors.
 
void buckets_foreach (t_vector *vector, void(*del)(void *), void(*f)(void *, void(*del)(void *)))
 Applies a function to every used bucket slot.
 
void bucket_free (void *bucket_ptr, void(*del)(void *))
 Frees a single bucket chain and its pairs.
 
bool bucket_contains (t_list bucket, const char *key)
 Tests whether a bucket chain holds a pair with the given key.
 
void bucket_detach (void *bucket, void(*_)(void *))
 Detaches every pair from a single bucket chain without freeing them.
 
void bucket_replace (t_list bucket, t_key_value *new, void(*del)(void *))
 Replaces the value of the pair matching new->key within a chain.
 

Function Documentation

◆ bucket_contains()

bool bucket_contains ( t_list  bucket,
const char *  key 
)

Tests whether a bucket chain holds a pair with the given key.

Parameters
bucketHead of the bucket chain to search (borrowed, may be NULL).
keyNUL-terminated key to look for (borrowed).
Returns
true if a matching pair is found, false otherwise.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ bucket_detach()

void bucket_detach ( void *  bucket,
void(*)(void *)  _ 
)

Detaches every pair from a single bucket chain without freeing them.

Sets each node's content to NULL. Shaped to be used as a buckets_foreach() callback; the destructor parameter is unused.

Parameters
bucketAddress of the bucket slot (a t_list *) to detach (borrowed).
_Unused destructor parameter (required by the callback signature).
Here is the caller graph for this function:

◆ bucket_free()

void bucket_free ( void *  bucket_ptr,
void(*)(void *)  del 
)

Frees a single bucket chain and its pairs.

Frees every pair in the chain (key and, through del, value) and removes all list nodes. Shaped to be used as a buckets_foreach() callback.

Parameters
bucket_ptrAddress of the bucket slot (a t_list *) to free (borrowed).
delOptional destructor applied to each stored value (may be NULL).
Here is the call graph for this function:
Here is the caller graph for this function:

◆ bucket_replace()

void bucket_replace ( t_list  bucket,
t_key_value new,
void(*)(void *)  del 
)

Replaces the value of the pair matching new->key within a chain.

Walks the chain and, for the node whose key matches new->key, releases the existing pair (its value through del) and stores new in its place.

Note
Ownership of new is transferred to the matching node. The previously stored pair is freed.
Parameters
bucketHead of the bucket chain to update (borrowed).
newReplacement pair carrying the matching key (ownership transferred).
delOptional destructor applied to the replaced value (may be NULL).
Here is the call graph for this function:
Here is the caller graph for this function:

◆ buckets_detach()

void buckets_detach ( t_vector buckets)

Detaches every pair from the bucket chains without freeing them.

Sets each list node's content to NULL across all buckets, severing the link between the chains and their pairs. Used during a resize so the old chains can be freed while the pairs themselves are kept (they have been moved into the new bucket array).

Parameters
bucketsPointer to the bucket vector to detach (borrowed).
Here is the call graph for this function:
Here is the caller graph for this function:

◆ buckets_foreach()

void buckets_foreach ( t_vector vector,
void(*)(void *)  del,
void(*)(void *, void(*del)(void *))  f 
)

Applies a function to every used bucket slot.

Iterates over the first vector->len slots and calls f with the address of each bucket slot and the del callback, allowing f to free or mutate the chain stored there.

Parameters
vectorPointer to the bucket vector to iterate (borrowed).
delDestructor forwarded to f for each slot (may be NULL).
fFunction applied to each bucket slot (borrowed).
Here is the caller graph for this function:

◆ buckets_free()

void buckets_free ( t_vector buckets,
void(*)(void *)  del 
)

Frees the bucket array and every pair it contains.

Frees each bucket chain (every pair's key and, through del, its value) and then frees the underlying vector. The vector is left in a freed state.

Parameters
bucketsPointer to the bucket vector to free (borrowed).
delOptional destructor applied to each stored value (may be NULL).
Here is the call graph for this function:
Here is the caller graph for this function:

◆ buckets_init()

bool buckets_init ( t_vector buckets,
size_t  init_cap 
)

Initializes the bucket array.

Allocates a vector of init_cap t_list slots and zeroes it, so every bucket starts as an empty (NULL) chain.

Parameters
bucketsPointer to the vector to initialize (uninitialized).
init_capNumber of buckets to allocate.
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:

◆ buckets_swap()

void buckets_swap ( t_vector buckets1,
t_vector buckets2 
)

Swaps the contents of two bucket vectors.

Exchanges the two t_vector structures by value; no allocation occurs and no pair is moved in memory.

Parameters
buckets1First bucket vector (borrowed).
buckets2Second bucket vector (borrowed).
Here is the caller graph for this function: