list.h File Reference

General pourpose double-linked lists. More...

#include <cfg/compiler.h>
#include <cfg/debug.h>

Go to the source code of this file.


Data Structures

struct  _Node
 This structure represents a node for bidirectional lists. More...
struct  _List
 Head of a doubly-linked list of Node structs. More...
struct  _PriNode
 Extended node for priority queues. More...

Defines

#define DECLARE_NODE_ANON(T)   T *succ; T *pred;
 Template for a naked node in a list of T structures.
#define DECLARE_NODE_TYPE(T)   typedef struct T##Node { T *succ; T *pred; } T##Node
 Declare a typesafe node for structures of type T.
#define DECLARE_LIST_TYPE(T)
 Template for a list of T structures.
#define LIST_HEAD(l)   ((l)->head.succ)
 Get a pointer to the first node in a list.
#define LIST_TAIL(l)   ((l)->tail.pred)
 Get a pointer to the last node in a list.
#define FOREACH_NODE(n, l)
 Iterate over all nodes in a list.
#define REVERSE_FOREACH_NODE(n, l)
 Iterate backwards over all nodes in a list.
#define LIST_INIT(l)
 Initialize a list.
#define LIST_ASSERT_VALID(l)
 Make sure that a list is valid (it was initialized and is not corrupted).
#define LIST_ASSERT_NOT_CONTAINS(list, node)
 Checks that a node isn't part of a given list.
#define LIST_EMPTY(l)   ( (void *)((l)->head.succ) == (void *)(&(l)->tail) )
 Tell whether a list is empty.
#define ADDHEAD(l, n)
 Add node to list head.
#define ADDTAIL(l, n)
 Add node to list tail.
#define INSERT_BEFORE(n, ln)
 Insert node n before node ln.
#define REMOVE(n)
 Remove n from whatever list it is in.
#define LIST_ENQUEUE(list, node)
 Insert a priority node in a priority queue.

Typedefs

typedef struct _Node Node
 This structure represents a node for bidirectional lists.
typedef struct _List List
 Head of a doubly-linked list of Node structs.
typedef struct _PriNode PriNode
 Extended node for priority queues.

Functions

Nodelist_remHead (List *l)
 Unlink a node from the head of the list l.
Nodelist_remTail (List *l)
 Unlink a node from the tail of the list l.

Detailed Description

General pourpose double-linked lists.

Version:
Id
list.h 2751 2009-07-08 13:13:35Z lottaviano
Author:
Bernie Innocenti <bernie@codewiz.org>

Definition in file list.h.


Define Documentation

#define ADDHEAD ( l,
 ) 

Value:

do { \
        LIST_ASSERT_NOT_CONTAINS((l),(n)); \
        (n)->succ = (l)->head.succ; \
        (n)->pred = (l)->head.succ->pred; \
        (n)->succ->pred = (n); \
        (n)->pred->succ = (n); \
    } while (0)
Add node to list head.

Definition at line 227 of file list.h.

#define ADDTAIL ( l,
 ) 

Value:

do { \
        LIST_ASSERT_NOT_CONTAINS((l),(n)); \
        (n)->succ = &(l)->tail; \
        (n)->pred = (l)->tail.pred; \
        (n)->pred->succ = (n); \
        (l)->tail.pred = (n); \
    } while (0)
Add node to list tail.

Definition at line 237 of file list.h.

#define DECLARE_LIST_TYPE (  ) 

Value:

DECLARE_NODE_TYPE(T); \
    typedef struct T##List { \
         T##Node head; \
         T##Node tail; \
    } T##List
Template for a list of T structures.

Definition at line 120 of file list.h.

#define DECLARE_NODE_ANON (  )     T *succ; T *pred;

Template for a naked node in a list of T structures.

To be used as data member in other structures:

    struct Foo
    {
        DECLARE_NODE_ANON(struct Foo)
        int a;
        float b;
    }

    DECLARE_LIST_TYPE(Foo);

    void foo(void)
    {
        static LIST_TYPE(Foo) foo_list;
        static Foo foo1, foo2;
        Foo *fp;

        LIST_INIT(&foo_list);
        ADDHEAD(&foo_list, &foo1);
        INSERT_BEFORE(&foo_list, &foo2);
        FOREACH_NODE(fp, &foo_list)
            fp->a = 10;
    }

Definition at line 112 of file list.h.

#define DECLARE_NODE_TYPE (  )     typedef struct T##Node { T *succ; T *pred; } T##Node

Declare a typesafe node for structures of type T.

Definition at line 116 of file list.h.

#define FOREACH_NODE ( n,
 ) 

Value:

for( \
        (n) = (TYPEOF_OR_VOIDPTR(n))LIST_HEAD(l); \
        ((Node *)(n))->succ; \
        (n) = (TYPEOF_OR_VOIDPTR(n))(((Node *)(n))->succ) \
    )
