map counts elements

Thu, 04 Oct 2012 19:46:10 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 04 Oct 2012 19:46:10 +0200
changeset 45
dd03226c1a6b
parent 44
46356d74e873
child 46
48ca036d7d9c

map counts elements

ucx/map.c file | annotate | diff | comparison | revisions
ucx/map.h file | annotate | diff | comparison | revisions
--- a/ucx/map.c	Thu Oct 04 18:46:57 2012 +0200
+++ b/ucx/map.c	Thu Oct 04 19:46:10 2012 +0200
@@ -8,6 +8,10 @@
 #include "map.h"
 
 UcxMap *ucx_map_new(size_t size) {
+    if(size == 0) {
+        size = 16;
+    }
+    
     UcxMap *map = (UcxMap*)malloc(sizeof(UcxMap));
     if(map == NULL) {
         return NULL;
@@ -19,6 +23,7 @@
         return NULL;
     }
     map->size = size;
+    map->count = 0;
 
     return map;
 }
@@ -40,7 +45,8 @@
 }
 
 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data) {
-    UcxMap *newmap = ucx_map_new(map->size);
+    size_t bs = (map->count * 5) >> 2;
+    UcxMap *newmap = ucx_map_new(bs > map->size ? bs : map->size);
     UcxMapIterator i = ucx_map_iterator(map);
     void *value;
     UCX_MAP_FOREACH(value, i) {
@@ -86,6 +92,7 @@
         memcpy(kd, key.data, key.len);
         key.data = kd;
         elm->key = key;
+        map->count++;
     }
     elm->data = data;
 
--- a/ucx/map.h	Thu Oct 04 18:46:57 2012 +0200
+++ b/ucx/map.h	Thu Oct 04 19:46:10 2012 +0200
@@ -24,6 +24,7 @@
 struct UcxMap {
     UcxMapElement **map;
     size_t        size;
+    size_t        count;
 };
 
 struct UcxKey {

mercurial