ucx/map.c

Fri, 24 Feb 2012 15:53:50 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 24 Feb 2012 15:53:50 +0100
changeset 30
23bb65cbf7a4
parent 29
bce0d7f2912b
child 31
91ac86557290
permissions
-rw-r--r--

some fixes

olaf@20 1 /*
olaf@20 2 *
olaf@20 3 */
olaf@2 4
olaf@20 5 #include <stdlib.h>
olaf@20 6 #include <string.h>
olaf@20 7
olaf@20 8 #include "map.h"
olaf@20 9
olaf@20 10 UcxMap *ucx_map_new(size_t size) {
olaf@20 11 UcxMap *map = (UcxMap*)malloc(sizeof(UcxMap));
olaf@20 12 if(map == NULL) {
olaf@20 13 return NULL;
olaf@20 14 }
olaf@20 15
universe@29 16 map->map = (UcxMapElement**)calloc(size, sizeof(UcxMapElement*));
olaf@20 17 if(map->map == NULL) {
olaf@20 18 free(map);
olaf@20 19 return NULL;
olaf@20 20 }
olaf@20 21 map->size = size;
olaf@20 22
olaf@20 23 return map;
olaf@20 24 }
olaf@20 25
universe@29 26 void ucx_map_free(UcxMap *map) {
universe@29 27 for (size_t n = 0 ; n < map->size ; n++) {
universe@29 28 UcxMapElement *elem = map->map[n];
universe@29 29 if (elem != NULL) {
universe@29 30 do {
universe@29 31 UcxMapElement *next = elem->next;
olaf@30 32 free(elem->key.data);
universe@29 33 free(elem);
universe@29 34 elem = next;
universe@29 35 } while (elem != NULL);
universe@29 36 }
universe@29 37 }
olaf@30 38 free(map->map);
universe@29 39 free(map);
universe@29 40 }
universe@29 41
olaf@20 42 int ucx_map_put(UcxMap *map, UcxKey key, void *data) {
olaf@20 43 if(key.hash == 0) {
olaf@20 44 key.hash = ucx_hash((char*)key.data, key.len);
olaf@20 45 }
olaf@20 46
universe@29 47 size_t slot = key.hash%map->size;
universe@29 48 UcxMapElement *elm = map->map[slot];
universe@29 49 UcxMapElement *prev = NULL;
universe@29 50
universe@29 51 while (elm != NULL && elm->key.hash < key.hash) {
universe@29 52 prev = elm;
universe@29 53 elm = elm->next;
universe@29 54 }
universe@29 55
universe@29 56 if (elm == NULL || elm->key.hash != key.hash) {
olaf@20 57 UcxMapElement *e = (UcxMapElement*)malloc(sizeof(UcxMapElement));
olaf@20 58 if(e == NULL) {
olaf@20 59 return -1;
olaf@20 60 }
olaf@30 61 e->key.data = NULL;
universe@29 62 if (prev == NULL) {
universe@29 63 map->map[slot] = e;
universe@29 64 } else {
universe@29 65 prev->next = e;
universe@29 66 }
universe@29 67 e->next = elm;
olaf@20 68 elm = e;
olaf@20 69 }
universe@29 70
olaf@30 71 if(elm->key.data == NULL) {
olaf@30 72 void *kd = malloc(key.len);
olaf@30 73 if (kd == NULL) {
olaf@30 74 return -1;
olaf@30 75 }
olaf@30 76 memcpy(kd, key.data, key.len);
olaf@30 77 key.data = kd;
olaf@30 78 elm->key = key;
olaf@30 79 }
olaf@20 80 elm->data = data;
olaf@20 81
olaf@20 82 return 0;
olaf@20 83 }
olaf@20 84
olaf@20 85 void* ucx_map_get(UcxMap *map, UcxKey key) {
olaf@20 86 if(key.hash == 0) {
olaf@20 87 key.hash = ucx_hash((char*)key.data, key.len);
olaf@20 88 }
olaf@20 89
universe@29 90 UcxMapElement *elm = map->map[key.hash%map->size];
universe@29 91 while (elm != NULL && elm->key.hash <= key.hash) {
olaf@20 92 if(elm->key.hash == key.hash) {
olaf@20 93 int n = (key.len > elm->key.len) ? elm->key.len : key.len;
universe@29 94 if (memcmp(elm->key.data, key.data, n) == 0) {
olaf@20 95 return elm->data;
olaf@20 96 }
olaf@20 97 }
olaf@20 98 elm = elm->next;
olaf@20 99 }
olaf@20 100
olaf@20 101 return NULL;
olaf@20 102 }
olaf@20 103
olaf@20 104 UcxKey ucx_key(void *data, size_t len) {
olaf@20 105 UcxKey key;
olaf@20 106 key.data = data;
olaf@20 107 key.len = len;
olaf@20 108 key.hash = ucx_hash(data, len);
olaf@20 109 return key;
olaf@20 110 }
olaf@20 111
olaf@20 112
olaf@20 113 int ucx_hash(char *data, size_t len) {
olaf@20 114 /* murmur hash 2 */
olaf@20 115
olaf@20 116 int m = 0x5bd1e995;
olaf@20 117 int r = 24;
olaf@20 118
olaf@20 119 int h = 25 ^ len;
olaf@20 120
olaf@20 121 int i = 0;
olaf@20 122 while (len >= 4) {
olaf@20 123 int k = data[i + 0] & 0xFF;
olaf@20 124 k |= (data[i + 1] & 0xFF) << 8;
olaf@20 125 k |= (data[i + 2] & 0xFF) << 16;
olaf@20 126 k |= (data[i + 3] & 0xFF) << 24;
olaf@20 127
olaf@20 128 k *= m;
olaf@20 129 k ^= k >> r;
olaf@20 130 k *= m;
olaf@20 131
olaf@20 132 h *= m;
olaf@20 133 h ^= k;
olaf@20 134
olaf@20 135 i += 4;
olaf@20 136 len -= 4;
olaf@20 137 }
olaf@20 138
olaf@20 139 switch (len) {
olaf@20 140 case 3: h ^= (data[i + 2] & 0xFF) << 16;
olaf@20 141 case 2: h ^= (data[i + 1] & 0xFF) << 8;
olaf@20 142 case 1: h ^= (data[i + 0] & 0xFF); h *= m;
olaf@20 143 }
olaf@20 144
olaf@20 145 h ^= h >> 13;
olaf@20 146 h *= m;
olaf@20 147 h ^= h >> 15;
olaf@20 148
olaf@20 149 return h;
olaf@20 150 }

mercurial