added some qualifiers + removed pointer alias in mergesort

Thu, 11 Oct 2012 11:42:31 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 11 Oct 2012 11:42:31 +0200
changeset 67
27e67e725d35
parent 66
fcfe8c5e9fe1
child 68
88dbea299440

added some qualifiers + removed pointer alias in mergesort

ucx/buffer.c file | annotate | diff | comparison | revisions
ucx/buffer.h file | annotate | diff | comparison | revisions
ucx/dlist.c file | annotate | diff | comparison | revisions
ucx/dlist.h file | annotate | diff | comparison | revisions
ucx/list.c file | annotate | diff | comparison | revisions
ucx/list.h file | annotate | diff | comparison | revisions
ucx/logging.c file | annotate | diff | comparison | revisions
ucx/logging.h file | annotate | diff | comparison | revisions
ucx/map.c file | annotate | diff | comparison | revisions
ucx/map.h file | annotate | diff | comparison | revisions
ucx/string.c file | annotate | diff | comparison | revisions
ucx/string.h file | annotate | diff | comparison | revisions
--- a/ucx/buffer.c	Thu Oct 11 08:42:56 2012 +0200
+++ b/ucx/buffer.c	Thu Oct 11 11:42:31 2012 +0200
@@ -33,8 +33,8 @@
     free(buffer);
 }
 
