src/map.c

changeset 1437
dde4903c0fd7
parent 1429
6e0c3a8a914a
child 1438
2ca9e03ceeec
equal deleted inserted replaced
1436:c331add0d9f8 1437:dde4903c0fd7
125 125
126 void cxMapFree(CxMap *map) { 126 void cxMapFree(CxMap *map) {
127 if (map == NULL) return; 127 if (map == NULL) return;
128 map->cl->deallocate(map); 128 map->cl->deallocate(map);
129 } 129 }
130
131 size_t cxMapClone(CxMap *dst, const CxMap *src, cx_clone_func clone_func,
132 const CxAllocator *clone_allocator, void *data) {
133 CxMapIterator src_iter = cxMapIterator(src);
134 size_t i = 0;
135 if (cxCollectionStoresPointers(dst)) {
136 for (; i < cxMapSize(src); i++) {
137 const CxMapEntry *entry = cxIteratorCurrent(src_iter);
138 void **dst_mem = cxMapEmplace(dst, *(entry->key));
139 if (dst_mem == NULL) {
140 return i;
141 }
142 void *dst_ptr = clone_func(NULL, entry->value, clone_allocator, data);
143 if (dst_ptr == NULL) {
144 // TODO: remove the entry to avoid calling destructors on uninitialized memory
145 return i;
146 }
147 *dst_mem = dst_ptr;
148 cxIteratorNext(src_iter);
149 }
150 } else {
151 for (; i < cxMapSize(src); i++) {
152 const CxMapEntry *entry = cxIteratorCurrent(src_iter);
153 void *dst_mem = cxMapEmplace(dst, *(entry->key));
154 if (dst_mem == NULL) {
155 return i;
156 }
157 if (clone_func(dst_mem, entry->value, clone_allocator, data) == NULL) {
158 // TODO: remove the entry to avoid calling destructors on uninitialized memory
159 return i;
160 }
161 cxIteratorNext(src_iter);
162 }
163 }
164 return i;
165 }

mercurial