From d1b63e047b6eeacc1eab9810c7482f7f75803afb Mon Sep 17 00:00:00 2001 From: scottmk Date: Sun, 15 Nov 2009 16:25:34 +0000 Subject: [PATCH] Created a new function osrfListClear() to take an existing osrfList and make it empty. M include/opensrf/osrf_list.h M src/libopensrf/osrf_list.c git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1851 9efc2488-bf62-4759-914b-345cdb29e865 --- include/opensrf/osrf_list.h | 2 ++ src/libopensrf/osrf_list.c | 38 ++++++++++++++++++++++++++++--------- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/include/opensrf/osrf_list.h b/include/opensrf/osrf_list.h index 8e43069..6f0ec6f 100644 --- a/include/opensrf/osrf_list.h +++ b/include/opensrf/osrf_list.h @@ -88,6 +88,8 @@ void* osrfListGetIndex( const osrfList* list, unsigned int position ); void osrfListFree( osrfList* list ); +void osrfListClear( osrfList* list ); + void* osrfListRemove( osrfList* list, unsigned int position ); void* osrfListExtract( osrfList* list, unsigned int position ); diff --git a/src/libopensrf/osrf_list.c b/src/libopensrf/osrf_list.c index dba2fcf..6eff711 100644 --- a/src/libopensrf/osrf_list.c +++ b/src/libopensrf/osrf_list.c @@ -1,5 +1,5 @@ /** - @file osrf_list.c + @file osrf_list.c @brief Implementation of osrfList, sort of like a vector class. */ @@ -32,8 +32,8 @@ osrfList* osrfNewListSize( unsigned int size ) { OSRF_MALLOC(list, sizeof(osrfList)); list->size = 0; list->freeItem = NULL; - if( size <= 0 ) size = 16; - list->arrsize = size; + if( size <= 0 ) size = 16; + list->arrsize = size; OSRF_MALLOC( list->arrlist, list->arrsize * sizeof(void*) ); // Nullify all pointers in the array @@ -78,7 +78,7 @@ int osrfListPush( osrfList* list, void* item ) { int osrfListPushFirst( osrfList* list, void* item ) { if(!(list && item)) return -1; int i; - for( i = 0; i < list->size; i++ ) + for( i = 0; i < list->size; i++ ) if(!list->arrlist[i]) break; osrfListSet( list, item, i ); return list->size; @@ -107,7 +107,7 @@ void* osrfListSet( osrfList* list, void* item, unsigned int position ) { int newsize = list->arrsize; - while( position >= newsize ) + while( position >= newsize ) newsize += OSRF_LIST_INC_SIZE; if( newsize > list->arrsize ) { /* expand the list if necessary */ @@ -115,7 +115,7 @@ void* osrfListSet( osrfList* list, void* item, unsigned int position ) { OSRF_MALLOC(newarr, newsize * sizeof(void*)); // Copy the old pointers, and nullify the new ones - + int i; for( i = 0; i < list->arrsize; i++ ) newarr[i] = list->arrlist[i]; @@ -161,7 +161,7 @@ void osrfListFree( osrfList* list ) { if( list->freeItem ) { int i; void* val; for( i = 0; i < list->size; i++ ) { - if( (val = list->arrlist[i]) ) + if( (val = list->arrlist[i]) ) list->freeItem(val); } } @@ -170,6 +170,26 @@ void osrfListFree( osrfList* list ) { free(list); } +/** + @brief Make an osrfList empty. + @param list Pointer to the osrfList to be cleared. + + Delete every item in the list. If a callback function is defined for freeing the items, + call it for every item. +*/ +void osrfListClear( osrfList* list ) { + if(!list) return; + + unsigned int i; + for( i = 0; i < list->size; ++i ) { + if( list->freeItem && list->arrlist[ i ] ) + list->freeItem( list->arrlist[ i ] ); + list->arrlist[ i ] = NULL; + } + + list->size = 0; +} + /** @brief Remove the pointer from a specified position. @param list A pointer to the osrfList. @@ -236,7 +256,7 @@ int osrfListFind( const osrfList* list, void* addr ) { if(!(list && addr)) return -1; int index; for( index = 0; index < list->size; index++ ) { - if( list->arrlist[index] == addr ) + if( list->arrlist[index] == addr ) return index; } return -1; @@ -361,7 +381,7 @@ void osrfListIteratorReset( osrfListIterator* itr ) { /** @brief Install a default item-freeing callback function (the ANSI standard free() function). @param list A pointer to the osrfList where the callback function will be installed. - */ +*/ void osrfListSetDefaultFree( osrfList* list ) { if(!list) return; list->freeItem = free; -- 2.43.2