1998-04-22 12:58:34 +00:00
|
|
|
/*
|
|
|
|
* BIRD Library -- Linked Lists
|
|
|
|
*
|
|
|
|
* (c) 1998 Martin Mares <mj@ucw.cz>
|
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _BIRD_LISTS_H_
|
|
|
|
#define _BIRD_LISTS_H_
|
|
|
|
|
2001-01-15 09:24:16 +00:00
|
|
|
/*
|
|
|
|
* I admit the list structure is very tricky and also somewhat awkward,
|
|
|
|
* but it's both efficient and easy to manipulate once one understands the
|
|
|
|
* basic trick: The list head always contains two synthetic nodes which are
|
|
|
|
* always present in the list: the head and the tail. But as the `next'
|
|
|
|
* entry of the tail and the `prev' entry of the head are both NULL, the
|
|
|
|
* nodes can overlap each other:
|
|
|
|
*
|
|
|
|
* head head_node.next
|
|
|
|
* null head_node.prev tail_node.next
|
|
|
|
* tail tail_node.prev
|
|
|
|
*/
|
|
|
|
|
1998-04-22 12:58:34 +00:00
|
|
|
typedef struct node {
|
|
|
|
struct node *next, *prev;
|
|
|
|
} node;
|
|
|
|
|
|
|
|
typedef struct list { /* In fact two overlayed nodes */
|
|
|
|
struct node *head, *null, *tail;
|
|
|
|
} list;
|
|
|
|
|
|
|
|
#define NODE (node *)
|
|
|
|
#define HEAD(list) ((void *)((list).head))
|
|
|
|
#define TAIL(list) ((void *)((list).tail))
|
2010-11-04 16:22:43 +00:00
|
|
|
#define NODE_NEXT(n) ((void *)((NODE (n))->next))
|
2012-07-07 08:40:00 +00:00
|
|
|
#define NODE_VALID(n) ((NODE (n))->next)
|
|
|
|
#define WALK_LIST(n,list) for(n=HEAD(list); NODE_VALID(n); n=NODE_NEXT(n))
|
2014-03-20 13:07:12 +00:00
|
|
|
#define WALK_LIST2(n,nn,list,pos) \
|
|
|
|
for(nn=(list).head; NODE_VALID(nn) && (n=SKIP_BACK(typeof(*n),pos,nn)); nn=nn->next)
|
1998-10-17 11:02:39 +00:00
|
|
|
#define WALK_LIST_DELSAFE(n,nxt,list) \
|
2010-11-04 16:22:43 +00:00
|
|
|
for(n=HEAD(list); nxt=NODE_NEXT(n); n=(void *) nxt)
|
2008-12-18 22:26:08 +00:00
|
|
|
/* WALK_LIST_FIRST supposes that called code removes each processed node */
|
|
|
|
#define WALK_LIST_FIRST(n,list) \
|
|
|
|
while(n=HEAD(list), (NODE (n))->next)
|
1999-08-03 19:31:30 +00:00
|
|
|
#define WALK_LIST_BACKWARDS(n,list) for(n=TAIL(list);(NODE (n))->prev; \
|
|
|
|
n=(void *)((NODE (n))->prev))
|
|
|
|
#define WALK_LIST_BACKWARDS_DELSAFE(n,prv,list) \
|
|
|
|
for(n=TAIL(list); prv=(void *)((NODE (n))->prev); n=(void *) prv)
|
1998-07-09 19:35:23 +00:00
|
|
|
|
1998-04-22 12:58:34 +00:00
|
|
|
#define EMPTY_LIST(list) (!(list).head->next)
|
|
|
|
|
2014-12-05 08:25:38 +00:00
|
|
|
|
2015-03-17 11:39:10 +00:00
|
|
|
|
2014-12-05 08:25:38 +00:00
|
|
|
#ifndef _BIRD_LISTS_C_
|
|
|
|
#define LIST_INLINE static inline
|
|
|
|
#include "lib/lists.c"
|
|
|
|
#undef LIST_INLINE
|
|
|
|
|
|
|
|
#else /* _BIRD_LISTS_C_ */
|
|
|
|
#define LIST_INLINE
|
1998-04-22 12:58:34 +00:00
|
|
|
void add_tail(list *, node *);
|
|
|
|
void add_head(list *, node *);
|
|
|
|
void rem_node(node *);
|
2013-09-16 21:57:40 +00:00
|
|
|
void rem2_node(node *);
|
1998-04-22 12:58:34 +00:00
|
|
|
void add_tail_list(list *, list *);
|
|
|
|
void init_list(list *);
|
|
|
|
void insert_node(node *, node *);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|