cu
list.h
Go to the documentation of this file.
1 #ifndef CU_LIST_H_
2 #define CU_LIST_H_
3 
4 #include <stdbool.h>
5 #include <stdlib.h>
6 
15 typedef struct listnode_t {
17  void *data;
19  struct listnode_t *next;
21  struct listnode_t *prev;
22 } listnode_t;
23 
27 typedef struct {
33  size_t size;
34 } list_t;
35 
42 #define LIST_FOR(IT, L) struct listnode_t *IT = NULL; for (IT = L->head; \
43  IT != NULL; \
44  IT = IT->next)
45 
49 #define LIST_INIT {.head = NULL, \
50  .tail = NULL, \
51  .size = 0}
52 
59 #define LIST_DEL(L, FN) list_clear(&L, FN); struct listnode_t *IT = NULL; \
60  for (IT = L.head; IT != NULL; IT = IT->next) { \
61  free(IT->prev); \
62  } \
63  free(L.tail);
64 
65 
70 extern list_t *list_new();
71 
72 
79 extern void *list_clone(const list_t *li, void *(*clone)(void *));
80 
81 
87 extern void list_clear(const list_t *li, void (*df)(void *));
88 
89 
94 extern void list_del(list_t *li);
95 
96 
102 extern void list_clear_del(list_t *li, void (*df)(void *));
103 
104 
110 extern void list_push_back(list_t *li, void *data);
111 
117 extern void list_push_front(list_t *li, void *data);
118 
125 extern void list_insert(list_t *li, const size_t index, void *data);
126 
132 extern void list_append(list_t *li, list_t *other);
133 
139 extern bool list_is_empty(const list_t *li);
140 
141 
147 extern size_t list_len(const list_t *li);
148 
149 
155 extern void list_foreach(const list_t *li, void (*f)(void *));
156 
157 
165 extern void *list_filter(const list_t *li,
166  const void *key,
167  bool (*pred)(const void *,
168  const void *));
169 
170 
178 extern void *list_find(const list_t *li,
179  const void *key,
180  bool (*pred)(const void *,
181  const void *));
189 extern bool list_contains(list_t *li,
190  const void *key,
191  int (*pred)(const void *, const void *));
192 
193 #endif /* CU_LIST_H_ */
Holds the reference to the head and tail of the list.
Definition: list.h:27
void * list_clone(const list_t *li, void *(*clone)(void *))
Clone given list.
void list_push_back(list_t *li, void *data)
Append an element to the list.
void * list_filter(const list_t *li, const void *key, bool(*pred)(const void *, const void *))
Filter elements by given predicate.
void list_foreach(const list_t *li, void(*f)(void *))
Apply a function to every element of the list.
struct listnode_t * prev
Definition: list.h:21
struct listnode_t * next
Definition: list.h:19
void list_push_front(list_t *li, void *data)
Prepend an element to the list.
void list_insert(list_t *li, const size_t index, void *data)
Definition: list.h:15
listnode_t * tail
Definition: list.h:31
bool list_contains(list_t *li, const void *key, int(*pred)(const void *, const void *))
void list_del(list_t *li)
Free the memory allocated.
void list_append(list_t *li, list_t *other)
Append another list_t to the current one.
list_t * list_new()
Create a new list.
void list_clear(const list_t *li, void(*df)(void *))
Remove all elements from the list.
listnode_t * head
Definition: list.h:29
size_t size
Definition: list.h:33
bool list_is_empty(const list_t *li)
Check whether the list is empty.
void * data
Definition: list.h:17
void list_clear_del(list_t *li, void(*df)(void *))
Remove all elements from the list and free it.
void * list_find(const list_t *li, const void *key, bool(*pred)(const void *, const void *))
Find a element in the list.
size_t list_len(const list_t *li)
Retrieve the length of a list.