]> git.sesse.net Git - vlc/blob - modules/codec/cmml/xarray.c
Removes trailing spaces. Removes tabs.
[vlc] / modules / codec / cmml / xarray.c
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
7  *
8  * $Id$
9  *
10  * Authors: Andre Pang <Andre.Pang@csiro.au>
11  *
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.
16  *
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.
21  *
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  ************************************************************************/
26
27 #include <stddef.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include "xarray.h"
31
32 #define XARRAY_ASSERT_NOT_NULL(xarray) \
33     { \
34         if (xarray == NULL) return XARRAY_ENULLPOINTER; \
35     }
36
37 #define XARRAY_BOUNDS_CHECK(xarray, index) \
38     { \
39         if (index < 0) \
40             return XARRAY_ENEGATIVEINDEX; \
41         else if (xarray->last_valid_element != -1 && \
42                  (int) index > xarray->last_valid_element) \
43             return XARRAY_EINDEXTOOLARGE; \
44     }
45
46 #define XARRAY_GROW_ARRAY(xarray) \
47     { \
48         xarray->array = (void *) realloc (xarray->array, xarray->size * 2); \
49         if (xarray->array == NULL) return XARRAY_ENOMEM; \
50     }
51
52 XSTATIC XArray * xarray_New (unsigned int initial_size_hint)
53 {
54     XArray *new_xarray = NULL;
55     void *inner_array;
56     unsigned int initial_size;
57
58     new_xarray = (XArray *) malloc (sizeof(XArray));
59     if (new_xarray == NULL) return NULL;
60
61     if (initial_size_hint <= 0)
62         initial_size = XARRAY_DEFAULT_SIZE;
63     else
64         initial_size = initial_size_hint;
65
66     inner_array = calloc (initial_size, sizeof(void *));
67
68     new_xarray->last_valid_element = -1;
69     new_xarray->size = initial_size;
70     new_xarray->last_error = 0;
71
72     if (inner_array == NULL)
73     {
74         free (new_xarray);
75         return NULL;
76     }
77
78     new_xarray->array = inner_array;
79
80     /* Make a dummy reference to other functions, so that we don't get
81      * warnings about unused functions from the compiler.  Ahem :) */
82     while (0)
83     {
84         void *dummy_reference;
85
86         dummy_reference = xarray_AddObject;
87         dummy_reference = xarray_InsertObject;
88         dummy_reference = xarray_RemoveLastObject;
89         dummy_reference = xarray_RemoveObject;
90         dummy_reference = xarray_RemoveObjects;
91         dummy_reference = xarray_RemoveObjectsAfter;
92         dummy_reference = xarray_ReplaceObject;
93
94         dummy_reference = xarray_ObjectAtIndex;
95         dummy_reference = xarray_Count;
96     }
97  
98     return new_xarray;
99 }
100
101 XSTATIC int xarray_ObjectAtIndex (XArray *xarray, unsigned int index,
102         void **out_object)
103 {
104     XARRAY_ASSERT_NOT_NULL (xarray);
105     XARRAY_BOUNDS_CHECK (xarray, index);
106
107     *out_object = xarray->array[index];
108
109     return XARRAY_SUCCESS;
110 }
111
112 XSTATIC int xarray_AddObject (XArray *xarray, void *object)
113 {
114     XARRAY_ASSERT_NOT_NULL (xarray);
115
116     ++xarray->last_valid_element;
117     if (xarray->last_valid_element >= (int) xarray->size)
118     {
119         XARRAY_GROW_ARRAY (xarray);
120     }
121
122     xarray->array[xarray->last_valid_element] = object;
123
124     return XARRAY_SUCCESS;
125 }
126
127 XSTATIC int xarray_InsertObject (XArray *xarray, void *object,
128         unsigned int at_index)
129 {
130     XARRAY_ASSERT_NOT_NULL (xarray);
131     ++xarray->last_valid_element;
132     XARRAY_BOUNDS_CHECK (xarray, at_index);
133     if (xarray->last_valid_element >= (int) xarray->size)
134     {
135         XARRAY_GROW_ARRAY (xarray);
136     }
137
138     /* Shift everything from a[i] onward one pointer forward */
139
140     if ((int) at_index < xarray->last_valid_element)
141     {
142         (void) memmove (&xarray->array[at_index + 1],
143                         &xarray->array[at_index],
144                         (xarray->last_valid_element - at_index) *
145                             sizeof(void *));
146     }
147
148     xarray->array[at_index] = object;
149
150     return XARRAY_SUCCESS;
151 }
152
153 XSTATIC int xarray_RemoveLastObject (XArray *xarray)
154 {
155     XARRAY_ASSERT_NOT_NULL (xarray);
156
157     if (xarray->last_valid_element == -1)
158         return XARRAY_EEMPTYARRAY;
159
160     xarray->array[xarray->last_valid_element] = NULL;
161     --xarray->last_valid_element;
162
163     return XARRAY_SUCCESS;
164 }
165
166 XSTATIC int xarray_RemoveObject (XArray *xarray, unsigned int at_index)
167 {
168     XARRAY_ASSERT_NOT_NULL (xarray);
169     XARRAY_BOUNDS_CHECK (xarray, at_index);
170
171     /* Shift everything from a[i] onward one pointer backward */
172
173     if ((int) at_index < xarray->last_valid_element)
174     {
175         (void) memmove (&xarray->array[at_index],
176                         &xarray->array[at_index + 1],
177                         (xarray->last_valid_element - at_index) *
178                             sizeof(void *));
179     }
180
181     xarray->array[xarray->last_valid_element] = NULL;
182     --xarray->last_valid_element;
183
184     return XARRAY_SUCCESS;
185 }
186
187 XSTATIC int xarray_RemoveObjects (XArray *xarray, unsigned int at_index,
188         int count)
189 {
190     int i;
191
192     XARRAY_ASSERT_NOT_NULL (xarray);
193     XARRAY_BOUNDS_CHECK (xarray, at_index);
194
195     if (count == 0) return XARRAY_SUCCESS;
196
197     if ((int) at_index + (count - 1) > xarray->last_valid_element)
198         return XARRAY_ECOUNTOUTOFBOUNDS;
199
200     for (i = 0; i < count; i++)
201     {
202         int e = xarray_RemoveObject (xarray, at_index);
203         if (e != XARRAY_SUCCESS) return e;
204     }
205
206     return XARRAY_SUCCESS;
207 }
208
209 XSTATIC int xarray_RemoveObjectsAfter (XArray *xarray, unsigned int index)
210 {
211     XARRAY_ASSERT_NOT_NULL (xarray);
212     XARRAY_BOUNDS_CHECK (xarray, index);
213
214     index++;
215
216     while ((int) index <= xarray->last_valid_element)
217     {
218         int e = xarray_RemoveObject (xarray, index);
219         if (e != XARRAY_SUCCESS) return e;
220     }
221
222     return XARRAY_SUCCESS;
223 }
224
225 XSTATIC int xarray_ReplaceObject (XArray *xarray, unsigned int index,
226         void *new_object)
227 {
228     XARRAY_ASSERT_NOT_NULL (xarray);
229     XARRAY_BOUNDS_CHECK (xarray, index);
230
231     xarray->array[index] = new_object;
232
233     return XARRAY_SUCCESS;
234 }
235
236 XSTATIC int xarray_Count (XArray *xarray, unsigned int *out_count)
237 {
238     XARRAY_ASSERT_NOT_NULL (xarray);
239
240     *out_count = xarray->last_valid_element + 1;
241
242     return XARRAY_SUCCESS;
243 }
244
245
246 #undef XARRAY_ASSERT_NOT_NULL
247 #undef XARRAY_BOUNDS_CHECK
248 #undef XARRAY_GROW_ARRAY
249