src/server/ucx/map.c

changeset 31
280250e45ba6
parent 21
627b09ee74e4
child 36
450d2d5f4735
--- a/src/server/ucx/map.c	Thu May 24 12:51:52 2012 +0200
+++ b/src/server/ucx/map.c	Fri May 25 18:16:24 2012 +0200
@@ -2,7 +2,6 @@
  *
  */
 
-#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
@@ -14,7 +13,7 @@
         return NULL;
     }
 
-    map->map = (UcxMapElement*)calloc(size, sizeof(UcxMapElement));
+    map->map = (UcxMapElement**)calloc(size, sizeof(UcxMapElement*));
     if(map->map == NULL) {
         free(map);
         return NULL;
@@ -24,45 +23,60 @@
     return map;
 }
 
+void ucx_map_free(UcxMap *map) {
+    for (size_t n = 0 ; n < map->size ; n++) {
+        UcxMapElement *elem = map->map[n];
+        if (elem != NULL) {
+            do {
+                UcxMapElement *next = elem->next;
+                free(elem->key.data);
+                free(elem);
+                elem = next;
+            } while (elem != NULL);
+        }
+    }
+    free(map->map);
+    free(map);
+}
+
 int ucx_map_put(UcxMap *map, UcxKey key, void *data) {
     if(key.hash == 0) {
         key.hash = ucx_hash((char*)key.data, key.len);
     }
-    void *kd = calloc(1, key.len);
-    memcpy(kd, key.data, key.len);
-    key.data = kd;
 
-    UcxMapElement *elm = &map->map[key.hash%map->size];
-    if(elm->key.hash != 0) {
-        UcxMapElement *e = elm;
-        for(;;) {
-            /* check if the key is already in use */
-            if(key.hash == e->key.hash) {
-                int n = (key.len > elm->key.len) ? elm->key.len : key.len;
-                if(memcmp(elm->key.data, key.data, n) == 0) {
-                    /* use elm as storage place */
-                    elm = e;
-                    break;
-                }
-            }
+    size_t slot = key.hash%map->size;
+    UcxMapElement *elm = map->map[slot];
+    UcxMapElement *prev = NULL;
 
-            /* check if this is the last element */
-            if(e->next == NULL) {
-                /* create new element */
-                UcxMapElement *en = (UcxMapElement*)malloc(sizeof(UcxMapElement));
-                if(en == NULL) {
-                    return -1;
-                }
-                e->next = en;
-                elm = en;
-                break;
-            }
-
-            e = e->next;
+    while (elm != NULL && elm->key.hash < key.hash) {
+        prev = elm;
+        elm = elm->next;
+    }
+    
+    if (elm == NULL || elm->key.hash != key.hash) {
+        UcxMapElement *e = (UcxMapElement*)malloc(sizeof(UcxMapElement));
+        if(e == NULL) {
+            return -1;
         }
+        e->key.data = NULL;
+        if (prev == NULL) {
+            map->map[slot] = e;
+        } else {
+            prev->next = e;
+        }
+        e->next = elm;
+        elm = e;
     }
-
-    elm->key = key;
+    
+    if(elm->key.data == NULL) {
+        void *kd = malloc(key.len);
+        if (kd == NULL) {
+            return -1;
+        }
+        memcpy(kd, key.data, key.len);
+        key.data = kd;
+        elm->key = key;
+    }
     elm->data = data;
 
     return 0;
@@ -72,12 +86,12 @@
     if(key.hash == 0) {
         key.hash = ucx_hash((char*)key.data, key.len);
     }
-
-    UcxMapElement *elm = &map->map[key.hash%map->size];
-    while(elm != NULL) {
+    
+    UcxMapElement *elm = map->map[key.hash%map->size];
+    while (elm != NULL && elm->key.hash <= key.hash) {
         if(elm->key.hash == key.hash) {
             int n = (key.len > elm->key.len) ? elm->key.len : key.len;
-            if(memcmp(elm->key.data, key.data, n) == 0) {
+            if (memcmp(elm->key.data, key.data, n) == 0) {
                 return elm->data;
             }
         }
@@ -134,3 +148,41 @@
 
     return h;
 }
+
+UcxMapIterator ucx_map_iterator(UcxMap *map) {
+    UcxMapIterator i;
+    i.map = map;
+    i.cur = NULL;
+    i.index = 0;
+    return i;
+}
+
+int ucx_map_iter_next(UcxMapIterator *i, void **elm) {
+    UcxMapElement *e = i->cur;
+    
+    if(e == NULL) {
+        e = i->map->map[0];
+    } else {
+        e = e->next;
+    }
+    
+    while(i->index < i->map->size) {
+        if(e != NULL) {
+            if(e->data != NULL) {
+                i->cur = e;
+                *elm = e->data;
+                return 0;
+            }
+
+            e = e->next;
+        } else {
+            i->index++;
+            
+            if(i->index < i->map->size) {
+                e = i->map->map[i->index];
+            }
+        }
+    }
+    
+    return 1;
+}

mercurial