]> rtime.felk.cvut.cz Git - canping.git/blob - include/ul_list.h
Second version of OCERA casestudies. This version fixes many errors, enhances the...
[canping.git] / include / ul_list.h
1 #ifndef _UL_LISTS_H
2 #define _UL_LISTS_H
3
4 #include "ul_utdefs.h"
5 #include "ul_listbase.h"
6 #include "ul_itbase.h"
7
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11
12 typedef struct list_head ul_list_node_t;
13 typedef struct list_head ul_list_head_t;
14
15 #define UL_LIST_CUST_DEC(cust_prefix, cust_head_t, cust_item_t,                 \
16                 cust_head_field, cust_node_field)                               \
17                                                                                 \
18 static inline cust_item_t *                                                     \
19 cust_prefix##_node2item(const cust_head_t *head, const ul_list_node_t *node)    \
20   {return UL_CONTAINEROF(node, cust_item_t, cust_node_field);}                  \
21                                                                                 \
22 static inline void                                                              \
23 cust_prefix##_init_head(cust_head_t *head)                                      \
24 {                                                                               \
25   INIT_LIST_HEAD(&head->cust_head_field);                                       \
26 }                                                                               \
27 static inline void                                                              \
28 cust_prefix##_init_detached(cust_item_t *item){                                 \
29   INIT_LIST_HEAD(&item->cust_node_field);                                       \
30 }                                                                               \
31 static inline cust_item_t *                                                     \
32 cust_prefix##_first(const cust_head_t *head)                                    \
33 {                                                                               \
34   ul_list_node_t *n=head->cust_head_field.next;                                 \
35   return (n!=&head->cust_head_field)?cust_prefix##_node2item(head,n):NULL;      \
36 }                                                                               \
37 static inline cust_item_t *                                                     \
38 cust_prefix##_last(const cust_head_t *head)                                     \
39 {                                                                               \
40   ul_list_node_t *n=head->cust_head_field.prev;                                 \
41   return (n!=&head->cust_head_field)?cust_prefix##_node2item(head,n):NULL;      \
42 }                                                                               \
43 static inline cust_item_t *                                                     \
44 cust_prefix##_next(const cust_head_t *head, const cust_item_t *item)            \
45 {                                                                               \
46   ul_list_node_t *n=item->cust_node_field.next;                                 \
47   return (n!=&head->cust_head_field)?cust_prefix##_node2item(head,n):NULL;      \
48 }                                                                               \
49 static inline cust_item_t *                                                     \
50 cust_prefix##_prev(const cust_head_t *head, const cust_item_t *item)            \
51 {                                                                               \
52   ul_list_node_t *n=item->cust_node_field.prev;                                 \
53   return (n!=&head->cust_head_field)?cust_prefix##_node2item(head,n):NULL;      \
54 }                                                                               \
55 static inline int                                                               \
56 cust_prefix##_is_empty(const cust_head_t *head)                                 \
57 {                                                                               \
58   return head->cust_head_field.next==&head->cust_head_field;                    \
59 }                                                                               \
60 static inline void                                                              \
61 cust_prefix##_ins_head(cust_head_t *head, cust_item_t *item)                    \
62 {                                                                               \
63   list_add(&item->cust_node_field, &head->cust_head_field);                     \
64 }                                                                               \
65 static inline void                                                              \
66 cust_prefix##_ins_tail(cust_head_t *head, cust_item_t *item)                    \
67 {                                                                               \
68   list_add_tail(&item->cust_node_field, &head->cust_head_field);                \
69 }                                                                               \
70 static inline void                                                              \
71 cust_prefix##_insert(cust_head_t *head, cust_item_t *item)                      \
72 {                                                                               \
73   cust_prefix##_ins_tail(head, item);                                           \
74 }                                                                               \
75 static inline void                                                              \
76 cust_prefix##_delete(cust_head_t *head, cust_item_t *item)                      \
77 {                                                                               \
78   list_del_init(&item->cust_node_field);                                        \
79 }                                                                               \
80 static inline void                                                              \
81 cust_prefix##_del_item(cust_item_t *item)                                       \
82 {                                                                               \
83   list_del_init(&item->cust_node_field);                                        \
84 }                                                                               \
85 static inline cust_item_t *                                                     \
86 cust_prefix##_cut_first(cust_head_t *head)                                      \
87 {                                                                               \
88   ul_list_node_t *n=head->cust_head_field.next;                                 \
89   if(n==&head->cust_head_field) return NULL;                                    \
90   list_del_init(n);                                                             \
91   return cust_prefix##_node2item(head,n);                                       \
92 }                                                                               \
93 /*** Iterators ***/                                                             \
94 UL_ITBASE_UL_DEC(cust_prefix, cust_head_t, cust_item_t)
95
96
97 #define ul_list_for_each(cust_prefix, head, ptr)                                        \
98         for(ptr=cust_prefix##_first(head);ptr;ptr=cust_prefix##_next((head),ptr))
99
100 #define ul_list_for_each_rev(cust_prefix, head, ptr)                                    \
101         for(ptr=cust_prefix##_last(head);ptr;ptr=cust_prefix##_prev((head),ptr))
102
103 #define ul_list_for_each_cut(cust_prefix, head, ptr)    \
104         for(;(ptr=cust_prefix##_cut_first(head));)
105
106 #ifdef __cplusplus
107 } /* extern "C"*/
108 #endif
109
110 #endif /* _UL_LISTS_H */