added list_prepend method
authorkramm <kramm>
Tue, 2 Dec 2008 17:06:27 +0000 (17:06 +0000)
committerkramm <kramm>
Tue, 2 Dec 2008 17:06:27 +0000 (17:06 +0000)
lib/q.c
lib/q.h

diff --git a/lib/q.c b/lib/q.c
index 543d579..989c47f 100644 (file)
--- a/lib/q.c
+++ b/lib/q.c
@@ -987,7 +987,7 @@ void list_append_(void*_list, void*entry)
     commonlist_t**list = (commonlist_t**)_list;
     commonlist_t* n = 0;
     if(!*list) {
-        n = malloc(sizeof(commonlist_t)+sizeof(listinfo_t));
+        n = (commonlist_t*)malloc(sizeof(commonlist_t)+sizeof(listinfo_t));
         *list = n;
         (*list)->info[0].size = 0;
     } else {
@@ -999,6 +999,23 @@ void list_append_(void*_list, void*entry)
     (*list)->info[0].last = n;
     (*list)->info[0].size++;
 }
+/* notice: prepending uses slighly more space than appending */
+void list_prepend_(void*_list, void*entry)
+{
+    commonlist_t**list = (commonlist_t**)_list;
+    commonlist_t* n = (commonlist_t*)malloc(sizeof(commonlist_t)+sizeof(listinfo_t));
+    int size = 0;
+    commonlist_t* last = 0;
+    if(*list) {
+        last = (*list)->info[0].last;
+        size = (*list)->info[0].size;
+    }
+    n->next = *list;
+    n->entry = entry;
+    *list = n;
+    (*list)->info[0].last = last;
+    (*list)->info[0].size = size+1;
+}
 void list_free_(void*_list) 
 {
     commonlist_t**list = (commonlist_t**)_list;
diff --git a/lib/q.h b/lib/q.h
index 64d8fda..58064f3 100644 (file)
--- a/lib/q.h
+++ b/lib/q.h
@@ -206,9 +206,11 @@ typedef struct _##x##_list x##_list_t;
 int list_length_(void*_list);
 void*list_clone_(void*_list);
 void list_append_(void*_list, void*entry);
+void list_prepend_(void*_list, void*entry);
 void list_free_(void*_list);
 #define list_new() ((void*)0)
 #define list_append(list, e) {sizeof((list)->next);list_append_(&(list),(e));}
+#define list_prepend(list, e) {sizeof((list)->next);list_prepend_(&(list),(e));}
 #define list_free(list) {sizeof((list)->next);list_free_(&(list));}
 #define list_clone(list) (sizeof((list)->next),list_clone_(&(list)))
 #define list_length(list) (sizeof((list)->next),list_length_(list))