blob: 71a187db49ab7862bd1d15163886fa3090782d4e [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 Landley9e2b6db2012-07-15 17:22:04 -05008// Call a function (such as free()) on each element of a linked list.
9void llist_traverse(void *list, void (*using)(void *data))
Rob Landley15bdc112006-11-01 22:28:46 -050010{
Rob Landleyfe91e682012-11-22 21:18:09 -060011 void *old = list;
12
Rob Landley7aa651a2012-11-13 17:14:08 -060013 while (list) {
14 void *pop = llist_pop(&list);
15 using(pop);
Rob Landleybdf037f2008-10-23 16:44:30 -050016
Rob Landley7aa651a2012-11-13 17:14:08 -060017 // End doubly linked list too.
Rob Landleyfe91e682012-11-22 21:18:09 -060018 if (old == list) break;
Rob Landley7aa651a2012-11-13 17:14:08 -060019 }
Rob Landley15bdc112006-11-01 22:28:46 -050020}
Rob Landley0a04b3e2006-11-03 00:05:52 -050021
22// Return the first item from the list, advancing the list (which must be called
23// as &list)
24void *llist_pop(void *list)
25{
Rob Landley7aa651a2012-11-13 17:14:08 -060026 // I'd use a void ** for the argument, and even accept the typecast in all
27 // callers as documentation you need the &, except the stupid compiler
28 // would then scream about type-punned pointers. Screw it.
29 void **llist = (void **)list;
30 void **next = (void **)*llist;
31 *llist = *next;
Rob Landley0a04b3e2006-11-03 00:05:52 -050032
Rob Landley7aa651a2012-11-13 17:14:08 -060033 return (void *)next;
Rob Landley0a04b3e2006-11-03 00:05:52 -050034}
Rob Landley6ef04ef2008-01-20 17:34:53 -060035
Rob Landley5f57bcc2013-09-09 04:26:03 -050036void *dlist_pop(void *list)
37{
38 struct double_list **pdlist = (struct double_list **)list, *dlist = *pdlist;
39
Rob Landleybb215e42013-09-09 05:26:52 -050040 if (dlist->next == dlist) *pdlist = 0;
41 else {
42 dlist->next->prev = dlist->prev;
43 dlist->prev->next = *pdlist = dlist->next;
44 }
Rob Landley5f57bcc2013-09-09 04:26:03 -050045
46 return dlist;
47}
48
Rob Landley2c482472012-03-12 00:25:40 -050049void dlist_add_nomalloc(struct double_list **list, struct double_list *new)
50{
Rob Landley7aa651a2012-11-13 17:14:08 -060051 if (*list) {
52 new->next = *list;
53 new->prev = (*list)->prev;
54 (*list)->prev->next = new;
55 (*list)->prev = new;
56 } else *list = new->next = new->prev = new;
Rob Landley2c482472012-03-12 00:25:40 -050057}
58
59
Rob Landley53c75042010-01-05 10:43:36 -060060// Add an entry to the end of a doubly linked list
Rob Landleybdf037f2008-10-23 16:44:30 -050061struct double_list *dlist_add(struct double_list **list, char *data)
Rob Landley6ef04ef2008-01-20 17:34:53 -060062{
Rob Landley7aa651a2012-11-13 17:14:08 -060063 struct double_list *new = xmalloc(sizeof(struct double_list));
Rob Landley6ef04ef2008-01-20 17:34:53 -060064
Rob Landley7aa651a2012-11-13 17:14:08 -060065 new->data = data;
66 dlist_add_nomalloc(list, new);
Rob Landleybdf037f2008-10-23 16:44:30 -050067
Rob Landley7aa651a2012-11-13 17:14:08 -060068 return new;
Rob Landley6ef04ef2008-01-20 17:34:53 -060069}