From f4c51e593eeecda6667d1d3a17e7f3f0b074a7af Mon Sep 17 00:00:00 2001 From: Stefan 'psYchotic' Zwanenburg Date: Fri, 17 Feb 2012 17:51:27 +0100 Subject: [PATCH] Internal array changed to array of bytes. This means that elements don't need to be allocated upon insertion anymore, as well as not having to free elements that are removed from the array. All in all, this improves performance. --- myarray.c | 20 +++++++------------- myarray.h | 5 +++-- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/myarray.c b/myarray.c index 9fc2b99..aa256dd 100644 --- a/myarray.c +++ b/myarray.c @@ -5,7 +5,7 @@ MArray myarray_new(int size, int incr, int elsize) { MArray myarray = malloc(sizeof(struct myarray)); - myarray->elements = calloc(size, sizeof(void *)); + myarray->elements = malloc(size * elsize); myarray->len = 0; myarray->maxlen = size; myarray->incr = incr; @@ -21,12 +21,10 @@ void myarray_set_cmpfunc(MArray array, int (*cmpfunc)(void *, void *)) { void myarray_enlarge(MArray array) { array->maxlen+= array->incr; - array->elements = realloc(array->elements, array->maxlen*sizeof(void *)); + array->elements = realloc(array->elements, array->maxlen*array->elsize); } void _myarray_insert(MArray array, void *element, int index) { - void *toinsert = malloc(array->elsize); - memcpy(toinsert, element, array->elsize); if (index < 0 || index > array->len) { fprintf(stderr, "Array index out of bounds %s:%d. Index: %d. Array size: %d\n", __FILE__, __LINE__, index, array->len); exit(EXIT_FAILURE); @@ -34,8 +32,9 @@ void _myarray_insert(MArray array, void *element, int index) { if (array->len == array->maxlen) { myarray_enlarge(array); } - memmove(&array->elements[index+1], &array->elements[index], (array->len - index)*sizeof(void *)); - array->elements[index] = toinsert; + memmove(array->elements + (index + 1) * array->elsize, array->elements + index * array->elsize, (array->len - index)*array->elsize); + void *toinsert = array->elements + (index * array->elsize); + memcpy(toinsert, element, array->elsize); array->len += 1; } @@ -49,7 +48,7 @@ void myarray_insert_sorted_helper(MArray array, void *element, int left, int rig } index = (left + right) / 2; - retVal = array->cmpfunc(*(void **) element, myarray_get(array, void *, index)); + retVal = array->cmpfunc(element, array->elements + index * array->elsize); if (retVal == 0) { _myarray_insert(array, element, index); @@ -69,16 +68,11 @@ void _myarray_insert_sorted(MArray array, void *element) { } void myarray_remove_index(MArray array, int index) { - free(array->elements[index]); - memmove(&array->elements[index], &array->elements[index+1], (array->len - index - 1)*sizeof(void *)); + memmove(array->elements + index * array->elsize, array->elements + (index + 1) * array->elsize, (array->len - index - 1)*array->elsize); array->len--; } void myarray_free(MArray array) { - int i; - for (i = 0; i < array->len; i++) { - free(array->elements[i]); - } free(array->elements); free(array); } diff --git a/myarray.h b/myarray.h index 93e69f9..8e8d248 100644 --- a/myarray.h +++ b/myarray.h @@ -1,9 +1,10 @@ #ifndef _MYARRAY_H #define _MYARRAY_H +#include #include typedef struct myarray { - void **elements; + uint8_t *elements; int len; int maxlen; int incr; @@ -18,7 +19,7 @@ MArray myarray_new(int, int, int); #define myarray_insert(a,e,i) _myarray_insert((a), &(e), (i)) void _myarray_insert(MArray, void *, int); -#define myarray_get(a,t,i) (*((t**) (a)->elements)[i]) +#define myarray_get(a,t,i) (((t*) ((a)->elements))[(i)]) void myarray_remove_index(MArray, int); -- 2.11.4.GIT