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 #if !defined( __LIBVLC__ )
25 #error You are not libvlc or one of its plugins. You cannot include this file
28 #ifndef _VLC_ARRAYS_H_
29 #define _VLC_ARRAYS_H_
32 * Simple dynamic array handling. Array is realloced at each insert/removal
34 #if defined( _MSC_VER ) && _MSC_VER < 1300 && !defined( UNDER_CE )
35 # define VLCCVP (void**) /* Work-around for broken compiler */
39 #define INSERT_ELEM( p_ar, i_oldsize, i_pos, elem ) \
42 if( !i_oldsize ) (p_ar) = NULL; \
43 (p_ar) = VLCCVP realloc( p_ar, ((i_oldsize) + 1) * sizeof(*(p_ar)) ); \
44 if( (i_oldsize) - (i_pos) ) \
46 memmove( (p_ar) + (i_pos) + 1, (p_ar) + (i_pos), \
47 ((i_oldsize) - (i_pos)) * sizeof( *(p_ar) ) ); \
49 (p_ar)[i_pos] = elem; \
54 #define REMOVE_ELEM( p_ar, i_oldsize, i_pos ) \
57 if( (i_oldsize) - (i_pos) - 1 ) \
59 memmove( (p_ar) + (i_pos), \
60 (p_ar) + (i_pos) + 1, \
61 ((i_oldsize) - (i_pos) - 1) * sizeof( *(p_ar) ) ); \
65 (p_ar) = realloc( p_ar, ((i_oldsize) - 1) * sizeof( *(p_ar) ) ); \
76 #define TAB_INIT( count, tab ) \
82 #define TAB_CLEAN( count, tab ) \
84 if( tab ) free( tab ); \
89 #define TAB_APPEND( count, tab, p ) \
92 (tab) = realloc( tab, sizeof( void ** ) * ( (count) + 1 ) ); \
94 (tab) = malloc( sizeof( void ** ) ); \
99 #define TAB_FIND( count, tab, p, index ) \
103 for( _i_ = 0; _i_ < (count); _i_++ ) \
105 if( (tab)[_i_] == (p) ) \
113 #define TAB_REMOVE( count, tab, p ) \
116 TAB_FIND( count, tab, p, _i_index_ ); \
117 if( _i_index_ >= 0 ) \
121 memmove( ((void**)(tab) + _i_index_), \
122 ((void**)(tab) + _i_index_+1), \
123 ( (count) - _i_index_ - 1 ) * sizeof( void* ) );\
135 * Binary search in a sorted array. The key must be comparable by < and >
136 * \param entries array of entries
137 * \param count number of entries
138 * \param elem key to check within an entry (like .id, or ->i_id)
139 * \param zetype type of the key
140 * \param key value of the key
141 * \param answer index of answer within the array. -1 if not found
143 #define BSEARCH( entries, count, elem, zetype, key, answer ) \
145 int low = 0, high = count - 1; \
147 while( low <= high ) {\
148 int mid = (low + high ) / 2; /* Just don't care about 2^30 tables */ \
149 zetype mid_val = entries[mid] elem;\
150 if( mid_val < key ) \
152 else if ( mid_val > key ) \
156 answer = mid; break; \
161 /************************************************************************
163 ************************************************************************/
165 /* This function is not intended to be crypto-secure, we only want it to be
166 * fast and not suck too much. This one is pretty fast and did 0 collisions
167 * in wenglish's dictionary.
169 static inline uint64_t DictHash( const char *psz_string, int i_int )
176 i_hash += *psz_string++;
177 i_hash += i_hash << 10;
178 i_hash ^= i_hash >> 8;
181 return i_hash + ( (uint64_t)i_int << 32 );
184 #define DICT_TYPE(name,type) \
185 typedef struct dict_entry_##name##_t { \
190 } dict_entry_##name##_t; \
191 typedef struct dict_##name##_t { \
192 dict_entry_##name##_t *p_entries; \
196 #define DICT_NEW( p_dict ) { \
197 p_dict = malloc( sizeof(int)+sizeof(void*) ); \
198 p_dict->i_entries = 0; \
199 p_dict->p_entries = NULL; \
202 #define DICT_CLEAR( zdict ) { \
204 for ( _i_dict = 0; _i_dict < zdict->i_entries; _i_dict++ ) \
206 FREE( zdict->p_entries[_i_dict].psz_string ); \
208 FREE( zdict->p_entries ); \
212 #define DICT_INSERT( zdict, zint, zstring, zdata ) { \
213 uint64_t i_hash = DictHash( (zstring), (zint) ); \
215 /* Find a free slot */ \
216 if( zdict->i_entries == 0 || i_hash <= zdict->p_entries[0].i_hash ) \
218 else if( i_hash >= zdict->p_entries[zdict->i_entries-1].i_hash ) \
219 i_new = zdict->i_entries;\
222 int i_low = 0, i_high = zdict->i_entries - 1; \
223 while( i_high - i_low > 1 ) \
225 int i_mid = (i_low + i_high)/2; \
226 fprintf(stderr, "Low %i, high %i\n", i_low, i_high); \
227 if( zdict->p_entries[i_mid].i_hash < i_hash ) { \
229 } else if( zdict->p_entries[i_mid].i_hash > i_hash ) { \
233 if( zdict->p_entries[i_low].i_hash < i_hash ) \
238 zdict->p_entries = realloc( zdict->p_entries, (zdict->i_entries + 1) * \
239 ( sizeof(zdata) + sizeof(int) + sizeof(void*) + sizeof(uint64_t) ) ); \
240 zdict->i_entries++; \
241 if( i_new != zdict->i_entries -1 ) \
242 memmove( &zdict->p_entries[i_new+1], &zdict->p_entries[i_new], \
243 ( zdict->i_entries - i_new - 1 ) * \
244 ( sizeof(zdata) + sizeof(int) + sizeof(void*) + sizeof(uint64_t) ) );\
246 zdict->p_entries[i_new].i_hash = i_hash; \
247 zdict->p_entries[i_new].i_int = (zint); \
249 zdict->p_entries[i_new].psz_string = strdup( (zstring) ); \
251 zdict->p_entries[i_new].psz_string = NULL; \
253 zdict->p_entries[i_new].data = zdata; \
256 #define DICT_LOOKUP( zdict, zint, zstring, answer ) do { \
259 vlc_bool_t b_found = VLC_FALSE; \
260 if( zdict->i_entries == 0 ) { \
265 i_hash = DictHash( (zstring), (zint) ); \
266 BSEARCH( zdict->p_entries, zdict->i_entries, .i_hash, uint64_t, \
268 if( i_pos == -1 ) { \
273 /* Hash found, let's check it looks like the entry */ \
274 if( !strcmp( (zstring), zdict->p_entries[i_pos].psz_string ) ) { \
279 /* Hash collision! This should be very rare, but we cannot guarantee \
280 * it will never happen. Just do an exhaustive search amongst all \
281 * entries with the same hash. */ \
282 for( i = i_pos - 1 ; i > 0 && i_hash == zdict->p_entries[i].i_hash ; i-- )\
284 if( !strcmp( (zstring), zdict->p_entries[i].psz_string ) && \
285 zdict->p_entries[i].i_int == (zint) ) { \
286 b_found = VLC_TRUE; \
291 if( b_found == VLC_TRUE ) \
293 for( i = i_pos + 1 ; i < zdict->i_entries && \
294 i_hash == zdict->p_entries[i].i_hash ; i++ ) \
296 if( !strcmp( (zstring), zdict->p_entries[i].psz_string ) && \
297 zdict->p_entries[i].i_int == (zint) ) { \
298 b_found = VLC_TRUE; \
303 /* Hash found, but entry not found (quite strange !) */ \
307 #define DICT_GET( zdict, i_int, psz_string, answer ) { \
309 DICT_LOOKUP( zdict, i_int, psz_string, int_answer ); \
310 if( int_answer >= 0 ) \
311 answer = zdict->p_entries[int_answer].data; \
314 /************************************************************************
315 * Dynamic arrays with progressive allocation
316 ************************************************************************/
318 /* Internal functions */
319 #define _ARRAY_ALLOC(array, newsize) { \
320 array.i_alloc = newsize; \
321 array.p_elems = VLCCVP realloc( array.p_elems, array.i_alloc * \
322 sizeof(*array.p_elems) ); \
323 assert(array.p_elems); \
326 #define _ARRAY_GROW1(array) { \
327 if( array.i_alloc < 10 ) \
328 _ARRAY_ALLOC(array, 10 ) \
329 else if( array.i_alloc == array.i_size ) \
330 _ARRAY_ALLOC(array, (int)(array.i_alloc * 1.5) ) \
333 #define _ARRAY_GROW(array,additional) { \
334 int i_first = array.i_alloc; \
335 while( array.i_alloc - i_first < additional ) \
337 if( array.i_alloc < 10 ) \
338 _ARRAY_ALLOC(array, 10 ) \
339 else if( array.i_alloc == array.i_size ) \
340 _ARRAY_ALLOC(array, (int)(array.i_alloc * 1.5) ) \
345 #define _ARRAY_SHRINK(array) { \
346 if( array.i_size > 10 && array.i_size < (int)(array.i_alloc / 1.5) ) { \
347 _ARRAY_ALLOC(array, array.i_size + 5); \
353 #define DECL_ARRAY(type) struct { \
359 #define TYPEDEF_ARRAY(type, name) typedef DECL_ARRAY(type) name;
361 #define ARRAY_INIT(array) \
364 array.p_elems = NULL;
366 #define ARRAY_RESET(array) \
369 free( array.p_elems ); array.p_elems = NULL;
371 #define ARRAY_APPEND(array, elem) { \
372 _ARRAY_GROW1(array); \
373 array.p_elems[array.i_size] = elem; \
377 #define ARRAY_INSERT(array,elem,pos) { \
378 _ARRAY_GROW1(array); \
379 if( array.i_size - pos ) { \
380 memmove( array.p_elems + pos + 1, array.p_elems + pos, \
381 (array.i_size-pos) * sizeof(*array.p_elems) ); \
383 array.p_elems[pos] = elem; \
387 #define ARRAY_REMOVE(array,pos) { \
388 if( array.i_size - (pos) - 1 ) \
390 memmove( array.p_elems + pos, array.p_elems + pos + 1, \
391 ( array.i_size - pos - 1 ) *sizeof(*array.p_elems) ); \
394 _ARRAY_SHRINK(array); \
397 #define ARRAY_VAL(array, pos) array.p_elems[pos]
399 #define ARRAY_BSEARCH(array, elem, zetype, key, answer) \
400 BSEARCH( array.p_elems, array.i_size, elem, zetype, key, answer)
402 #define FOREACH_ARRAY( item, array ) { \
404 for( fe_idx = 0 ; fe_idx < array.i_size ; fe_idx++ ) \
406 item = array.p_elems[fe_idx];
408 #define FOREACH_END() } }