Mercurial > code
view array.h @ 117:bbe86bdb55da
Update parray for a lot of things
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 28 Feb 2012 20:57:48 +0100 |
parents | d534fdcbb319 |
children | 3c51b1f2974e |
line wrap: on
line source
/* * 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_BSIZE #define ARRAY_DEFAULT_BSIZE 128 #endif enum array_type { ARRAY_AUTO = 0, ARRAY_FIXED = 1 }; struct array { enum array_type type; /* array's type (default FIXED) */ void *data; /* array of data */ int length; /* number of element inside */ size_t size; /* current buffer size (allocated memory) */ size_t unit; /* unit size (sizeof the object) */ int bsize; /* block size (used when growing array) */ int i; /* only for ARRAY_FOREACH */ /* Own allocation functions */ void * (*malloc)(size_t); void * (*realloc)(void *, size_t); }; typedef void (*array_map_fn)(void *, void *); typedef int (*array_cmp_fn)(void *, void *); 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_remove(struct array *, int); void array_unref(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_fn, void *); int array_find(const struct array *, array_cmp_fn, void *, void *); void array_clear(struct array *); void array_free(struct array *); #define ARRAY_HEAD(a, type) \ (((type *)a->data)[0]) #define ARRAY_TAIL(a, type) \ (((type *)a->data)[(a->length == 0) ? 0 : a->length - 1]) #define ARRAY_INDEX(a, i, type) \ ((type *)(a)->data)[((i) < 0) \ ? 0 : ((i) >= (a)->length) ? (a)->length - 1 : (i)] #define ARRAY_FOREACH(a, var) \ for ((a)->i = 0, (var) = (a)->data; \ (a)->i < (a)->length; \ ++(a)->i, ++(var)) /* Only for ARRAY_FIXED */ #define ARRAY_FULL(a) \ ((a)->length == (a)->bsize) #endif /* _ARRAY_H_ */