Nilorea Library
C utilities for networking, threading, graphics
LISTS: generic type list

Data Structures

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

Macros

#define link_node(__NODE_1, __NODE_2)
 Macro helper for linking two nodes. More...
 
#define list_foreach(__ITEM_, __LIST_)
 ForEach macro helper. More...
 
#define list_pop(__LIST_, __TYPE_)   ( __TYPE_ *)list_pop_f( __LIST_ )
 Pop macro helper for void pointer casting. More...
 
#define list_shift(__LIST_, __TYPE_)   (__TYPE_ *)list_shift_f( __LIST_ )
 Shift macro helper for void pointer casting. More...
 
#define remove_list_node(__LIST_, __NODE_, __TYPE_)   (__TYPE_ *)remove_list_node_f( __LIST_ , __NODE_ )
 Remove macro helper for void pointer casting. More...
 

Functions

int list_destroy (LIST **list)
 Empty and Free a list container. More...
 
int list_empty (LIST *list)
 Empty a LIST list of pointers. More...
 
int list_empty_with_f (LIST *list, void(*free_fnct)(void *ptr))
 Empty a LIST list of pointers. More...
 
LIST_NODElist_node_pop (LIST *list)
 Get a LIST_NODE pointer from the end of the list. More...
 
int list_node_push (LIST *list, LIST_NODE *node)
 Add a filled node to the end of the list. More...
 
LIST_NODElist_node_shift (LIST *list)
 Get a LIST_NODE pointer from the start of the list. More...
 
int list_node_unshift (LIST *list, LIST_NODE *node)
 Add a pointer at the start of the list. More...
 
void * list_pop_f (LIST *list)
 Get a pointer from the end of the list. More...
 
int list_push (LIST *list, void *ptr, void(*destructor)(void *ptr))
 Add a pointer to the end of the list. More...
 
int list_push_sorted (LIST *list, void *ptr, int(*comparator)(const void *a, const void *b), void(*destructor)(void *ptr))
 Add a pointer sorted in the list , starting by the end of the list. More...
 
LIST_NODElist_search (LIST *list, void *ptr)
 search ptr in list More...
 
LIST_NODElist_search_with_f (LIST *list, int(*checkfunk)(void *ptr))
 search ptr in list More...
 
void * list_shift_f (LIST *list)
 Get a pointer from the start of the list. More...
 
int list_unshift (LIST *list, void *ptr, void(*destructor)(void *ptr))
 Add a pointer at the start of the list. More...
 
int list_unshift_sorted (LIST *list, void *ptr, int(*comparator)(const void *a, const void *b), void(*destructor)(void *ptr))
 Add a pointer sorted in the list , starting by the start of the list. More...
 
LISTnew_generic_list (int max_items)
 Initialiaze a generic list container to max_items pointers. More...
 
LIST_NODEnew_list_node (void *ptr, void(*destructor)(void *ptr))
 Allocate a new node to link in a list. More...
 
void * remove_list_node_f (LIST *list, LIST_NODE *node)
 Internal function called each time we need to get a node out of a list. More...
 

Detailed Description


Data Structure Documentation

◆ LIST

struct LIST

Structure of a generic LIST container.

Examples
ex_gui_dictionary.c, ex_gui_particles.c, ex_hash.c, and ex_list.c.

Definition at line 44 of file n_list.h.

+ Collaboration diagram for LIST:
Data Fields
LIST_NODE * end pointer to the end of the list
int nb_items number of item currently in the list
int nb_max_items maximum number of item in the list.

Unlimited 0 or negative

LIST_NODE * start pointer to the start of the list

◆ LIST_NODE

struct LIST_NODE

Structure of a generic list node.

Definition at line 26 of file n_list.h.

+ Collaboration diagram for LIST_NODE:

Data Fields

void(* destroy_func )(void *ptr)
 pointer to destructor function if any, else NULL More...
 
struct LIST_NODEnext
 pointer to the next node More...
 
struct LIST_NODEprev
 pointer to the previous node More...
 
void * ptr
 void pointer to store More...
 

Field Documentation

◆ destroy_func

void(* LIST_NODE::destroy_func) (void *ptr)

pointer to destructor function if any, else NULL

Definition at line 32 of file n_list.h.

◆ next

struct LIST_NODE* LIST_NODE::next

pointer to the next node

Definition at line 35 of file n_list.h.

◆ prev

struct LIST_NODE* LIST_NODE::prev

pointer to the previous node

Definition at line 37 of file n_list.h.

◆ ptr

void* LIST_NODE::ptr

void pointer to store

Definition at line 29 of file n_list.h.

Macro Definition Documentation

◆ link_node

#define link_node (   __NODE_1,
  __NODE_2 
)
Value:
do \
{ \
__NODE_2 -> prev = __NODE_1 ; \
__NODE_1 -> next = __NODE_2 ; \
} \
while( 0 )

Macro helper for linking two nodes.

Definition at line 61 of file n_list.h.

◆ list_foreach

