libanim

diff src/dynarr.c @ 0:fad4701f484e

libanim mercurial repo
author John Tsiombikas <nuclear@mutantstargoat.com>
date Sun, 08 Jan 2012 05:13:13 +0200
parents
children 60a46a122b0f
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/dynarr.c	Sun Jan 08 05:13:13 2012 +0200
     1.3 @@ -0,0 +1,122 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <string.h>
     1.7 +#include "dynarr.h"
     1.8 +
     1.9 +/* The array descriptor keeps auxilliary information needed to manipulate
    1.10 + * the dynamic array. It's allocated adjacent to the array buffer.
    1.11 + */
    1.12 +struct arrdesc {
    1.13 +	int nelem, szelem;
    1.14 +	int max_elem;
    1.15 +	int bufsz;	/* not including the descriptor */
    1.16 +};
    1.17 +
    1.18 +#define DESC(x)		((struct arrdesc*)((char*)(x) - sizeof(struct arrdesc)))
    1.19 +
    1.20 +void *dynarr_alloc(int elem, int szelem)
    1.21 +{
    1.22 +	struct arrdesc *desc;
    1.23 +
    1.24 +	if(!(desc = malloc(elem * szelem + sizeof *desc))) {
    1.25 +		return 0;
    1.26 +	}
    1.27 +	desc->nelem = desc->max_elem = elem;
    1.28 +	desc->szelem = szelem;
    1.29 +	desc->bufsz = elem * szelem;
    1.30 +	return (char*)desc + sizeof *desc;
    1.31 +}
    1.32 +
    1.33 +void dynarr_free(void *da)
    1.34 +{
    1.35 +	if(da) {
    1.36 +		free(DESC(da));
    1.37 +	}
    1.38 +}
    1.39 +
    1.40 +void *dynarr_resize(void *da, int elem)
    1.41 +{
    1.42 +	int newsz;
    1.43 +	void *tmp;
    1.44 +	struct arrdesc *desc;
    1.45 +
    1.46 +	if(!da) return 0;
    1.47 +	desc = DESC(da);
    1.48 +
    1.49 +	newsz = desc->szelem * elem;
    1.50 +
    1.51 +	if(!(tmp = realloc(desc, newsz + sizeof *desc))) {
    1.52 +		return 0;
    1.53 +	}
    1.54 +	desc = tmp;
    1.55 +
    1.56 +	desc->nelem = desc->max_elem = elem;
    1.57 +	desc->bufsz = newsz;
    1.58 +	return (char*)desc + sizeof *desc;
    1.59 +}
    1.60 +
    1.61 +int dynarr_empty(void *da)
    1.62 +{
    1.63 +	return DESC(da)->nelem ? 0 : 1;
    1.64 +}
    1.65 +
    1.66 +int dynarr_size(void *da)
    1.67 +{
    1.68 +	return DESC(da)->nelem;
    1.69 +}
    1.70 +
    1.71 +
    1.72 +/* stack semantics */
    1.73 +void *dynarr_push(void *da, void *item)
    1.74 +{
    1.75 +	struct arrdesc *desc;
    1.76 +	int nelem;
    1.77 +
    1.78 +	desc = DESC(da);
    1.79 +	nelem = desc->nelem;
    1.80 +
    1.81 +	if(nelem >= desc->max_elem) {
    1.82 +		/* need to resize */
    1.83 +		struct arrdesc *tmp;
    1.84 +		int newsz = desc->max_elem ? desc->max_elem * 2 : 1;
    1.85 +
    1.86 +		if(!(tmp = dynarr_resize(da, newsz))) {
    1.87 +			fprintf(stderr, "failed to resize\n");
    1.88 +			return da;
    1.89 +		}
    1.90 +		da = tmp;
    1.91 +		desc = DESC(da);
    1.92 +		desc->nelem = nelem;
    1.93 +	}
    1.94 +
    1.95 +	memcpy((char*)da + desc->nelem++ * desc->szelem, item, desc->szelem);
    1.96 +	return da;
    1.97 +}
    1.98 +
    1.99 +void *dynarr_pop(void *da)
   1.100 +{
   1.101 +	struct arrdesc *desc;
   1.102 +	int nelem;
   1.103 +
   1.104 +	desc = DESC(da);
   1.105 +	nelem = desc->nelem;
   1.106 +
   1.107 +	if(!nelem) return da;
   1.108 +
   1.109 +	if(nelem <= desc->max_elem / 3) {
   1.110 +		/* reclaim space */
   1.111 +		struct arrdesc *tmp;
   1.112 +		int newsz = desc->max_elem / 2;
   1.113 +
   1.114 +		if(!(tmp = dynarr_resize(da, newsz))) {
   1.115 +			fprintf(stderr, "failed to resize\n");
   1.116 +			return da;
   1.117 +		}
   1.118 +		da = tmp;
   1.119 +		desc = DESC(da);
   1.120 +		desc->nelem = nelem;
   1.121 +	}
   1.122 +	desc->nelem--;
   1.123 +
   1.124 +	return da;
   1.125 +}