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