view parray.h @ 147:535f12e0a5af

Add a flag to prevent insertions at out of bounds
author David Demelier <markand@malikania.fr>
date Fri, 11 May 2012 20:06:41 +0200
parents e3cf5ac9a5aa
children 1558251b2cf2
line wrap: on
line source

/*
 * parray.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_

#include <stdarg.h>

#ifdef __cplusplus
extern "C" {
#endif

#ifndef PARRAY_DEFAULT_BSIZE
#define PARRAY_DEFAULT_BSIZE	128
#endif

enum {
	PARRAY_AUTO		= 0,		/* array grows automatically */
	PARRAY_FIXED		= (1 << 0),	/* fixed size length */
	PARRAY_FASTREMOVE	= (1 << 1),	/* use last object when removing */
	PARRAY_NULLEND		= (1 << 2),	/* always has a NULL pointer at end */
	PARRAY_INSERTSAFE	= (1 << 3)	/* insertion must have valid indexes */
};

struct parray {
	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) */
	int	bsize;		/* (rw) block size (used when growing array) */

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

typedef void (*parray_map_t)(void *, void *);
typedef int (*parray_cmp_t)(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_t, void *);

int
parray_find(const struct parray *, parray_cmp_t, void *, void *);

void *
parray_first(const struct parray *);

void *
parray_last(const struct parray *);

void *
parray_index(const struct parray *, int);

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_ */