view parray.h @ 120:68784ea3fabb

Fix (p)array_find
author David Demelier <markand@malikania.fr>
date Tue, 28 Feb 2012 21:49:20 +0100
parents bbe86bdb55da
children 5917096facb9
line wrap: on
line source

/*
 * array.h -- manipulate dynamic pointer 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 _PARRAY_H_
#define _PARRAY_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <stdarg.h>

#ifndef PARRAY_DEFAULT_BSIZE
#define PARRAY_DEFAULT_BSIZE	128
#endif

#define PARRAY_AUTO	0
#define PARRAY_FIXED	1

struct parray {
	int	type;		/* array type (default FIXED) */
	void	**data;		/* array of data */
	int	length;		/* number of element inside */
	size_t	size;		/* current buffer size (allocated memory) */
	int	bsize;		/* block size (used when growing array) */

	/* Own allocation functions */
	void * (*malloc)(size_t);
	void * (*realloc)(void *, size_t);
};

typedef void (*parray_map_fn)(void *, void *);
typedef int (*parray_cmp_fn)(void *, void *);

int	parray_init(struct parray *);
void	parray_set(struct parray *, const char *, ...);
int	parray_push(struct parray *, void *);
int	parray_insert(struct parray *, void *, int);
int	parray_append(struct parray *, void *);
void	parray_pop(struct parray *);
void	parray_unqueue(struct parray *);
void	parray_iremove(struct parray *, int);
void	parray_premove(struct parray *, const void *);
int	parray_iswap(struct parray *, int, int);
int	parray_pswap(struct parray *, const void *, const void *);
void	parray_map(const struct parray *, parray_map_fn, void *);
int	parray_find(const struct parray *, parray_cmp_fn, void *, void *);
void	parray_clear(struct parray *);
void	parray_free(struct parray *);

#define PARRAY_FOREACH(a, o, i)						\
	for (i = 0, o = (a)->data[i];					\
	    i < (a)->length;						\
	    ++i, o = (a)->data[i])

#ifdef __cplusplus
}
#endif

#endif /* _PARRAY_H_ */