blob: 91a5084ddaceddcd82a7ca6c839137e6323f5919 [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>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020022#include <common/config.h>
willy tarreau12350152005-12-18 01:03:27 +010023
24/*****************************************************************************
25 * *
26 * Define a structure for linked list elements. *
27 * *
28 *****************************************************************************/
29
30typedef struct ListElmt_ {
Willy Tarreaubaaee002006-06-26 02:48:02 +020031 void *data;
32 struct ListElmt_ *next;
willy tarreau12350152005-12-18 01:03:27 +010033} ListElmt;
34
35/*****************************************************************************
36 * *
37 * Define a structure for linked lists. *
38 * *
39 *****************************************************************************/
40
41typedef struct List_ {
Willy Tarreaubaaee002006-06-26 02:48:02 +020042 int size;
43 int (*match)(const void *key1, const void *key2);
44 void (*destroy)(void *data);
willy tarreau12350152005-12-18 01:03:27 +010045
Willy Tarreaubaaee002006-06-26 02:48:02 +020046 ListElmt *head;
47 ListElmt *tail;
willy tarreau12350152005-12-18 01:03:27 +010048} List;
49
50/*****************************************************************************
51 * *
52 * --------------------------- Public Interface --------------------------- *
53 * *
54 *****************************************************************************/
55
56void list_init(List *list, void (*destroy)(void *data));
57
58void list_destroy(List *list);
59
60int list_ins_next(List *list, ListElmt *element, const void *data);
61
62int list_rem_next(List *list, ListElmt *element, void **data);
63
64#define list_size(list) ((list)->size)
65
66#define list_head(list) ((list)->head)
67
68#define list_tail(list) ((list)->tail)
69
70#define list_is_head(list, element) ((element) == (list)->head ? 1 : 0)
71
72#define list_is_tail(element) ((element)->next == NULL ? 1 : 0)
73
74#define list_data(element) ((element)->data)
75
76#define list_next(element) ((element)->next)
77
Willy Tarreau2dd0d472006-06-29 17:53:05 +020078#endif /* _COMMON_LIST_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +020079
80/*
81 * Local variables:
82 * c-indent-level: 8
83 * c-basic-offset: 8
84 * End:
85 */