# HG changeset patch # User Mike Becker # Date 1674670769 -3600 # Node ID 55cc3b373c5e67cfccae915e4066ebb51b0f0b48 # Parent 309e8b08c60e34d56b4dcad404cb03118c42332f simplify list class - fixes #236 diff -r 309e8b08c60e -r 55cc3b373c5e src/array_list.c --- a/src/array_list.c Mon Jan 23 20:34:18 2023 +0100 +++ b/src/array_list.c Wed Jan 25 19:19:29 2023 +0100 @@ -168,23 +168,6 @@ cxFree(list->allocator, arl->data); } -static int cx_arl_add( - struct cx_list_s *list, - void const *elem -) { - cx_array_list *arl = (cx_array_list *) list; - return cx_array_copy( - &arl->data, - &list->size, - &list->capacity, - list->size, - elem, - list->itemsize, - 1, - &arl->reallocator - ); -} - static size_t cx_arl_insert_array( struct cx_list_s *list, size_t index, @@ -241,48 +224,6 @@ } } -static size_t cx_arl_add_array( - struct cx_list_s *list, - void const *array, - size_t n -) { - return cx_arl_insert_array(list, list->size, array, n); -} - -static int cx_arl_insert( - struct cx_list_s *list, - size_t index, - void const *elem -) { - if (index > list->size) { - return 1; - } else if (index == list->size) { - return cx_arl_add(list, elem); - } else { - cx_array_list *arl = (cx_array_list *) list; - - // move elements starting at index to the right - if (cx_array_copy( - &arl->data, - &list->size, - &list->capacity, - index + 1, - ((char *) arl->data) + index * list->itemsize, - list->itemsize, - list->size - index, - &arl->reallocator - )) { - return 1; - } - - // place the element - memcpy(((char *) arl->data) + index * list->itemsize, - elem, list->itemsize); - - return 0; - } -} - static int cx_arl_insert_iter( struct cx_mut_iterator_s *iter, void const *elem, @@ -290,10 +231,11 @@ ) { struct cx_list_s *list = iter->src_handle; if (iter->index < list->size) { - int result = cx_arl_insert( + int result = 1 != cx_arl_insert_array( list, iter->index + 1 - prepend, - elem + elem, + 1 ); if (result == 0 && prepend != 0) { iter->index++; @@ -301,7 +243,7 @@ } return result; } else { - int result = cx_arl_add(list, elem); + int result = 1 != cx_arl_insert_array(list, list->size, elem, 1); iter->index = list->size; return result; } @@ -463,24 +405,8 @@ return iter; } -static struct cx_mut_iterator_s cx_arl_mut_iterator( - struct cx_list_s *list, - size_t index -) { - CxIterator it = cx_arl_iterator(list, index); - it.base.mutating = true; - - // we know the iterators share the same memory layout - CxMutIterator iter; - memcpy(&iter, &it, sizeof(CxMutIterator)); - return iter; -} - static cx_list_class cx_array_list_class = { cx_arl_destructor, - cx_arl_add, - cx_arl_add_array, - cx_arl_insert, cx_arl_insert_array, cx_arl_insert_iter, cx_arl_remove, @@ -490,7 +416,6 @@ cx_arl_compare, cx_arl_reverse, cx_arl_iterator, - cx_arl_mut_iterator, }; CxList *cxArrayListCreate( diff -r 309e8b08c60e -r 55cc3b373c5e src/cx/list.h --- a/src/cx/list.h Mon Jan 23 20:34:18 2023 +0100 +++ b/src/cx/list.h Wed Jan 25 19:19:29 2023 +0100 @@ -122,33 +122,8 @@ void (*destructor)(struct cx_list_s *list); /** - * Member function for adding an element. - */ - int (*add)( - struct cx_list_s *list, - void const *elem - ); - - /** - * Member function for adding multiple elements. - */ - size_t (*add_array)( - struct cx_list_s *list, - void const *array, - size_t n - ); - - /** - * Member function for inserting an element. - */ - int (*insert)( - struct cx_list_s *list, - size_t index, - void const *elem - ); - - /** * Member function for inserting multiple elements. + * Implementors SHOULD see to performant implementations for corner cases. */ size_t (*insert_array)( struct cx_list_s *list, @@ -209,20 +184,12 @@ void (*reverse)(struct cx_list_s *list); /** - * Returns an iterator pointing to the specified index. + * Member function for returning an iterator pointing to the specified index. */ struct cx_iterator_s (*iterator)( struct cx_list_s const *list, size_t index ); - - /** - * Returns a mutating iterator pointing to the specified index. - */ - struct cx_mut_iterator_s (*mut_iterator)( - struct cx_list_s *list, - size_t index - ); }; /** @@ -243,7 +210,7 @@ CxList *list, void const *elem ) { - return list->cl->add(list, elem); + return list->cl->insert_array(list, list->size, elem, 1) != 1; } /** @@ -265,7 +232,7 @@ void const *array, size_t n ) { - return list->cl->add_array(list, array, n); + return list->cl->insert_array(list, list->size, array, n); } /** @@ -287,7 +254,7 @@ size_t index, void const *elem ) { - return list->cl->insert(list, index, elem); + return list->cl->insert_array(list, index, elem, 1) != 1; } /** @@ -422,12 +389,10 @@ * @return a new iterator */ __attribute__((__nonnull__, __warn_unused_result__)) -static inline CxMutIterator cxListMutIterator( +CxMutIterator cxListMutIterator( CxList *list, size_t index -) { - return list->cl->mut_iterator(list, index); -} +); /** * Returns an iterator pointing to the first item of the list. @@ -456,7 +421,7 @@ */ __attribute__((__nonnull__, __warn_unused_result__)) static inline CxMutIterator cxListBeginMut(CxList *list) { - return list->cl->mut_iterator(list, 0); + return cxListMutIterator(list, 0); } /** diff -r 309e8b08c60e -r 55cc3b373c5e src/linked_list.c --- a/src/linked_list.c Mon Jan 23 20:34:18 2023 +0100 +++ b/src/linked_list.c Wed Jan 25 19:19:29 2023 +0100 @@ -546,29 +546,6 @@ return n; } -static int cx_ll_insert( - struct cx_list_s *list, - size_t index, - void const *elem -) { - return cx_ll_insert_array(list, index, elem, 1) != 1; -} - -static int cx_ll_add( - struct cx_list_s *list, - void const *elem -) { - return cx_ll_insert(list, list->size, elem); -} - -static size_t cx_ll_add_array( - struct cx_list_s *list, - void const *array, - size_t n -) { - return cx_ll_insert_array(list, list->size, array, n); -} - static int cx_ll_remove( struct cx_list_s *list, size_t index @@ -691,19 +668,6 @@ return iter; } -static CxMutIterator cx_ll_mut_iterator( - struct cx_list_s *list, - size_t index -) { - CxIterator it = cx_ll_iterator(list, index); - it.base.mutating = true; - - // we know the iterators share the same memory layout - CxMutIterator iter; - memcpy(&iter, &it, sizeof(CxMutIterator)); - return iter; -} - static int cx_ll_insert_iter( CxMutIterator *iter, void const *elem, @@ -718,7 +682,7 @@ iter->index += prepend * (0 == result); return result; } else { - int result = cx_ll_insert(list, list->size, elem); + int result = cx_ll_insert_array(list, list->size, elem, 1) != 1; iter->index = list->size; return result; } @@ -738,9 +702,6 @@ static cx_list_class cx_linked_list_class = { cx_ll_destructor, - cx_ll_add, - cx_ll_add_array, - cx_ll_insert, cx_ll_insert_array, cx_ll_insert_iter, cx_ll_remove, @@ -750,7 +711,6 @@ cx_ll_compare, cx_ll_reverse, cx_ll_iterator, - cx_ll_mut_iterator, }; CxList *cxLinkedListCreate( diff -r 309e8b08c60e -r 55cc3b373c5e src/list.c --- a/src/list.c Mon Jan 23 20:34:18 2023 +0100 +++ b/src/list.c Wed Jan 25 19:19:29 2023 +0100 @@ -28,6 +28,8 @@ #include "cx/list.h" +#include + void cxListDestroy(CxList *list) { switch (list->content_destructor_type) { case CX_DESTRUCTOR_SIMPLE: { @@ -80,3 +82,16 @@ } } } + +CxMutIterator cxListMutIterator( + CxList *list, + size_t index +) { + CxIterator it = list->cl->iterator(list, index); + it.base.mutating = true; + + // we know the iterators share the same memory layout + CxMutIterator iter; + memcpy(&iter, &it, sizeof(CxMutIterator)); + return iter; +}