58 # define LLIST_MACRO static inline 61 # define LLIST_MACRO static __inline__ 63 # define LLIST_MACRO static 67 #if defined(LLIST_INTERFACE) 69 #define LLIST_FUNC(proto, ...) proto 70 #elif defined(LLIST_IMPLEMENTATION) 72 #define LLIST_FUNC(proto, ...) proto { __VA_ARGS__ } 75 #define LLIST_FUNC(proto, ...) LLIST_MACRO proto { __VA_ARGS__ } 91 typedef llist * LList;
92 typedef const llist * const_LList;
93 typedef llist ** LList_ref;
99 #define LLIST_AUTO(name) llist name = {&name,&name} 105 #define llist_insert_head(list, element) llist_insert_next (list, element) 106 #define llist_insert_tail(list, element) llist_insert_prev (list, element) 107 #define llist_head llist_next 108 #define llist_tail llist_prev 121 #define LLIST_TO_STRUCTP(llist, type, member) \ 122 ((type*)(((char*)(llist)) - offsetof(type, member))) 129 #define LLIST_FOREACH(list, node) \ 130 for (LList node = llist_head (list); \ 131 ! llist_is_end (node, list); \ 132 llist_forward (&node)) 139 #define LLIST_FOREACH_REV(list, node) \ 140 for (LList node = llist_tail (list); \ 141 ! llist_is_end (node, list); \ 142 llist_backward (&node)) 151 #define LLIST_FORRANGE(start, end, node) \ 152 for (LList node = start; \ 154 llist_forward (&node)) 162 #define LLIST_FORRANGE_REV(rstart, rend, node) \ 163 for (LList node = rstart; \ 165 llist_backward (&node)) 174 #define LLIST_WHILE_HEAD(list, head) \ 175 for (LList head = llist_head (list); \ 176 !llist_is_empty (list); \ 177 head = llist_head (list)) 185 #define LLIST_WHILE_TAIL(list, tail) \ 186 for (LList tail = llist_tail (list); \ 187 !llist_is_empty (list); \ 188 tail = llist_tail (list)) 197 return self->next = self->prev =
self;
203 LLIST_FUNC (
int llist_is_empty (const_LList
self),
204 return self->next ==
self;
211 LLIST_FUNC (
int llist_is_single (const_LList
self),
212 return self->next->next ==
self;
220 LLIST_FUNC (
int llist_is_head (const_LList
self, const_LList head),
221 return self->next == head;
229 LLIST_FUNC (
int llist_is_tail (const_LList
self, const_LList tail),
230 return self->prev == tail;
239 LLIST_FUNC (
int llist_is_end (const_LList
self, const_LList end),
248 LLIST_FUNC (
int llist_is_member (const_LList
self, const_LList member),
249 for (const_LList i = member->next; i != member; i = i->next)
263 LLIST_FUNC (
int llist_is_before_after (const_LList
self, const_LList before, const_LList after),
264 for (const_LList i = before->next; i !=
self; i = i->next)
277 LLIST_FUNC (
unsigned llist_count (const_LList
self),
279 const_LList i =
self;
280 for (; i->next !=
self; ++cnt, i = i->next);
285 LLIST_FUNC (
void llist_unlink_fast_ (LList
self),
286 LList nxt = self->next, pre = self->prev;
297 llist_unlink_fast_ (
self);
298 return self->next =
self->prev =
self;
310 LLIST_FUNC (LList llist_relocate (LList
self),
311 return self->next->prev = self->prev->next =
self;
321 LLIST_FUNC (LList llist_insert_next (LList
self, LList next),
322 llist_unlink_fast_ (next);
323 self->next->prev = next;
325 next->next =
self->next;
336 LLIST_FUNC (LList llist_insert_prev (LList
self, LList prev),
337 llist_unlink_fast_ (prev);
338 self->prev->next = prev;
340 prev->prev =
self->prev;
353 LLIST_FUNC (LList llist_insertlist_next (LList
self, LList next),
354 if (!llist_is_empty (next))
356 self->next->prev = next->prev;
357 next->prev->next =
self->next;
358 self->next = next->next;
359 next->next->prev =
self;
361 next->prev = next->next = next;
373 LLIST_FUNC (LList llist_insertlist_prev (LList
self, LList prev),
374 if (!llist_is_empty (prev))
376 self->prev->next = prev->next;
377 prev->next->prev =
self->prev;
378 self->prev = prev->prev;
379 prev->prev->next =
self;
381 prev->prev = prev->next = prev;
386 #if 0 //BUG("needs temporary") 393 LLIST_FUNC (LList llist_insertafter_range (LList
self, LList start, LList end),
394 self->next->prev = end->prev;
395 end->prev->next = self->next;
396 end->prev = start->prev;
397 start->prev->next = end;
404 #if 0 //BUG("needs temporary") 411 LLIST_FUNC (LList llist_inserbefore_range (LList
self, LList start, LList end),
412 self->prev->next = start;
413 start->prev->next = end;
414 end->prev = start->prev;
415 start->prev = self->prev;
416 self->prev = end->prev;
417 end->prev->next =
self;
429 LList tmp = self->next->next;
431 self->next->prev = self->prev;
432 self->prev->next = self->next;
433 self->prev = self->next;
434 self->next->next =
self;
446 LList tmp = self->prev->prev;
448 self->prev->next = self->next;
449 self->next->prev = self->prev;
450 self->next = self->prev;
451 self->prev->prev =
self;
463 LLIST_FUNC (LList llist_next (const_LList
self),
473 LLIST_FUNC (LList llist_prev (const_LList
self),
482 LLIST_FUNC (
void llist_forward (LList_ref
self),
483 *
self = (*self)->next;
491 LLIST_FUNC (
void llist_backward (LList_ref
self),
492 *
self = (*self)->prev;
501 LLIST_FUNC (LList llist_nth (LList
self,
int n),
504 self = llist_next (
self);
507 self = llist_prev (
self);
517 LLIST_FUNC (LList llist_get_nth_stop (LList
self,
int n, const_LList stop),
521 self = llist_next (
self);
528 self = llist_prev (
self);
545 typedef int (*
llist_cmpfn)(const_LList a, const_LList b,
void* extra);
561 if (!llist_is_single (
self))
564 llist_insert_prev (++n & 1 ? &left : &right, head);
566 llist_sort (&left, cmp, extra);
567 llist_sort (&right, cmp, extra);
569 while (!llist_is_empty (&left) && !llist_is_empty (&right))
570 llist_insert_prev (
self, cmp (left.next, right.next, extra) < 0 ? left.next : right.next);
572 if (!llist_is_empty (&left))
573 llist_insertlist_prev (
self, &left);
574 if (!llist_is_empty (&right))
575 llist_insertlist_prev (
self, &right);
593 if (!cmp (node, templ, extra))
612 if (!cmp (node, templ, extra))
614 if (llist_next(
self) != node)
615 llist_insert_next (
self, node);
635 int c = cmp (node, templ, extra);
int(* llist_cmpfn)(const_LList a, const_LList b, void *extra)
The comparison function function type.
#define LLIST_FOREACH(list, node)
Iterate forward over a list.
LLIST_FUNC(LList llist_init(LList self), return self->next=self->prev=self;)
Initialise a new llist.
#define LLIST_WHILE_HEAD(list, head)
Consume a list from head.