Sun, 13 Nov 2022 13:21:48 +0100
implement array list ctor and dtor
also lays out the "glue level" functions
src/array_list.c | file | annotate | diff | comparison | revisions |
1.1 --- a/src/array_list.c Sat Nov 12 15:56:58 2022 +0100 1.2 +++ b/src/array_list.c Sun Nov 13 13:21:48 2022 +0100 1.3 @@ -28,11 +28,124 @@ 1.4 1.5 #include "cx/array_list.h" 1.6 1.7 +/* LOW LEVEL ARRAY LIST FUNCTIONS */ 1.8 + 1.9 + 1.10 + 1.11 +/* HIGH LEVEL ARRAY LIST FUNCTIONS */ 1.12 + 1.13 +typedef struct { 1.14 + struct cx_list_s base; 1.15 + void *data; 1.16 +} cx_array_list; 1.17 + 1.18 +static void cx_arl_destructor(struct cx_list_s *list) { 1.19 + cx_array_list *arl = (cx_array_list*) list; 1.20 + cxFree(list->allocator, arl->data); 1.21 +} 1.22 + 1.23 +static int cx_arl_add( 1.24 + struct cx_list_s *list, 1.25 + void const *elem 1.26 +) { 1.27 + return 1; 1.28 +} 1.29 + 1.30 +static int cx_arl_insert( 1.31 + struct cx_list_s *list, 1.32 + size_t index, 1.33 + void const *elem 1.34 +) { 1.35 + return 1; 1.36 +} 1.37 + 1.38 +static int cx_arl_insert_iter( 1.39 + struct cx_iterator_s *iter, 1.40 + void const *elem, 1.41 + int prepend 1.42 +) { 1.43 + return 1; 1.44 +} 1.45 + 1.46 +static int cx_arl_remove( 1.47 + struct cx_list_s *list, 1.48 + size_t index 1.49 +) { 1.50 + return 1; 1.51 +} 1.52 + 1.53 +static void * cx_arl_at( 1.54 + struct cx_list_s const *list, 1.55 + size_t index 1.56 +) { 1.57 + return NULL; 1.58 +} 1.59 + 1.60 +static size_t cx_arl_find( 1.61 + struct cx_list_s const *list, 1.62 + void const *elem 1.63 +) { 1.64 + return 0; 1.65 +} 1.66 + 1.67 +static void cx_arl_sort(struct cx_list_s *list) { 1.68 + 1.69 +} 1.70 + 1.71 +static int cx_arl_compare( 1.72 + struct cx_list_s const *list, 1.73 + struct cx_list_s const *other 1.74 +) { 1.75 + 1.76 +} 1.77 + 1.78 +static void cx_arl_reverse(struct cx_list_s *list) { 1.79 + 1.80 +} 1.81 + 1.82 +static struct cx_iterator_s cx_arl_iterator( 1.83 + struct cx_list_s *list, 1.84 + size_t index 1.85 +) { 1.86 + struct cx_iterator_s iter; 1.87 + 1.88 + return iter; 1.89 +} 1.90 + 1.91 +static cx_list_class cx_array_list_class = { 1.92 + cx_arl_destructor, 1.93 + cx_arl_add, 1.94 + cx_arl_insert, 1.95 + cx_arl_insert_iter, 1.96 + cx_arl_remove, 1.97 + cx_arl_at, 1.98 + cx_arl_find, 1.99 + cx_arl_sort, 1.100 + cx_arl_compare, 1.101 + cx_arl_reverse, 1.102 + cx_arl_iterator, 1.103 +}; 1.104 + 1.105 CxList *cxArrayListCreate( 1.106 CxAllocator const *allocator, 1.107 CxListComparator comparator, 1.108 size_t item_size, 1.109 size_t initial_capacity 1.110 ) { 1.111 - return NULL; 1.112 + cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list)); 1.113 + if (list == NULL) return NULL; 1.114 + 1.115 + list->data = cxCalloc(allocator, initial_capacity, item_size); 1.116 + if (list->data == NULL) { 1.117 + cxFree(allocator, list); 1.118 + return NULL; 1.119 + } 1.120 + 1.121 + list->base.cl = &cx_array_list_class; 1.122 + list->base.allocator = allocator; 1.123 + list->base.cmpfunc = comparator; 1.124 + list->base.itemsize = item_size; 1.125 + list->base.capacity = initial_capacity; 1.126 + 1.127 + return (CxList *) list; 1.128 }