Mercurial > code
diff array.h @ 6:25cc379de564
Added array.c and array.h:
Various function to manipulate dynamic array. This let you adding as
much as you want object into the array. You may add object to the head,
end or at a specified index.
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 06 Sep 2011 22:19:18 +0200 |
parents | |
children | 127254037b30 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/array.h Tue Sep 06 22:19:18 2011 +0200 @@ -0,0 +1,57 @@ +/* + * array.h -- manipulate dymanic array + * + * Copyright (c) 2011, 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_ + +#define ARRAY_DEFAULT_BSIZE 128 + +struct array { + void **data; /* array of data */ + size_t length; /* number of element in array */ + +#define ARRAY_FIXED 0x00000000 +#define ARRAY_AUTO 0x00000001 + int flags; /* array's flags (default FIXED) */ + + /* Private should not be modified by user */ + size_t size; /* current buffer size */ + size_t bsize; /* block size */ +}; + +struct array *array_new(const void *, size_t, int); +#define array_new_auto(size) array_new(NULL, size, ARRAY_AUTO) +#define array_new_fixed(max) array_new(NULL, max, ARRAY_FIXED) + +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_remove(struct array *, int); +void array_map(struct array *, void (*fn)(void *, void *), void *); +void *array_find(struct array *, int (*fn)(void *, void *), void *, int *); +void array_clear(struct array *, int); +void array_free(struct array *, int); + +#define ARRAY_FOREACH(array, entry, tmp, type) \ + for ((tmp) = (type **) (array)->data, entry = *tmp; \ + (entry) != NULL; \ + ++(tmp), (entry) = (*tmp)) + +#endif /* _ARRAY_H */