src/array_list.c

changeset 1065
6eb7b54975ee
parent 1047
40aad3f0bc9e
equal deleted inserted replaced
1064:f3b04cd60776 1065:6eb7b54975ee
177 // perform reallocation 177 // perform reallocation
178 void *newmem = reallocator->realloc( 178 void *newmem = reallocator->realloc(
179 *array, newcap, elem_size, reallocator 179 *array, newcap, elem_size, reallocator
180 ); 180 );
181 if (newmem == NULL) { 181 if (newmem == NULL) {
182 return 1; 182 return 1; // LCOV_EXCL_LINE
183 } 183 }
184 184
185 // store new pointer 185 // store new pointer
186 *array = newmem; 186 *array = newmem;
187 187
363 *target, new_capacity, elem_size, reallocator 363 *target, new_capacity, elem_size, reallocator
364 ); 364 );
365 if (new_mem == NULL) { 365 if (new_mem == NULL) {
366 // give it up right away, there is no contract 366 // give it up right away, there is no contract
367 // that requires us to insert as much as we can 367 // that requires us to insert as much as we can
368 return 1; 368 return 1; // LCOV_EXCL_LINE
369 } 369 }
370 *target = new_mem; 370 *target = new_mem;
371 *capacity = new_capacity; 371 *capacity = new_capacity;
372 } 372 }
373 373
777 list->collection.size -= remove; 777 list->collection.size -= remove;
778 return remove; 778 return remove;
779 } 779 }
780 780
781 // just move the elements to the left 781 // just move the elements to the left
782 int result = cx_array_copy( 782 cx_array_copy(
783 &arl->data, 783 &arl->data,
784 &list->collection.size, 784 &list->collection.size,
785 &arl->capacity, 785 &arl->capacity,
786 0, 786 0,
787 index, 787 index,
789 list->collection.elem_size, 789 list->collection.elem_size,
790 list->collection.size - index - remove, 790 list->collection.size - index - remove,
791 &arl->reallocator 791 &arl->reallocator
792 ); 792 );
793 793
794 // cx_array_copy cannot fail, array cannot grow
795 assert(result == 0);
796
797 // decrease the size 794 // decrease the size
798 list->collection.size -= remove; 795 list->collection.size -= remove;
799 796
800 return remove; 797 return remove;
801 } 798 }
860 if (0 == list->collection.cmpfunc(elem, cur)) { 857 if (0 == list->collection.cmpfunc(elem, cur)) {
861 if (remove) { 858 if (remove) {
862 if (1 == cx_arl_remove(list, i, 1, NULL)) { 859 if (1 == cx_arl_remove(list, i, 1, NULL)) {
863 return i; 860 return i;
864 } else { 861 } else {
865 return -1; 862 // should be unreachable
863 return -1; // LCOV_EXCL_LINE
866 } 864 }
867 } else { 865 } else {
868 return i; 866 return i;
869 } 867 }
870 } 868 }
1017 cxListStorePointers((CxList *) list); 1015 cxListStorePointers((CxList *) list);
1018 } 1016 }
1019 1017
1020 // allocate the array after the real elem_size is known 1018 // allocate the array after the real elem_size is known
1021 list->data = cxCalloc(allocator, initial_capacity, elem_size); 1019 list->data = cxCalloc(allocator, initial_capacity, elem_size);
1022 if (list->data == NULL) { 1020 if (list->data == NULL) { // LCOV_EXCL_START
1023 cxFree(allocator, list); 1021 cxFree(allocator, list);
1024 return NULL; 1022 return NULL;
1025 } 1023 } // LCOV_EXCL_STOP
1026 1024
1027 // configure the reallocator 1025 // configure the reallocator
1028 list->reallocator = cx_array_reallocator(allocator, NULL); 1026 list->reallocator = cx_array_reallocator(allocator, NULL);
1029 1027
1030 return (CxList *) list; 1028 return (CxList *) list;

mercurial