Mercurial > code
diff C/array.h @ 186:d4b8416e9ab1
Move C
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sat, 23 Nov 2013 16:14:05 +0100 |
parents | array.h@970e491d93cb |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C/array.h Sat Nov 23 16:14:05 2013 +0100 @@ -0,0 +1,125 @@ +/* + * array.h -- manipulate dynamic arrays + * + * Copyright (c) 2011, 2012, David Demelier <markand@malikania.fr> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef _ARRAY_H_ +#define _ARRAY_H_ + +#include <stdarg.h> + +#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 */ +}; + +struct array { + 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 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)(const void *, const void *); + +#ifdef __cplusplus +extern "C" { +#endif + +int +array_init(struct array *, size_t); + +void +array_set(struct array *, const char *, ...); + +int +array_push(struct array *, const void *); + +int +array_insert(struct array *, const void *, int); + +int +array_append(struct array *, const void *); + +void +array_pop(struct array *); + +void +array_unqueue(struct array *); + +void +array_iremove(struct array *, int); + +void +array_premove(struct array *, const void *); + +int +array_iswap(struct array *, int, int); + +int +array_pswap(struct array *, const void *, const void *); + +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 *); + +void * +array_first(const struct array *); + +void * +array_get(const struct array *, int); + +void * +array_last(const struct array *); + +void +array_clear(struct array *); + +void +array_free(struct array *); + +void * +array_trim(struct array *); + +#define ARRAY_FOREACH(a, var, i) \ + for (i = 0, (var) = array_first((a)); \ + i < (a)->length; \ + (var) = array_get(a, ++i)) + +#ifdef __cplusplus +} +#endif + +#endif /* _ARRAY_H_ */