6 Module LIST
gullradriel edited this page 2026-10-29 20:54:40 +01:00

LISTS: generic type list

Structures

Name
struct LIST_NODE
Structure of a generic list node.
struct LIST
Structure of a generic LIST container.

Functions

Name
void * remove_list_node_f(LIST * list, LIST_NODE * node)
remove a node
LIST_NODE * new_list_node(void * ptr, void(*)(void *ptr) destructor)
create a new node
LIST * new_generic_list(size_t max_items)
initialize a list
int list_unshift_sorted(LIST * list, void * ptr, int(*)(const void *a, const void b) comparator, void()(void *ptr) destructor)
put a pointer sorted via comparator from the start to the end
int list_unshift(LIST * list, void * ptr, void(*)(void *ptr) destructor)
put a pointer at the beginning of list
void * list_shift_f(LIST * list, char * file, size_t line)
get first ptr from list
LIST_NODE * list_search_with_f(LIST * list, int(*)(void *ptr) checkfunk)
search for data in list
LIST_NODE * list_search(LIST * list, const void * ptr)
search ptr in list
int list_push_sorted(LIST * list, void * ptr, int(*)(const void *a, const void b) comparator, void()(void *ptr) destructor)
add a pointer sorted via comparator from the end of the list
int list_push(LIST * list, void * ptr, void(*)(void *ptr) destructor)
add a pointer at the end of the list
void * list_pop_f(LIST * list)
get last ptr from list
int list_node_unshift(LIST * list, LIST_NODE * node)
unshift a node at the beginning of the list
LIST_NODE * list_node_shift(LIST * list)
shift a node from the beginning of the list
int list_node_push(LIST * list, LIST_NODE * node)
push a node at the end of the list
LIST_NODE * list_node_pop(LIST * list)
pop a node from the end of the list
int list_empty_with_f(LIST * list, void(*)(void *ptr) free_fnct)
empty the list with a custom free function
int list_empty(LIST * list)
empty the list
int list_destroy(LIST ** list)
free the list

Defines

Name
remove_list_node(_LIST, _NODE, _TYPE)
Remove macro helper for void pointer casting.
list_shift(_LIST, _TYPE)
Shift macro helper for void pointer casting.
list_pop(_LIST, _TYPE)
Pop macro helper for void pointer casting.
list_foreach(_ITEM, _LIST)
ForEach macro helper, safe for node removal during iteration.
link_node(__NODE_1, __NODE_2)
Macro helper for linking two nodes, with NULL safety.
UNLIMITED_LIST_ITEMS
flag to pass to new_generic_list for an unlimited number of item in the list.
MAX_LIST_ITEMS
flag to pass to new_generic_list for the maximum possible number of item in a list

Functions Documentation

function remove_list_node_f

void * remove_list_node_f(
    LIST * list,
    LIST_NODE * node
)

remove a node

Parameters:

  • list The list to pick in
  • node The node to remove in the list

Return: The node holded pointer or NULL

Internal function called each time we need to get a node out of a list.

remove a node

function new_list_node

LIST_NODE * new_list_node(
    void * ptr,
    void(*)(void *ptr) destructor
)

create a new node

Parameters:

  • ptr The pointer you want to put in the node.
  • destructor Pointer to the ptr type destructor function. Leave to NULL if there isn't.

Return: A new node or NULL on error. Check error messages in DEBUG mode.

Allocate a new node to link in a list.

create a new node

function new_generic_list

LIST * new_generic_list(
    size_t max_items
)

initialize a list

Parameters:

  • max_items Specify a max size for the list container. From UNLIMITED_LIST_ITEMS (0) to MAX_LIST_ITEMS (SIZE_MAX).

Return: a new LIST or NULL

Initialiaze a generic list container to max_items pointers.

initialize a list

function list_unshift_sorted

int list_unshift_sorted(
    LIST * list,
    void * ptr,
    int(*)(const void *a, const void *b) comparator,
    void(*)(void *ptr) destructor
)

put a pointer sorted via comparator from the start to the end

