Nilorea Library

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Macros Groups
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
12 extern "C"
13 {
14 #endif
15
21 #include "stdio.h"
22 #include "stdlib.h"
23 #include "n_log.h"
24 #include "n_common.h"
25
27 typedef struct LIST_NODE
28 {
30 void *ptr ;
31
33 void (*destroy_func)(void *ptr );
34
36 struct LIST_NODE *next ;
38 struct LIST_NODE *prev ;
39
40 } LIST_NODE ;
41
42
43
45 typedef struct LIST
46 {
48 int nb_items ;
50 int nb_max_items ;
51
53 LIST_NODE *start ;
55 LIST_NODE *end ;
56
57 } LIST ;
58
59
60
62 #define link_node( __NODE_1 , __NODE_2 ) \
63 do \
64 { \
65 __NODE_2 -> prev = __NODE_1 ; \
66 __NODE_1 -> next = __NODE_2 ; \
67 } \
68 while( 0 )
69
71 #define list_foreach( __ITEM_ , __LIST_ ) \
72 if( !__LIST_ ) \
73 { \
74 n_log( LOG_ERR , "Error in list_foreach, %s is NULL" , #__LIST_ ); \
75 } \
76 else \
77 for( LIST_NODE *__ITEM_ = __LIST_ -> start ; __ITEM_ != NULL; __ITEM_ = __ITEM_ -> next ) \
78
79
80 #define list_pop( __LIST_ , __TYPE_ ) ( __TYPE_ *)list_pop_f( __LIST_ )
81
82 #define list_shift( __LIST_ , __TYPE_ ) (__TYPE_ *)list_shift_f( __LIST_ )
83
84 #define remove_list_node( __LIST_ ,__NODE_ , __TYPE_ ) (__TYPE_ *)remove_list_node_f( __LIST_ , __NODE_ )
85
86 /* initialize a list */
87 LIST *new_generic_list( int max_items );
88 /* create a new node */
89 LIST_NODE *new_list_node( void *ptr , void (*destructor)( void *ptr ) );
90 /* remove a node */
91 void *remove_list_node_f( LIST *list , LIST_NODE *node );
92
93 /* add a pointer at the end of the list */
94 int list_push( LIST *list , void *ptr , void (*destructor)( void *ptr ) ) ;
95 /* add a pointer sorted via comparator from the end of the list */
96 int list_push_sorted( LIST *list , void *ptr , int (*comparator)( const void *a , const void *b ) , void (*destructor)( void *ptr ) );
97 /* put a pointer at the beginning of list */
98 int list_unshift( LIST *list , void *ptr , void (*destructor)( void *ptr ) );
99 /* put a point pointer sorted via comparator from the start to the end */
100 int list_unshift_sorted( LIST *list , void *ptr , int (*comparator)( void *a , void *b ) , void (*destructor)( void *ptr ) );
101
102 /* get last ptr from list */
103 void *list_pop_f( LIST *list ) ;
104 /* get first ptr from list */
105 void *list_shift_f( LIST *list );
106
107 /* empty the list */
108 int list_empty( LIST *list );
109 int list_empty_with_f( LIST *list , void (*free_fnct)( void *ptr ) );
110 /* free the list */
111 int list_destroy( LIST **list );
112
115 #ifdef __cplusplus
116 }
117 #endif
118
119 #endif