Mercurial > hg > toybox
annotate lib/llist.c @ 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.
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 12 Mar 2012 00:25:40 -0500 |
parents | f1e9da78c5dd |
children | 1e8b9acdafeb |
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 |
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
9 // Free all the elements of a linked list |
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
10 // if freeit!=NULL call freeit() on each element before freeing it. |
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 void llist_free(void *list, void (*freeit)(void *data)) |
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
13 { |
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
14 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
|
15 void *pop = llist_pop(&list); |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
16 if (freeit) freeit(pop); |
315
aaac01796688
Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents:
238
diff
changeset
|
17 else free(pop); |
aaac01796688
Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents:
238
diff
changeset
|
18 |
aaac01796688
Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents:
238
diff
changeset
|
19 // 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
|
20 if (list==pop) break; |
15
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
21 } |
2a56fdc40035
Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
22 } |
20
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
23 |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
24 // 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
|
25 // as &list) |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
26 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
|
27 { |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
28 // 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
|
29 // 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
|
30 // 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
|
31 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
|
32 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
|
33 *llist = *next; |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
34 |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
15
diff
changeset
|
35 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
|
36 } |
238
630b2e12db16
Move dlist_add() to lib/llist.c
Rob Landley <rob@landley.net>
parents:
20
diff
changeset
|
37 |
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
|
38 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
|
39 { |
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 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
|
41 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
|
42 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
|
43 (*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
|
44 (*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
|
45 } 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
|
46 } |
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
|
47 |
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
|
48 |
366 | 49 // 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
|
50 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
|
51 { |
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
|
52 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
|
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 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
|
55 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
|
56 |
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
|
57 return new; |
238
630b2e12db16
Move dlist_add() to lib/llist.c
Rob Landley <rob@landley.net>
parents:
20
diff
changeset
|
58 } |