Mercurial > code
diff array.c @ 163:66d317ba4b80
Added a new _qsort function for array and parray
author | David Demelier <markand@malikania.fr> |
---|---|
date | Mon, 03 Sep 2012 21:40:54 +0200 |
parents | 94847374833b |
children | 970e491d93cb |
line wrap: on
line diff
--- 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;