]> mj.ucw.cz Git - libucw.git/blobdiff - lib/clists.h
Don't forget to increase run counter.
[libucw.git] / lib / clists.h
index e9826eb6601f81f021e6d9019377bde97fd7193c..4f1848d8d2fc903a9327cab05df40c444df6a171 100644 (file)
@@ -1,14 +1,14 @@
 /*
 /*
- *     Sherlock Library -- Circular Linked Lists
+ *     UCW Library -- Circular Linked Lists
  *
  *
- *     (c) 2003 Martin Mares <mj@ucw.cz>
+ *     (c) 2003--2005 Martin Mares <mj@ucw.cz>
  *
  *     This software may be freely distributed and used according to the terms
  *     of the GNU Lesser General Public License.
  */
 
  *
  *     This software may be freely distributed and used according to the terms
  *     of the GNU Lesser General Public License.
  */
 
-#ifndef _SHERLOCK_CLISTS_H
-#define _SHERLOCK_CLISTS_H
+#ifndef _UCW_CLISTS_H
+#define _UCW_CLISTS_H
 
 typedef struct cnode {
   struct cnode *next, *prev;
 
 typedef struct cnode {
   struct cnode *next, *prev;
@@ -38,25 +38,44 @@ static inline void *clist_prev(clist *l, cnode *n)
   return (n->prev != &l->head) ? (void *) n->prev : NULL;
 }
 
   return (n->prev != &l->head) ? (void *) n->prev : NULL;
 }
 
+static inline int clist_empty(clist *l)
+{
+  return (l->head.next == &l->head);
+}
+
 #define CLIST_WALK(n,list) for(n=(void*)(list).head.next; (cnode*)(n) != &(list).head; n=(void*)((cnode*)(n))->next)
 #define CLIST_WALK(n,list) for(n=(void*)(list).head.next; (cnode*)(n) != &(list).head; n=(void*)((cnode*)(n))->next)
+#define CLIST_WALK_DELSAFE(n,list,tmp) for(n=(void*)(list).head.next; tmp=(void*)((cnode*)(n))->next, (cnode*)(n) != &(list).head; n=(void*)tmp)
+#define CLIST_FOR_EACH(type,n,list) for(type n=(void*)(list).head.next; (cnode*)(n) != &(list).head; n=(void*)((cnode*)(n))->next)
+#define CLIST_FOR_EACH_DELSAFE(type,n,list,tmp) for(type n=(void*)(list).head.next; tmp=(void*)((cnode*)(n))->next, (cnode*)(n) != &(list).head; n=(void*)tmp)
+
+#define CLIST_FOR_EACH_BACKWARDS(type,n,list) for(type n=(void*)(list).head.prev; (cnode*)(n) != &(list).head; n=(void*)((cnode*)(n))->prev)
 
 
-static inline void clist_insert(cnode *what, cnode *after)
+static inline void clist_insert_after(cnode *what, cnode *after)
 {
   cnode *before = after->next;
   what->next = before;
 {
   cnode *before = after->next;
   what->next = before;
-  what->prev = before->prev;
+  what->prev = after;
+  before->prev = what;
+  after->next = what;
+}
+
+static inline void clist_insert_before(cnode *what, cnode *before)
+{
+  cnode *after = before->prev;
+  what->next = before;
+  what->prev = after;
   before->prev = what;
   after->next = what;
 }
 
 static inline void clist_add_tail(clist *l, cnode *n)
 {
   before->prev = what;
   after->next = what;
 }
 
 static inline void clist_add_tail(clist *l, cnode *n)
 {
-  clist_insert(n, l->head.prev);
+  clist_insert_before(n, &l->head);
 }
 
 static inline void clist_add_head(clist *l, cnode *n)
 {
 }
 
 static inline void clist_add_head(clist *l, cnode *n)
 {
-  clist_insert(n, &l->head);
+  clist_insert_after(n, &l->head);
 }
 
 static inline void clist_remove(cnode *n)
 }
 
 static inline void clist_remove(cnode *n)
@@ -73,4 +92,17 @@ static inline void clist_init(clist *l)
   head->next = head->prev = head;
 }
 
   head->next = head->prev = head;
 }
 
+static inline void clist_insert_list_after(clist *what, cnode *after)
+{
+  if (!clist_empty(what))
+    {
+      cnode *w = &what->head;
+      w->prev->next = after->next;
+      after->next->prev = w->prev;
+      w->next->prev = after;
+      after->next = w->next;
+      clist_init(what);
+    }
+}
+
 #endif
 #endif