Parameters:

  • list An initilized list container. A null value will cause an error and a _log message.
  • ptr The pointer you wan to put in the list. A null value will cause an error and a _log message.
  • comparator A pointer to a function which take two void * pointers and return an int.
  • destructor Pointer to the ptr type destructor function. Leave to NULL if there isn't.

Return: TRUE or FALSE. Check error messages in DEBUG mode.

Add a pointer sorted in the list , starting by the start of the list.

put a pointer sorted via comparator from the start to the end

function list_unshift

int list_unshift(
    LIST * list,
    void * ptr,
    void(*)(void *ptr) destructor
)

put a pointer at the beginning of list

Parameters:

  • list An initilized list container. A null value will cause an error and a _log message.
  • ptr The pointer you wan to put in the list. A null value will cause an error and a _log message.
  • destructor Pointer to the ptr type destructor function. Leave to NULL if there isn't.

Return: A void *ptr or NULL. Check error messages in DEBUG mode.

Add a pointer at the start of the list.

put a pointer at the beginning of list

function list_shift_f

void * list_shift_f(
    LIST * list,
    char * file,
    size_t line
)

get first ptr from list

Parameters:

  • list An initilized list container. A null value will cause an error and a _log message.
  • file holder for FILE
  • line holder for LINE

Return: A void *ptr or NULL. Check error messages in DEBUG mode.

Get a pointer from the start of the list.

get first ptr from list

Helper function for the list_shift macro.

function list_search_with_f

LIST_NODE * list_search_with_f(
    LIST * list,
    int(*)(void *ptr) checkfunk
)

search for data in list

Parameters:

  • list The target list
  • checkfunk Function pointer who should return 1 if it matches or 0 if it's not

Return: The LIST_NODE *node or NULL

search ptr in list

search for data in list

LIST_NODE * list_search(
    LIST * list,
    const void * ptr
)

search ptr in list

Parameters:

  • list The target list
  • ptr Searched pointer

Return: The LIST_NODE *node or NULL

function list_push_sorted

int list_push_sorted(
    LIST * list,
    void * ptr,
    int(*)(const void *a, const void *b) comparator,
    void(*)(void *ptr) destructor
)

add a pointer sorted via comparator from the end of the list

Parameters:

  • list An initilized list container. A null value will cause an error and a _log message.
  • ptr The pointer you wan to put in the list. A null value will cause an error and a _log message.
  • comparator A pointer to a function which take two void * pointers and return an int.
  • destructor Pointer to the ptr type destructor function. Leave to NULL if there isn't.

Return: TRUE or FALSE. Check error messages in DEBUG mode.

Add a pointer sorted in the list , starting by the end of the list.

add a pointer sorted via comparator from the end of the list

function list_push

int list_push(
    LIST * list,
    void * ptr,
    void(*)(void *ptr) destructor
)

add a pointer at the end of the list

Parameters:

  • list An initilized list container. A null value will cause an error and a _log message.
  • ptr The pointer you wan to put in the list. A null value will cause an error and a _log message.
  • destructor Pointer to the ptr type destructor function. Leave to NULL if there isn't.

Return: TRUE or FALSE. Check error messages in DEBUG mode.

Add a pointer to the end of the list.

add a pointer at the end of the list

function list_pop_f

void * list_pop_f(
    LIST * list
)

get last ptr from list

Parameters:

  • list An initilized list container. A null value will cause an error and a _log message.

Return: A void *ptr or NULL. Check error messages in DEBUG mode.

Get a pointer from the end of the list.

get last ptr from list

function list_node_unshift

int list_node_unshift(
    LIST * list,
    LIST_NODE * node
)

unshift a node at the beginning of the list

Parameters:

  • list An initilized list container. A null value will cause an error and a _log message.
  • node The node pointer you wan to put in the list. A null value will cause an error and a _log message.

Return: TRUE or FALSE

Add a pointer at the start of the list.

unshift a node at the beginning of the list

function list_node_shift

LIST_NODE * list_node_shift(
    LIST * list
)

shift a node from the beginning of the list

Parameters:

  • list An initilized list container. A null value will cause an error and a _log message.

