src/array_list.c

changeset 670
4ad8ea3aee49
parent 667
2f88a7c13a28
child 676
d0680a23d850
equal deleted inserted replaced
669:dce9b8450656 670:4ad8ea3aee49
496 cx_arl_compare, 496 cx_arl_compare,
497 cx_arl_reverse, 497 cx_arl_reverse,
498 cx_arl_iterator, 498 cx_arl_iterator,
499 }; 499 };
500 500
501 static CxList *cx_array_list_create(
502 CxAllocator const *allocator,
503 CxListComparator comparator,
504 size_t item_size,
505 size_t initial_capacity
506 ) {
507 cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list));
508 if (list == NULL) return NULL;
509
510 list->data = cxCalloc(allocator, initial_capacity, item_size);
511 if (list->data == NULL) {
512 cxFree(allocator, list);
513 return NULL;
514 }
515
516 list->base.cl = &cx_array_list_class;
517 list->base.allocator = allocator;
518 list->base.cmpfunc = comparator;
519 list->base.capacity = initial_capacity;
520
521 if (item_size > 0) {
522 list->base.itemsize = item_size;
523 } else {
524 cxListStorePointers((CxList *) list);
525 }
526
527 // configure the reallocator
528 list->reallocator.realloc = cx_arl_realloc;
529 list->reallocator.ptr1 = (void *) allocator;
530
531 return (CxList *) list;
532 }
533
534 CxList *cxArrayListCreate( 501 CxList *cxArrayListCreate(
535 CxAllocator const *allocator, 502 CxAllocator const *allocator,
536 CxListComparator comparator, 503 CxListComparator comparator,
537 size_t item_size, 504 size_t item_size,
538 size_t initial_capacity 505 size_t initial_capacity
539 ) { 506 ) {
540 return cx_array_list_create(allocator, comparator, 507 if (allocator == NULL) {
541 item_size, initial_capacity); 508 allocator = cxDefaultAllocator;
542 } 509 }
543 510
544 CxList *cxArrayListCreateSimple( 511 cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list));
545 size_t item_size, 512 if (list == NULL) return NULL;
546 size_t initial_capacity 513
547 ) { 514 list->data = cxCalloc(allocator, initial_capacity, item_size);
548 return cx_array_list_create(cxDefaultAllocator, NULL, 515 if (list->data == NULL) {
549 item_size, initial_capacity); 516 cxFree(allocator, list);
550 } 517 return NULL;
518 }
519
520 list->base.cl = &cx_array_list_class;
521 list->base.allocator = allocator;
522 list->base.cmpfunc = comparator;
523 list->base.capacity = initial_capacity;
524
525 if (item_size > 0) {
526 list->base.itemsize = item_size;
527 } else {
528 cxListStorePointers((CxList *) list);
529 }
530
531 // configure the reallocator
532 list->reallocator.realloc = cx_arl_realloc;
533 list->reallocator.ptr1 = (void *) allocator;
534
535 return (CxList *) list;
536 }

mercurial