Mercurial > hg > toybox
annotate lib/llist.c @ 624:1e8b9acdafeb
Genericize llist code a bit: rename llist_free() to llist_traverse(), and no longer accept NULL as a synonym for free.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sun, 15 Jul 2012 17:22:04 -0500 |
parents | c2f39708a4c4 |
children | 786841fdb1e0 |
rev | line source |
---|---|
15
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
1 /* vi: set sw=4 ts=4 : |
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
2 * llist.c - Linked list functions |
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
3 * |
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
4 * Linked list structures have a next pointer as their first element. |
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
5 */ |
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
6 |
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
7 #include "toys.h" |
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
8 |
624
1e8b9acdafeb
Genericize llist code a bit: rename llist_free() to llist_traverse(), and no longer accept NULL as a synonym for free.
Rob Landley <rob@landley.net>
parents:
540
diff
changeset
|
9 // Call a function (such as free()) on each element of a linked list. |
1e8b9acdafeb
Genericize llist code a bit: rename llist_free() to llist_traverse(), and no longer accept NULL as a synonym for free.
Rob Landley <rob@landley.net>
parents:
540
diff
changeset
|
10 void llist_traverse(void *list, void (*using)(void *data)) |
15
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
11 { |
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
12 while (list) { |
20
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
13 void *pop = llist_pop(&list); |
624
1e8b9acdafeb
Genericize llist code a bit: rename llist_free() to llist_traverse(), and no longer accept NULL as a synonym for free.
Rob Landley <rob@landley.net>
parents:
540
diff
changeset
|
14 using(pop); |
315
aaac01796688
Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents:
238
diff
changeset
|
15 |
aaac01796688
Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents:
238
diff
changeset
|
16 // End doubly linked list too. |
aaac01796688
Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents:
238
diff
changeset
|
17 if (list==pop) break; |
15
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
18 } |
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
19 } |
20
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
20 |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
21 // Return the first item from the list, advancing the list (which must be called |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
22 // as &list) |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
23 void *llist_pop(void *list) |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
24 { |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
25 // I'd use a void ** for the argument, and even accept the typecast in all |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
26 // callers as documentation you need the &, except the stupid compiler |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
27 // would then scream about type-punned pointers. Screw it. |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
28 void **llist = (void **)list; |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
29 void **next = (void **)*llist; |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
30 *llist = *next; |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
31 |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
32 return (void *)next; |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
33 } |
238
630b2e12db16
Move dlist_add() to lib/llist.c
Rob Landley <rob@landley.net>
parents:
20
diff
changeset
|
34 |
540
c2f39708a4c4
Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents:
366
diff
changeset
|
35 void dlist_add_nomalloc(struct double_list **list, struct double_list *new) |
c2f39708a4c4
Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents:
366
diff
changeset
|
36 { |
c2f39708a4c4
Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents:
366
diff
changeset
|
37 if (*list) { |
c2f39708a4c4
Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents:
366
diff
changeset
|
38 new->next = *list; |
c2f39708a4c4
Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents:
366
diff
changeset
|
39 new->prev = (*list)->prev; |
c2f39708a4c4
Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents:
366
diff
changeset
|
40 (*list)->prev->next = new; |
c2f39708a4c4
Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents:
366
diff
changeset
|
41 (*list)->prev = new; |
c2f39708a4c4
Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents:
366
diff
changeset
|
42 } else *list = new->next = new->prev = new; |
c2f39708a4c4
Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents:
366
diff
changeset
|
43 } |
c2f39708a4c4
Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents:
366
diff
changeset
|
44 |
c2f39708a4c4
Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents:
366
diff
changeset
|
45 |
366 | 46 // Add an entry to the end of a doubly linked list |
315
aaac01796688
Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents:
238
diff
changeset
|
47 struct double_list *dlist_add(struct double_list **list, char *data) |
238
630b2e12db16
Move dlist_add() to lib/llist.c
Rob Landley <rob@landley.net>
parents:
20
diff
changeset
|
48 { |
540
c2f39708a4c4
Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents:
366
diff
changeset
|
49 struct double_list *new = xmalloc(sizeof(struct double_list)); |
238
630b2e12db16
Move dlist_add() to lib/llist.c
Rob Landley <rob@landley.net>
parents:
20
diff
changeset
|
50 |
540
c2f39708a4c4
Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents:
366
diff
changeset
|
51 new->data = data; |
c2f39708a4c4
Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents:
366
diff
changeset
|
52 dlist_add_nomalloc(list, new); |
315
aaac01796688
Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents:
238
diff
changeset
|
53 |
540
c2f39708a4c4
Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents:
366
diff
changeset
|
54 return new; |
238
630b2e12db16
Move dlist_add() to lib/llist.c
Rob Landley <rob@landley.net>
parents:
20
diff
changeset
|
55 } |