Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_list.h
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
27#ifndef N_GENERIC_LIST
28#define N_GENERIC_LIST
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
39#include <stdio.h>
40#include <stdlib.h>
41
43typedef struct LIST_NODE {
45 void* ptr;
46
48 void (*destroy_func)(void* ptr);
49
51 struct LIST_NODE* next;
53 struct LIST_NODE* prev;
54
55} LIST_NODE;
56
58typedef struct LIST {
60 size_t nb_items;
63
68
69} LIST;
70
72#define UNLIMITED_LIST_ITEMS 0
74#define MAX_LIST_ITEMS SIZE_MAX
75
77#define link_node(__NODE_1, __NODE_2) \
78 do { \
79 if (!(__NODE_1) || !(__NODE_2)) { \
80 n_log(LOG_ERR, "link_node: NULL argument (%s=%p, %s=%p)", #__NODE_1, (void*)(__NODE_1), #__NODE_2, (void*)(__NODE_2)); \
81 } else { \
82 (__NODE_2)->prev = (__NODE_1); \
83 (__NODE_1)->next = (__NODE_2); \
84 } \
85 } while (0)
86
88#define list_foreach(__ITEM_, __LIST_) \
89 for (LIST_NODE* __ITEM_ = (__LIST_) ? (__LIST_)->start : NULL, *__next_##__ITEM_ = __ITEM_ ? __ITEM_->next : NULL; __ITEM_; __ITEM_ = __next_##__ITEM_, \
90 __next_##__ITEM_ = __ITEM_ ? __ITEM_->next : NULL)
91
93#define list_pop(__LIST_, __TYPE_) (__TYPE_*)list_pop_f(__LIST_)
95#define list_shift(__LIST_, __TYPE_) (__TYPE_*)list_shift_f(__LIST_, __FILE__, __LINE__)
97#define remove_list_node(__LIST_, __NODE_, __TYPE_) (__TYPE_*)remove_list_node_f(__LIST_, __NODE_)
98
100LIST* new_generic_list(size_t max_items);
102LIST_NODE* new_list_node(void* ptr, void (*destructor)(void* ptr));
104void* remove_list_node_f(LIST* list, LIST_NODE* node);
106int list_node_push(LIST* list, LIST_NODE* node);
112int list_node_unshift(LIST* list, LIST_NODE* node);
113
115int list_push(LIST* list, void* ptr, void (*destructor)(void* ptr));
117int list_push_sorted(LIST* list, void* ptr, int (*comparator)(const void* a, const void* b), void (*destructor)(void* ptr));
119int list_unshift(LIST* list, void* ptr, void (*destructor)(void* ptr));
121int list_unshift_sorted(LIST* list, void* ptr, int (*comparator)(const void* a, const void* b), void (*destructor)(void* ptr));
122
124void* list_pop_f(LIST* list);
126void* list_shift_f(LIST* list, char* file, size_t line);
127
129LIST_NODE* list_search(LIST* list, const void* ptr);
131LIST_NODE* list_search_with_f(LIST* list, int (*checkfunk)(void* ptr));
132
134int list_empty(LIST* list);
136int list_empty_with_f(LIST* list, void (*free_fnct)(void* ptr));
137
139int list_destroy(LIST** list);
140
145#ifdef __cplusplus
146}
147#endif
148
149#endif
LIST_NODE * end
pointer to the end of the list
Definition n_list.h:67
void * ptr
void pointer to store
Definition n_list.h:45
size_t nb_max_items
Maximum number of items in the list.
Definition n_list.h:62
struct LIST_NODE * prev
pointer to the previous node
Definition n_list.h:53
LIST_NODE * start
pointer to the start of the list
Definition n_list.h:65
size_t nb_items
number of item currently in the list
Definition n_list.h:60
void(* destroy_func)(void *ptr)
pointer to destructor function if any, else NULL
Definition n_list.h:48
struct LIST_NODE * next
pointer to the next node
Definition n_list.h:51
void * list_pop_f(LIST *list)
get last ptr from list
Definition n_list.c:402
void * list_shift_f(LIST *list, char *file, size_t line)
get first ptr from list
Definition n_list.c:435
int list_empty(LIST *list)
empty the list
Definition n_list.c:500
LIST_NODE * list_search(LIST *list, const void *ptr)
search ptr in list
Definition n_list.c:468
int list_push(LIST *list, void *ptr, void(*destructor)(void *ptr))
add a pointer at the end of the list
Definition n_list.c:227
int list_node_unshift(LIST *list, LIST_NODE *node)
unshift a node at the beginning of the list
Definition n_list.c:199
int list_unshift(LIST *list, void *ptr, void(*destructor)(void *ptr))
put a pointer at the beginning of list
Definition n_list.c:316
LIST_NODE * list_node_shift(LIST *list)
shift a node from the beginning of the list
Definition n_list.c:169
LIST_NODE * new_list_node(void *ptr, void(*destructor)(void *ptr))
create a new node
Definition n_list.c:56
int list_destroy(LIST **list)
free the list
Definition n_list.c:547
int list_unshift_sorted(LIST *list, void *ptr, int(*comparator)(const void *a, const void *b), void(*destructor)(void *ptr))
put a pointer sorted via comparator from the start to the end
Definition n_list.c:348
LIST_NODE * list_node_pop(LIST *list)
pop a node from the end of the list
Definition n_list.c:142
void * remove_list_node_f(LIST *list, LIST_NODE *node)
remove a node
Definition n_list.c:75
LIST * new_generic_list(size_t max_items)
initialize a list
Definition n_list.c:36
int list_push_sorted(LIST *list, void *ptr, int(*comparator)(const void *a, const void *b), void(*destructor)(void *ptr))
add a pointer sorted via comparator from the end of the list
Definition n_list.c:260
int list_empty_with_f(LIST *list, void(*free_fnct)(void *ptr))
empty the list with a custom free function
Definition n_list.c:525
LIST_NODE * list_search_with_f(LIST *list, int(*checkfunk)(void *ptr))
search for data in list
Definition n_list.c:484
int list_node_push(LIST *list, LIST_NODE *node)
push a node at the end of the list
Definition n_list.c:116
Structure of a generic LIST container.
Definition n_list.h:58
Structure of a generic list node.
Definition n_list.h:43