Mercurial > code
diff array.h @ 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 | 1558251b2cf2 |
children | 654f32079cdc |
line wrap: on
line diff
--- 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 <stdarg.h> -#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 }