Add a #prefix_list_next() function to lists functions

That may seem useless, but it's not.
This commit is contained in:
Julien Danjou 2008-03-07 11:26:21 +01:00
parent 867b2f0775
commit e81bd8de02
1 changed files with 115 additions and 110 deletions

View File

@ -23,116 +23,121 @@
#ifndef AWESOME_COMMON_LIST_H #ifndef AWESOME_COMMON_LIST_H
#define AWESOME_COMMON_LIST_H #define AWESOME_COMMON_LIST_H
#define DO_SLIST(type, prefix, dtor) \ #define DO_SLIST(type, prefix, dtor) \
static inline type *prefix##_list_pop(type **list) \ static inline type *prefix##_list_next(type **list __attribute__ ((unused)), \
{ \ type *item) \
if (*list) \ { \
{ \ return item->next; \
type *res = *list; \ } \
*list = res->next; \ static inline type *prefix##_list_pop(type **list) \
res->next = NULL; \ { \
return res; \ if (*list) \
} \ { \
return NULL; \ type *res = *list; \
} \ *list = res->next; \
static inline void prefix##_list_push(type **list, type *item) \ res->next = NULL; \
{ \ return res; \
item->next = *list; \ } \
*list = item; \ return NULL; \
} \ } \
\ static inline void prefix##_list_push(type **list, type *item) \
static inline type **prefix##_list_last(type **list) \ { \
{ \ item->next = *list; \
while (*list && (*list)->next) \ *list = item; \
list = &(*list)->next; \ } \
return list; \ \
} \ static inline type **prefix##_list_last(type **list) \
\ { \
static inline void prefix##_list_append(type **list, type *item) \ while (*list && (*list)->next) \
{ \ list = &(*list)->next; \
if(*(list = prefix##_list_last(list))) \ return list; \
(*list)->next = item; \ } \
else \ \
(*list) = item; \ static inline void prefix##_list_append(type **list, type *item) \
} \ { \
static inline type *prefix##_list_rev(type *list) \ if(*(list = prefix##_list_last(list))) \
{ \ (*list)->next = item; \
type *l = NULL; \ else \
while (list) \ (*list) = item; \
prefix##_list_push(&l, prefix##_list_pop(&list)); \ } \
return l; \ static inline type *prefix##_list_rev(type *list) \
} \ { \
\ type *l = NULL; \
static inline type **prefix##_list_init(type **list) \ while (list) \
{ \ prefix##_list_push(&l, prefix##_list_pop(&list)); \
*list = NULL; \ return l; \
return list; \ } \
} \ \
static inline void prefix##_list_wipe(type **list) \ static inline type **prefix##_list_init(type **list) \
{ \ { \
while (*list) \ *list = NULL; \
{ \ return list; \
type *item = prefix##_list_pop(list); \ } \
dtor(&item); \ static inline void prefix##_list_wipe(type **list) \
} \ { \
} \ while (*list) \
static inline type *prefix##_list_prev(type **list, type *item) \ { \
{ \ type *item = prefix##_list_pop(list); \
type *tmp; \ dtor(&item); \
if(*list == item) return NULL; \ } \
for(tmp = *list; tmp && tmp->next != item; tmp = tmp->next); \ } \
return tmp; \ static inline type *prefix##_list_prev(type **list, type *item) \
} \ { \
static inline void prefix##_list_swap(type **list, type *item1, \ type *tmp; \
type *item2) \ if(*list == item) return NULL; \
{ \ for(tmp = *list; tmp && tmp->next != item; tmp = tmp->next); \
if(!item1 || !item2) return; \ return tmp; \
\ } \
type *i1n = item1->next; \ static inline void prefix##_list_swap(type **list, type *item1, \
type *i2n = item2->next; \ type *item2) \
type * i1p = prefix##_list_prev(list, item1); \ { \
type * i2p = prefix##_list_prev(list, item2); \ if(!item1 || !item2) return; \
item1->next = i2n == item1 ? item2 : i2n; \ \
item2->next = i1n == item2 ? item1 : i1n; \ type *i1n = item1->next; \
\ type *i2n = item2->next; \
\ type * i1p = prefix##_list_prev(list, item1); \
if(i1p && i1p != item2) \ type * i2p = prefix##_list_prev(list, item2); \
i1p->next = item2; \ item1->next = i2n == item1 ? item2 : i2n; \
if(i2p && i2p != item1) \ item2->next = i1n == item2 ? item1 : i1n; \
i2p->next = item1; \ \
\ \
if(*list == item1) \ if(i1p && i1p != item2) \
*list = item2; \ i1p->next = item2; \
else if(*list == item2) \ if(i2p && i2p != item1) \
*list = item1; \ i2p->next = item1; \
} \ \
static inline type *prefix##_list_prev_cycle(type **list, type *item) \ if(*list == item1) \
{ \ *list = item2; \
type *tmp = prefix##_list_prev(list, item); \ else if(*list == item2) \
if(!tmp) \ *list = item1; \
tmp = *prefix##_list_last(list); \ } \
return tmp; \ static inline type *prefix##_list_prev_cycle(type **list, type *item) \
} \ { \
static inline type *prefix##_list_next_cycle(type **list, type *item) \ type *tmp = prefix##_list_prev(list, item); \
{ \ if(!tmp) \
if(item) \ tmp = *prefix##_list_last(list); \
{ \ return tmp; \
if(item->next) \ } \
return item->next; \ static inline type *prefix##_list_next_cycle(type **list, type *item) \
else \ { \
return *list; \ if(item) \
} \ { \
return NULL; \ if(item->next) \
} \ return item->next; \
static inline void prefix##_list_detach(type **list, type *item) \ else \
{ \ return *list; \
type *prev = prefix##_list_prev(list, item); \ } \
if(prev) \ return NULL; \
prev->next = item->next; \ } \
if(item == *list) \ static inline void prefix##_list_detach(type **list, type *item) \
*list = item->next; \ { \
item->next = NULL; \ type *prev = prefix##_list_prev(list, item); \
} \ if(prev) \
prev->next = item->next; \
if(item == *list) \
*list = item->next; \
item->next = NULL; \
} \
#endif #endif
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80