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_
34 * Simple dynamic array handling. Array is realloced at each insert/removal
36 #if defined( _MSC_VER ) && _MSC_VER < 1300 && !defined( UNDER_CE )
37 # define VLCCVP (void**) /* Work-around for broken compiler */
41 #define INSERT_ELEM( p_ar, i_oldsize, i_pos, elem ) \
44 if( !i_oldsize ) (p_ar) = NULL; \
45 (p_ar) = VLCCVP realloc( p_ar, ((i_oldsize) + 1) * sizeof(*(p_ar)) ); \
46 if( (i_oldsize) - (i_pos) ) \
48 memmove( (p_ar) + (i_pos) + 1, (p_ar) + (i_pos), \
49 ((i_oldsize) - (i_pos)) * sizeof( *(p_ar) ) ); \
51 (p_ar)[i_pos] = elem; \
56 #define REMOVE_ELEM( p_ar, i_oldsize, i_pos ) \
59 if( (i_oldsize) - (i_pos) - 1 ) \
61 memmove( (p_ar) + (i_pos), \
62 (p_ar) + (i_pos) + 1, \
63 ((i_oldsize) - (i_pos) - 1) * sizeof( *(p_ar) ) ); \
67 (p_ar) = realloc( p_ar, ((i_oldsize) - 1) * sizeof( *(p_ar) ) ); \
78 #define TAB_INIT( count, tab ) \
84 #define TAB_CLEAN( count, tab ) \
86 if( tab ) free( tab ); \
91 #define TAB_APPEND_CAST( cast, count, tab, p ) \
94 (tab) = cast realloc( tab, sizeof( void ** ) * ( (count) + 1 ) ); \
96 (tab) = cast malloc( sizeof( void ** ) ); \
101 #define TAB_APPEND( count, tab, p ) \
102 TAB_APPEND_CAST( , count, tab, p )
103 #define TAB_APPEND_CPP( type, count, tab, p ) \
104 TAB_APPEND_CAST( (type**), count, tab, p )
106 #define TAB_FIND( count, tab, p, index ) \
110 for( _i_ = 0; _i_ < (count); _i_++ ) \
112 if( (tab)[_i_] == (p) ) \
120 #define TAB_REMOVE( count, tab, p ) \
123 TAB_FIND( count, tab, p, _i_index_ ); \
124 if( _i_index_ >= 0 ) \
128 memmove( ((void**)(tab) + _i_index_), \
129 ((void**)(tab) + _i_index_+1), \
130 ( (count) - _i_index_ - 1 ) * sizeof( void* ) );\
141 #define TAB_INSERT_CAST( cast, count, tab, p, index ) do { \
143 (tab) = cast realloc( tab, sizeof( void ** ) * ( (count) + 1 ) ); \
145 (tab) = cast malloc( sizeof( void ** ) ); \
146 if( (count) - (index) > 0 ) \
147 memmove( (void**)(tab) + (index) + 1, \
148 (void**)(tab) + (index), \
149 ((count) - (index)) * sizeof(*(tab)) );\
150 (tab)[(index)] = (p); \
154 #define TAB_INSERT( count, tab, p, index ) \
155 TAB_INSERT_CAST( , count, tab, p, index )
158 * Binary search in a sorted array. The key must be comparable by < and >
159 * \param entries array of entries
160 * \param count number of entries
161 * \param elem key to check within an entry (like .id, or ->i_id)
162 * \param zetype type of the key
163 * \param key value of the key
164 * \param answer index of answer within the array. -1 if not found
166 #define BSEARCH( entries, count, elem, zetype, key, answer ) \
168 int low = 0, high = count - 1; \
170 while( low <= high ) {\
171 int mid = (low + high ) / 2; /* Just don't care about 2^30 tables */ \
172 zetype mid_val = entries[mid] elem;\
173 if( mid_val < key ) \
175 else if ( mid_val > key ) \
179 answer = mid; break; \
184 /************************************************************************
186 ************************************************************************/
188 /* This function is not intended to be crypto-secure, we only want it to be
189 * fast and not suck too much. This one is pretty fast and did 0 collisions
190 * in wenglish's dictionary.
192 static inline uint64_t DictHash( const char *psz_string )
199 i_hash += *psz_string++;
200 i_hash += i_hash << 10;
201 i_hash ^= i_hash >> 8;
207 typedef struct vlc_dictionary_t
209 struct vlc_dictionary_entries_t
218 static void * const kVLCDictionaryNotFound = (void *)-1;
220 static inline void vlc_dictionary_init( vlc_dictionary_t * p_dict )
222 p_dict->i_entries = 0;
223 p_dict->p_entries = NULL;
226 static inline void vlc_dictionary_clear( vlc_dictionary_t * p_dict )
229 for ( i = 0; i < p_dict->i_entries; i++ )
231 free( p_dict->p_entries[i].psz_key );
233 free( p_dict->p_entries );
234 p_dict->i_entries = 0;
235 p_dict->p_entries = NULL;
240 vlc_dictionary_value_for_key( vlc_dictionary_t * p_dict, const char * psz_key )
245 if( p_dict->i_entries == 0 )
246 return kVLCDictionaryNotFound;
248 i_hash = DictHash( psz_key );
249 BSEARCH( p_dict->p_entries, p_dict->i_entries, .i_hash, uint64_t,
252 return kVLCDictionaryNotFound;
254 /* Hash found, let's check it looks like the entry */
255 if( !strcmp( psz_key, p_dict->p_entries[i_pos].psz_key ) )
256 return p_dict->p_entries[i_pos].p_value;
258 /* Hash collision! This should be very rare, but we cannot guarantee
259 * it will never happen. Just do an exhaustive search amongst all
260 * entries with the same hash. */
261 for( i = i_pos - 1 ; i > 0 && i_hash == p_dict->p_entries[i].i_hash ; i-- )
263 if( !strcmp( psz_key, p_dict->p_entries[i].psz_key ) )
264 return p_dict->p_entries[i_pos].p_value;
266 for( i = i_pos + 1 ; i < p_dict->i_entries &&
267 i_hash == p_dict->p_entries[i].i_hash ; i++ )
269 if( !strcmp( psz_key, p_dict->p_entries[i].psz_key ))
270 return p_dict->p_entries[i_pos].p_value;
272 /* Hash found, but entry not found (shouldn't happen!) */
273 return kVLCDictionaryNotFound;
277 vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key, void * p_value )
279 uint64_t i_hash = DictHash( psz_key );
282 /* First, caller should take care not to insert twice the same key */
283 /* This could be removed for optimization */
284 assert( vlc_dictionary_value_for_key( p_dict, psz_key ) == kVLCDictionaryNotFound );
286 /* Find a free slot */
287 if( p_dict->i_entries == 0 || i_hash <= p_dict->p_entries[0].i_hash )
289 else if( i_hash >= p_dict->p_entries[p_dict->i_entries-1].i_hash )
290 i_new = p_dict->i_entries;
293 int i_low = 0, i_high = p_dict->i_entries - 1;
294 while( i_high - i_low > 1 )
296 int i_mid = (i_low + i_high)/2;
297 if( p_dict->p_entries[i_mid].i_hash < i_hash ) {
299 } else if( p_dict->p_entries[i_mid].i_hash > i_hash ) {
303 if( p_dict->p_entries[i_low].i_hash < i_hash )
308 p_dict->p_entries = realloc( p_dict->p_entries, (p_dict->i_entries + 1) *
309 sizeof( struct vlc_dictionary_entries_t ) );
311 if( i_new != p_dict->i_entries -1 )
313 memmove( &p_dict->p_entries[i_new+1], &p_dict->p_entries[i_new],
314 ( p_dict->i_entries - i_new - 1 ) *
315 sizeof(struct vlc_dictionary_entries_t) );
318 p_dict->p_entries[i_new].i_hash = i_hash;
319 p_dict->p_entries[i_new].psz_key = strdup( psz_key );
320 p_dict->p_entries[i_new].p_value = p_value;
324 /************************************************************************
325 * Dynamic arrays with progressive allocation
326 ************************************************************************/
328 /* Internal functions */
329 #define _ARRAY_ALLOC(array, newsize) { \
330 array.i_alloc = newsize; \
331 array.p_elems = VLCCVP realloc( array.p_elems, array.i_alloc * \
332 sizeof(*array.p_elems) ); \
333 assert(array.p_elems); \
336 #define _ARRAY_GROW1(array) { \
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) ) \
343 #define _ARRAY_GROW(array,additional) { \
344 int i_first = array.i_alloc; \
345 while( array.i_alloc - i_first < additional ) \
347 if( array.i_alloc < 10 ) \
348 _ARRAY_ALLOC(array, 10 ) \
349 else if( array.i_alloc == array.i_size ) \
350 _ARRAY_ALLOC(array, (int)(array.i_alloc * 1.5) ) \
355 #define _ARRAY_SHRINK(array) { \
356 if( array.i_size > 10 && array.i_size < (int)(array.i_alloc / 1.5) ) { \
357 _ARRAY_ALLOC(array, array.i_size + 5); \
363 #define DECL_ARRAY(type) struct { \
369 #define TYPEDEF_ARRAY(type, name) typedef DECL_ARRAY(type) name;
371 #define ARRAY_INIT(array) \
374 array.p_elems = NULL;
376 #define ARRAY_RESET(array) \
379 free( array.p_elems ); array.p_elems = NULL;
381 #define ARRAY_APPEND(array, elem) { \
382 _ARRAY_GROW1(array); \
383 array.p_elems[array.i_size] = elem; \
387 #define ARRAY_INSERT(array,elem,pos) { \
388 _ARRAY_GROW1(array); \
389 if( array.i_size - pos ) { \
390 memmove( array.p_elems + pos + 1, array.p_elems + pos, \
391 (array.i_size-pos) * sizeof(*array.p_elems) ); \
393 array.p_elems[pos] = elem; \
397 #define ARRAY_REMOVE(array,pos) { \
398 if( array.i_size - (pos) - 1 ) \
400 memmove( array.p_elems + pos, array.p_elems + pos + 1, \
401 ( array.i_size - pos - 1 ) *sizeof(*array.p_elems) ); \
404 _ARRAY_SHRINK(array); \
407 #define ARRAY_VAL(array, pos) array.p_elems[pos]
409 #define ARRAY_BSEARCH(array, elem, zetype, key, answer) \
410 BSEARCH( array.p_elems, array.i_size, elem, zetype, key, answer)
412 #define FOREACH_ARRAY( item, array ) { \
414 for( fe_idx = 0 ; fe_idx < array.i_size ; fe_idx++ ) \
416 item = array.p_elems[fe_idx];
418 #define FOREACH_END() } }