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 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 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;
88 int xarray_ObjectAtIndex (XArray *xarray, unsigned int index,
91 XARRAY_ASSERT_NOT_NULL (xarray);
92 XARRAY_BOUNDS_CHECK (xarray, index);
94 *out_object = xarray->array[index];
96 return XARRAY_SUCCESS;
99 int xarray_AddObject (XArray *xarray, void *object)
101 XARRAY_ASSERT_NOT_NULL (xarray);
103 ++xarray->last_valid_element;
104 if (xarray->last_valid_element >= (int) xarray->size)
106 XARRAY_GROW_ARRAY (xarray);
109 xarray->array[xarray->last_valid_element] = object;
111 return XARRAY_SUCCESS;
114 int xarray_InsertObject (XArray *xarray, void *object,
115 unsigned int at_index)
117 XARRAY_ASSERT_NOT_NULL (xarray);
118 ++xarray->last_valid_element;
119 XARRAY_BOUNDS_CHECK (xarray, at_index);
120 if (xarray->last_valid_element >= (int) xarray->size)
122 XARRAY_GROW_ARRAY (xarray);
125 /* Shift everything from a[i] onward one pointer forward */
127 if ((int) at_index < xarray->last_valid_element)
129 (void) memmove (&xarray->array[at_index + 1],
130 &xarray->array[at_index],
131 (xarray->last_valid_element - at_index) *
135 xarray->array[at_index] = object;
137 return XARRAY_SUCCESS;
140 int xarray_RemoveLastObject (XArray *xarray)
142 XARRAY_ASSERT_NOT_NULL (xarray);
144 if (xarray->last_valid_element == -1)
145 return XARRAY_EEMPTYARRAY;
147 xarray->array[xarray->last_valid_element] = NULL;
148 --xarray->last_valid_element;
150 return XARRAY_SUCCESS;
153 int xarray_RemoveObject (XArray *xarray, unsigned int at_index)
155 XARRAY_ASSERT_NOT_NULL (xarray);
156 XARRAY_BOUNDS_CHECK (xarray, at_index);
158 /* Shift everything from a[i] onward one pointer backward */
160 if ((int) at_index < xarray->last_valid_element)
162 (void) memmove (&xarray->array[at_index],
163 &xarray->array[at_index + 1],
164 (xarray->last_valid_element - at_index) *
168 xarray->array[xarray->last_valid_element] = NULL;
169 --xarray->last_valid_element;
171 return XARRAY_SUCCESS;
174 int xarray_RemoveObjects (XArray *xarray, unsigned int at_index,
179 XARRAY_ASSERT_NOT_NULL (xarray);
180 XARRAY_BOUNDS_CHECK (xarray, at_index);
182 if (count == 0) return XARRAY_SUCCESS;
184 if ((int) at_index + (count - 1) > xarray->last_valid_element)
185 return XARRAY_ECOUNTOUTOFBOUNDS;
187 for (i = 0; i < count; i++)
189 int e = xarray_RemoveObject (xarray, at_index);
190 if (e != XARRAY_SUCCESS) return e;
193 return XARRAY_SUCCESS;
196 int xarray_RemoveObjectsAfter (XArray *xarray, unsigned int index)
198 XARRAY_ASSERT_NOT_NULL (xarray);
199 XARRAY_BOUNDS_CHECK (xarray, index);
203 while ((int) index <= xarray->last_valid_element)
205 int e = xarray_RemoveObject (xarray, index);
206 if (e != XARRAY_SUCCESS) return e;
209 return XARRAY_SUCCESS;
212 int xarray_ReplaceObject (XArray *xarray, unsigned int index,
215 XARRAY_ASSERT_NOT_NULL (xarray);
216 XARRAY_BOUNDS_CHECK (xarray, index);
218 xarray->array[index] = new_object;
220 return XARRAY_SUCCESS;
223 int xarray_Count (XArray *xarray, unsigned int *out_count)
225 XARRAY_ASSERT_NOT_NULL (xarray);
227 *out_count = xarray->last_valid_element + 1;
229 return XARRAY_SUCCESS;
233 #undef XARRAY_ASSERT_NOT_NULL
234 #undef XARRAY_BOUNDS_CHECK
235 #undef XARRAY_GROW_ARRAY