src/array_list.c

changeset 662
d0d95740071b
parent 660
4738a9065907
child 664
af5bf4603a5d
equal deleted inserted replaced
661:0a71ac9547fd 662:d0d95740071b
458 cx_arl_compare, 458 cx_arl_compare,
459 cx_arl_reverse, 459 cx_arl_reverse,
460 cx_arl_iterator, 460 cx_arl_iterator,
461 }; 461 };
462 462
463 static CxList *cx_array_list_create(
464 CxAllocator const *allocator,
465 CxListComparator comparator,
466 size_t item_size,
467 size_t initial_capacity
468 ) {
469 cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list));
470 if (list == NULL) return NULL;
471
472 list->data = cxCalloc(allocator, initial_capacity, item_size);
473 if (list->data == NULL) {
474 cxFree(allocator, list);
475 return NULL;
476 }
477
478 list->base.cl = &cx_array_list_class;
479 list->base.allocator = allocator;
480 list->base.cmpfunc = comparator;
481 list->base.itemsize = item_size;
482 list->base.capacity = initial_capacity;
483
484 // configure the reallocator
485 list->reallocator.realloc = cx_arl_realloc;
486 list->reallocator.ptr1 = (void *) allocator;
487
488 return (CxList *) list;
489 }
490
463 CxList *cxArrayListCreate( 491 CxList *cxArrayListCreate(
464 CxAllocator const *allocator, 492 CxAllocator const *allocator,
465 CxListComparator comparator, 493 CxListComparator comparator,
466 size_t item_size, 494 size_t item_size,
467 size_t initial_capacity 495 size_t initial_capacity
468 ) { 496 ) {
469 cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list)); 497 return cx_array_list_create(allocator, comparator,
470 if (list == NULL) return NULL; 498 item_size, initial_capacity);
471 499 }
472 list->data = cxCalloc(allocator, initial_capacity, item_size); 500
473 if (list->data == NULL) { 501 CxList *cxArrayListCreateSimple(
474 cxFree(allocator, list); 502 size_t item_size,
475 return NULL; 503 size_t initial_capacity
476 } 504 ) {
477 505 return cx_array_list_create(cxDefaultAllocator, NULL,
478 list->base.cl = &cx_array_list_class; 506 item_size, initial_capacity);
479 list->base.allocator = allocator; 507 }
480 list->base.cmpfunc = comparator;
481 list->base.itemsize = item_size;
482 list->base.capacity = initial_capacity;
483
484 // configure the reallocator
485 list->reallocator.realloc = cx_arl_realloc;
486 list->reallocator.ptr1 = (void *) allocator;
487
488 return (CxList *) list;
489 }

mercurial