project_files/frontlib/util/list.h
changeset 7275 15f722e0b96f
parent 7269 5b0aeef8ba2a
child 7314 6171f0bad318
--- a/project_files/frontlib/util/list.h	Mon Jun 25 15:21:18 2012 +0200
+++ b/project_files/frontlib/util/list.h	Wed Jun 27 18:02:45 2012 +0200
@@ -6,19 +6,60 @@
 #define LIST_H_
 
 #include <stddef.h>
+#include <string.h>
+#include <stdlib.h>
+#include "util.h"
+#include "logging.h"
 
 /**
- * Insert element into the list and increase listSize.
- * Returns a pointer to the modified list on success, NULL on failure. On success, the old
- * pointer is no longer valid, and on failure the list remains unchanged (similar to realloc)
+ * Generate a static function that inserts a new value into a heap array of the given type,
+ * using realloc and memmove to increase the capacity and shift existing values.
+ * The function takes a pointer to the array variable and a pointer to the size variable
+ * because both can be changed by this operation (realloc / increment).
+ * The function returns 0 on success and leaves the array unchanged on error.
  */
-void *flib_list_insert(void *list, int *listSizePtr, size_t elementSize, void *elementPtr, int pos);
+#define GENERATE_STATIC_LIST_INSERT(fname, type) \
+	static int fname(type **listptr, int *listSizePtr, type element, int pos) { \
+		int result = -1; \
+		if(!listptr || !listSizePtr || pos < 0 || pos > *listSizePtr) { \
+			flib_log_e("Invalid parameter in "#fname); \
+		} else { \
+			type *newList = flib_realloc(*listptr, ((*listSizePtr)+1)*sizeof(type)); \
+			if(newList) { \
+				memmove(newList + (pos+1), newList + pos, ((*listSizePtr)-pos)*sizeof(type)); \
+				newList[pos] = element; \
+				(*listSizePtr)++; \
+				*listptr = newList; \
+				result = 0; \
+			} \
+		} \
+		return result; \
+	}
 
 /**
- * Remove an element from the list and decrease listSize.
- * Returns a pointer to the modified list on success, NULL on failure. On success, the old
- * pointer is no longer valid, and on failure the list remains unchanged (similar to realloc)
+ * Generate a static function that deletes a value from a heap array of the given type,
+ * using realloc and memmove to decrease the capacity and shift existing values.
+ * The function takes a pointer to the array variable and a pointer to the size variable
+ * because both can be changed by this operation (realloc / decrement).
+ * The function returns 0 on success and leaves the array unchanged on error.
  */
-void *flib_list_delete(void *list, int *listSizePtr, size_t elementSize, int pos);
+#define GENERATE_STATIC_LIST_DELETE(fname, type) \
+	static int fname(type **listPtr, int *listSizePtr, int pos) { \
+		int result = -1; \
+		if(!listPtr || !listSizePtr || pos < 0 || pos >= *listSizePtr) { \
+			flib_log_e("Invalid parameter in "#fname); \
+		} else { \
+			memmove((*listPtr) + pos, (*listPtr) + (pos+1), ((*listSizePtr)-(pos+1))*sizeof(type)); \
+			(*listSizePtr)--; \
+			\
+			size_t newCharSize = (*listSizePtr)*sizeof(type); \
+			type *newList = flib_realloc((*listPtr), newCharSize); \
+			if(newList || newCharSize==0) { \
+				(*listPtr) = newList; \
+			} /* If the realloc fails, just keep using the old buffer...*/ \
+			result = 0; \
+		} \
+		return result; \
+	}
 
 #endif /* LIST_H_ */