libresman
diff src/dynarr.c @ 5:bd9b4ff19c93
more stuff
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 01 Feb 2014 08:02:08 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/dynarr.c Sat Feb 01 08:02:08 2014 +0200 1.3 @@ -0,0 +1,124 @@ 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 + if(item) { 1.96 + memcpy((char*)da + desc->nelem++ * desc->szelem, item, desc->szelem); 1.97 + } 1.98 + return da; 1.99 +} 1.100 + 1.101 +void *dynarr_pop(void *da) 1.102 +{ 1.103 + struct arrdesc *desc; 1.104 + int nelem; 1.105 + 1.106 + desc = DESC(da); 1.107 + nelem = desc->nelem; 1.108 + 1.109 + if(!nelem) return da; 1.110 + 1.111 + if(nelem <= desc->max_elem / 3) { 1.112 + /* reclaim space */ 1.113 + struct arrdesc *tmp; 1.114 + int newsz = desc->max_elem / 2; 1.115 + 1.116 + if(!(tmp = dynarr_resize(da, newsz))) { 1.117 + fprintf(stderr, "failed to resize\n"); 1.118 + return da; 1.119 + } 1.120 + da = tmp; 1.121 + desc = DESC(da); 1.122 + desc->nelem = nelem; 1.123 + } 1.124 + desc->nelem--; 1.125 + 1.126 + return da; 1.127 +}