src/map.c

changeset 253
e19825a1430a
parent 251
fae240d633fc
child 259
2f5dea574a75
equal deleted inserted replaced
252:6342cbbd1922 253:e19825a1430a
97 ucx_map_free_elmlist_contents(map); 97 ucx_map_free_elmlist_contents(map);
98 memset(map->map, 0, map->size*sizeof(UcxMapElement*)); 98 memset(map->map, 0, map->size*sizeof(UcxMapElement*));
99 map->count = 0; 99 map->count = 0;
100 } 100 }
101 101
102 int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to, 102 int ucx_map_copy(UcxMap *from, UcxMap *to, copy_func fnc, void *data) {
103 copy_func fnc, void *data) {
104 UcxMapIterator i = ucx_map_iterator(from); 103 UcxMapIterator i = ucx_map_iterator(from);
105 void *value; 104 void *value;
106 UCX_MAP_FOREACH(key, value, i) { 105 UCX_MAP_FOREACH(key, value, i) {
107 if (ucx_map_put(to, key, fnc ? fnc(value, data) : value)) { 106 if (ucx_map_put(to, key, fnc ? fnc(value, data) : value)) {
108 return 1; 107 return 1;
153 if (key.hash == 0) { 152 if (key.hash == 0) {
154 key.hash = ucx_hash((char*)key.data, key.len); 153 key.hash = ucx_hash((char*)key.data, key.len);
155 } 154 }
156 155
157 size_t slot = key.hash%map->size; 156 size_t slot = key.hash%map->size;
158 UcxMapElement *restrict elm = map->map[slot]; 157 UcxMapElement *elm = map->map[slot];
159 UcxMapElement *restrict prev = NULL; 158 UcxMapElement *prev = NULL;
160 159
161 while (elm && elm->key.hash < key.hash) { 160 while (elm && elm->key.hash < key.hash) {
162 prev = elm; 161 prev = elm;
163 elm = elm->next; 162 elm = elm->next;
164 } 163 }
192 elm->data = data; 191 elm->data = data;
193 192
194 return 0; 193 return 0;
195 } 194 }
196 195
197 void* ucx_map_get_and_remove(UcxMap *map, UcxKey key, _Bool remove) { 196 static void* ucx_map_get_and_remove(UcxMap *map, UcxKey key, int remove) {
198 if(key.hash == 0) { 197 if(key.hash == 0) {
199 key.hash = ucx_hash((char*)key.data, key.len); 198 key.hash = ucx_hash((char*)key.data, key.len);
200 } 199 }
201 200
202 size_t slot = key.hash%map->size; 201 size_t slot = key.hash%map->size;
203 UcxMapElement *restrict elm = map->map[slot]; 202 UcxMapElement *elm = map->map[slot];
204 UcxMapElement *restrict pelm = NULL; 203 UcxMapElement *pelm = NULL;
205 while (elm && elm->key.hash <= key.hash) { 204 while (elm && elm->key.hash <= key.hash) {
206 if(elm->key.hash == key.hash) { 205 if(elm->key.hash == key.hash) {
207 int n = (key.len > elm->key.len) ? elm->key.len : key.len; 206 int n = (key.len > elm->key.len) ? elm->key.len : key.len;
208 if (memcmp(elm->key.data, key.data, n) == 0) { 207 if (memcmp(elm->key.data, key.data, n) == 0) {
209 void *data = elm->data; 208 void *data = elm->data;

mercurial