# HG changeset patch # User David Demelier # Date 1346701254 -7200 # Node ID 66d317ba4b808930e2c047650f18f0c27d1fb32a # Parent 3e63d94b495f174c1436299f02cda8a08b286366 Added a new _qsort function for array and parray diff -r 3e63d94b495f -r 66d317ba4b80 array.c --- a/array.c Mon Sep 03 21:40:04 2012 +0200 +++ b/array.c Mon Sep 03 21:40:54 2012 +0200 @@ -24,7 +24,7 @@ #define OFFSET(x) (arr->unit * (x)) -static int grow(struct array *); +static int grow(struct array *); int array_init(struct array *arr, size_t unit) @@ -33,10 +33,10 @@ return -1; arr->unit = unit; - arr->size = OFFSET(arr->bsize); + arr->size = OFFSET(arr->chksize); /* Set defaults if needed */ - arr->bsize = (arr->bsize <= 0) ? ARRAY_DEFAULT_BSIZE : arr->bsize; + arr->chksize = (arr->chksize <= 0) ? ARRAY_DEFAULT_CHKSIZE : arr->chksize; arr->malloc = (arr->malloc == NULL) ? &malloc : arr->malloc; arr->realloc = (arr->realloc == NULL) ? &realloc : arr->realloc; @@ -56,7 +56,6 @@ * r -> realloc function that must matches void * (*realloc)(void *, size_t) * f -> array flags of type int */ - void array_set(struct array *arr, const char *fmt, ...) { @@ -67,7 +66,7 @@ for (p = fmt; *p != '\0'; ++p) switch (*p) { case 'l': - arr->bsize = va_arg(ap, int); + arr->chksize = va_arg(ap, int); break; case 'm': arr->malloc = va_arg(ap, void *(*)(size_t)); @@ -88,7 +87,6 @@ * of object (about 100000). If you need to add a lot of data please consider * using linked list instead. Returns -1 on failure or 0 on success. */ - int array_push(struct array *arr, const void *data) { @@ -105,7 +103,6 @@ * Insert the data at the specified index. The function returns -1 on * allocation failure or the position of the added element. */ - int array_insert(struct array *arr, const void *data, int index) { @@ -130,7 +127,6 @@ * Append the data to the end of array. Returns -1 on failure or the position * of the added element. */ - int array_append(struct array *arr, const void *data) { @@ -145,7 +141,6 @@ /* * Remove the array's head. */ - void array_pop(struct array *arr) { @@ -155,7 +150,6 @@ /* * Remove the array's tail. */ - void array_unqueue(struct array *arr) { @@ -165,7 +159,6 @@ /* * Remove the data at the specified index. Bounds are checked. */ - void array_iremove(struct array *arr, int index) { @@ -188,7 +181,6 @@ * Remove the object referenced by the `data' argument. Useful when you * don't know the index. */ - void array_premove(struct array *arr, const void *data) { @@ -210,7 +202,6 @@ * to allocate data to swap elements thus if the functions fails it returns -1 * otherwise 0 is returned. */ - int array_iswap(struct array *arr, int i1, int i2) { @@ -245,7 +236,6 @@ * may be slow on large arrays since it must travel all the object * to find the indexes. */ - int array_pswap(struct array *arr, const void *o1, const void *o2) { @@ -270,7 +260,6 @@ * Apply the function `fn' on each object and give the optional `udata' * argument to the function too. */ - void array_map(const struct array *arr, array_map_t fn, void *udata) { @@ -281,11 +270,19 @@ } /* + * Call qsort function to sort the array. + */ +void +array_sort(struct array *arr, array_cmp_t fn) +{ + qsort(arr->data, arr->length, arr->unit, fn); +} + +/* * Compare each object with the user supplied function. If the `fn' function * returns 1, 1 is returned and dst points to the correct object, dst should * be a pointer to a pointer of object, like (int **) for a array of int. */ - int array_find(const struct array *arr, array_cmp_t fn, void *dst, void *u) { @@ -317,7 +314,7 @@ } void * -array_index(const struct array *arr, int idx) +array_get(const struct array *arr, int idx) { if (idx < 0) return array_first(arr); @@ -330,7 +327,6 @@ /* * Erase every bytes and set the length to 0. */ - void array_clear(struct array *arr) { @@ -341,7 +337,6 @@ /* * Same as array_clear except it also free the array object. */ - void array_free(struct array *arr) { @@ -357,7 +352,6 @@ * it returns -1 on full buffer otherwise 0 is returned if allocation * succeeded. */ - static int grow(struct array *arr) { @@ -366,12 +360,12 @@ if (!(arr->flags & ARRAY_FIXED)) { if ((arr->data = arr->realloc(arr->data, arr->size + - OFFSET(arr->bsize))) == NULL) { + OFFSET(arr->chksize))) == NULL) { arr->size = arr->length = 0; return -1; } - arr->size += OFFSET(arr->bsize); + arr->size += OFFSET(arr->chksize); } else return -1; diff -r 3e63d94b495f -r 66d317ba4b80 array.h --- a/array.h Mon Sep 03 21:40:04 2012 +0200 +++ b/array.h Mon Sep 03 21:40:54 2012 +0200 @@ -21,37 +21,35 @@ #include -#ifdef __cplusplus -extern "C" { -#endif - -#ifndef ARRAY_DEFAULT_BSIZE -#define ARRAY_DEFAULT_BSIZE 128 +#ifndef ARRAY_DEFAULT_CHKSIZE +#define ARRAY_DEFAULT_CHKSIZE 128 #endif -enum array_flags { - ARRAY_AUTO = 0, /* array grows automatically */ - ARRAY_FIXED = (1 << 0), /* fixed size length */ - ARRAY_FASTREMOVE = (1 << 1), /* use last object when removing */ - ARRAY_CLEARBITS = (1 << 2), /* clear data when inserting/removing */ - ARRAY_INSERTSAFE = (1 << 3) /* insertion must have valid indexes */ -}; +#define ARRAY_AUTO 0, /* array grows automatically */ +#define ARRAY_FIXED (1 << 0) /* fixed size length */ +#define ARRAY_FASTREMOVE (1 << 1) /* use last object when removing */ +#define ARRAY_CLEARBITS (1 << 2) /* clear data when inserting/removing */ +#define ARRAY_INSERTSAFE (1 << 3) /* insertion must have valid indexes */ struct array { - enum array_flags flags; /* (ro) array flags (default AUTO) */ + int flags; /* (ro) array flags (default AUTO) */ void *data; /* (rw) array of data */ int length; /* (ro) number of element inside */ size_t size; /* (ro) current buffer size (allocated memory) */ size_t unit; /* (ro) unit size (sizeof the object) */ - int bsize; /* (rw) block size (used when growing array) */ + int chksize; /* (rw) chunk size (used when growing array) */ /* Own allocation functions */ void * (*malloc)(size_t); void * (*realloc)(void *, size_t); }; -typedef void (*array_map_t)(void *, void *); -typedef int (*array_cmp_t)(void *, void *); +typedef void (*array_map_t)(void *, void *); +typedef int (*array_cmp_t)(const void *, const void *); + +#ifdef __cplusplus +extern "C" { +#endif int array_init(struct array *, size_t); @@ -89,6 +87,9 @@ void array_map(const struct array *, array_map_t, void *); +void +array_sort(struct array *, array_cmp_t); + int array_find(const struct array *, array_cmp_t, void *, void *); @@ -96,10 +97,10 @@ array_first(const struct array *); void * -array_last(const struct array *); +array_get(const struct array *, int); void * -array_index(const struct array *, int); +array_last(const struct array *); void array_clear(struct array *); @@ -107,10 +108,10 @@ void array_free(struct array *); -#define ARRAY_FOREACH(a, var, i) \ - for (i = 0, (var) = array_first((a)); \ - i < (a)->length; \ - (var) = array_index(a, ++i)) +#define ARRAY_FOREACH(a, var, i) \ + for (i = 0, (var) = array_first((a)); \ + i < (a)->length; \ + (var) = array_get(a, ++i)) #ifdef __cplusplus } diff -r 3e63d94b495f -r 66d317ba4b80 parray.c --- a/parray.c Mon Sep 03 21:40:04 2012 +0200 +++ b/parray.c Mon Sep 03 21:40:54 2012 +0200 @@ -24,7 +24,7 @@ #define LENGTH(x) ((x) * (sizeof (void *))) -static int grow(struct parray *); +static int grow(struct parray *); int parray_init(struct parray *arr) @@ -52,7 +52,6 @@ * r -> realloc function that must matches void * (*realloc)(void *, size_t) * f -> parray flags of type int */ - void parray_set(struct parray *arr, const char *fmt, ...) { @@ -84,7 +83,6 @@ * of object (about 100000). If you need to add a lot of data please consider * using linked list instead. Returns -1 on failure or 0 on success. */ - int parray_push(struct parray *arr, void *data) { @@ -104,7 +102,6 @@ * Insert the data at the specified index. The function returns -1 on * allocation failure or the position of the added element. */ - int parray_insert(struct parray *arr, void *data, int index) { @@ -132,7 +129,6 @@ * Append the data to the end of array. Returns -1 on failure or the position * of the added element. */ - int parray_append(struct parray *arr, void *data) { @@ -150,7 +146,6 @@ /* * Remove the array's head. */ - void parray_pop(struct parray *arr) { @@ -160,7 +155,6 @@ /* * Remove the array's tail. */ - void parray_unqueue(struct parray *arr) { @@ -170,7 +164,6 @@ /* * Remove the data at the specified index. Bounds are checked. */ - void parray_iremove(struct parray *arr, int index) { @@ -189,7 +182,6 @@ * Remove the object referenced by the `data' argument. Useful when you * don't know the index. */ - void parray_premove(struct parray *arr, const void *data) { @@ -209,7 +201,6 @@ * to allocate data to swap elements thus if the functions fails it returns -1 * otherwise 0 is returned. */ - int parray_iswap(struct parray *arr, int i1, int i2) { @@ -231,7 +222,6 @@ * may be slow on large arrays since it must travel all the object * to find the indexes. */ - int parray_pswap(struct parray *arr, const void *o1, const void *o2) { @@ -256,7 +246,6 @@ * Apply the function `fn' on each object and give the optional `udata' * argument to the function too. */ - void parray_map(const struct parray *arr, parray_map_t fn, void *udata) { @@ -272,7 +261,6 @@ * data points to the array data at the correct index. If the comparison * function nevers returns 1, array_find returns -1. */ - int parray_find(const struct parray *arr, parray_cmp_t fn, void *ptr, void *u) { @@ -294,17 +282,7 @@ } void * -parray_last(const struct parray *arr) -{ - if (arr->length == 0) - return parray_first(arr); - - return arr->data[arr->length - 1]; - -} - -void * -parray_index(const struct parray *arr, int idx) +parray_get(const struct parray *arr, int idx) { if (idx < 0) return parray_first(arr); @@ -314,10 +292,18 @@ return arr->data[idx]; } +void * +parray_last(const struct parray *arr) +{ + if (arr->length == 0) + return parray_first(arr); + + return arr->data[arr->length - 1]; +} + /* * Reset the array by setting each pointer to NULL and the length to 0. */ - void parray_clear(struct parray *arr) { @@ -328,7 +314,6 @@ /* * Same as parray_clear except it also free the array object. */ - void parray_free(struct parray *arr) { @@ -344,13 +329,10 @@ * it returns -1 on full buffer otherwise 0 is returned if allocation * succeeded. */ - static int grow(struct parray *arr) { - unsigned int toadd; - - toadd = (arr->flags & PARRAY_NULLEND) ? 2 : 1; + unsigned int toadd = (arr->flags & PARRAY_NULLEND) ? 2 : 1; if ((arr->size / sizeof (void *)) - arr->length >= toadd) return 0; diff -r 3e63d94b495f -r 66d317ba4b80 parray.h --- a/parray.h Mon Sep 03 21:40:04 2012 +0200 +++ b/parray.h Mon Sep 03 21:40:54 2012 +0200 @@ -21,10 +21,6 @@ #include -#ifdef __cplusplus -extern "C" { -#endif - #ifndef PARRAY_DEFAULT_BSIZE #define PARRAY_DEFAULT_BSIZE 128 #endif @@ -52,6 +48,10 @@ typedef void (*parray_map_t)(void *, void *); typedef int (*parray_cmp_t)(void *, void *); +#ifdef __cplusplus +extern "C" { +#endif + int parray_init(struct parray *); @@ -95,10 +95,10 @@ parray_first(const struct parray *); void * -parray_last(const struct parray *); +parray_get(const struct parray *, int); void * -parray_index(const struct parray *, int); +parray_last(const struct parray *); void parray_clear(struct parray *);