ucx/map.c

Tue, 21 Feb 2012 01:13:17 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 21 Feb 2012 01:13:17 +0100
changeset 29
bce0d7f2912b
parent 20
db7d9860dbbd
child 30
23bb65cbf7a4
permissions
-rw-r--r--

fixed map with the help of new tests

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

mercurial