]> mj.ucw.cz Git - libucw.git/blob - lib/lists.h
ce03656b10bc7eedc00d9ef5586ad9b0b1a0e34d
[libucw.git] / lib / lists.h
1 /*
2  *      Sherlock Library -- Linked Lists
3  *
4  *      (c) 1997--1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
5  */
6
7 #ifndef _SHERLOCK_LISTS_H
8 #define _SHERLOCK_LISTS_H
9
10 typedef struct node {
11   struct node *next, *prev;
12 } node;
13
14 typedef struct list {                   /* In fact two overlayed nodes */
15   struct node *head, *null, *tail;
16 } list;
17
18 #define NODE (node *)
19 #define HEAD(list) ((void *)((list).head))
20 #define TAIL(list) ((void *)((list).tail))
21 #define WALK_LIST(n,list) for(n=HEAD(list);(NODE (n))->next; \
22                                 n=(void *)((NODE (n))->next))
23 #define DO_FOR_ALL(n,list) WALK_LIST(n,list)
24 #define WALK_LIST_DELSAFE(n,nxt,list) \
25      for(n=HEAD(list); nxt=(void *)((NODE (n))->next); n=(void *) nxt)
26 #define WALK_LIST_BACKWARDS(n,list) for(n=TAIL(list);(NODE (n))->prev; \
27                                 n=(void *)((NODE (n))->prev))
28 #define WALK_LIST_BACKWARDS_DELSAFE(n,prv,list) \
29      for(n=TAIL(list); prv=(void *)((NODE (n))->prev); n=(void *) prv)
30
31 #define EMPTY_LIST(list) (!(list).head->next)
32
33 void add_tail(list *, node *);
34 void add_head(list *, node *);
35 void rem_node(node *);
36 void add_tail_list(list *, list *);
37 void init_list(list *);
38 void insert_node(node *, node *);
39
40 #if !defined(_SHERLOCK_LISTS_C) && defined(__GNUC__)
41 #define LIST_INLINE static inline
42 #include "lib/lists.c"
43 #undef LIST_INLINE
44 #else
45 #define LIST_INLINE
46 #endif
47
48 #endif