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 ************************************************************************/
35 /* local prototypes */
36 XSTATIC XArray * xarray_New (unsigned int);
39 #define XARRAY_ASSERT_NOT_NULL(xarray) \
41 if (xarray == NULL) return XARRAY_ENULLPOINTER; \
44 #define XARRAY_BOUNDS_CHECK(xarray, index) \
46 if (xarray->last_valid_element != -1 && \
47 (int) index > xarray->last_valid_element) \
48 return XARRAY_EINDEXTOOLARGE; \
51 #define XARRAY_GROW_ARRAY(xarray) \
53 xarray->array = (void *) realloc (xarray->array, xarray->size * 2); \
54 if (xarray->array == NULL) return XARRAY_ENOMEM; \
57 XSTATIC XArray * xarray_New (unsigned int initial_size_hint)
59 XArray *new_xarray = NULL;
61 unsigned int initial_size;
63 new_xarray = (XArray *) malloc (sizeof(XArray));
64 if (new_xarray == NULL) return NULL;
66 if (initial_size_hint == 0)
67 initial_size = XARRAY_DEFAULT_SIZE;
69 initial_size = initial_size_hint;
71 inner_array = calloc (initial_size, sizeof(void *));
73 new_xarray->last_valid_element = -1;
74 new_xarray->size = initial_size;
75 new_xarray->last_error = 0;
77 if (inner_array == NULL)
83 new_xarray->array = inner_array;
85 /* Make a dummy reference to other functions, so that we don't get
86 * warnings about unused functions from the compiler. Ahem :) */
89 void *dummy_reference;
91 dummy_reference = xarray_AddObject;
92 dummy_reference = xarray_InsertObject;
93 dummy_reference = xarray_RemoveLastObject;
94 dummy_reference = xarray_RemoveObject;
95 dummy_reference = xarray_RemoveObjects;
96 dummy_reference = xarray_RemoveObjectsAfter;
97 dummy_reference = xarray_ReplaceObject;
99 dummy_reference = xarray_ObjectAtIndex;
100 dummy_reference = xarray_Count;
106 XSTATIC int xarray_ObjectAtIndex (XArray *xarray, unsigned int index,
109 XARRAY_ASSERT_NOT_NULL (xarray);
110 XARRAY_BOUNDS_CHECK (xarray, index);
112 *out_object = xarray->array[index];
114 return XARRAY_SUCCESS;
117 XSTATIC int xarray_AddObject (XArray *xarray, void *object)
119 XARRAY_ASSERT_NOT_NULL (xarray);
121 ++xarray->last_valid_element;
122 if (xarray->last_valid_element >= (int) xarray->size)
124 XARRAY_GROW_ARRAY (xarray);
127 xarray->array[xarray->last_valid_element] = object;
129 return XARRAY_SUCCESS;
132 XSTATIC int xarray_InsertObject (XArray *xarray, void *object,
133 unsigned int at_index)
135 XARRAY_ASSERT_NOT_NULL (xarray);
136 ++xarray->last_valid_element;
137 XARRAY_BOUNDS_CHECK (xarray, at_index);
138 if (xarray->last_valid_element >= (int) xarray->size)
140 XARRAY_GROW_ARRAY (xarray);
143 /* Shift everything from a[i] onward one pointer forward */
145 if ((int) at_index < xarray->last_valid_element)
147 (void) memmove (&xarray->array[at_index + 1],
148 &xarray->array[at_index],
149 (xarray->last_valid_element - at_index) *
153 xarray->array[at_index] = object;
155 return XARRAY_SUCCESS;
158 XSTATIC int xarray_RemoveLastObject (XArray *xarray)
160 XARRAY_ASSERT_NOT_NULL (xarray);
162 if (xarray->last_valid_element == -1)
163 return XARRAY_EEMPTYARRAY;
165 xarray->array[xarray->last_valid_element] = NULL;
166 --xarray->last_valid_element;
168 return XARRAY_SUCCESS;
171 XSTATIC int xarray_RemoveObject (XArray *xarray, unsigned int at_index)
173 XARRAY_ASSERT_NOT_NULL (xarray);
174 XARRAY_BOUNDS_CHECK (xarray, at_index);
176 /* Shift everything from a[i] onward one pointer backward */
178 if ((int) at_index < xarray->last_valid_element)
180 (void) memmove (&xarray->array[at_index],
181 &xarray->array[at_index + 1],
182 (xarray->last_valid_element - at_index) *
186 xarray->array[xarray->last_valid_element] = NULL;
187 --xarray->last_valid_element;
189 return XARRAY_SUCCESS;
192 XSTATIC int xarray_RemoveObjects (XArray *xarray, unsigned int at_index,
197 XARRAY_ASSERT_NOT_NULL (xarray);
198 XARRAY_BOUNDS_CHECK (xarray, at_index);
200 if (count == 0) return XARRAY_SUCCESS;
202 if ((int) at_index + (count - 1) > xarray->last_valid_element)
203 return XARRAY_ECOUNTOUTOFBOUNDS;
205 for (i = 0; i < count; i++)
207 int e = xarray_RemoveObject (xarray, at_index);
208 if (e != XARRAY_SUCCESS) return e;
211 return XARRAY_SUCCESS;
214 XSTATIC int xarray_RemoveObjectsAfter (XArray *xarray, unsigned int index)
216 XARRAY_ASSERT_NOT_NULL (xarray);
217 XARRAY_BOUNDS_CHECK (xarray, index);
221 while ((int) index <= xarray->last_valid_element)
223 int e = xarray_RemoveObject (xarray, index);
224 if (e != XARRAY_SUCCESS) return e;
227 return XARRAY_SUCCESS;
230 XSTATIC int xarray_ReplaceObject (XArray *xarray, unsigned int index,
233 XARRAY_ASSERT_NOT_NULL (xarray);
234 XARRAY_BOUNDS_CHECK (xarray, index);
236 xarray->array[index] = new_object;
238 return XARRAY_SUCCESS;
241 XSTATIC int xarray_Count (XArray *xarray, unsigned int *out_count)
243 XARRAY_ASSERT_NOT_NULL (xarray);
245 *out_count = xarray->last_valid_element + 1;
247 return XARRAY_SUCCESS;
251 #undef XARRAY_ASSERT_NOT_NULL
252 #undef XARRAY_BOUNDS_CHECK
253 #undef XARRAY_GROW_ARRAY