summaryrefslogtreecommitdiff
path: root/bores/darray.c
blob: a461aeb5e27144efa7c8b6013b952a362efa0a90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
LICENSE INFORMATION:
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public
License along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
Copyright (c) 2002 Bruno T. C. de Oliveira

INFORMA��ES DE LICEN�A:
Este programa � um software de livre distribui��o; voc� pode
redistribu�-lo e/ou modific�-lo sob os termos da GNU General
Public License, conforme publicado pela Free Software Foundation,
pela vers�o 2 da licen�a ou qualquer vers�o posterior.

Este programa � distribu�do na esperan�a de que ele ser� �til
aos seus usu�rios, por�m, SEM QUAISQUER GARANTIAS; sem sequer
a garantia impl�cita de COMERCIABILIDADE ou DE ADEQUA��O A
QUALQUER FINALIDADE ESPEC�FICA. Consulte a GNU General Public
License para obter mais detalhes (uma c�pia acompanha este
programa, armazenada no arquivo COPYING).
*/


#include "darray.h"
#include "util.h"
#include <stdio.h>
#include <stdlib.h>

#define DEFAULT_CAPACITY 20

#define CAPACITY_AHEAD 20  /* when a value is assigned to an out of bounds 
                              item, this is how far beyond it the capacity
                              grows. */

struct DArray_ {
   int len;
   int cap;
   void **items;
   void (*value_destroyer)(void*);
};

DArray* darray_create() {
   return darray_create_ex(DEFAULT_CAPACITY, NULL);
}

DArray* darray_create_ex(int cap, void (*vd)(void*)) {
   DArray* da = zalloc(sizeof(DArray));

   da->len = 0;
   da->cap = (cap > 0) ? cap : DEFAULT_CAPACITY;
   da->items = zalloc(sizeof(void*) * da->cap);
   da->value_destroyer = vd;

   return da;
}

void darray_destroy(DArray* da) {
   int i;
   if (!da) return;
   if (da->value_destroyer) {
      /* call value destroyer for each item */
      for (i = 0; i < da->len; i++)
         if (da->items[i]) (*da->value_destroyer)(da->items[i]);
   }

   /* free up the items array then the darray itself */
   sfree(da->items);
   sfree(da);
}

int darray_len(DArray* da) {
   /* this one is really complicated */
   return da->len;
}

void* darray_get(DArray* da, int i) {
   return (i >= 0 && i < da->len) ? da->items[i] : NULL;
}

void darray_set(DArray* da, int i, const void *v) {
   if (i < 0) {
      fprintf(stderr, "*** FATAL ERROR ***\n"
                      "Attempt to darray_set with negative index %d\n", i);
      abort();
   }
   else if (i >= da->len) {
      da->cap = i + CAPACITY_AHEAD;
      da->items = srealloc(da->items, sizeof(void*) * da->cap);

      /* grow array while assignung NULLs to all newly included items */
      while (i >= da->len)  da->items[da->len++] = NULL;
   }
   else {
      /* call value destroyer if there was anything there */
      if (da->items[i] && da->value_destroyer)
         (*da->value_destroyer)(da->items[i]);
   }

   da->items[i] = (void*) v;
}

void* darray_snatch(DArray *da, int i) {
   void *v;
   if (i >= 0 && i < da->len) {
      v = da->items[i];
      da->items[i] = NULL;
      return v;
   }
   else return NULL;
}

void darray_append(DArray *da, const void *v) {
   darray_set(da, da->len, v);
}

void darray_remove(DArray *da, int i) {
   if (i < 0 || i >= da->len) return;
   if (da->items[i] && da->value_destroyer)
      (*da->value_destroyer)(da->items[i]);

   while (i < da->len - 1) {
      da->items[i] = da->items[i+1];
      i++;
   }

   da->len--;
}