view parray.h @ 169:29531c2f8213

Update nsock
author David Demelier <markand@malikania.fr>
date Mon, 26 Aug 2013 22:23:06 +0200
parents 970e491d93cb
children
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>

#ifndef PARRAY_DEFAULT_BSIZE
#define PARRAY_DEFAULT_BSIZE	128
#endif

enum parray_flags {
	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 {
	enum parray_flags	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 *);

#ifdef __cplusplus
extern "C" {
#endif

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_get(const struct parray *, int);

void *
parray_last(const struct parray *);

void
parray_clear(struct parray *);

void
parray_free(struct parray *);

void *
parray_trim(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_ */