Mercurial > code
view array.h @ 20:a7ff7f59c7f6
Add a portable version of stdbool.h
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 09 Sep 2011 01:15:25 +0200 |
parents | 170059122a99 |
children | ecdf21f1d0c6 |
line wrap: on
line source
/* * array.h -- manipulate dynamic arrays * * 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 { char *data; /* array of data */ int length; /* number of element inside */ #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 (used when growing array) */ size_t unit; /* unit size (sizeof the object) */ }; struct array *array_new(const void *, size_t, size_t, int); #define array_new_auto(size) array_new(NULL, size, 0, ARRAY_AUTO) #define array_new_fixed(size, max) array_new(NULL, size, 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 *); void array_free(struct array *); #define ARRAY_FOREACH(array, var, i, type) \ for (i = 0, var = (type *) array->data; \ i < array->length; \ ++i, ++var) #endif /* _ARRAY_H_ */