42 } |
42 } |
43 free(map->map); |
43 free(map->map); |
44 free(map); |
44 free(map); |
45 } |
45 } |
46 |
46 |
47 int ucx_map_copy(UcxMap *from, UcxMap *to, copy_func fnc, void *data) { |
47 int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to, |
|
48 copy_func fnc, void *data) { |
48 UcxMapIterator i = ucx_map_iterator(from); |
49 UcxMapIterator i = ucx_map_iterator(from); |
49 void *value; |
50 void *value; |
50 UCX_MAP_FOREACH(value, i) { |
51 UCX_MAP_FOREACH(value, i) { |
51 int ret = ucx_map_put(to, i.cur->key, fnc ? fnc(value, data) : value); |
52 int ret = ucx_map_put(to, i.cur->key, fnc ? fnc(value, data) : value); |
52 if(ret != 0) { |
53 if(ret != 0) { |
80 *map = oldmap; |
81 *map = oldmap; |
81 return 1; |
82 return 1; |
82 } |
83 } |
83 map->count = 0; |
84 map->count = 0; |
84 ucx_map_copy(&oldmap, map, NULL, NULL); |
85 ucx_map_copy(&oldmap, map, NULL, NULL); |
|
86 /* TODO: free the UcxMapElement list of oldmap */ |
85 } |
87 } |
86 return 0; |
88 return 0; |
87 } |
89 } |
88 |
90 |
89 int ucx_map_put(UcxMap *map, UcxKey key, void *data) { |
91 int ucx_map_put(UcxMap *map, UcxKey key, void *data) { |
90 if(key.hash == 0) { |
92 if(key.hash == 0) { |
91 key.hash = ucx_hash((char*)key.data, key.len); |
93 key.hash = ucx_hash((char*)key.data, key.len); |
92 } |
94 } |
93 |
95 |
94 size_t slot = key.hash%map->size; |
96 size_t slot = key.hash%map->size; |
95 UcxMapElement *elm = map->map[slot]; |
97 UcxMapElement *restrict elm = map->map[slot]; |
96 UcxMapElement *prev = NULL; |
98 UcxMapElement *restrict prev = NULL; |
97 |
99 |
98 while (elm != NULL && elm->key.hash < key.hash) { |
100 while (elm != NULL && elm->key.hash < key.hash) { |
99 prev = elm; |
101 prev = elm; |
100 elm = elm->next; |
102 elm = elm->next; |
101 } |
103 } |
134 if(key.hash == 0) { |
136 if(key.hash == 0) { |
135 key.hash = ucx_hash((char*)key.data, key.len); |
137 key.hash = ucx_hash((char*)key.data, key.len); |
136 } |
138 } |
137 |
139 |
138 size_t slot = key.hash%map->size; |
140 size_t slot = key.hash%map->size; |
139 UcxMapElement *elm = map->map[slot]; |
141 UcxMapElement *restrict elm = map->map[slot]; |
140 UcxMapElement *pelm = NULL; |
142 UcxMapElement *restrict pelm = NULL; |
141 while (elm && elm->key.hash <= key.hash) { |
143 while (elm && elm->key.hash <= key.hash) { |
142 if(elm->key.hash == key.hash) { |
144 if(elm->key.hash == key.hash) { |
143 int n = (key.len > elm->key.len) ? elm->key.len : key.len; |
145 int n = (key.len > elm->key.len) ? elm->key.len : key.len; |
144 if (memcmp(elm->key.data, key.data, n) == 0) { |
146 if (memcmp(elm->key.data, key.data, n) == 0) { |
145 void *data = elm->data; |
147 void *data = elm->data; |