view array.h @ 132:bdd511d1a556

Add attributes
author David Demelier <markand@malikania.fr>
date Mon, 12 Mar 2012 21:46:39 +0100
parents 5917096facb9
children 195cdc5f4a32
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_

#ifdef __cplusplus
extern "C" {
#endif

#include <stdarg.h>

#ifndef ARRAY_DEFAULT_BSIZE
#define ARRAY_DEFAULT_BSIZE	128
#endif

enum {
	ARRAY_AUTO	= 0,
	ARRAY_FIXED	= 1
};

struct array {
	int	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) */

	/* 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_iremove(struct array *, int);
void	array_premove(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_FOREACH(a, var)							\
	for ((a)->i = 0, (var) = (a)->data;					\
		(a)->i < (a)->length;						\
		++(a)->i, ++(var))

#ifdef __cplusplus
}
#endif

#endif /* _ARRAY_H_ */