summaryrefslogtreecommitdiff
path: root/bores/darray.h
blob: 252391489ece6a28061cc0d4aa3ece71422e2009 (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
/*
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).
*/

#ifndef btco_bores_darray_h
#define btco_bores_darray_h

/* opaque dynamic array type */
struct DArray_;
typedef struct DArray_ DArray;

/* creates a new dynamic array of default initial capacity and no
 * value destroyer function. */
DArray* darray_create();

/* creates a new dynamic array of initial capacity cap (must be positive)
 * and value destroyer function f. The value destroyer is a function
 * called whenever a value stored in the array must be destroyed or
 * will be overwritten. */
DArray* darray_create_ex(int cap, void (*vd)(void*));

/* destroys the given dynamic array. The value destroyer function will
 * be called for all non-NULL values in the array at this point. */
void darray_destroy(DArray*);

/* obtains the length of a dynamic array. This is NOT the capacity. The
 * length is how many elements are actually there. You should not be
 * interested in the capacity, since it is internally managed. */
int darray_len(DArray*);

/* obtains the item from the array whose index is i. If i is out of
 * bounds, NULL is returned (and no error occurs). PLEASE notice
 * that this function is for consultation only, the value will still
 * be owned by the darray (and thus can be destroyed at any moment
 * by the value destroyer function). If you want to gain ownership
 * over the value you get, use darray_snatch instead. */
void* darray_get(DArray*, int i);

/* sets the value of item index i to NULL, without calling the value
 * destroyer function for it. Returns the value the item had before
 * being set to NULL. If the index i is out of bounds, nothing is done
 * and NULL is returned. */
void* darray_snatch(DArray*, int i);

/* sets the value of item index i to v. If the item had a non-NULL value
 * before, the value destroyer function will be called for it before
 * it is overwritten. If the index is out of bounds and positive, the
 * array will be dynamically expanded to make it valid. If it is
 * negative a fatal error occurs. 
 *
 * If i is out of bounds and v is NULL, this function does absolutely 
 * nothing, because expanding the array to put a NULL value is
 * really the same as just leaving it as it is, because darray_get
 * returns NULL if you pass it an out-of-bounds index.
 *
 * Whenever the array expands, the newly created items are set to NULL,
 * that is, they are not left with trash in them.
 */
void darray_set(DArray*, int i, const void *v);

/* Same as darray_set(da, darray_len(da), v) */
void darray_append(DArray*, const void *v);

/* removes item i from array, shifting all others up */
void darray_remove(DArray*, int i);

#endif