blob: 6b4b8f2ce767ec684285ce13d387435ecf88a8a3 [file] [log] [blame]
Rob Landley7aa651a2012-11-13 17:14:08 -06001/* llist.c - Linked list functions
Rob Landley15bdc112006-11-01 22:28:46 -05002 *
3 * Linked list structures have a next pointer as their first element.
4 */
5
6#include "toys.h"
7
Rob Landleye604d532014-05-21 06:57:43 -05008// Callback function to free data pointer of double_list or arg_list
9
10void llist_free_arg(void *node)
11{
12 struct arg_list *d = node;
13
14 free(d->arg);
15 free(d);
16}
17
18void llist_free_double(void *node)
19{
20 struct double_list *d = node;
21
22 free(d->data);
23 free(d);
24}
25
Rob Landley9e2b6db2012-07-15 17:22:04 -050026// Call a function (such as free()) on each element of a linked list.
Rob Landleye604d532014-05-21 06:57:43 -050027void llist_traverse(void *list, void (*using)(void *node))
Rob Landley15bdc112006-11-01 22:28:46 -050028{
Rob Landleyfe91e682012-11-22 21:18:09 -060029 void *old = list;
30
Rob Landley7aa651a2012-11-13 17:14:08 -060031 while (list) {
32 void *pop = llist_pop(&list);
33 using(pop);
Rob Landleybdf037f2008-10-23 16:44:30 -050034
Rob Landley7aa651a2012-11-13 17:14:08 -060035 // End doubly linked list too.
Rob Landleyfe91e682012-11-22 21:18:09 -060036 if (old == list) break;
Rob Landley7aa651a2012-11-13 17:14:08 -060037 }
Rob Landley15bdc112006-11-01 22:28:46 -050038}
Rob Landley0a04b3e2006-11-03 00:05:52 -050039
40// Return the first item from the list, advancing the list (which must be called
41// as &list)
42void *llist_pop(void *list)
43{
Rob Landley7aa651a2012-11-13 17:14:08 -060044 // I'd use a void ** for the argument, and even accept the typecast in all
45 // callers as documentation you need the &, except the stupid compiler
46 // would then scream about type-punned pointers. Screw it.
47 void **llist = (void **)list;
48 void **next = (void **)*llist;
49 *llist = *next;
Rob Landley0a04b3e2006-11-03 00:05:52 -050050
Rob Landley7aa651a2012-11-13 17:14:08 -060051 return (void *)next;
Rob Landley0a04b3e2006-11-03 00:05:52 -050052}
Rob Landley6ef04ef2008-01-20 17:34:53 -060053
Rob Landley5f57bcc2013-09-09 04:26:03 -050054void *dlist_pop(void *list)
55{
56 struct double_list **pdlist = (struct double_list **)list, *dlist = *pdlist;
57
Rob Landleybb215e42013-09-09 05:26:52 -050058 if (dlist->next == dlist) *pdlist = 0;
59 else {
60 dlist->next->prev = dlist->prev;
61 dlist->prev->next = *pdlist = dlist->next;
62 }
Rob Landley5f57bcc2013-09-09 04:26:03 -050063
64 return dlist;
65}
66
Rob Landley2c482472012-03-12 00:25:40 -050067void dlist_add_nomalloc(struct double_list **list, struct double_list *new)
68{
Rob Landley7aa651a2012-11-13 17:14:08 -060069 if (*list) {
70 new->next = *list;
71 new->prev = (*list)->prev;
72 (*list)->prev->next = new;
73 (*list)->prev = new;
74 } else *list = new->next = new->prev = new;
Rob Landley2c482472012-03-12 00:25:40 -050075}
76
77
Rob Landley53c75042010-01-05 10:43:36 -060078// Add an entry to the end of a doubly linked list
Rob Landleybdf037f2008-10-23 16:44:30 -050079struct double_list *dlist_add(struct double_list **list, char *data)
Rob Landley6ef04ef2008-01-20 17:34:53 -060080{
Rob Landley7aa651a2012-11-13 17:14:08 -060081 struct double_list *new = xmalloc(sizeof(struct double_list));
Rob Landley6ef04ef2008-01-20 17:34:53 -060082
Rob Landley7aa651a2012-11-13 17:14:08 -060083 new->data = data;
84 dlist_add_nomalloc(list, new);
Rob Landleybdf037f2008-10-23 16:44:30 -050085
Rob Landley7aa651a2012-11-13 17:14:08 -060086 return new;
Rob Landley6ef04ef2008-01-20 17:34:53 -060087}
Rob Landleydc640252014-05-29 05:22:02 -050088
89// Terminate circular list for traversal in either direction. Returns end *.
90void *dlist_terminate(void *list)
91{
92 struct double_list *end = list;
93
94 if (!list) return 0;
95
96 end = end->prev;
97 end->next->prev = 0;
98 end->next = 0;
99
100 return end;
101}