Return: A LIST_NODE *node or NULL. Check error messages in DEBUG mode.

Get a LIST_NODE pointer from the start of the list.

shift a node from the beginning of the list

function list_node_push

int list_node_push(
    LIST * list,
    LIST_NODE * node
)

push a node at the end of the list

Parameters:

  • list An initilized list container. A null value will cause an error and a _log message.
  • node The node pointer you want to put in the list. A null value will cause an error and a _log message.

Return: TRUE or FALSE

Add a filled node to the end of the list.

push a node at the end of the list

function list_node_pop

LIST_NODE * list_node_pop(
    LIST * list
)

pop a node from the end of the list

Parameters:

  • list An initilized list container. A null value will cause an error and a _log message.

Return: A LIST_NODE *node or NULL. Check error messages in DEBUG mode.

Get a LIST_NODE pointer from the end of the list.

pop a node from the end of the list

function list_empty_with_f

int list_empty_with_f(
    LIST * list,
    void(*)(void *ptr) free_fnct
)

empty the list with a custom free function

Parameters:

  • list The list to empty. An already empty or null list will cause an error and a _log message.
  • free_fnct a function ptr to a void func( void *ptr )

Return: TRUE or FALSE. Check error messages in DEBUG mode.

Empty a LIST list of pointers.

empty the list with a custom free function

function list_empty

int list_empty(
    LIST * list
)

empty the list

Parameters:

  • list The list to empty. An already empty or null list will cause an error and a _log message.

Return: TRUE or FALSE. Check error messages in DEBUG mode.

Empty a LIST list of pointers.

empty the list

function list_destroy

int list_destroy(
    LIST ** list
)

free the list

Parameters:

  • list The list to destroy. An already empty or null list will cause an error and a _log message.

Return: TRUE or FALSE. Check error messages in DEBUG mode.

Empty and Free a list container.

free the list

Call the destructor each time.

Macros Documentation

define remove_list_node

#define remove_list_node(
    __LIST_,
    __NODE_,
    __TYPE_
)
(__TYPE_*)remove_list_node_f(__LIST_, __NODE_)

Remove macro helper for void pointer casting.

define list_shift

#define list_shift(
    __LIST_,
    __TYPE_
)
(__TYPE_*)list_shift_f(__LIST_, __FILE__, __LINE__)

Shift macro helper for void pointer casting.

define list_pop

#define list_pop(
    __LIST_,
    __TYPE_
)
(__TYPE_*)list_pop_f(__LIST_)

Pop macro helper for void pointer casting.

define list_foreach

#define list_foreach(
    __ITEM_,
    __LIST_
)
for (LIST_NODE* __ITEM_ = (__LIST_) ? (__LIST_)->start : NULL, *__next_##__ITEM_ = __ITEM_ ? __ITEM_->next : NULL; __ITEM_; __ITEM_ = __next_##__ITEM_, \
                __next_##__ITEM_ = __ITEM_ ? __ITEM_->next : NULL)

ForEach macro helper, safe for node removal during iteration.

#define link_node(
    __NODE_1,
    __NODE_2
)
do {                                                                                                                           \
    if (!(__NODE_1) || !(__NODE_2)) {                                                                                          \
        n_log(LOG_ERR, "link_node: NULL argument (%s=%p, %s=%p)", #__NODE_1, (void*)(__NODE_1), #__NODE_2, (void*)(__NODE_2)); \
    } else {                                                                                                                   \
        (__NODE_2)->prev = (__NODE_1);                                                                                         \
        (__NODE_1)->next = (__NODE_2);                                                                                         \
    }                                                                                                                          \
} while (0)

Macro helper for linking two nodes, with NULL safety.

define UNLIMITED_LIST_ITEMS

#define UNLIMITED_LIST_ITEMS 0

flag to pass to new_generic_list for an unlimited number of item in the list.

Can be dangerous !

define MAX_LIST_ITEMS

#define MAX_LIST_ITEMS SIZE_MAX

flag to pass to new_generic_list for the maximum possible number of item in a list