1 /*
2  * libmowgli: A collection of useful routines for programming.
3  * list.h: Linked lists.
4  *
5  * Copyright (c) 2007 William Pitcock <nenolod -at- sacredspiral.co.uk>
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice is present in all copies.
10  *
11  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
12  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
13  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
14  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
15  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
16  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
19  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
20  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21  * POSSIBILITY OF SUCH DAMAGE.
22  */
23 
24 #ifndef __MOWGLI_LIST_H__
25 #define __MOWGLI_LIST_H__
26 
27 /* macros for linked lists */
28 #define MOWGLI_LIST_FOREACH(n, head) \
29 	for (n = (head); n; n = n->next)
30 #define MOWGLI_LIST_FOREACH_NEXT(n, head) \
31 	for (n = (head); n->next; n = n->next)
32 #define MOWGLI_LIST_FOREACH_PREV(n, tail) \
33 	for (n = (tail); n; n = n->prev)
34 
35 #define MOWGLI_LIST_LENGTH(list) (list)->count
36 
37 #define MOWGLI_LIST_FOREACH_SAFE(n, tn, head) \
38 	for (n = (head), tn = n ? n->next : NULL; n != NULL; n = tn, tn = n ? n->next : NULL)
39 
40 /* list node struct */
41 typedef struct mowgli_node_ mowgli_node_t;
42 typedef struct mowgli_list_ mowgli_list_t;
43 
44 struct mowgli_node_
45 {
46 	struct mowgli_node_ *next, *prev;
47 
48 	void *data;	/* pointer to real structure */
49 };
50 
51 /* node list struct */
52 struct mowgli_list_
53 {
54 	mowgli_node_t *head, *tail;
55 	size_t count;	/* how many entries in the list */
56 };
57 
58 extern mowgli_node_t *mowgli_node_create(void);
59 extern void mowgli_node_free(mowgli_node_t *n);
60 extern void mowgli_node_add(void *data, mowgli_node_t *n, mowgli_list_t *l);
61 extern void mowgli_node_add_head(void *data, mowgli_node_t *n, mowgli_list_t *l);
62 extern void mowgli_node_add_before(void *data, mowgli_node_t *n, mowgli_list_t *l, mowgli_node_t *before);
63 extern void mowgli_node_add_after(void *data, mowgli_node_t *n, mowgli_list_t *l, mowgli_node_t *before);
64 extern void mowgli_node_insert(void *data, mowgli_node_t *n, mowgli_list_t *l, size_t position);
65 extern ssize_t mowgli_node_index(mowgli_node_t *n, mowgli_list_t *l);
66 extern void mowgli_node_delete(mowgli_node_t *n, mowgli_list_t *l);
67 extern mowgli_node_t *mowgli_node_find(void *data, mowgli_list_t *l);
68 extern void mowgli_node_move(mowgli_node_t *m, mowgli_list_t *oldlist, mowgli_list_t *newlist);
69 extern mowgli_node_t *mowgli_node_nth(mowgli_list_t *l, size_t pos);
70 extern void *mowgli_node_nth_data(mowgli_list_t *l, size_t pos);
71 
72 typedef int (*mowgli_list_comparator_t)(mowgli_node_t *n, mowgli_node_t *n2, void *opaque);
73 
74 extern mowgli_list_t *mowgli_list_create(void);
75 extern void mowgli_list_free(mowgli_list_t *l);
76 extern void mowgli_list_concat(mowgli_list_t *l, mowgli_list_t *l2);
77 extern void mowgli_list_reverse(mowgli_list_t *l);
78 extern void mowgli_list_sort(mowgli_list_t *l, mowgli_list_comparator_t comp, void *opaque);
79 
80 #endif
81