src/array_list.c

changeset 1496
1a982f6f2407
parent 1494
f027a95d93f2
equal deleted inserted replaced
1495:beee442be85a 1496:1a982f6f2407
365 365
366 // corner case 366 // corner case
367 if (elem_count == 0) return 0; 367 if (elem_count == 0) return 0;
368 368
369 // overflow check 369 // overflow check
370 // LCOV_EXCL_START
370 if (elem_count > SIZE_MAX - *size) { 371 if (elem_count > SIZE_MAX - *size) {
371 errno = EOVERFLOW; 372 errno = EOVERFLOW;
372 return 1; 373 return 1;
373 } 374 }
375 // LCOV_EXCL_STOP
374 376
375 // store some counts 377 // store some counts
376 const size_t old_size = *size; 378 const size_t old_size = *size;
377 const size_t old_capacity = *capacity; 379 const size_t old_capacity = *capacity;
378 // the necessary capacity is the worst case assumption, including duplicates 380 // the necessary capacity is the worst case assumption, including duplicates
790 if (cxReallocateArray( 792 if (cxReallocateArray(
791 list->collection.allocator, 793 list->collection.allocator,
792 &arl->data, new_capacity, 794 &arl->data, new_capacity,
793 list->collection.elem_size) 795 list->collection.elem_size)
794 ) { 796 ) {
795 return 0; 797 return 0; // LCOV_EXCL_LINE
796 } 798 }
797 arl->capacity = new_capacity; 799 arl->capacity = new_capacity;
798 } 800 }
799 801
800 // determine insert position 802 // determine insert position
834 list->collection.elem_size, 836 list->collection.elem_size,
835 n, 837 n,
836 &arl->reallocator 838 &arl->reallocator
837 )) { 839 )) {
838 // array list implementation is "all or nothing" 840 // array list implementation is "all or nothing"
839 return 0; 841 return 0; // LCOV_EXCL_LINE
840 } else { 842 } else {
841 return n; 843 return n;
842 } 844 }
843 } 845 }
844 846
859 list->collection.elem_size, 861 list->collection.elem_size,
860 n, 862 n,
861 &arl->reallocator 863 &arl->reallocator
862 )) { 864 )) {
863 // array list implementation is "all or nothing" 865 // array list implementation is "all or nothing"
864 return 0; 866 return 0; // LCOV_EXCL_LINE
865 } else { 867 } else {
866 return n; 868 return n;
867 } 869 }
868 } 870 }
869 871
886 ) { 888 ) {
887 struct cx_list_s *list = iter->src_handle; 889 struct cx_list_s *list = iter->src_handle;
888 if (iter->index < list->collection.size) { 890 if (iter->index < list->collection.size) {
889 if (cx_arl_insert_element(list, 891 if (cx_arl_insert_element(list,
890 iter->index + 1 - prepend, elem) == NULL) { 892 iter->index + 1 - prepend, elem) == NULL) {
891 return 1; 893 return 1; // LCOV_EXCL_LINE
892 } 894 }
893 iter->elem_count++; 895 iter->elem_count++;
894 if (prepend != 0) { 896 if (prepend != 0) {
895 iter->index++; 897 iter->index++;
896 iter->elem_handle = ((char *) iter->elem_handle) + list->collection.elem_size; 898 iter->elem_handle = ((char *) iter->elem_handle) + list->collection.elem_size;
897 } 899 }
898 return 0; 900 return 0;
899 } else { 901 } else {
900 if (cx_arl_insert_element(list, list->collection.size, elem) == NULL) { 902 if (cx_arl_insert_element(list, list->collection.size, elem) == NULL) {
901 return 1; 903 return 1; // LCOV_EXCL_LINE
902 } 904 }
903 iter->elem_count++; 905 iter->elem_count++;
904 iter->index = list->collection.size; 906 iter->index = list->collection.size;
905 return 0; 907 return 0;
906 } 908 }

mercurial