1 |
/* |
2 |
* Copyright © 2005 William Pitcock, et al. |
3 |
* Rights to this code are as documented in doc/pod/license.pod. |
4 |
* |
5 |
* Data structures and macros for manipulating linked lists. |
6 |
* |
7 |
* $Id: dlink.h,v 1.2 2007-07-21 01:29:07 pippijn Exp $ |
8 |
*/ |
9 |
|
10 |
#ifndef NODE_H |
11 |
#define NODE_H |
12 |
|
13 |
#include <common/memory.h> |
14 |
|
15 |
/* macros for linked lists */ |
16 |
#define LIST_FOREACH(n, head) for (n = (head); n; n = n->next) |
17 |
#define LIST_FOREACH_NEXT(n, head) for (n = (head); n->next; n = n->next) |
18 |
#define LIST_FOREACH_PREV(n, tail) for (n = (tail); n; n = n->prev) |
19 |
|
20 |
#define LIST_LENGTH(list) (list)->count |
21 |
|
22 |
#define LIST_FOREACH_SAFE(n, tn, head) for (n = (head), tn = n ? n->next : NULL; n != NULL; n = tn, tn = n ? n->next : NULL) |
23 |
|
24 |
/* list node struct */ |
25 |
struct node_t : zero_initialised |
26 |
{ |
27 |
node_t *next; |
28 |
node_t *prev; |
29 |
void *data; /* pointer to real structure */ |
30 |
}; |
31 |
|
32 |
/* node list struct */ |
33 |
struct list_t : zero_initialised |
34 |
{ |
35 |
node_t *head; |
36 |
node_t *tail; |
37 |
unsigned int count; /* how many entries in the list */ |
38 |
|
39 |
list_t () |
40 |
: head (0), tail (0), count (0) |
41 |
{ |
42 |
} |
43 |
}; |
44 |
|
45 |
E node_t *node_create (void); |
46 |
E void node_free (node_t *n); |
47 |
E void node_add (void *data, node_t *n, list_t *l); |
48 |
E void node_add_head (void *data, node_t *n, list_t *l); |
49 |
E void node_add_before (void *data, node_t *n, list_t *l, node_t *before); |
50 |
E void node_del (node_t *n, list_t *l); |
51 |
E node_t *node_find (void *data, list_t *l); |
52 |
E void node_move (node_t *m, list_t *oldlist, list_t *newlist); |
53 |
|
54 |
#endif |