#define list_foreach (   __ITEM_,
  __LIST_ 
)
Value:
if( !__LIST_ ) \
{ \
n_log( LOG_ERR , "Error in list_foreach, %s is NULL" , #__LIST_ ); \
} \
else \
for( LIST_NODE *__ITEM_ = __LIST_ -> start ; __ITEM_ ; __ITEM_ = __ITEM_ -> next )
Structure of a generic list node.
Definition: n_list.h:27
#define LOG_ERR
error conditions
Definition: n_log.h:58

ForEach macro helper.

Examples
ex_gui_dictionary.c, ex_hash.c, and ex_list.c.

Definition at line 70 of file n_list.h.

◆ list_pop

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

Pop macro helper for void pointer casting.

Definition at line 79 of file n_list.h.

◆ list_shift

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

Shift macro helper for void pointer casting.

Definition at line 81 of file n_list.h.

◆ remove_list_node

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

Remove macro helper for void pointer casting.

Definition at line 83 of file n_list.h.

Function Documentation

◆ list_destroy()

int list_destroy ( LIST **  list)

Empty and Free a list container.

Call the destructor each time.

Parameters
listThe list to destroy. An already empty or null list will cause an error and a _log message.
Returns
TRUE or FALSE. Check error messages in DEBUG mode.

Definition at line 603 of file n_list.c.

References __n_assert, Free, list_empty(), LOG_ERR, and n_log.

Referenced by _destroy_ht(), _ht_search(), _ht_search_trie(), delete_msg(), destroy_config_file(), destroy_threaded_pool(), ht_get_completion_list(), ht_resize(), n_kafka_delete(), n_kafka_event_destroy_ptr(), netw_close(), netw_set(), new_ht(), and userlist_destroy().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ list_empty()

int list_empty ( LIST list)

Empty a LIST list of pointers.

Parameters
listThe list to empty. An already empty or null list will cause an error and a _log message.
Returns
TRUE or FALSE. Check error messages in DEBUG mode.

Definition at line 547 of file n_list.c.

References __n_assert, Free, LOG_ERR, and n_log.

Referenced by empty_msg(), list_destroy(), netw_set(), userlist_del_user(), userlist_send_waiting_msgs(), and userlist_user_send_waiting_msgs().

+ Here is the caller graph for this function:

◆ list_empty_with_f()

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

Empty a LIST list of pointers.

Parameters
listThe list to empty. An already empty or null list will cause an error and a _log message.
free_fncta function ptr to a void func( void *ptr )
Returns
TRUE or FALSE. Check error messages in DEBUG mode.

Definition at line 577 of file n_list.c.

References __n_assert, Free, LOG_ERR, and n_log.

◆ list_node_pop()

LIST_NODE * list_node_pop ( LIST list)

Get a LIST_NODE pointer from the end of the list.

Parameters
listAn initilized list container. A null value will cause an error and a _log message.
Returns
A LIST_NODE *node or NULL. Check error messages in DEBUG mode.

Definition at line 150 of file n_list.c.

References __n_assert, LOG_ERR, and n_log.

◆ list_node_push()

int list_node_push ( LIST list,
LIST_NODE node 
)

Add a filled node to the end of the list.

Parameters
listAn initilized list container. A null value will cause an error and a _log message.
nodeThe node pointer you want to put in the list. A null value will cause an error and a _log message.
Returns
TRUE or FALSE

Definition at line 120 of file n_list.c.

References __n_assert, LOG_ERR, and n_log.

Referenced by ht_resize().

+ Here is the caller graph for this function:

◆ list_node_shift()

LIST_NODE * list_node_shift ( LIST list)

Get a LIST_NODE pointer from the start of the list.

Parameters
listAn initilized list container. A null value will cause an error and a _log message.
Returns
A LIST_NODE *node or NULL. Check error messages in DEBUG mode.

Definition at line 179 of file n_list.c.

References __n_assert, LOG_ERR, and n_log.

Referenced by ht_resize().

+ Here is the caller graph for this function:

◆ list_node_unshift()

int list_node_unshift ( LIST list,
LIST_NODE node 
)

Add a pointer at the start of the list.

Parameters
listAn initilized list container. A null value will cause an error and a _log message.
nodeThe node pointer you wan to put in the list. A null value will cause an error and a _log message.
Returns
TRUE or FALSE

Definition at line 212 of file n_list.c.

References __n_assert, link_node, LOG_ERR, and n_log.

◆ list_pop_f()

void * list_pop_f ( LIST list)

Get a pointer from the end of the list.

Parameters
listAn initilized list container. A null value will cause an error and a _log message.
Returns
A void *ptr or NULL. Check error messages in DEBUG mode.

Definition at line 437 of file n_list.c.

References __n_assert, Free, LOG_ERR, and n_log.

◆ list_push()

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

Add a pointer to the end of the list.

Parameters
listAn initilized list container. A null value will cause an error and a _log message.
ptrThe pointer you wan to put in the list. A null value will cause an error and a _log message.
destructorPointer to the ptr type destructor function. Leave to NULL if there isn't.
Returns
TRUE or FALSE. Check error messages in DEBUG mode.

Definition at line 244 of file n_list.c.

References __n_assert, LOG_ERR, n_log, and new_list_node().

Referenced by _ht_depth_first_search(), _ht_put_double(), _ht_put_int(), _ht_put_ptr(), _ht_put_string(), _ht_put_string_ptr(), _ht_search(), _ht_search_trie_helper(), add_int_to_msg(), add_nb_to_msg(), add_nstrdup_to_msg(), add_nstrptr_to_msg(), add_particle(), add_strdup_to_msg(), add_threaded_process(), ht_get_completion_list(), ht_put_ptr_ex(), list_push_sorted(), list_unshift_sorted(), load_config_file(), n_kafka_poll(), n_kafka_produce(), netw_add_msg(), netw_add_msg_ex(), netw_pool_add(), netw_recv_func(), new_n_fluid(), scan_dir_ex(), and userlist_user_add_waiting_msg().

+ Here is the call graph for this function:
+ Here is the caller graph for this 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 in the list , starting by the end of the list.

Parameters
listAn initilized list container. A null value will cause an error and a _log message.
ptrThe pointer you wan to put in the list. A null value will cause an error and a _log message.
comparatorA pointer to a function which take two void * pointers and return an int.
destructorPointer to the ptr type destructor function. Leave to NULL if there isn't.
Returns
TRUE or FALSE. Check error messages in DEBUG mode.

Definition at line 285 of file n_list.c.

References __n_assert, link_node, list_push(), list_unshift(), LOG_ERR, n_log, and new_list_node().

Referenced by n_scan_dir().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ list_search()

LIST_NODE * list_search ( LIST list,
void *  ptr 
)

search ptr in list

Parameters
listThe target list
ptrSearched pointer
Returns
The LIST_NODE *node or NULL

Definition at line 508 of file n_list.c.

References __n_assert, and list_foreach.

Referenced by netw_pool_remove().

+ Here is the caller graph for this function:

◆ list_search_with_f()

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

search ptr in list

Parameters
listThe target list
checkfunkFunction pointer who should return 1 if it matches or 0 if it's not
Returns
The LIST_NODE *node or NULL

Definition at line 528 of file n_list.c.

References __n_assert, and list_foreach.

◆ list_shift_f()

void * list_shift_f ( LIST list)

Get a pointer from the start of the list.

Parameters
listAn initilized list container. A null value will cause an error and a _log message.
Returns
A void *ptr or NULL. Check error messages in DEBUG mode.

Definition at line 472 of file n_list.c.

References __n_assert, Free, LOG_ERR, and n_log.

◆ list_unshift()

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

Add a pointer at the start of the list.

Parameters
listAn initilized list container. A null value will cause an error and a _log message.
ptrThe pointer you wan to put in the list. A null value will cause an error and a _log message.
destructorPointer to the ptr type destructor function. Leave to NULL if there isn't.
Returns
A void *ptr or NULL. Check error messages in DEBUG mode.

Definition at line 342 of file n_list.c.

References __n_assert, link_node, LOG_ERR, n_log, and new_list_node().

Referenced by list_push_sorted(), and list_unshift_sorted().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ list_unshift_sorted()

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

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

Parameters
listAn initilized list container. A null value will cause an error and a _log message.
ptrThe pointer you wan to put in the list. A null value will cause an error and a _log message.
comparatorA pointer to a function which take two void * pointers and return an int.
destructorPointer to the ptr type destructor function. Leave to NULL if there isn't.
Returns
TRUE or FALSE. Check error messages in DEBUG mode.

Definition at line 382 of file n_list.c.

References __n_assert, link_node, list_push(), list_unshift(), LOG_ERR, n_log, and new_list_node().

+ Here is the call graph for this function:

◆ new_generic_list()

LIST * new_generic_list ( int  max_items)

Initialiaze a generic list container to max_items pointers.

Parameters
max_itemsSpecify a max size for the list container, 0 or negative for unlimited lists.
Returns
a new LIST or NULL

Definition at line 20 of file n_list.c.

References __n_assert, and Malloc.

Referenced by _ht_search(), _ht_search_trie(), create_msg(), ht_get_completion_list(), ht_resize(), init_particle_system(), load_config_file(), n_kafka_new(), n_kafka_poll(), netw_new(), new_ht(), new_n_fluid(), new_thread_pool(), and userlist_new().

+ Here is the caller graph for this function:

◆ new_list_node()

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

Allocate a new node to link in a list.

Parameters
ptrThe pointer you want to put in the node.
destructorPointer to the ptr type destructor function. Leave to NULL if there isn't.
Returns
A new node or NULL on error. Check error messages in DEBUG mode.

Definition at line 46 of file n_list.c.

References __n_assert, LOG_ERR, Malloc, and n_log.

Referenced by list_push(), list_push_sorted(), list_unshift(), and list_unshift_sorted().

+ Here is the caller graph for this function:

◆ remove_list_node_f()

void * remove_list_node_f ( LIST list,
LIST_NODE node 
)

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

Parameters
listThe list to pick in
nodeThe node to remove in the list
Returns
The node holded pointer or NULL

Definition at line 68 of file n_list.c.

References __n_assert, end, Free, LOG_ERR, n_log, and start.