ucx/map.c

changeset 162
18892c0a9adc
parent 157
0b33b9396851
equal deleted inserted replaced
161:b1eac0878ce7 162:18892c0a9adc
101 ucx_map_free_elmlist_contents(map); 101 ucx_map_free_elmlist_contents(map);
102 memset(map->map, 0, map->size*sizeof(UcxMapElement*)); 102 memset(map->map, 0, map->size*sizeof(UcxMapElement*));
103 map->count = 0; 103 map->count = 0;
104 } 104 }
105 105
106 int ucx_map_copy(UcxMap *from, UcxMap *to, copy_func fnc, void *data) { 106 int ucx_map_copy(UcxMap const *from, UcxMap *to, copy_func fnc, void *data) {
107 UcxMapIterator i = ucx_map_iterator(from); 107 UcxMapIterator i = ucx_map_iterator(from);
108 void *value; 108 void *value;
109 UCX_MAP_FOREACH(key, value, i) { 109 UCX_MAP_FOREACH(key, value, i) {
110 if (ucx_map_put(to, key, fnc ? fnc(value, data) : value)) { 110 if (ucx_map_put(to, key, fnc ? fnc(value, data) : value)) {
111 return 1; 111 return 1;
112 } 112 }
113 } 113 }
114 return 0; 114 return 0;
115 } 115 }
116 116
117 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data) { 117 UcxMap *ucx_map_clone(UcxMap const *map, copy_func fnc, void *data) {
118 return ucx_map_clone_a(ucx_default_allocator(), map, fnc, data);
119 }
120
121 UcxMap *ucx_map_clone_a(UcxAllocator *allocator,
122 UcxMap const *map, copy_func fnc, void *data) {
118 size_t bs = (map->count * 5) >> 1; 123 size_t bs = (map->count * 5) >> 1;
119 UcxMap *newmap = ucx_map_new(bs > map->size ? bs : map->size); 124 UcxMap *newmap = ucx_map_new_a(allocator, bs > map->size ? bs : map->size);
120 if (!newmap) { 125 if (!newmap) {
121 return NULL; 126 return NULL;
122 } 127 }
123 ucx_map_copy(map, newmap, fnc, data); 128 ucx_map_copy(map, newmap, fnc, data);
124 return newmap; 129 return newmap;
233 } 238 }
234 239
235 return NULL; 240 return NULL;
236 } 241 }
237 242
238 void *ucx_map_get(UcxMap *map, UcxKey key) { 243 void *ucx_map_get(UcxMap const *map, UcxKey key) {
239 return ucx_map_get_and_remove(map, key, 0); 244 return ucx_map_get_and_remove((UcxMap *)map, key, 0);
240 } 245 }
241 246
242 void *ucx_map_remove(UcxMap *map, UcxKey key) { 247 void *ucx_map_remove(UcxMap *map, UcxKey key) {
243 return ucx_map_get_and_remove(map, key, 1); 248 return ucx_map_get_and_remove(map, key, 1);
244 } 249 }
292 h ^= h >> 15; 297 h ^= h >> 15;
293 298
294 return h; 299 return h;
295 } 300 }
296 301
297 UcxMapIterator ucx_map_iterator(UcxMap *map) { 302 UcxMapIterator ucx_map_iterator(UcxMap const *map) {
298 UcxMapIterator i; 303 UcxMapIterator i;
299 i.map = map; 304 i.map = map;
300 i.cur = NULL; 305 i.cur = NULL;
301 i.index = 0; 306 i.index = 0;
302 return i; 307 return i;
333 } 338 }
334 339
335 return 0; 340 return 0;
336 } 341 }
337 342
343 UcxMap* ucx_map_union(const UcxMap *first, const UcxMap *second,
344 copy_func cpfnc, void* cpdata) {
345 return ucx_map_union_a(ucx_default_allocator(),
346 first, second, cpfnc, cpdata);
347 }
348
349 UcxMap* ucx_map_union_a(UcxAllocator *allocator,
350 const UcxMap *first, const UcxMap *second,
351 copy_func cpfnc, void* cpdata) {
352 UcxMap* result = ucx_map_clone_a(allocator, first, cpfnc, cpdata);
353 ucx_map_copy(second, result, cpfnc, cpdata);
354 return result;
355 }
356
357 UcxMap* ucx_map_intersection(const UcxMap *first, const UcxMap *second,
358 copy_func cpfnc, void* cpdata) {
359 return ucx_map_intersection_a(ucx_default_allocator(),
360 first, second, cpfnc, cpdata);
361 }
362
363 UcxMap* ucx_map_intersection_a(UcxAllocator *allocator,
364 const UcxMap *first, const UcxMap *second,
365 copy_func cpfnc, void* cpdata) {
366 UcxMap *result = ucx_map_new_a(allocator, first->size < second->size ?
367 first->size : second->size);
368
369 UcxMapIterator iter = ucx_map_iterator(first);
370 void* value;
371 UCX_MAP_FOREACH(key, value, iter) {
372 if (ucx_map_get(second, key)) {
373 ucx_map_put(result, key, cpfnc ? cpfnc(value, cpdata) : value);
374 }
375 }
376
377 return result;
378 }
379
380 UcxMap* ucx_map_difference(const UcxMap *first, const UcxMap *second,
381 copy_func cpfnc, void* cpdata) {
382 return ucx_map_difference_a(ucx_default_allocator(),
383 first, second, cpfnc, cpdata);
384 }
385
386 UcxMap* ucx_map_difference_a(UcxAllocator *allocator,
387 const UcxMap *first, const UcxMap *second,
388 copy_func cpfnc, void* cpdata) {
389
390 UcxMap *result = ucx_map_new_a(allocator, first->size - second->count);
391
392 UcxMapIterator iter = ucx_map_iterator(first);
393 void* value;
394 UCX_MAP_FOREACH(key, value, iter) {
395 if (!ucx_map_get(second, key)) {
396 ucx_map_put(result, key, cpfnc ? cpfnc(value, cpdata) : value);
397 }
398 }
399
400 ucx_map_rehash(result);
401 return result;
402 }

mercurial