blob: 4537d8d986340a3d4a161beda0c9ee9dbfcba63e [file] [log] [blame]
willy tarreau12350152005-12-18 01:03:27 +01001/*
2 This File is copied from
3
4 http://www.oreilly.com/catalog/masteralgoc/index.html
5 Mastering Algorithms with C
6 By Kyle Loudon
7 ISBN: 1-56592-453-3
8 Publishd by O'Reilly
9
10 */
11
12/*****************************************************************************
13* *
14* -------------------------------- list.h -------------------------------- *
15* *
16*****************************************************************************/
17
Willy Tarreau2dd0d472006-06-29 17:53:05 +020018#ifndef _COMMON_LIST_H
19#define _COMMON_LIST_H
willy tarreau12350152005-12-18 01:03:27 +010020
21#include <stdlib.h>
22
23/*****************************************************************************
24 * *
25 * Define a structure for linked list elements. *
26 * *
27 *****************************************************************************/
28
29typedef struct ListElmt_ {
Willy Tarreaubaaee002006-06-26 02:48:02 +020030 void *data;
31 struct ListElmt_ *next;
willy tarreau12350152005-12-18 01:03:27 +010032} ListElmt;
33
34/*****************************************************************************
35 * *
36 * Define a structure for linked lists. *
37 * *
38 *****************************************************************************/
39
40typedef struct List_ {
Willy Tarreaubaaee002006-06-26 02:48:02 +020041 int size;
42 int (*match)(const void *key1, const void *key2);
43 void (*destroy)(void *data);
willy tarreau12350152005-12-18 01:03:27 +010044
Willy Tarreaubaaee002006-06-26 02:48:02 +020045 ListElmt *head;
46 ListElmt *tail;
willy tarreau12350152005-12-18 01:03:27 +010047} List;
48
49/*****************************************************************************
50 * *
51 * --------------------------- Public Interface --------------------------- *
52 * *
53 *****************************************************************************/
54
55void list_init(List *list, void (*destroy)(void *data));
56
57void list_destroy(List *list);
58
59int list_ins_next(List *list, ListElmt *element, const void *data);
60
61int list_rem_next(List *list, ListElmt *element, void **data);
62
63#define list_size(list) ((list)->size)
64
65#define list_head(list) ((list)->head)
66
67#define list_tail(list) ((list)->tail)
68
69#define list_is_head(list, element) ((element) == (list)->head ? 1 : 0)
70
71#define list_is_tail(element) ((element)->next == NULL ? 1 : 0)
72
73#define list_data(element) ((element)->data)
74
75#define list_next(element) ((element)->next)
76
Willy Tarreau2dd0d472006-06-29 17:53:05 +020077#endif /* _COMMON_LIST_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +020078
79/*
80 * Local variables:
81 * c-indent-level: 8
82 * c-basic-offset: 8
83 * End:
84 */