-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.h
230 lines (200 loc) · 5.17 KB
/
list.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/***************************************************************************
begin........: June 2012
copyright....: Sebastian Fedrau
email........: [email protected]
***************************************************************************/
/***************************************************************************
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License v3 as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License v3 for more details.
***************************************************************************/
/**
* \file list.h
* \brief Doubly-linked list.
* \author Sebastian Fedrau <[email protected]>
*/
#ifndef LIST_H
#define LIST_H
#include <stdint.h>
#include <stdbool.h>
#include "datatypes.h"
#include "pool.h"
/**
* \struct ListItem
* \brief Structure holding a list item.
*/
typedef struct _ListItem
{
/*! Stored data. */
void *data;
/*! Pointer to next list item. */
struct _ListItem *next;
/*! Pointer to previous list item. */
struct _ListItem *prev;
} ListItem;
/**
* \struct List
* \brief Doubly-linked list.
*/
typedef struct
{
/*! Head of the list. */
ListItem *head;
/*! Tail of the list. */
ListItem *tail;
/*! Number of stored items. */
size_t count;
/*! Function to compare data of two list items. */
CompareFunc compare;
/*! Function to free item data. */
FreeFunc free;
/*! A memory pool for creating new list items. */
Pool *pool;
} List;
/**
*\param compare function to compare item data
*\param free function to free item data or NULL
*\param pool a user-defined memory pool for creating/destroying ListItems or NULL
*\return a new List
*
* Creates a new List.
*/
List *list_new(CompareFunc compare, FreeFunc free, Pool *pool);
/**
*\param list a List
*\param compare function to compare item data
*\param free function to free item data or NULL
*\param pool a user-defined memory pool for creating/destroying ListItems or NULL
*
* Initializes a List.
*/
void list_init(List *list, CompareFunc compare, FreeFunc free, Pool *pool);
/**
*\param list a List
*
* Frees all items in the list and the list pointer.
*/
void list_destroy(List *list);
/**
*\param list a List
*
* Frees all items in the list without freeing the list pointer.
*/
void list_free(List *list);
/**
*\param list a List
*\param data data to append
*\return a new ListItem
*
* Appends data to the list.
*/
ListItem *list_append(List *list, void *data);
/**
*\param list a List
*\param data data to prepend
*\return a new ListItem
*
* Prepends data to the list.
*/
ListItem *list_prepend(List *list, void *data);
/**
*\param list a List
*\param data data to insert
*\return a new ListItem
*
* Inserts data into list using the associated compare function to determine its position.
*/
ListItem *list_insert_sorted(List *list, void *data);
/**
*\param list a List
*\return head of the list
*
* Gets the head of the list.
*/
ListItem *list_head(const List *list);
/**
*\param list a List
*\return tail of the list
*
* Gets the tail of the list.
*/
ListItem *list_tail(const List *list);
/**
*\param list a List
*\return number of items
*
* Gets the number of items.
*/
size_t list_count(const List *list);
/**
*\param list a List
*\return true if list is empty
*
* Checks if a list is empty.
*/
bool list_empty(const List *list);
/**
*\param list a List
*\param item a ListItem
*
* Removes a list item from the list.
*/
void list_remove(List *list, ListItem *item);
/**
*\param list a List
*\param data data
*\param remove_all true to remove all items with associated data
*
* Removes first list item (or all items) with associated data from the list.
*/
void list_remove_by_data(List *list, void *data, bool remove_all);
/**
*\param list a List
*\return list item data
*
* Removes first element from list and returns its associated data.
*/
void *list_pop(List *list);
/**
*\param list a List
*\param data data
*\return true if data exists in list
*
* Tests if a list contains the specified data.
*/
bool list_contains(const List *list, const void *data);
/**
*\param list a List
*
* Clears a list.
*/
void list_clear(List *list);
/**
*\param list a List
*\param offset position to start search from
*\param data data to search
*\return found ListItem or NULL
*
* Searches for the specified data.
*/
ListItem *list_find(const List *list, ListItem *offset, void const *data);
/*! Gets next list item. */
#define list_item_next(item) item->next
/*! Gets previous list item. */
#define list_item_prev(item) item->prev
/*! Gets data of the specified ListItem. */
#define list_item_get_data(item) item->data
/*! Sets data of the specified ListItem. */
#define list_item_set_data(item, value) item->data = value
/**
*\param list a List
*\param item a ListItem
*
* Sets the associated list item data to NULL and frees its memory.
*/
void list_item_free_data(const List *list, ListItem *item);
#endif