project_files/frontlib/util/list.h
changeset 7275 15f722e0b96f
parent 7269 5b0aeef8ba2a
child 7314 6171f0bad318
equal deleted inserted replaced
7273:8eed495fd8da 7275:15f722e0b96f
     4 
     4 
     5 #ifndef LIST_H_
     5 #ifndef LIST_H_
     6 #define LIST_H_
     6 #define LIST_H_
     7 
     7 
     8 #include <stddef.h>
     8 #include <stddef.h>
       
     9 #include <string.h>
       
    10 #include <stdlib.h>
       
    11 #include "util.h"
       
    12 #include "logging.h"
     9 
    13 
    10 /**
    14 /**
    11  * Insert element into the list and increase listSize.
    15  * Generate a static function that inserts a new value into a heap array of the given type,
    12  * Returns a pointer to the modified list on success, NULL on failure. On success, the old
    16  * using realloc and memmove to increase the capacity and shift existing values.
    13  * pointer is no longer valid, and on failure the list remains unchanged (similar to realloc)
    17  * The function takes a pointer to the array variable and a pointer to the size variable
       
    18  * because both can be changed by this operation (realloc / increment).
       
    19  * The function returns 0 on success and leaves the array unchanged on error.
    14  */
    20  */
    15 void *flib_list_insert(void *list, int *listSizePtr, size_t elementSize, void *elementPtr, int pos);
    21 #define GENERATE_STATIC_LIST_INSERT(fname, type) \
       
    22 	static int fname(type **listptr, int *listSizePtr, type element, int pos) { \
       
    23 		int result = -1; \
       
    24 		if(!listptr || !listSizePtr || pos < 0 || pos > *listSizePtr) { \
       
    25 			flib_log_e("Invalid parameter in "#fname); \
       
    26 		} else { \
       
    27 			type *newList = flib_realloc(*listptr, ((*listSizePtr)+1)*sizeof(type)); \
       
    28 			if(newList) { \
       
    29 				memmove(newList + (pos+1), newList + pos, ((*listSizePtr)-pos)*sizeof(type)); \
       
    30 				newList[pos] = element; \
       
    31 				(*listSizePtr)++; \
       
    32 				*listptr = newList; \
       
    33 				result = 0; \
       
    34 			} \
       
    35 		} \
       
    36 		return result; \
       
    37 	}
    16 
    38 
    17 /**
    39 /**
    18  * Remove an element from the list and decrease listSize.
    40  * Generate a static function that deletes a value from a heap array of the given type,
    19  * Returns a pointer to the modified list on success, NULL on failure. On success, the old
    41  * using realloc and memmove to decrease the capacity and shift existing values.
    20  * pointer is no longer valid, and on failure the list remains unchanged (similar to realloc)
    42  * The function takes a pointer to the array variable and a pointer to the size variable
       
    43  * because both can be changed by this operation (realloc / decrement).
       
    44  * The function returns 0 on success and leaves the array unchanged on error.
    21  */
    45  */
    22 void *flib_list_delete(void *list, int *listSizePtr, size_t elementSize, int pos);
    46 #define GENERATE_STATIC_LIST_DELETE(fname, type) \
       
    47 	static int fname(type **listPtr, int *listSizePtr, int pos) { \
       
    48 		int result = -1; \
       
    49 		if(!listPtr || !listSizePtr || pos < 0 || pos >= *listSizePtr) { \
       
    50 			flib_log_e("Invalid parameter in "#fname); \
       
    51 		} else { \
       
    52 			memmove((*listPtr) + pos, (*listPtr) + (pos+1), ((*listSizePtr)-(pos+1))*sizeof(type)); \
       
    53 			(*listSizePtr)--; \
       
    54 			\
       
    55 			size_t newCharSize = (*listSizePtr)*sizeof(type); \
       
    56 			type *newList = flib_realloc((*listPtr), newCharSize); \
       
    57 			if(newList || newCharSize==0) { \
       
    58 				(*listPtr) = newList; \
       
    59 			} /* If the realloc fails, just keep using the old buffer...*/ \
       
    60 			result = 0; \
       
    61 		} \
       
    62 		return result; \
       
    63 	}
    23 
    64 
    24 #endif /* LIST_H_ */
    65 #endif /* LIST_H_ */