1 /*****************************************************************************
2 * vlc_arrays.h : Arrays and data structures handling
3 *****************************************************************************
4 * Copyright (C) 1999-2004 the VideoLAN team
5 * $Id: vlc_playlist.h 17108 2006-10-15 15:28:34Z zorglub $
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Clément Stenac <zorglub@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************/
24 #ifndef _VLC_ARRAYS_H_
25 #define _VLC_ARRAYS_H_
28 * Simple dynamic array handling. Array is realloced at each insert/removal
30 #if defined( _MSC_VER ) && _MSC_VER < 1300 && !defined( UNDER_CE )
31 # define VLCCVP (void**) /* Work-around for broken compiler */
35 #define INSERT_ELEM( p_ar, i_oldsize, i_pos, elem ) \
38 if( !i_oldsize ) (p_ar) = NULL; \
39 (p_ar) = VLCCVP realloc( p_ar, ((i_oldsize) + 1) * sizeof(*(p_ar)) ); \
40 if( (i_oldsize) - (i_pos) ) \
42 memmove( (p_ar) + (i_pos) + 1, (p_ar) + (i_pos), \
43 ((i_oldsize) - (i_pos)) * sizeof( *(p_ar) ) ); \
45 (p_ar)[i_pos] = elem; \
50 #define REMOVE_ELEM( p_ar, i_oldsize, i_pos ) \
53 if( (i_oldsize) - (i_pos) - 1 ) \
55 memmove( (p_ar) + (i_pos), \
56 (p_ar) + (i_pos) + 1, \
57 ((i_oldsize) - (i_pos) - 1) * sizeof( *(p_ar) ) ); \
61 (p_ar) = realloc( p_ar, ((i_oldsize) - 1) * sizeof( *(p_ar) ) ); \
73 #define TAB_APPEND( count, tab, p ) \
76 (tab) = realloc( tab, sizeof( void ** ) * ( (count) + 1 ) ); \
80 (tab) = malloc( sizeof( void ** ) ); \
85 #define TAB_FIND( count, tab, p, index ) \
89 for( _i_ = 0; _i_ < (count); _i_++ ) \
91 if( (tab)[_i_] == (p) ) \
99 #define TAB_REMOVE( count, tab, p ) \
102 TAB_FIND( count, tab, p, _i_index_ ); \
103 if( _i_index_ >= 0 ) \
107 memmove( ((void**)(tab) + _i_index_), \
108 ((void**)(tab) + _i_index_+1), \
109 ( (count) - _i_index_ - 1 ) * sizeof( void* ) );\
121 * Binary search in a sorted array. The key must be comparable by < and >
122 * \param entries array of entries
123 * \param count number of entries
124 * \param elem key to check within an entry (like .id, or ->i_id)
125 * \param zetype type of the key
126 * \param key value of the key
127 * \param answer index of answer within the array. -1 if not found
129 #define BSEARCH( entries, count, elem, zetype, key, answer ) { \
130 int low = 0, high = count - 1; \
132 while( low <= high ) {\
133 int mid = (low + high ) / 2; /* Just don't care about 2^30 tables */ \
134 zetype mid_val = entries[mid] elem;\
135 if( mid_val < key ) \
137 else if ( mid_val > key ) \
141 answer = mid; break; \
146 /* Dictionnary handling */
157 dict_entry_t *p_entries;
161 VLC_EXPORT( dict_t *, vlc_DictNew, (void) );
162 VLC_EXPORT( void, vlc_DictClear, (dict_t * ) );
163 VLC_EXPORT( void, vlc_DictInsert, (dict_t *, int, const char *, void * ) );
164 VLC_EXPORT( void*, vlc_DictGet, (dict_t *, int, const char * ) );
165 VLC_EXPORT( int, vlc_DictLookup, (dict_t *, int, const char * ) );
168 /************************************************************************
169 * Dynamic arrays with progressive allocation
170 ************************************************************************/
172 /* Internal functions */
173 //printf("Realloc from %i to %i\n", array.i_alloc, newsize);
174 #define _ARRAY_ALLOC(array, newsize) { \
175 array.i_alloc = newsize; \
176 array.p_elems = VLCCVP realloc( array.p_elems, array.i_alloc * \
177 sizeof(*array.p_elems) ); \
178 assert(array.p_elems); \
181 #define _ARRAY_GROW1(array) { \
182 if( array.i_alloc < 10 ) \
183 _ARRAY_ALLOC(array, 10 ) \
184 else if( array.i_alloc == array.i_size ) \
185 _ARRAY_ALLOC(array, (int)(array.i_alloc * 1.5) ) \
188 #define _ARRAY_GROW(array,additional) { \
189 int i_first = array.i_alloc; \
190 while( array.i_alloc - i_first < additional ) \
192 if( array.i_alloc < 10 ) \
193 _ARRAY_ALLOC(array, 10 ) \
194 else if( array.i_alloc == array.i_size ) \
195 _ARRAY_ALLOC(array, (int)(array.i_alloc * 1.5) ) \
200 #define _ARRAY_SHRINK(array) { \
201 if( array.i_size > 10 && array.i_size < (int)(array.i_alloc / 1.5) ) { \
202 _ARRAY_ALLOC(array, array.i_size + 5); \
208 #define DECL_ARRAY(type) struct { \
214 #define TYPEDEF_ARRAY(type, name) typedef DECL_ARRAY(type) name;
216 #define ARRAY_INIT(array) \
219 array.p_elems = NULL;
221 #define ARRAY_RESET(array) \
224 free( array.p_elems ); array.p_elems = NULL;
226 #define ARRAY_APPEND(array, elem) { \
227 _ARRAY_GROW1(array); \
228 array.p_elems[array.i_size] = elem; \
232 #define ARRAY_INSERT(array,elem,pos) { \
233 _ARRAY_GROW1(array); \
234 if( array.i_size - pos ) { \
235 memmove( array.p_elems + pos + 1, array.p_elems + pos, \
236 (array.i_size-pos) * sizeof(*array.p_elems) ); \
238 array.p_elems[pos] = elem; \
242 #define ARRAY_REMOVE(array,pos) { \
243 if( array.i_size - (pos) - 1 ) \
245 memmove( array.p_elems + pos, array.p_elems + pos + 1, \
246 ( array.i_size - pos - 1 ) *sizeof(*array.p_elems) ); \
249 _ARRAY_SHRINK(array); \
252 #define ARRAY_VAL(array, pos) array.p_elems[pos]
254 #define ARRAY_BSEARCH(array, elem, zetype, key, answer) \
255 BSEARCH( array.p_elems, array.i_size, elem, zetype, key, answer)
257 #define FOREACH_ARRAY( item, array ) { \
259 for( fe_idx = 0 ; fe_idx < array.i_size ; fe_idx++ ) \
261 item = array.p_elems[fe_idx];
263 #define FOREACH_END() } }