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 ************************************************************************/
27 #include <string.h> /* memmove(1) */
31 #define XARRAY_ASSERT_NOT_NULL(xarray) \
33 if (xarray == NULL) return XARRAY_ENULLPOINTER; \
36 #define XARRAY_BOUNDS_CHECK(xarray, index) \
39 return XARRAY_ENEGATIVEINDEX; \
40 else if (xarray->last_valid_element != -1 && \
41 (int) index > xarray->last_valid_element) \
42 return XARRAY_EINDEXTOOLARGE; \
45 #define XARRAY_GROW_ARRAY(xarray) \
47 xarray->array = (void *) realloc (xarray->array, xarray->size * 2); \
48 if (xarray->array == NULL) return XARRAY_ENOMEM; \
51 XSTATIC XArray * xarray_New (unsigned int initial_size_hint)
53 XArray *new_xarray = NULL;
55 unsigned int initial_size;
57 new_xarray = (XArray *) malloc (sizeof(XArray));
58 if (new_xarray == NULL) return NULL;
60 if (initial_size_hint <= 0)
61 initial_size = XARRAY_DEFAULT_SIZE;
63 initial_size = initial_size_hint;
65 inner_array = calloc (initial_size, sizeof(void *));
67 new_xarray->last_valid_element = -1;
68 new_xarray->size = initial_size;
69 new_xarray->last_error = 0;
71 if (inner_array == NULL)
77 new_xarray->array = inner_array;
79 /* Make a dummy reference to other functions, so that we don't get
80 * warnings about unused functions from the compiler. Ahem :) */
83 void *dummy_reference;
85 dummy_reference = xarray_AddObject;
86 dummy_reference = xarray_InsertObject;
87 dummy_reference = xarray_RemoveLastObject;
88 dummy_reference = xarray_RemoveObject;
89 dummy_reference = xarray_RemoveObjects;
90 dummy_reference = xarray_RemoveObjectsAfter;
91 dummy_reference = xarray_ReplaceObject;
93 dummy_reference = xarray_ObjectAtIndex;
94 dummy_reference = xarray_Count;
100 XSTATIC int xarray_ObjectAtIndex (XArray *xarray, unsigned int index,
103 XARRAY_ASSERT_NOT_NULL (xarray);
104 XARRAY_BOUNDS_CHECK (xarray, index);
106 *out_object = xarray->array[index];
108 return XARRAY_SUCCESS;
111 XSTATIC int xarray_AddObject (XArray *xarray, void *object)
113 XARRAY_ASSERT_NOT_NULL (xarray);
115 ++xarray->last_valid_element;
116 if (xarray->last_valid_element >= (int) xarray->size)
118 XARRAY_GROW_ARRAY (xarray);
121 xarray->array[xarray->last_valid_element] = object;
123 return XARRAY_SUCCESS;
126 XSTATIC int xarray_InsertObject (XArray *xarray, void *object,
127 unsigned int at_index)
129 XARRAY_ASSERT_NOT_NULL (xarray);
130 ++xarray->last_valid_element;
131 XARRAY_BOUNDS_CHECK (xarray, at_index);
132 if (xarray->last_valid_element >= (int) xarray->size)
134 XARRAY_GROW_ARRAY (xarray);
137 /* Shift everything from a[i] onward one pointer forward */
139 if ((int) at_index < xarray->last_valid_element)
141 (void) memmove (&xarray->array[at_index + 1],
142 &xarray->array[at_index],
143 (xarray->last_valid_element - at_index) *
147 xarray->array[at_index] = object;
149 return XARRAY_SUCCESS;
152 XSTATIC int xarray_RemoveLastObject (XArray *xarray)
154 XARRAY_ASSERT_NOT_NULL (xarray);
156 if (xarray->last_valid_element == -1)
157 return XARRAY_EEMPTYARRAY;
159 xarray->array[xarray->last_valid_element] = NULL;
160 --xarray->last_valid_element;
162 return XARRAY_SUCCESS;
165 XSTATIC int xarray_RemoveObject (XArray *xarray, unsigned int at_index)
167 XARRAY_ASSERT_NOT_NULL (xarray);
168 XARRAY_BOUNDS_CHECK (xarray, at_index);
170 /* Shift everything from a[i] onward one pointer backward */
172 if ((int) at_index < xarray->last_valid_element)
174 (void) memmove (&xarray->array[at_index],
175 &xarray->array[at_index + 1],
176 (xarray->last_valid_element - at_index) *
180 xarray->array[xarray->last_valid_element] = NULL;
181 --xarray->last_valid_element;
183 return XARRAY_SUCCESS;
186 XSTATIC int xarray_RemoveObjects (XArray *xarray, unsigned int at_index,
191 XARRAY_ASSERT_NOT_NULL (xarray);
192 XARRAY_BOUNDS_CHECK (xarray, at_index);
194 if (count == 0) return XARRAY_SUCCESS;
196 if ((int) at_index + (count - 1) > xarray->last_valid_element)
197 return XARRAY_ECOUNTOUTOFBOUNDS;
199 for (i = 0; i < count; i++)
201 int e = xarray_RemoveObject (xarray, at_index);
202 if (e != XARRAY_SUCCESS) return e;
205 return XARRAY_SUCCESS;
208 XSTATIC int xarray_RemoveObjectsAfter (XArray *xarray, unsigned int index)
210 XARRAY_ASSERT_NOT_NULL (xarray);
211 XARRAY_BOUNDS_CHECK (xarray, index);
215 while ((int) index <= xarray->last_valid_element)
217 int e = xarray_RemoveObject (xarray, index);
218 if (e != XARRAY_SUCCESS) return e;
221 return XARRAY_SUCCESS;
224 XSTATIC int xarray_ReplaceObject (XArray *xarray, unsigned int index,
227 XARRAY_ASSERT_NOT_NULL (xarray);
228 XARRAY_BOUNDS_CHECK (xarray, index);
230 xarray->array[index] = new_object;
232 return XARRAY_SUCCESS;
235 XSTATIC int xarray_Count (XArray *xarray, unsigned int *out_count)
237 XARRAY_ASSERT_NOT_NULL (xarray);
239 *out_count = xarray->last_valid_element + 1;
241 return XARRAY_SUCCESS;
245 #undef XARRAY_ASSERT_NOT_NULL
246 #undef XARRAY_BOUNDS_CHECK
247 #undef XARRAY_GROW_ARRAY