ucx/map.c

changeset 17
11dffb40cd91
parent 5
88625853ae74
child 70
88092b88ec00
--- a/ucx/map.c	Fri Aug 16 12:41:30 2013 +0200
+++ b/ucx/map.c	Sat Aug 17 12:04:04 2013 +0200
@@ -45,7 +45,7 @@
     }
     
     UcxMap *map = (UcxMap*)allocator->malloc(allocator->pool, sizeof(UcxMap));
-    if(map == NULL) {
+    if (!map) {
         return NULL;
     }
     
@@ -89,8 +89,7 @@
     UcxMapIterator i = ucx_map_iterator(from);
     void *value;
     UCX_MAP_FOREACH(key, value, i) {
-        int ret = ucx_map_put(to, i.cur->key, fnc ? fnc(value, data) : value);
-        if(ret != 0) {
+        if (ucx_map_put(to, key, fnc ? fnc(value, data) : value)) {
             return 1;
         }
     }
@@ -100,7 +99,7 @@
 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data) {
     size_t bs = (map->count * 5) >> 1;
     UcxMap *newmap = ucx_map_new(bs > map->size ? bs : map->size);
-    if(newmap == NULL) {
+    if (!newmap) {
         return NULL;
     }
     ucx_map_copy(map, newmap, fnc, data);
@@ -121,7 +120,7 @@
                 map->allocator->pool,
                 map->size,
                 sizeof(UcxMapElement*));
-        if(map->map == NULL) {
+        if (!map->map) {
             *map = oldmap;
             return 1;
         }
@@ -137,7 +136,7 @@
 int ucx_map_put(UcxMap *map, UcxKey key, void *data) {
     UcxAllocator *allocator = map->allocator;
     
-    if(key.hash == 0) {
+    if (key.hash == 0) {
         key.hash = ucx_hash((char*)key.data, key.len);
     }
 
@@ -145,16 +144,16 @@
     UcxMapElement *restrict elm = map->map[slot];
     UcxMapElement *restrict prev = NULL;
 
-    while (elm != NULL && elm->key.hash < key.hash) {
+    while (elm && elm->key.hash < key.hash) {
         prev = elm;
         elm = elm->next;
     }
     
-    if (elm == NULL || elm->key.hash != key.hash) {
+    if (!elm || elm->key.hash != key.hash) {
         UcxMapElement *e = (UcxMapElement*)allocator->malloc(
                 allocator->pool,
                 sizeof(UcxMapElement));
-        if(e == NULL) {
+        if (!e) {
             return -1;
         }
         e->key.data = NULL;
@@ -167,9 +166,9 @@
         elm = e;
     }
     
-    if(elm->key.data == NULL) {
+    if (!elm->key.data) {
         void *kd = allocator->malloc(allocator->pool, key.len);
-        if (kd == NULL) {
+        if (!kd) {
             return -1;
         }
         memcpy(kd, key.data, key.len);
@@ -201,6 +200,7 @@
                     } else {
                         map->map[slot] = elm->next;
                     }
+                    map->allocator->free(map->allocator->pool, elm->key.data);
                     map->allocator->free(map->allocator->pool, elm);
                     map->count--;
                 }
@@ -285,31 +285,31 @@
 int ucx_map_iter_next(UcxMapIterator *i, UcxKey *key, void **elm) {
     UcxMapElement *e = i->cur;
     
-    if(e == NULL) {
+    if (e) {
+        e = e->next;
+    } else {
         e = i->map->map[0];
-    } else {
-        e = e->next;
     }
     
-    while(i->index < i->map->size) {
-        if(e != NULL) {
-            if(e->data != NULL) {
+    while (i->index < i->map->size) {
+        if (e) {
+            if (e->data) {
                 i->cur = e;
                 *elm = e->data;
                 *key = e->key;
-                return 0;
+                return 1;
             }
 
             e = e->next;
         } else {
             i->index++;
             
-            if(i->index < i->map->size) {
+            if (i->index < i->map->size) {
                 e = i->map->map[i->index];
             }
         }
     }
     
-    return 1;
+    return 0;
 }
 

mercurial