willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 1 | /* |
| 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 Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 18 | #ifndef _COMMON_LIST_H |
| 19 | #define _COMMON_LIST_H |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 20 | |
| 21 | #include <stdlib.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 22 | #include <common/config.h> |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 23 | |
| 24 | /***************************************************************************** |
| 25 | * * |
| 26 | * Define a structure for linked list elements. * |
| 27 | * * |
| 28 | *****************************************************************************/ |
| 29 | |
| 30 | typedef struct ListElmt_ { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 31 | void *data; |
| 32 | struct ListElmt_ *next; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 33 | } ListElmt; |
| 34 | |
| 35 | /***************************************************************************** |
| 36 | * * |
| 37 | * Define a structure for linked lists. * |
| 38 | * * |
| 39 | *****************************************************************************/ |
| 40 | |
| 41 | typedef struct List_ { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 42 | int size; |
| 43 | int (*match)(const void *key1, const void *key2); |
| 44 | void (*destroy)(void *data); |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 45 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 46 | ListElmt *head; |
| 47 | ListElmt *tail; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 48 | } List; |
| 49 | |
| 50 | /***************************************************************************** |
| 51 | * * |
| 52 | * --------------------------- Public Interface --------------------------- * |
| 53 | * * |
| 54 | *****************************************************************************/ |
| 55 | |
| 56 | void list_init(List *list, void (*destroy)(void *data)); |
| 57 | |
| 58 | void list_destroy(List *list); |
| 59 | |
| 60 | int list_ins_next(List *list, ListElmt *element, const void *data); |
| 61 | |
| 62 | int 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 Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 78 | #endif /* _COMMON_LIST_H */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 79 | |
| 80 | /* |
| 81 | * Local variables: |
| 82 | * c-indent-level: 8 |
| 83 | * c-basic-offset: 8 |
| 84 | * End: |
| 85 | */ |