Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_list.h
Go to the documentation of this file.
1
9#ifndef N_GENERIC_LIST
10#define N_GENERIC_LIST
11
12#ifdef __cplusplus
13extern "C" {
14#endif
15
21#include "stdio.h"
22#include "stdlib.h"
23
25typedef struct LIST_NODE {
27 void* ptr;
28
30 void (*destroy_func)(void* ptr);
31
33 struct LIST_NODE* next;
35 struct LIST_NODE* prev;
36
37} LIST_NODE;
38
40typedef struct LIST {
42 size_t nb_items;
45
50
51} LIST;
52
54#define UNLIMITED_LIST_ITEMS 0
56#define MAX_LIST_ITEMS SIZE_MAX
57
59#define link_node(__NODE_1, __NODE_2) \
60 do { \
61 __NODE_2->prev = __NODE_1; \
62 __NODE_1->next = __NODE_2; \
63 } while (0)
64
66#define list_foreach(__ITEM_, __LIST_) \
67 if (!__LIST_) { \
68 n_log(LOG_ERR, "Error in list_foreach, %s is NULL", #__LIST_); \
69 } else \
70 for (LIST_NODE* __ITEM_ = __LIST_->start; __ITEM_; __ITEM_ = __ITEM_->next)
71
73#define list_pop(__LIST_, __TYPE_) (__TYPE_*)list_pop_f(__LIST_)
75#define list_shift(__LIST_, __TYPE_) (__TYPE_*)list_shift_f(__LIST_, __FILE__, __LINE__)
77#define remove_list_node(__LIST_, __NODE_, __TYPE_) (__TYPE_*)remove_list_node_f(__LIST_, __NODE_)
78
79/* initialize a list */
80LIST* new_generic_list(size_t max_items);
81/* create a new node */
82LIST_NODE* new_list_node(void* ptr, void (*destructor)(void* ptr));
83/* remove a node */
84void* remove_list_node_f(LIST* list, LIST_NODE* node);
85int list_node_push(LIST* list, LIST_NODE* node);
88int list_node_unshift(LIST* list, LIST_NODE* node);
89
90/* add a pointer at the end of the list */
91int list_push(LIST* list, void* ptr, void (*destructor)(void* ptr));
92/* add a pointer sorted via comparator from the end of the list */
93int list_push_sorted(LIST* list, void* ptr, int (*comparator)(const void* a, const void* b), void (*destructor)(void* ptr));
94/* put a pointer at the beginning of list */
95int list_unshift(LIST* list, void* ptr, void (*destructor)(void* ptr));
96/* put a point pointer sorted via comparator from the start to the end */
97int list_unshift_sorted(LIST* list, void* ptr, int (*comparator)(const void* a, const void* b), void (*destructor)(void* ptr));
98
99/* get last ptr from list */
100void* list_pop_f(LIST* list);
101/* get first ptr from list */
102void* list_shift_f(LIST* list, char* file, size_t line);
103
104/* search ptr in list */
105LIST_NODE* list_search(LIST* list, void* ptr);
106/* search for data in list */
107LIST_NODE* list_search_with_f(LIST* list, int (*checkfunk)(void* ptr));
108
109/* empty the list */
110int list_empty(LIST* list);
111int list_empty_with_f(LIST* list, void (*free_fnct)(void* ptr));
112
113/* free the list */
114int list_destroy(LIST** list);
115
120#ifdef __cplusplus
121}
122#endif
123
124#endif
LIST_NODE * end
pointer to the end of the list
Definition n_list.h:49
void * ptr
void pointer to store
Definition n_list.h:27
size_t nb_max_items
maximum number of item in the list.
Definition n_list.h:44
struct LIST_NODE * prev
pointer to the previous node
Definition n_list.h:35
LIST_NODE * start
pointer to the start of the list
Definition n_list.h:47
size_t nb_items
number of item currently in the list
Definition n_list.h:42
void(* destroy_func)(void *ptr)
pointer to destructor function if any, else NULL
Definition n_list.h:30
struct LIST_NODE * next
pointer to the next node
Definition n_list.h:33
void * list_pop_f(LIST *list)
Get a pointer from the end of the list.
Definition n_list.c:375
void * list_shift_f(LIST *list, char *file, size_t line)
Get a pointer from the start of the list.
Definition n_list.c:408
int list_empty(LIST *list)
Empty a LIST list of pointers.
Definition n_list.c:472
int list_push(LIST *list, void *ptr, void(*destructor)(void *ptr))
Add a pointer to the end of the list.
Definition n_list.c:200
int list_node_unshift(LIST *list, LIST_NODE *node)
Add a pointer at the start of the list.
Definition n_list.c:174
int list_unshift(LIST *list, void *ptr, void(*destructor)(void *ptr))
Add a pointer at the start of the list.
Definition n_list.c:289
LIST_NODE * list_node_shift(LIST *list)
Get a LIST_NODE pointer from the start of the list.
Definition n_list.c:146
LIST_NODE * new_list_node(void *ptr, void(*destructor)(void *ptr))
Allocate a new node to link in a list.
Definition n_list.c:39
int list_destroy(LIST **list)
Empty and Free a list container.
Definition n_list.c:519
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:322
LIST_NODE * list_node_pop(LIST *list)
Get a LIST_NODE pointer from the end of the list.
Definition n_list.c:121
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:58
LIST_NODE * list_search(LIST *list, void *ptr)
search ptr in list
Definition n_list.c:441
LIST * new_generic_list(size_t max_items)
Initialiaze a generic list container to max_items pointers.
Definition n_list.c:19
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:234
int list_empty_with_f(LIST *list, void(*free_fnct)(void *ptr))
Empty a LIST list of pointers.
Definition n_list.c:497
LIST_NODE * list_search_with_f(LIST *list, int(*checkfunk)(void *ptr))
search ptr in list
Definition n_list.c:457
int list_node_push(LIST *list, LIST_NODE *node)
Add a filled node to the end of the list.
Definition n_list.c:97
Structure of a generic LIST container.
Definition n_list.h:40
Structure of a generic list node.
Definition n_list.h:25