ucx/map.c

changeset 29
bce0d7f2912b
parent 20
db7d9860dbbd
child 30
23bb65cbf7a4
     1.1 --- a/ucx/map.c	Mon Feb 20 15:30:45 2012 +0100
     1.2 +++ b/ucx/map.c	Tue Feb 21 01:13:17 2012 +0100
     1.3 @@ -13,7 +13,7 @@
     1.4          return NULL;
     1.5      }
     1.6  
     1.7 -    map->map = (UcxMapElement*)calloc(size, sizeof(UcxMapElement));
     1.8 +    map->map = (UcxMapElement**)calloc(size, sizeof(UcxMapElement*));
     1.9      if(map->map == NULL) {
    1.10          free(map);
    1.11          return NULL;
    1.12 @@ -23,27 +23,54 @@
    1.13      return map;
    1.14  }
    1.15  
    1.16 +void ucx_map_free(UcxMap *map) {
    1.17 +    for (size_t n = 0 ; n < map->size ; n++) {
    1.18 +        UcxMapElement *elem = map->map[n];
    1.19 +        if (elem != NULL) {
    1.20 +            do {
    1.21 +                UcxMapElement *next = elem->next;
    1.22 +                free(elem);
    1.23 +                elem = next;
    1.24 +            } while (elem != NULL);
    1.25 +        }
    1.26 +    }
    1.27 +    free(map);
    1.28 +}
    1.29 +
    1.30  int ucx_map_put(UcxMap *map, UcxKey key, void *data) {
    1.31      if(key.hash == 0) {
    1.32          key.hash = ucx_hash((char*)key.data, key.len);
    1.33      }
    1.34      void *kd = malloc(key.len);
    1.35 +    if (kd == NULL) {
    1.36 +        return -1;
    1.37 +    }
    1.38      memcpy(kd, key.data, key.len);
    1.39      key.data = kd;
    1.40  
    1.41 -    UcxMapElement *elm = &map->map[key.hash%map->size];
    1.42 -    if(elm->next != NULL) {
    1.43 -        while(elm->next != NULL) {
    1.44 -            elm = elm->next;
    1.45 -        }
    1.46 +    size_t slot = key.hash%map->size;
    1.47 +    UcxMapElement *elm = map->map[slot];
    1.48 +    UcxMapElement *prev = NULL;
    1.49 +
    1.50 +    while (elm != NULL && elm->key.hash < key.hash) {
    1.51 +        prev = elm;
    1.52 +        elm = elm->next;
    1.53 +    }
    1.54 +    
    1.55 +    if (elm == NULL || elm->key.hash != key.hash) {
    1.56          UcxMapElement *e = (UcxMapElement*)malloc(sizeof(UcxMapElement));
    1.57          if(e == NULL) {
    1.58              return -1;
    1.59          }
    1.60 -        elm->next = e;
    1.61 +        if (prev == NULL) {
    1.62 +            map->map[slot] = e;
    1.63 +        } else {
    1.64 +            prev->next = e;
    1.65 +        }
    1.66 +        e->next = elm;
    1.67          elm = e;
    1.68      }
    1.69 -
    1.70 +    
    1.71      elm->key = key;
    1.72      elm->data = data;
    1.73  
    1.74 @@ -55,11 +82,11 @@
    1.75          key.hash = ucx_hash((char*)key.data, key.len);
    1.76      }
    1.77      
    1.78 -    UcxMapElement *elm = &map->map[key.hash%map->size];
    1.79 -    while(elm != NULL) {
    1.80 +    UcxMapElement *elm = map->map[key.hash%map->size];
    1.81 +    while (elm != NULL && elm->key.hash <= key.hash) {
    1.82          if(elm->key.hash == key.hash) {
    1.83              int n = (key.len > elm->key.len) ? elm->key.len : key.len;
    1.84 -            if(memcmp(elm->key.data, key.data, n) == 0) {
    1.85 +            if (memcmp(elm->key.data, key.data, n) == 0) {
    1.86                  return elm->data;
    1.87              }
    1.88          }

mercurial