1 /*************************************************************************
2 * xarray.c: Mutable (dynamically growable) array
3 *************************************************************************
4 * Copyright (C) 2004 Commonwealth Scientific and Industrial Research
5 * Organisation (CSIRO) Australia
6 * Copyright (C) 2004 the VideoLAN team
10 * Authors: Andre Pang <Andre.Pang@csiro.au>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 ************************************************************************/
30 #define XARRAY_ASSERT_NOT_NULL(xarray) \
32 if (xarray == NULL) return XARRAY_ENULLPOINTER; \
35 #define XARRAY_BOUNDS_CHECK(xarray, index) \
38 return XARRAY_ENEGATIVEINDEX; \
39 else if (xarray->last_valid_element != -1 && \
40 (int) index > xarray->last_valid_element) \
41 return XARRAY_EINDEXTOOLARGE; \
44 #define XARRAY_GROW_ARRAY(xarray) \
46 xarray->array = (void *) realloc (xarray->array, xarray->size * 2); \
47 if (xarray->array == NULL) return XARRAY_ENOMEM; \
50 XSTATIC XArray * xarray_New (unsigned int initial_size_hint)
52 XArray *new_xarray = NULL;
54 unsigned int initial_size;
56 new_xarray = (XArray *) malloc (sizeof(XArray));
57 if (new_xarray == NULL) return NULL;
59 if (initial_size_hint <= 0)
60 initial_size = XARRAY_DEFAULT_SIZE;
62 initial_size = initial_size_hint;
64 inner_array = calloc (initial_size, sizeof(void *));
66 new_xarray->last_valid_element = -1;
67 new_xarray->size = initial_size;
68 new_xarray->last_error = 0;
70 if (inner_array == NULL)
76 new_xarray->array = inner_array;
78 /* Make a dummy reference to other functions, so that we don't get
79 * warnings about unused functions from the compiler. Ahem :) */
82 void *dummy_reference;
84 dummy_reference = xarray_AddObject;
85 dummy_reference = xarray_InsertObject;
86 dummy_reference = xarray_RemoveLastObject;
87 dummy_reference = xarray_RemoveObject;
88 dummy_reference = xarray_RemoveObjects;
89 dummy_reference = xarray_RemoveObjectsAfter;
90 dummy_reference = xarray_ReplaceObject;
92 dummy_reference = xarray_ObjectAtIndex;
93 dummy_reference = xarray_Count;
99 XSTATIC int xarray_ObjectAtIndex (XArray *xarray, unsigned int index,
102 XARRAY_ASSERT_NOT_NULL (xarray);
103 XARRAY_BOUNDS_CHECK (xarray, index);
105 *out_object = xarray->array[index];
107 return XARRAY_SUCCESS;
110 XSTATIC int xarray_AddObject (XArray *xarray, void *object)
112 XARRAY_ASSERT_NOT_NULL (xarray);
114 ++xarray->last_valid_element;
115 if (xarray->last_valid_element >= (int) xarray->size)
117 XARRAY_GROW_ARRAY (xarray);
120 xarray->array[xarray->last_valid_element] = object;
122 return XARRAY_SUCCESS;
125 XSTATIC int xarray_InsertObject (XArray *xarray, void *object,
126 unsigned int at_index)
128 XARRAY_ASSERT_NOT_NULL (xarray);
129 ++xarray->last_valid_element;
130 XARRAY_BOUNDS_CHECK (xarray, at_index);
131 if (xarray->last_valid_element >= (int) xarray->size)
133 XARRAY_GROW_ARRAY (xarray);
136 /* Shift everything from a[i] onward one pointer forward */
138 if ((int) at_index < xarray->last_valid_element)
140 (void) memmove (&xarray->array[at_index + 1],
141 &xarray->array[at_index],
142 (xarray->last_valid_element - at_index) *
146 xarray->array[at_index] = object;
148 return XARRAY_SUCCESS;
151 XSTATIC int xarray_RemoveLastObject (XArray *xarray)
153 XARRAY_ASSERT_NOT_NULL (xarray);
155 if (xarray->last_valid_element == -1)
156 return XARRAY_EEMPTYARRAY;
158 xarray->array[xarray->last_valid_element] = NULL;
159 --xarray->last_valid_element;
161 return XARRAY_SUCCESS;
164 XSTATIC int xarray_RemoveObject (XArray *xarray, unsigned int at_index)
166 XARRAY_ASSERT_NOT_NULL (xarray);
167 XARRAY_BOUNDS_CHECK (xarray, at_index);
169 /* Shift everything from a[i] onward one pointer backward */
171 if ((int) at_index < xarray->last_valid_element)
173 (void) memmove (&xarray->array[at_index],
174 &xarray->array[at_index + 1],
175 (xarray->last_valid_element - at_index) *
179 xarray->array[xarray->last_valid_element] = NULL;
180 --xarray->last_valid_element;
182 return XARRAY_SUCCESS;
185 XSTATIC int xarray_RemoveObjects (XArray *xarray, unsigned int at_index,
190 XARRAY_ASSERT_NOT_NULL (xarray);
191 XARRAY_BOUNDS_CHECK (xarray, at_index);
193 if (count == 0) return XARRAY_SUCCESS;
195 if ((int) at_index + (count - 1) > xarray->last_valid_element)
196 return XARRAY_ECOUNTOUTOFBOUNDS;
198 for (i = 0; i < count; i++)
200 int e = xarray_RemoveObject (xarray, at_index);
201 if (e != XARRAY_SUCCESS) return e;
204 return XARRAY_SUCCESS;
207 XSTATIC int xarray_RemoveObjectsAfter (XArray *xarray, unsigned int index)
209 XARRAY_ASSERT_NOT_NULL (xarray);
210 XARRAY_BOUNDS_CHECK (xarray, index);
214 while ((int) index <= xarray->last_valid_element)
216 int e = xarray_RemoveObject (xarray, index);
217 if (e != XARRAY_SUCCESS) return e;
220 return XARRAY_SUCCESS;
223 XSTATIC int xarray_ReplaceObject (XArray *xarray, unsigned int index,
226 XARRAY_ASSERT_NOT_NULL (xarray);
227 XARRAY_BOUNDS_CHECK (xarray, index);
229 xarray->array[index] = new_object;
231 return XARRAY_SUCCESS;
234 XSTATIC int xarray_Count (XArray *xarray, unsigned int *out_count)
236 XARRAY_ASSERT_NOT_NULL (xarray);
238 *out_count = xarray->last_valid_element + 1;
240 return XARRAY_SUCCESS;
244 #undef XARRAY_ASSERT_NOT_NULL
245 #undef XARRAY_BOUNDS_CHECK
246 #undef XARRAY_GROW_ARRAY