cctools
list.h
Go to the documentation of this file.
1/*
2Copyright (C) 2022 The University of Notre Dame
3This software is distributed under the GNU General Public License.
4See the file COPYING for details.
5*/
6
35#ifndef LIST_H
36#define LIST_H
37
38#include <limits.h>
39#include <stdbool.h>
40
41/*
42It turns out that many libraries and tools make use of
43symbols like "debug" and "fatal". This causes strange
44failures when we link against such codes. Rather than change
45all of our code, we simply insert these defines to
46transparently modify the linker namespace we are using.
47*/
48
49#ifndef DOXYGEN
50
51#define list_create cctools_list_create
52#define list_destroy cctools_list_destroy
53#define list_length cctools_list_length
54#define list_cursor_create cctools_list_cursor_create
55#define list_cursor_destroy cctools_list_cursor_destroy
56#define list_cursor_clone cctools_list_cursor_clone
57#define list_reset cctools_list_reset
58#define list_seek cctools_list_seek
59#define list_tell cctools_list_tell
60#define list_next cctools_list_next
61#define list_prev cctools_list_prev
62#define list_get cctools_list_get
63#define list_set cctools_list_set
64#define list_drop cctools_list_drop
65#define list_insert cctools_list_insert
66
67#define list_size cctools_list_size
68#define list_delete cctools_list_delete
69#define list_free cctools_list_free
70#define list_pop_head cctools_list_pop_head
71#define list_peek_head cctools_list_peek_head
72#define list_pop_tail cctools_list_pop_tail
73#define list_peek_tail cctools_list_peek_tail
74#define list_peek_current cctools_list_peek_current
75#define list_remove cctools_list_remove
76#define list_find cctools_list_find
77#define list_splice cctools_list_splice
78#define list_split cctools_list_split
79#define list_push_head cctools_list_push_head
80#define list_push_tail cctools_list_push_tail
81#define list_push_priority cctools_list_push_priority
82#define list_iterate cctools_list_iterate
83#define list_iterate_reverse cctools_list_iterate_reverse
84#define list_first_item cctools_list_first_item
85#define list_next_item cctools_list_next_item
86
87#endif
88
92struct list *list_create(void);
93
103bool list_destroy(struct list *list);
104
109unsigned list_length(struct list *list);
110
117struct list_cursor *list_cursor_create(struct list *list);
118
125void list_cursor_destroy(struct list_cursor *cur);
126
133struct list_cursor *list_cursor_clone(struct list_cursor *cur);
134
140void list_reset(struct list_cursor *cur);
141
152bool list_seek(struct list_cursor *cur, int index);
153
165bool list_tell(struct list_cursor *cur, unsigned *index);
166
172bool list_next(struct list_cursor *cur);
173
179bool list_prev(struct list_cursor *cur);
180
190bool list_get(struct list_cursor *cur, void **item);
191
200bool list_set(struct list_cursor *cur, void *item);
201
210bool list_drop(struct list_cursor *cur);
211
219void list_insert(struct list_cursor *cur, void *item);
220
221
222// Utility functions
223
224typedef int (*list_op_t) (void *item, const void *arg);
225typedef double (*list_priority_t) (void *item);
226
232int list_size(struct list *list);
233
241struct list *list_duplicate(struct list *list);
242
249void list_delete(struct list *list);
250
256void list_free(struct list *list);
257
265void list_clear(struct list *list, void (*delete_func)(void*item) );
266
273struct list *list_splice(struct list *top, struct list *bottom);
274
284struct list *list_split(struct list *src, list_op_t cmp, const void *arg);
285
291int list_push_head(struct list *list, void *item);
292
297void *list_pop_head(struct list *list);
298
303void *list_peek_head(struct list *list);
304
310int list_push_tail(struct list *list, void *item);
311
316void *list_pop_tail(struct list *list);
317
322void *list_rotate(struct list *list);
323
328void *list_peek_tail(struct list *list);
329
334void *list_peek_current(struct list *list);
335
348void list_push_priority(struct list *list, list_priority_t p, void *item);
349
358void *list_find(struct list *list, list_op_t cmp, const void *arg);
359
367void *list_remove(struct list *list, const void *value);
368
375void list_first_item(struct list *list);
376
384void *list_next_item(struct list *list);
385
393int list_iterate(struct list *list, list_op_t op, const void *arg);
394
401int list_iterate_reverse(struct list *list, list_op_t op, const void *arg);
402
408struct list *list_sort(struct list *list, int (*comparator) (const void *, const void *));
409
422#define LIST_ITERATE( list, item ) list_first_item(list); while((item=list_next_item(list)))
423
424#endif
bool list_prev(struct list_cursor *cur)
Move a cursor to the previous item.
int list_iterate(struct list *list, list_op_t op, const void *arg)
Apply a function to a list.
void * list_next_item(struct list *list)
Continue traversing a list.
struct list * list_splice(struct list *top, struct list *bottom)
Splice two lists together.
struct list_cursor * list_cursor_clone(struct list_cursor *cur)
Get a copy of an existing cursor.
void * list_find(struct list *list, list_op_t cmp, const void *arg)
Find an element within a list This function searches the list, comparing each element in the list to ...
bool list_seek(struct list_cursor *cur, int index)
Move a cursor to an item by index.
void * list_peek_current(struct list *list)
Peek at the current element in the iteration.
bool list_drop(struct list_cursor *cur)
Remove the item under the cursor.
void list_push_priority(struct list *list, list_priority_t p, void *item)
Push an item onto of a list in priority order.
void * list_pop_tail(struct list *list)
Pop an item off of the list tail.
struct list * list_sort(struct list *list, int(*comparator)(const void *, const void *))
Sort a list using a comparator function.
unsigned list_length(struct list *list)
Get the number of items in a list.
void list_free(struct list *list)
Free every item referred to by the list.
struct list * list_split(struct list *src, list_op_t cmp, const void *arg)
Split a list into two at the given item If arg is NULL or not found, list_split returns NULL and the ...
void list_first_item(struct list *list)
Begin traversing a list.
bool list_tell(struct list_cursor *cur, unsigned *index)
Get the position of a cursor within a list.
void * list_peek_head(struct list *list)
Peek at the list head.
int list_iterate_reverse(struct list *list, list_op_t op, const void *arg)
Apply a function to a list in reverse.
struct list * list_create(void)
Create an empty linked list.
void * list_pop_head(struct list *list)
Pop an item off of the list head.
void list_clear(struct list *list, void(*delete_func)(void *item))
Delete every item contained within this list, using the provided function.
void list_delete(struct list *list)
Delete a linked list.
void * list_peek_tail(struct list *list)
Peek at the list tail.
int list_push_head(struct list *list, void *item)
Push an item onto the list head.
int list_size(struct list *list)
Count the elements in a list.
struct list_cursor * list_cursor_create(struct list *list)
Create a new cursor on a list.
int list_push_tail(struct list *list, void *item)
Push an item onto the list tail.
void list_reset(struct list_cursor *cur)
Reset the position of a cursor.
void * list_remove(struct list *list, const void *value)
Remove an item from the list This function searches the list for the item pointed to by value and rem...
bool list_set(struct list_cursor *cur, void *item)
Set the value under the cursor.
void * list_rotate(struct list *list)
Move the list head to the tail.
bool list_get(struct list_cursor *cur, void **item)
Get the item under a cursor.
bool list_next(struct list_cursor *cur)
Move a cursor to the next item.
void list_insert(struct list_cursor *cur, void *item)
Insert an item to the left of the cursor.
struct list * list_duplicate(struct list *list)
Duplicate a linked list Returns a copy of the linked list.
bool list_destroy(struct list *list)
Delete an empty list.
void list_cursor_destroy(struct list_cursor *cur)
Delete a previously created cursor.