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 #define XARRAY_ASSERT_NOT_NULL(xarray) \
37 if (xarray == NULL) return XARRAY_ENULLPOINTER; \
40 #define XARRAY_BOUNDS_CHECK(xarray, index) \
43 return XARRAY_ENEGATIVEINDEX; \
44 else if (xarray->last_valid_element != -1 && \
45 (int) index > xarray->last_valid_element) \
46 return XARRAY_EINDEXTOOLARGE; \
49 #define XARRAY_GROW_ARRAY(xarray) \
51 xarray->array = (void *) realloc (xarray->array, xarray->size * 2); \
52 if (xarray->array == NULL) return XARRAY_ENOMEM; \
55 XSTATIC XArray * xarray_New (unsigned int initial_size_hint)
57 XArray *new_xarray = NULL;
59 unsigned int initial_size;
61 new_xarray = (XArray *) malloc (sizeof(XArray));
62 if (new_xarray == NULL) return NULL;
64 if (initial_size_hint <= 0)
65 initial_size = XARRAY_DEFAULT_SIZE;
67 initial_size = initial_size_hint;
69 inner_array = calloc (initial_size, sizeof(void *));
71 new_xarray->last_valid_element = -1;
72 new_xarray->size = initial_size;
73 new_xarray->last_error = 0;
75 if (inner_array == NULL)
81 new_xarray->array = inner_array;
83 /* Make a dummy reference to other functions, so that we don't get
84 * warnings about unused functions from the compiler. Ahem :) */
87 void *dummy_reference;
89 dummy_reference = xarray_AddObject;
90 dummy_reference = xarray_InsertObject;
91 dummy_reference = xarray_RemoveLastObject;
92 dummy_reference = xarray_RemoveObject;
93 dummy_reference = xarray_RemoveObjects;
94 dummy_reference = xarray_RemoveObjectsAfter;
95 dummy_reference = xarray_ReplaceObject;
97 dummy_reference = xarray_ObjectAtIndex;
98 dummy_reference = xarray_Count;
104 XSTATIC int xarray_ObjectAtIndex (XArray *xarray, unsigned int index,
107 XARRAY_ASSERT_NOT_NULL (xarray);
108 XARRAY_BOUNDS_CHECK (xarray, index);
110 *out_object = xarray->array[index];
112 return XARRAY_SUCCESS;
115 XSTATIC int xarray_AddObject (XArray *xarray, void *object)
117 XARRAY_ASSERT_NOT_NULL (xarray);
119 ++xarray->last_valid_element;
120 if (xarray->last_valid_element >= (int) xarray->size)
122 XARRAY_GROW_ARRAY (xarray);
125 xarray->array[xarray->last_valid_element] = object;
127 return XARRAY_SUCCESS;
130 XSTATIC int xarray_InsertObject (XArray *xarray, void *object,
131 unsigned int at_index)
133 XARRAY_ASSERT_NOT_NULL (xarray);
134 ++xarray->last_valid_element;
135 XARRAY_BOUNDS_CHECK (xarray, at_index);
136 if (xarray->last_valid_element >= (int) xarray->size)
138 XARRAY_GROW_ARRAY (xarray);
141 /* Shift everything from a[i] onward one pointer forward */
143 if ((int) at_index < xarray->last_valid_element)
145 (void) memmove (&xarray->array[at_index + 1],
146 &xarray->array[at_index],
147 (xarray->last_valid_element - at_index) *
151 xarray->array[at_index] = object;
153 return XARRAY_SUCCESS;
156 XSTATIC int xarray_RemoveLastObject (XArray *xarray)
158 XARRAY_ASSERT_NOT_NULL (xarray);
160 if (xarray->last_valid_element == -1)
161 return XARRAY_EEMPTYARRAY;
163 xarray->array[xarray->last_valid_element] = NULL;
164 --xarray->last_valid_element;
166 return XARRAY_SUCCESS;
169 XSTATIC int xarray_RemoveObject (XArray *xarray, unsigned int at_index)
171 XARRAY_ASSERT_NOT_NULL (xarray);
172 XARRAY_BOUNDS_CHECK (xarray, at_index);
174 /* Shift everything from a[i] onward one pointer backward */
176 if ((int) at_index < xarray->last_valid_element)
178 (void) memmove (&xarray->array[at_index],
179 &xarray->array[at_index + 1],
180 (xarray->last_valid_element - at_index) *
184 xarray->array[xarray->last_valid_element] = NULL;
185 --xarray->last_valid_element;
187 return XARRAY_SUCCESS;
190 XSTATIC int xarray_RemoveObjects (XArray *xarray, unsigned int at_index,
195 XARRAY_ASSERT_NOT_NULL (xarray);
196 XARRAY_BOUNDS_CHECK (xarray, at_index);
198 if (count == 0) return XARRAY_SUCCESS;
200 if ((int) at_index + (count - 1) > xarray->last_valid_element)
201 return XARRAY_ECOUNTOUTOFBOUNDS;
203 for (i = 0; i < count; i++)
205 int e = xarray_RemoveObject (xarray, at_index);
206 if (e != XARRAY_SUCCESS) return e;
209 return XARRAY_SUCCESS;
212 XSTATIC int xarray_RemoveObjectsAfter (XArray *xarray, unsigned int index)
214 XARRAY_ASSERT_NOT_NULL (xarray);
215 XARRAY_BOUNDS_CHECK (xarray, index);
219 while ((int) index <= xarray->last_valid_element)
221 int e = xarray_RemoveObject (xarray, index);
222 if (e != XARRAY_SUCCESS) return e;
225 return XARRAY_SUCCESS;
228 XSTATIC int xarray_ReplaceObject (XArray *xarray, unsigned int index,
231 XARRAY_ASSERT_NOT_NULL (xarray);
232 XARRAY_BOUNDS_CHECK (xarray, index);
234 xarray->array[index] = new_object;
236 return XARRAY_SUCCESS;
239 XSTATIC int xarray_Count (XArray *xarray, unsigned int *out_count)
241 XARRAY_ASSERT_NOT_NULL (xarray);
243 *out_count = xarray->last_valid_element + 1;
245 return XARRAY_SUCCESS;
249 #undef XARRAY_ASSERT_NOT_NULL
250 #undef XARRAY_BOUNDS_CHECK
251 #undef XARRAY_GROW_ARRAY