Iterate over all nodes in a list.

This macro generates a "for" statement using the following parameters:

Parameters:
n Node pointer to be used in each iteration.
l Pointer to list.

Definition at line 158 of file list.h.

#define INSERT_BEFORE ( n,
ln   ) 

Value:

do { \
        ASSERT_VALID_PTR(n); \
        ASSERT_VALID_PTR(ln); \
        (n)->succ = (ln); \
        (n)->pred = (ln)->pred; \
        (ln)->pred->succ = (n); \
        (ln)->pred = (n); \
    } while (0)
Insert node n before node ln.

Note:
You can't pass in a list header as ln, but it is safe to pass list->head of an empty list.

Definition at line 252 of file list.h.

#define LIST_ASSERT_VALID (  ) 

Value:

do { \
            Node *n, *pred; \
            ASSERT((l)->head.succ != NULL); \
            ASSERT((l)->head.pred == NULL); \
            ASSERT((l)->tail.succ == NULL); \
            ASSERT((l)->tail.pred != NULL); \
            pred = &(l)->head; \
            FOREACH_NODE(n, l) \
            { \
                ASSERT(n->pred == pred); \
                pred = n; \
            } \
            ASSERT(n == &(l)->tail); \
        } while (0)
Make sure that a list is valid (it was initialized and is not corrupted).

Definition at line 190 of file list.h.

#define LIST_EMPTY (  )     ( (void *)((l)->head.succ) == (void *)(&(l)->tail) )

Tell whether a list is empty.

Definition at line 224 of file list.h.

#define LIST_ENQUEUE ( list,
node   ) 

Value:

do { \
        PriNode *ln; \
        LIST_ASSERT_NOT_CONTAINS((list),(node)); \
        FOREACH_NODE(ln, (list)) \
            if (ln->pri < (node)->pri) \
                break; \
        INSERT_BEFORE(&(node)->link, &ln->link); \
    } while (0)
Insert a priority node in a priority queue.

The new node is inserted immediately before the first node with lower priority or appended to the tail if no such node exists.

Definition at line 283 of file list.h.

#define LIST_HEAD (  )     ((l)->head.succ)

Get a pointer to the first node in a list.

If l is empty, result points to l->tail.

Definition at line 135 of file list.h.

#define LIST_INIT (  ) 

Value:

do { \
        (l)->head.succ = (TYPEOF_OR_VOIDPTR((l)->head.succ)) &(l)->tail; \
        (l)->head.pred = NULL; \
        (l)->tail.succ = NULL; \
        (l)->tail.pred = (TYPEOF_OR_VOIDPTR((l)->tail.pred)) &(l)->head; \
    } while (0)
Initialize a list.

Definition at line 180 of file list.h.

#define LIST_TAIL (  )     ((l)->tail.pred)

Get a pointer to the last node in a list.

If l is empty, result points to l->head.

Definition at line 142 of file list.h.

#define REMOVE (  ) 

Value:

do { \
        ASSERT_VALID_PTR(n); \
        (n)->pred->succ = (n)->succ; \
        (n)->succ->pred = (n)->pred; \
        INVALIDATE_NODE(n); \
    } while (0)
Remove n from whatever list it is in.

Note:
Removing a node that has not previously been inserted into a list invokes undefined behavior.

Definition at line 268 of file list.h.

#define REVERSE_FOREACH_NODE ( n,
 ) 

Value:

for( \
        (n) = (TYPEOF_OR_VOIDPTR(n))LIST_TAIL(l); \
        ((Node *)(n))->pred; \
        (n) = (TYPEOF_OR_VOIDPTR(n))(((Node *)(n))->pred) \
    )
Iterate backwards over all nodes in a list.

This macro generates a "for" statement using the following parameters:

Parameters:
n Node pointer to be used in each iteration.
l Pointer to list.

Definition at line 172 of file list.h.


Typedef Documentation

typedef struct _List List

Head of a doubly-linked list of Node structs.

Lists must be initialized with LIST_INIT() prior to use.

Nodes can be added and removed from either end of the list with O(1) performance. Iterating over these lists can be tricky: use the FOREACH_NODE() macro instead.

typedef struct _Node Node

This structure represents a node for bidirectional lists.

Data is usually appended to nodes by making them the first field of another struture, as a poor-man's form of inheritance.


Function Documentation

Node* list_remHead ( List l  )  [inline]

Unlink a node from the head of the list l.

Returns:
Pointer to node, or NULL if the list was empty.

Definition at line 299 of file list.h.

Node* list_remTail ( List l  )  [inline]

Unlink a node from the tail of the list l.

Returns:
Pointer to node, or NULL if the list was empty.

Definition at line 321 of file list.h.