Nilorea Library
C utilities for networking, threading, graphics
n_list.h
Go to the documentation of this file.
1
8#ifndef N_GENERIC_LIST
9#define N_GENERIC_LIST
10
11#ifdef __cplusplus
12extern "C"
13{
14#endif
15
21#include "stdio.h"
22#include "stdlib.h"
23
24
26typedef struct LIST_NODE
27{
29 void *ptr ;
30
32 void (*destroy_func)(void *ptr );
33
35 struct LIST_NODE *next ;
37 struct LIST_NODE *prev ;
38
39} LIST_NODE ;
40
41
42
44typedef struct LIST
45{
50
55
56} LIST ;
57
58
59
61#define link_node( __NODE_1 , __NODE_2 ) \
62 do \
63 { \
64 __NODE_2 -> prev = __NODE_1 ; \
65 __NODE_1 -> next = __NODE_2 ; \
66 } \
67 while( 0 )
68
70#define list_foreach( __ITEM_ , __LIST_ ) \
71 if( !__LIST_ ) \
72 { \
73 n_log( LOG_ERR , "Error in list_foreach, %s is NULL" , #__LIST_ ); \
74 } \
75 else \
76 for( LIST_NODE *__ITEM_ = __LIST_ -> start ; __ITEM_ ; __ITEM_ = __ITEM_ -> next )
77
79#define list_pop( __LIST_ , __TYPE_ ) ( __TYPE_ *)list_pop_f( __LIST_ )
81#define list_shift( __LIST_ , __TYPE_ ) (__TYPE_ *)list_shift_f( __LIST_ )
83#define remove_list_node( __LIST_ ,__NODE_ , __TYPE_ ) (__TYPE_ *)remove_list_node_f( __LIST_ , __NODE_ )
84
85/* initialize a list */
86LIST *new_generic_list( int max_items );
87/* create a new node */
88LIST_NODE *new_list_node( void *ptr, void (*destructor)( void *ptr ) );
89/* remove a node */
90void *remove_list_node_f( LIST *list, LIST_NODE *node );
91int list_node_push( LIST *list, LIST_NODE *node );
94int list_node_unshift( LIST *list, LIST_NODE *node );
95
96/* add a pointer at the end of the list */
97int list_push( LIST *list, void *ptr, void (*destructor)( void *ptr ) ) ;
98/* add a pointer sorted via comparator from the end of the list */
99int list_push_sorted( LIST *list, void *ptr, int (*comparator)( const void *a, const void *b ), void (*destructor)( void *ptr ) );
100/* put a pointer at the beginning of list */
101int list_unshift( LIST *list, void *ptr, void (*destructor)( void *ptr ) );
102/* put a point pointer sorted via comparator from the start to the end */
103int list_unshift_sorted( LIST *list, void *ptr, int (*comparator)( const void *a, const void *b ), void (*destructor)( void *ptr ) );
104
105/* get last ptr from list */
106void *list_pop_f( LIST *list ) ;
107/* get first ptr from list */
108void *list_shift_f( LIST *list );
109
110/* search ptr in list */
111LIST_NODE *list_search( LIST *list, void *ptr );
112/* search for data in list */
113LIST_NODE *list_search_with_f( LIST *list, int (*checkfunk)( void *ptr ) );
114
115/* empty the list */
116int list_empty( LIST *list );
117int list_empty_with_f( LIST *list, void (*free_fnct)( void *ptr ) );
118
119/* free the list */
120int list_destroy( LIST **list );
121
126#ifdef __cplusplus
127}
128#endif
129
130#endif
int nb_items
number of item currently in the list
Definition: n_list.h:47
LIST_NODE * end
pointer to the end of the list
Definition: n_list.h:54
void * ptr
void pointer to store
Definition: n_list.h:29
struct LIST_NODE * prev
pointer to the previous node
Definition: n_list.h:37
LIST_NODE * start
pointer to the start of the list
Definition: n_list.h:52
void(* destroy_func)(void *ptr)
pointer to destructor function if any, else NULL
Definition: n_list.h:32
struct LIST_NODE * next
pointer to the next node
Definition: n_list.h:35
int nb_max_items
maximum number of item in the list.
Definition: n_list.h:49
void * list_pop_f(LIST *list)
Get a pointer from the end of the list.
Definition: n_list.c:437
int list_empty(LIST *list)
Empty a LIST list of pointers.
Definition: n_list.c:547
int list_push(LIST *list, void *ptr, void(*destructor)(void *ptr))
Add a pointer to the end of the list.
Definition: n_list.c:244
int list_node_unshift(LIST *list, LIST_NODE *node)
Add a pointer at the start of the list.
Definition: n_list.c:212
int list_unshift(LIST *list, void *ptr, void(*destructor)(void *ptr))
Add a pointer at the start of the list.
Definition: n_list.c:342
LIST_NODE * list_node_shift(LIST *list)
Get a LIST_NODE pointer from the start of the list.
Definition: n_list.c:179
LIST_NODE * new_list_node(void *ptr, void(*destructor)(void *ptr))
Allocate a new node to link in a list.
Definition: n_list.c:46
int list_destroy(LIST **list)
Empty and Free a list container.
Definition: n_list.c:603
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.
Definition: n_list.c:382
LIST_NODE * list_node_pop(LIST *list)
Get a LIST_NODE pointer from the end of the list.
Definition: n_list.c:150
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.
Definition: n_list.c:68
void * list_shift_f(LIST *list)
Get a pointer from the start of the list.
Definition: n_list.c:472
LIST * new_generic_list(int max_items)
Initialiaze a generic list container to max_items pointers.
Definition: n_list.c:20
LIST_NODE * list_search(LIST *list, void *ptr)
search ptr in list
Definition: n_list.c:508
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.
Definition: n_list.c:285
int list_empty_with_f(LIST *list, void(*free_fnct)(void *ptr))
Empty a LIST list of pointers.
Definition: n_list.c:577
LIST_NODE * list_search_with_f(LIST *list, int(*checkfunk)(void *ptr))
search ptr in list
Definition: n_list.c:528
int list_node_push(LIST *list, LIST_NODE *node)
Add a filled node to the end of the list.
Definition: n_list.c:120
Structure of a generic LIST container.
Definition: n_list.h:45
Structure of a generic list node.
Definition: n_list.h:27