-UcxBuffer *ucx_buffer_extract(
-        UcxBuffer *src, size_t start, size_t length, int flags) {
+UcxBuffer *restrict ucx_buffer_extract(
+        UcxBuffer *restrict src, size_t start, size_t length, int flags) {
     if (length == 0) {
         length = src->size - start;
     }
@@ -42,7 +42,7 @@
         return NULL;
     }
 
-    UcxBuffer *dst = (UcxBuffer*) malloc(sizeof(UcxBuffer));
+    UcxBuffer *restrict dst = (UcxBuffer*) malloc(sizeof(UcxBuffer));
     if (dst) {
         dst->space = malloc(length);
         if (!dst->space) {
--- a/ucx/buffer.h	Thu Oct 11 08:42:56 2012 +0200
+++ b/ucx/buffer.h	Thu Oct 11 11:42:31 2012 +0200
@@ -30,7 +30,7 @@
  * if length is zero, the whole remaining buffer shall be extracted
  * the position of the new buffer is set to zero
  */
-UcxBuffer *ucx_buffer_extract(UcxBuffer *src,
+UcxBuffer *restrict ucx_buffer_extract(UcxBuffer *restrict src,
         size_t start, size_t length, int flags);
 #define ucx_buffer_clone(src,flags) \
     ucx_buffer_extract(src, 0, 0, flags)
--- a/ucx/dlist.c	Thu Oct 11 08:42:56 2012 +0200
+++ b/ucx/dlist.c	Thu Oct 11 11:42:31 2012 +0200
@@ -1,6 +1,7 @@
 #include "dlist.h"
 
-UcxDlist *ucx_dlist_clone(UcxDlist *l, copy_func fnc, void *data) {
+UcxDlist *restrict ucx_dlist_clone(UcxDlist *restrict l,
+        copy_func fnc, void *data) {
     UcxDlist *ret = NULL;
     while (l != NULL) {
         if (fnc != NULL) {
@@ -13,7 +14,8 @@
     return ret;
 }
 
-int ucx_dlist_equals(UcxDlist *l1, UcxDlist *l2, cmp_func fnc, void* data) {
+int ucx_dlist_equals(const UcxDlist *l1, const UcxDlist *l2,
+        cmp_func fnc, void* data) {
     if (l1 == l2) return 1;
     
     while (l1 != NULL && l2 != NULL) {
@@ -66,7 +68,7 @@
     return nl;
 }
 
-UcxDlist *ucx_dlist_concat(UcxDlist *l1, UcxDlist *l2) {
+UcxDlist *ucx_dlist_concat(UcxDlist *restrict l1, UcxDlist *restrict l2) {
     if (l1 == NULL) {
         return l2;
     } else {
@@ -77,32 +79,32 @@
     }
 }
 
-UcxDlist *ucx_dlist_last(UcxDlist *l) {
+UcxDlist *ucx_dlist_last(const UcxDlist *l) {
     if (l == NULL) return NULL;
     
-    UcxDlist *e = l;
+    const UcxDlist *e = l;
     while (e->next != NULL) {
         e = e->next;
     }
-    return e;
+    return (UcxDlist*)e;
 }
 
-UcxDlist *ucx_dlist_get(UcxDlist *l, int index) {
+UcxDlist *ucx_dlist_get(const UcxDlist *l, int index) {
     if (l == NULL) return NULL;
 
-    UcxDlist *e = l;
+    const UcxDlist *e = l;
     while (e->next != NULL && index > 0) {
         e = e->next;
         index--;
     }
     
-    return index == 0 ? e : NULL;
+    return (UcxDlist*)(index == 0 ? e : NULL);
 }
 
-size_t ucx_dlist_size(UcxDlist *l) {
+size_t ucx_dlist_size(const UcxDlist *l) {
     if (l == NULL) return 0;
     
-    UcxDlist *e = l;
+    const UcxDlist *e = l;
     size_t s = 1;
     while (e->next != NULL) {
         e = e->next;
@@ -113,14 +115,14 @@
 }
 
 UcxDlist *ucx_dlist_sort_merge(int length,
-        UcxDlist* ls, UcxDlist* rs, UcxDlist* le, UcxDlist* re,
+        UcxDlist* restrict ls, UcxDlist* restrict le, UcxDlist* restrict re,
         cmp_func fnc, void* data) {
     UcxDlist *sorted[length];
     UcxDlist *rc, *lc;
 
-    lc = ls; rc = rs;
+    lc = ls; rc = le;
     int n = 0;
-    while (lc != le && rc != re) {
+    while (lc && lc != le && rc != re) {
         if (fnc(lc->data, rc->data, data) <= 0) {
             sorted[n] = lc;
             lc = lc->next;
@@ -130,12 +132,12 @@
         }
         n++;
     }
-    while (lc != le) {
+    while (lc && lc != le) {
         sorted[n] = lc;
         lc = lc->next;
         n++;
     }
-    while (rc != re) {
+    while (rc && rc != re) {
         sorted[n] = rc;
         rc = rc->next;
         n++;
@@ -160,7 +162,7 @@
     UcxDlist *lc;
     int ln = 1;
 
-    UcxDlist *ls = l, *le;
+    UcxDlist *restrict ls = l, *restrict le, *restrict re;
     lc = ls;
     while (lc->next != NULL && fnc(lc->next->data, lc->data, data) > 0) {
         lc = lc->next;
@@ -168,13 +170,12 @@
     }
     le = lc->next;
 
-    UcxDlist *rs = le, *re;
-    if (rs == NULL) {
+    if (le == NULL) {
         return l; // this list is already sorted :)
     } else {
         UcxDlist *rc;
         int rn = 1;
-        rc = rs;
+        rc = le;
         while (rc->next != NULL && fnc(rc->next->data, rc->data, data) > 0) {
             rc = rc->next;
             rn++;
@@ -190,27 +191,26 @@
 
         // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them
         UcxDlist *sorted = ucx_dlist_sort_merge(ln+rn,
-                ls, rs, le, re,
+                ls, le, re,
                 fnc, data);
 
         // merge sorted list with (also sorted) remainder
         l = ucx_dlist_sort_merge(ln+rn+remainder_length,
-                sorted, remainder, NULL, NULL,
-                fnc, data);
+                sorted, remainder, NULL, fnc, data);
 
         return l;
     }
 }
 
 /* dlist specific functions */
-UcxDlist *ucx_dlist_first(UcxDlist *l) {
+UcxDlist *ucx_dlist_first(const UcxDlist *l) {
     if (l == NULL) return NULL;
     
-    UcxDlist *e = l;
+    const UcxDlist *e = l;
     while (e->prev != NULL) {
         e = e->prev;
     }
-    return e;
+    return (UcxDlist *)e;
 }
 
 UcxDlist *ucx_dlist_remove(UcxDlist *l, UcxDlist *e) {
--- a/ucx/dlist.h	Thu Oct 11 08:42:56 2012 +0200
+++ b/ucx/dlist.h	Thu Oct 11 11:42:31 2012 +0200
@@ -15,25 +15,27 @@
 typedef struct UcxDlist UcxDlist;
 struct UcxDlist {
     void     *data;
-    UcxDlist *next;
-    UcxDlist *prev;
+    UcxDlist *restrict next;
+    UcxDlist *restrict prev;
 };
 
-UcxDlist *ucx_dlist_clone(UcxDlist *l, copy_func fnc, void* data);
-int ucx_dlist_equals(UcxDlist *l1, UcxDlist *l2, cmp_func fnc, void* data);
+UcxDlist *restrict ucx_dlist_clone(UcxDlist *restrict l,
+        copy_func fnc, void* data);
+int ucx_dlist_equals(const UcxDlist *l1, const UcxDlist *l2,
+        cmp_func fnc, void* data);
 
 void ucx_dlist_free(UcxDlist *l);
 UcxDlist *ucx_dlist_append(UcxDlist *l, void *data);
 UcxDlist *ucx_dlist_prepend(UcxDlist *l, void *data);
-UcxDlist *ucx_dlist_concat(UcxDlist *l1, UcxDlist *l2);
-UcxDlist *ucx_dlist_last(UcxDlist *l);
-UcxDlist *ucx_dlist_get(UcxDlist *l, int index);
-size_t ucx_dlist_size(UcxDlist *l);
+UcxDlist *ucx_dlist_concat(UcxDlist *restrict l1, UcxDlist *restrict l2);
+UcxDlist *ucx_dlist_last(const UcxDlist *l);
+UcxDlist *ucx_dlist_get(const UcxDlist *l, int index);
+size_t ucx_dlist_size(const UcxDlist *l);
 
 UcxDlist *ucx_dlist_sort(UcxDlist *l, cmp_func fnc, void *data);
 
 /* dlist specific functions */
-UcxDlist *ucx_dlist_first(UcxDlist *l);
+UcxDlist *ucx_dlist_first(const UcxDlist *l);
 UcxDlist *ucx_dlist_remove(UcxDlist *l, UcxDlist *e);
 
 #ifdef	__cplusplus
--- a/ucx/list.c	Thu Oct 11 08:42:56 2012 +0200
+++ b/ucx/list.c	Thu Oct 11 11:42:31 2012 +0200
@@ -1,6 +1,7 @@
 #include "list.h"
 
-UcxList *ucx_list_clone(UcxList *l, copy_func fnc, void *data) {
+UcxList *restrict ucx_list_clone(UcxList *restrict l,
+        copy_func fnc, void *data) {
     UcxList *ret = NULL;
     while (l != NULL) {
         if (fnc != NULL) {
@@ -13,7 +14,8 @@
     return ret;
 }
 
-int ucx_list_equals(UcxList *l1, UcxList *l2, cmp_func fnc, void* data) {
+int ucx_list_equals(const UcxList *l1, const UcxList *l2,
+        cmp_func fnc, void* data) {
     if (l1 == l2) return 1;
     
     while (l1 != NULL && l2 != NULL) {
@@ -63,7 +65,7 @@
     return nl;
 }
 
-UcxList *ucx_list_concat(UcxList *l1, UcxList *l2) {
+UcxList *ucx_list_concat(UcxList *restrict l1, UcxList *restrict l2) {
     if (l1 == NULL) {
         return l2;
     } else {
@@ -73,32 +75,32 @@
     }
 }
 
-UcxList *ucx_list_last(UcxList *l) {
+UcxList *ucx_list_last(const UcxList *l) {
     if (l == NULL) return NULL;
     
-    UcxList *e = l;
+    const UcxList *e = l;
     while (e->next != NULL) {
         e = e->next;
     }
-    return e;
+    return (UcxList*)e;
 }
 
-UcxList *ucx_list_get(UcxList *l, int index) {
+UcxList *ucx_list_get(const UcxList *l, int index) {
     if (l == NULL) return NULL;
 
-    UcxList *e = l;
+    const UcxList *e = l;
     while (e->next != NULL && index > 0) {
         e = e->next;
         index--;
     }
     
-    return index == 0 ? e : NULL;
+    return (UcxList*)(index == 0 ? e : NULL);
 }
 
-size_t ucx_list_size(UcxList *l) {
+size_t ucx_list_size(const UcxList *l) {
     if (l == NULL) return 0;
     
-    UcxList *e = l;
+    const UcxList *e = l;
     size_t s = 1;
     while (e->next != NULL) {
         e = e->next;
@@ -109,14 +111,14 @@
 }
 
 UcxList *ucx_list_sort_merge(int length,
-        UcxList* ls, UcxList* rs, UcxList* le, UcxList* re,
+        UcxList* restrict ls, UcxList* restrict le, UcxList* restrict re,
         cmp_func fnc, void* data) {
     UcxList *sorted[length];
     UcxList *rc, *lc;
 
-    lc = ls; rc = rs;
+    lc = ls; rc = le;
     int n = 0;
-    while (lc != le && rc != re) {
+    while (lc && lc != le && rc != re) {
         if (fnc(lc->data, rc->data, data) <= 0) {
             sorted[n] = lc;
             lc = lc->next;
@@ -126,12 +128,12 @@
         }
         n++;
     }
-    while (lc != le) {
+    while (lc && lc != le) {
         sorted[n] = lc;
         lc = lc->next;
         n++;
     }
-    while (rc != re) {
+    while (rc && rc != re) {
         sorted[n] = rc;
         rc = rc->next;
         n++;
@@ -154,7 +156,7 @@
     UcxList *lc;
     int ln = 1;
 
-    UcxList *ls = l, *le;
+    UcxList *restrict ls = l, *restrict le, *restrict re;
     lc = ls;
     while (lc->next != NULL && fnc(lc->next->data, lc->data, data) > 0) {
         lc = lc->next;
@@ -162,13 +164,12 @@
     }
     le = lc->next;
 
-    UcxList *rs = le, *re;
-    if (rs == NULL) {
+    if (le == NULL) {
         return l; // this list is already sorted :)
     } else {
         UcxList *rc;
         int rn = 1;
-        rc = rs;
+        rc = le;
         while (rc->next != NULL && fnc(rc->next->data, rc->data, data) > 0) {
             rc = rc->next;
             rn++;
@@ -184,13 +185,12 @@
 
         // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them
         UcxList *sorted = ucx_list_sort_merge(ln+rn,
-                ls, rs, le, re,
+                ls, le, re,
                 fnc, data);
 
         // merge sorted list with (also sorted) remainder
         l = ucx_list_sort_merge(ln+rn+remainder_length,
-                sorted, remainder, NULL, NULL,
-                fnc, data);
+                sorted, remainder, NULL, fnc, data);
 
         return l;
     }
--- a/ucx/list.h	Thu Oct 11 08:42:56 2012 +0200
+++ b/ucx/list.h	Thu Oct 11 11:42:31 2012 +0200
@@ -18,16 +18,18 @@
     UcxList *next;
 };
 
-UcxList *ucx_list_clone(UcxList *l, copy_func fnc, void *data);
-int ucx_list_equals(UcxList *l1, UcxList *l2, cmp_func fnc, void *data);
+UcxList *restrict ucx_list_clone(UcxList *restrict l,
+        copy_func fnc, void *data);
+int ucx_list_equals(const UcxList *l1, const UcxList *l2,
+        cmp_func fnc, void *data);
 
 void ucx_list_free(UcxList *l);
 UcxList *ucx_list_append(UcxList *l, void *data);
 UcxList *ucx_list_prepend(UcxList *l, void *data);
-UcxList *ucx_list_concat(UcxList *l1, UcxList *l2);
-UcxList *ucx_list_last(UcxList *l);
-UcxList *ucx_list_get(UcxList *l, int index);
-size_t ucx_list_size(UcxList *l);
+UcxList *ucx_list_concat(UcxList *restrict l1, UcxList *restrict l2);
+UcxList *ucx_list_last(const UcxList *l);
+UcxList *ucx_list_get(const UcxList *l, int index);
+size_t ucx_list_size(const UcxList *l);
 
 UcxList *ucx_list_sort(UcxList *l, cmp_func fnc, void *data);
 
--- a/ucx/logging.c	Thu Oct 11 08:42:56 2012 +0200
+++ b/ucx/logging.c	Thu Oct 11 11:42:31 2012 +0200
@@ -11,7 +11,8 @@
     return logger;
 }
 
-void ucx_logger_log(UcxLogger *logger, unsigned int level, sstr_t message) {
+void ucx_logger_log(UcxLogger *logger, unsigned int level,
+        const sstr_t message) {
     if (level <= logger->level) {
         fwrite(message.ptr, 1, message.length, logger->stream);
         fflush(logger->stream);
--- a/ucx/logging.h	Thu Oct 11 08:42:56 2012 +0200
+++ b/ucx/logging.h	Thu Oct 11 11:42:31 2012 +0200
@@ -22,7 +22,8 @@
 UcxLogger *ucx_logger_new(FILE *stream, unsigned int level);
 /* neither provide a free function nor a parameter for an allocator */
 
-void ucx_logger_log(UcxLogger *logger, unsigned int level, sstr_t message);
+void ucx_logger_log(UcxLogger *logger, unsigned int level,
+        const sstr_t message);
 #define ucx_logger_error(l,m) ucx_logger_log(l, UCX_LOGGER_ERROR, m)
 #define ucx_logger_info(l,m) ucx_logger_log(l, UCX_LOGGER_INFO, m)
 #define ucx_logger_warn(l,m) ucx_logger_log(l, UCX_LOGGER_WARN, m)
--- a/ucx/map.c	Thu Oct 11 08:42:56 2012 +0200
+++ b/ucx/map.c	Thu Oct 11 11:42:31 2012 +0200
@@ -44,7 +44,8 @@
     free(map);
 }
 
-int ucx_map_copy(UcxMap *from, UcxMap *to, copy_func fnc, void *data) {
+int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to,
+        copy_func fnc, void *data) {
     UcxMapIterator i = ucx_map_iterator(from);
     void *value;
     UCX_MAP_FOREACH(value, i) {
@@ -82,6 +83,7 @@
         }
         map->count = 0;
         ucx_map_copy(&oldmap, map, NULL, NULL);
+        /* TODO: free the UcxMapElement list of oldmap */
     }
     return 0;
 }
@@ -92,8 +94,8 @@
     }
 
     size_t slot = key.hash%map->size;
-    UcxMapElement *elm = map->map[slot];
-    UcxMapElement *prev = NULL;
+    UcxMapElement *restrict elm = map->map[slot];
+    UcxMapElement *restrict prev = NULL;
 
     while (elm != NULL && elm->key.hash < key.hash) {
         prev = elm;
@@ -136,8 +138,8 @@
     }
     
     size_t slot = key.hash%map->size;
-    UcxMapElement *elm = map->map[slot];
-    UcxMapElement *pelm = NULL;
+    UcxMapElement *restrict elm = map->map[slot];
+    UcxMapElement *restrict pelm = NULL;
     while (elm && elm->key.hash <= key.hash) {
         if(elm->key.hash == key.hash) {
             int n = (key.len > elm->key.len) ? elm->key.len : key.len;
@@ -180,7 +182,7 @@
 }
 
 
-int ucx_hash(char *data, size_t len) {
+int ucx_hash(const char *data, size_t len) {
     /* murmur hash 2 */
 
     int m = 0x5bd1e995;
--- a/ucx/map.h	Thu Oct 11 08:42:56 2012 +0200
+++ b/ucx/map.h	Thu Oct 11 11:42:31 2012 +0200
@@ -58,7 +58,8 @@
 UcxMap *ucx_map_new(size_t size);
 void ucx_map_free(UcxMap *map);
 /* you cannot clone maps with more than 390 mio entries */
-int ucx_map_copy(UcxMap *from, UcxMap *to, copy_func fnc, void *data);
+int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to,
+        copy_func fnc, void *data);
 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data);
 int ucx_map_rehash(UcxMap *map);
 
@@ -75,7 +76,7 @@
 
 UcxKey ucx_key(void *data, size_t len);
 
-int ucx_hash(char *data, size_t len);
+int ucx_hash(const char *data, size_t len);
 
 UcxMapIterator ucx_map_iterator(UcxMap *map);
 
--- a/ucx/string.c	Thu Oct 11 08:42:56 2012 +0200
+++ b/ucx/string.c	Thu Oct 11 11:42:31 2012 +0200
@@ -25,7 +25,7 @@
     return string;
 }
 
-size_t sstrnlen (size_t n, sstr_t s, ...) {
+size_t sstrnlen (size_t n, const sstr_t s, ...) {
     va_list ap;
     size_t size = s.length;
     va_start(ap, s);
@@ -54,7 +54,7 @@
     return s;
 }
 
-sstr_t sstrncat (size_t n, sstr_t s, sstr_t c1, ...) {
+sstr_t sstrncat (size_t n, sstr_t s, const sstr_t c1, ...) {
     va_list ap;
     va_start(ap, c1);
     s.ptr[0] = 0;
@@ -82,11 +82,11 @@
     return s;
 }
 
-sstr_t sstrsubs (sstr_t s, size_t start) {
+sstr_t sstrsubs (const sstr_t s, size_t start) {
     return sstrsubsl (s, start, s.length-start);
 }
 
-sstr_t sstrsubsl (sstr_t s, size_t start, size_t length) {
+sstr_t sstrsubsl (const sstr_t s, size_t start, size_t length) {
     sstr_t new_sstr;
     if (start < 0 || start >= s.length || length < 0) {
         return s;
@@ -99,7 +99,7 @@
     return new_sstr;
 }
 
-sstr_t* sstrsplit(sstr_t s, sstr_t d, size_t *n) {
+sstr_t* sstrsplit(const sstr_t s, const sstr_t d, size_t *n) {
     if (d.length == 0) {
         return NULL;
     }
@@ -155,11 +155,11 @@
     return result;
 }
 
-int sstrcmp(sstr_t s1, sstr_t s2) {
+int sstrcmp(const sstr_t s1, const sstr_t s2) {
     return strncmp(s1.ptr, s2.ptr, s1.length>s2.length ? s2.length: s1.length);
 }
 
-sstr_t sstrdup(sstr_t s) {
+sstr_t sstrdup(const sstr_t s) {
     sstr_t newstring;
     newstring.ptr = (char*) malloc(s.length + 1);
     if (newstring.ptr != NULL) {
--- a/ucx/string.h	Thu Oct 11 08:42:56 2012 +0200
+++ b/ucx/string.h	Thu Oct 11 11:42:31 2012 +0200
@@ -46,7 +46,7 @@
  * s    string
  * ...  strings
  */
-size_t sstrnlen (size_t n, sstr_t s, ...);
+size_t sstrnlen (size_t n, const sstr_t s, ...);
 
 
 /*
@@ -56,18 +56,18 @@
  * s    new string with enough memory allocated
  * ...  strings
  */
-sstr_t sstrncat (size_t n, sstr_t s, sstr_t c1, ...);
+sstr_t sstrncat (size_t n, sstr_t s, const sstr_t c1, ...);
 
 
 /*
  *
  */
-sstr_t sstrsubs (sstr_t s, size_t start);
+sstr_t sstrsubs (const sstr_t s, size_t start);
 
 /*
  *
  */
-sstr_t sstrsubsl (sstr_t s, size_t start, size_t length);
+sstr_t sstrsubsl (const sstr_t s, size_t start, size_t length);
 
 /*
  * splits s into n parts
@@ -85,11 +85,11 @@
  *
  * Returns NULL on error
  */
-sstr_t* sstrsplit(sstr_t s, sstr_t d, size_t *n);
+sstr_t* sstrsplit(const sstr_t s, const sstr_t d, size_t *n);
 
-int sstrcmp(sstr_t s1, sstr_t s2);
+int sstrcmp(const sstr_t s1, const sstr_t s2);
 
-sstr_t sstrdup(sstr_t s);
+sstr_t sstrdup(const sstr_t s);
 
 #ifdef	__cplusplus
 }

mercurial