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 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
28#ifndef N_GENERIC_LIST
29#define N_GENERIC_LIST
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
40#include <stdio.h>
41#include <stdlib.h>
42
44typedef struct LIST_NODE {
46 void* ptr;
47
49 void (*destroy_func)(void* ptr);
50
52 struct LIST_NODE* next;
54 struct LIST_NODE* prev;
55
56} LIST_NODE;
57
59typedef struct LIST {
61 size_t nb_items;
64
69
70} LIST;
71
73#define UNLIMITED_LIST_ITEMS 0
75#define MAX_LIST_ITEMS SIZE_MAX
76
78#define link_node(__NODE_1, __NODE_2) \
79 do { \
80 if (!(__NODE_1) || !(__NODE_2)) { \
81 n_log(LOG_ERR, "link_node: NULL argument (%s=%p, %s=%p)", #__NODE_1, (void*)(__NODE_1), #__NODE_2, (void*)(__NODE_2)); \
82 } else { \
83 (__NODE_2)->prev = (__NODE_1); \
84 (__NODE_1)->next = (__NODE_2); \
85 } \
86 } while (0)
87
89#define list_foreach(__ITEM_, __LIST_) \
90 for (LIST_NODE* __ITEM_ = (__LIST_) ? (__LIST_)->start : NULL, *__next_##__ITEM_ = __ITEM_ ? __ITEM_->next : NULL; __ITEM_; __ITEM_ = __next_##__ITEM_, \
91 __next_##__ITEM_ = __ITEM_ ? __ITEM_->next : NULL)
92
94#define list_pop(__LIST_, __TYPE_) (__TYPE_*)list_pop_f(__LIST_)
96#define list_shift(__LIST_, __TYPE_) (__TYPE_*)list_shift_f(__LIST_, __FILE__, __LINE__)
98#define remove_list_node(__LIST_, __NODE_, __TYPE_) (__TYPE_*)remove_list_node_f(__LIST_, __NODE_)
99
101LIST* new_generic_list(size_t max_items);
103LIST_NODE* new_list_node(void* ptr, void (*destructor)(void* ptr));
105void* remove_list_node_f(LIST* list, LIST_NODE* node);
107int list_node_push(LIST* list, LIST_NODE* node);
113int list_node_unshift(LIST* list, LIST_NODE* node);
114
116int list_push(LIST* list, void* ptr, void (*destructor)(void* ptr));
118int list_push_sorted(LIST* list, void* ptr, int (*comparator)(const void* a, const void* b), void (*destructor)(void* ptr));
120int list_unshift(LIST* list, void* ptr, void (*destructor)(void* ptr));
122int list_unshift_sorted(LIST* list, void* ptr, int (*comparator)(const void* a, const void* b), void (*destructor)(void* ptr));
123
125void* list_pop_f(LIST* list);
127void* list_shift_f(LIST* list, char* file, size_t line);
128
130LIST_NODE* list_search(LIST* list, const void* ptr);
132LIST_NODE* list_search_with_f(LIST* list, int (*checkfunk)(void* ptr));
133
135int list_empty(LIST* list);
137int list_empty_with_f(LIST* list, void (*free_fnct)(void* ptr));
138
140int list_destroy(LIST** list);
141
146#ifdef __cplusplus
147}
148#endif
149
150#endif
LIST_NODE * end
pointer to the end of the list
Definition n_list.h:68
void * ptr
void pointer to store
Definition n_list.h:46
size_t nb_max_items
Maximum number of items in the list.
Definition n_list.h:63
struct LIST_NODE * prev
pointer to the previous node
Definition n_list.h:54
LIST_NODE * start
pointer to the start of the list
Definition n_list.h:66
size_t nb_items
number of item currently in the list
Definition n_list.h:61
void(* destroy_func)(void *ptr)
pointer to destructor function if any, else NULL
Definition n_list.h:49
struct LIST_NODE * next
pointer to the next node
Definition n_list.h:52
void * list_pop_f(LIST *list)
get last ptr from list
Definition n_list.c:403
void * list_shift_f(LIST *list, char *file, size_t line)
get first ptr from list
Definition n_list.c:436
int list_empty(LIST *list)
empty the list
Definition n_list.c:501
LIST_NODE * list_search(LIST *list, const void *ptr)
search ptr in list
Definition n_list.c:469
int list_push(LIST *list, void *ptr, void(*destructor)(void *ptr))
add a pointer at the end of the list
Definition n_list.c:228
int list_node_unshift(LIST *list, LIST_NODE *node)
unshift a node at the beginning of the list
Definition n_list.c:200
int list_unshift(LIST *list, void *ptr, void(*destructor)(void *ptr))
put a pointer at the beginning of list
Definition n_list.c:317
LIST_NODE * list_node_shift(LIST *list)
shift a node from the beginning of the list
Definition n_list.c:170
LIST_NODE * new_list_node(void *ptr, void(*destructor)(void *ptr))
create a new node
Definition n_list.c:57
int list_destroy(LIST **list)
free the list
Definition n_list.c:548
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:349
LIST_NODE * list_node_pop(LIST *list)
pop a node from the end of the list
Definition n_list.c:143
void * remove_list_node_f(LIST *list, LIST_NODE *node)
remove a node
Definition n_list.c:76
LIST * new_generic_list(size_t max_items)
initialize a list
Definition n_list.c:37
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:261
int list_empty_with_f(LIST *list, void(*free_fnct)(void *ptr))
empty the list with a custom free function
Definition n_list.c:526
LIST_NODE * list_search_with_f(LIST *list, int(*checkfunk)(void *ptr))
search for data in list
Definition n_list.c:485
int list_node_push(LIST *list, LIST_NODE *node)
push a node at the end of the list
Definition n_list.c:117
Structure of a generic LIST container.
Definition n_list.h:59
Structure of a generic list node.
Definition n_list.h:44