1 /*****************************************************************************
2 * vlc_arrays.h : Arrays and data structures handling
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VLC authors and VideoLAN
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 it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
30 * This file defines functions, structures and macros for handling arrays in vlc
33 /* realloc() that never fails *if* downsizing */
34 static inline void *realloc_down( void *ptr, size_t size )
36 void *ret = realloc( ptr, size );
37 return ret ? ret : ptr;
41 * Simple dynamic array handling. Array is realloced at each insert/removal
43 #define INSERT_ELEM( p_ar, i_oldsize, i_pos, elem ) \
46 if( !(i_oldsize) ) (p_ar) = NULL; \
47 (p_ar) = realloc( p_ar, ((i_oldsize) + 1) * sizeof(*(p_ar)) ); \
48 if( !(p_ar) ) abort(); \
49 if( (i_oldsize) - (i_pos) ) \
51 memmove( (p_ar) + (i_pos) + 1, (p_ar) + (i_pos), \
52 ((i_oldsize) - (i_pos)) * sizeof( *(p_ar) ) ); \
54 (p_ar)[(i_pos)] = elem; \
59 #define REMOVE_ELEM( p_ar, i_size, i_pos ) \
62 if( (i_size) - (i_pos) - 1 ) \
64 memmove( (p_ar) + (i_pos), \
65 (p_ar) + (i_pos) + 1, \
66 ((i_size) - (i_pos) - 1) * sizeof( *(p_ar) ) ); \
69 (p_ar) = realloc_down( p_ar, ((i_size) - 1) * sizeof( *(p_ar) ) );\
79 #define TAB_INIT( count, tab ) \
85 #define TAB_CLEAN( count, tab ) \
92 #define TAB_APPEND_CAST( cast, count, tab, p ) \
95 (tab) = cast realloc( tab, sizeof( void ** ) * ( (count) + 1 ) ); \
97 (tab) = cast malloc( sizeof( void ** ) ); \
98 if( !(tab) ) abort(); \
103 #define TAB_APPEND( count, tab, p ) \
104 TAB_APPEND_CAST( , count, tab, p )
106 #define TAB_FIND( count, tab, p, index ) \
109 for( int i = 0; i < (count); i++ ) \
110 if( (tab)[i] == (p) ) \
118 #define TAB_REMOVE( count, tab, p ) \
121 TAB_FIND( count, tab, p, i_index ); \
126 memmove( ((void**)(tab) + i_index), \
127 ((void**)(tab) + i_index+1), \
128 ( (count) - i_index - 1 ) * sizeof( void* ) );\
139 #define TAB_INSERT_CAST( cast, count, tab, p, index ) do { \
141 (tab) = cast realloc( tab, sizeof( void ** ) * ( (count) + 1 ) ); \
143 (tab) = cast malloc( sizeof( void ** ) ); \
144 if( !(tab) ) abort(); \
145 if( (count) - (index) > 0 ) \
146 memmove( (void**)(tab) + (index) + 1, \
147 (void**)(tab) + (index), \
148 ((count) - (index)) * sizeof(*(tab)) );\
149 (tab)[(index)] = (p); \
153 #define TAB_INSERT( count, tab, p, index ) \
154 TAB_INSERT_CAST( , count, tab, p, index )
157 * Binary search in a sorted array. The key must be comparable by < and >
158 * \param entries array of entries
159 * \param count number of entries
160 * \param elem key to check within an entry (like .id, or ->i_id)
161 * \param zetype type of the key
162 * \param key value of the key
163 * \param answer index of answer within the array. -1 if not found
165 #define BSEARCH( entries, count, elem, zetype, key, answer ) \
167 int low = 0, high = count - 1; \
169 while( low <= high ) {\
170 int mid = (low + high ) / 2; /* Just don't care about 2^30 tables */ \
171 zetype mid_val = entries[mid] elem;\
172 if( mid_val < key ) \
174 else if ( mid_val > key ) \
178 answer = mid; break; \
184 /************************************************************************
185 * Dynamic arrays with progressive allocation
186 ************************************************************************/
188 /* Internal functions */
189 #define _ARRAY_ALLOC(array, newsize) { \
190 (array).i_alloc = newsize; \
191 (array).p_elems = realloc( (array).p_elems, (array).i_alloc * \
192 sizeof(*(array).p_elems) ); \
193 if( !(array).p_elems ) abort(); \
196 #define _ARRAY_GROW1(array) { \
197 if( (array).i_alloc < 10 ) \
198 _ARRAY_ALLOC(array, 10 ) \
199 else if( (array).i_alloc == (array).i_size ) \
200 _ARRAY_ALLOC(array, (int)(array.i_alloc * 1.5) ) \
203 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
206 #define DECL_ARRAY(type) struct { \
212 #define TYPEDEF_ARRAY(type, name) typedef DECL_ARRAY(type) name;
214 #define ARRAY_INIT(array) \
216 (array).i_alloc = 0; \
217 (array).i_size = 0; \
218 (array).p_elems = NULL; \
221 #define ARRAY_RESET(array) \
223 (array).i_alloc = 0; \
224 (array).i_size = 0; \
225 free( (array).p_elems ); (array).p_elems = NULL; \
228 #define ARRAY_APPEND(array, elem) \
230 _ARRAY_GROW1(array); \
231 (array).p_elems[(array).i_size] = elem; \
235 #define ARRAY_INSERT(array,elem,pos) \
237 _ARRAY_GROW1(array); \
238 if( (array).i_size - pos ) { \
239 memmove( (array).p_elems + pos + 1, (array).p_elems + pos, \
240 ((array).i_size-pos) * sizeof(*(array).p_elems) ); \
242 (array).p_elems[pos] = elem; \
246 #define _ARRAY_SHRINK(array) { \
247 if( (array).i_size > 10 && (array).i_size < (int)((array).i_alloc / 1.5) ) { \
248 _ARRAY_ALLOC(array, (array).i_size + 5); \
252 #define ARRAY_REMOVE(array,pos) \
254 if( (array).i_size - (pos) - 1 ) \
256 memmove( (array).p_elems + pos, (array).p_elems + pos + 1, \
257 ( (array).i_size - pos - 1 ) *sizeof(*(array).p_elems) ); \
260 _ARRAY_SHRINK(array); \
263 #define ARRAY_VAL(array, pos) array.p_elems[pos]
265 #define ARRAY_BSEARCH(array, elem, zetype, key, answer) \
266 BSEARCH( (array).p_elems, (array).i_size, elem, zetype, key, answer)
268 #define FOREACH_ARRAY( item, array ) { \
270 for( fe_idx = 0 ; fe_idx < (array).i_size ; fe_idx++ ) \
272 item = (array).p_elems[fe_idx];
274 #define FOREACH_END() } }
277 /************************************************************************
278 * Dynamic arrays with progressive allocation (Preferred API)
279 ************************************************************************/
280 typedef struct vlc_array_t
286 static inline void vlc_array_init( vlc_array_t * p_array )
288 memset( p_array, 0, sizeof(vlc_array_t) );
291 static inline void vlc_array_clear( vlc_array_t * p_array )
293 free( p_array->pp_elems );
294 memset( p_array, 0, sizeof(vlc_array_t) );
297 static inline vlc_array_t * vlc_array_new( void )
299 vlc_array_t * ret = (vlc_array_t *)malloc( sizeof(vlc_array_t) );
300 if( ret ) vlc_array_init( ret );
304 static inline void vlc_array_destroy( vlc_array_t * p_array )
308 vlc_array_clear( p_array );
315 vlc_array_count( vlc_array_t * p_array )
317 return p_array->i_count;
321 vlc_array_item_at_index( vlc_array_t * p_array, int i_index )
323 return p_array->pp_elems[i_index];
327 vlc_array_index_of_item( vlc_array_t * p_array, void * item )
330 for( i = 0; i < p_array->i_count; i++)
332 if( p_array->pp_elems[i] == item )
340 vlc_array_insert( vlc_array_t * p_array, void * p_elem, int i_index )
342 TAB_INSERT_CAST( (void **), p_array->i_count, p_array->pp_elems, p_elem, i_index );
346 vlc_array_append( vlc_array_t * p_array, void * p_elem )
348 vlc_array_insert( p_array, p_elem, p_array->i_count );
352 vlc_array_remove( vlc_array_t * p_array, int i_index )
356 if( p_array->i_count > 1 )
358 memmove( p_array->pp_elems + i_index,
359 p_array->pp_elems + i_index+1,
360 ( p_array->i_count - i_index - 1 ) * sizeof( void* ) );
363 if( p_array->i_count == 0 )
365 free( p_array->pp_elems );
366 p_array->pp_elems = NULL;
372 /************************************************************************
374 ************************************************************************/
376 /* This function is not intended to be crypto-secure, we only want it to be
377 * fast and not suck too much. This one is pretty fast and did 0 collisions
378 * in wenglish's dictionary.
380 static inline uint64_t DictHash( const char *psz_string, int hashsize )
387 i_hash += *psz_string++;
388 i_hash += i_hash << 10;
389 i_hash ^= i_hash >> 8;
392 return i_hash % hashsize;
395 typedef struct vlc_dictionary_entry_t
399 struct vlc_dictionary_entry_t * p_next;
400 } vlc_dictionary_entry_t;
402 typedef struct vlc_dictionary_t
405 vlc_dictionary_entry_t ** p_entries;
408 static void * const kVLCDictionaryNotFound = NULL;
410 static inline void vlc_dictionary_init( vlc_dictionary_t * p_dict, int i_size )
412 p_dict->p_entries = NULL;
416 p_dict->p_entries = (vlc_dictionary_entry_t **)calloc( i_size, sizeof(*p_dict->p_entries) );
417 if( !p_dict->p_entries )
420 p_dict->i_size = i_size;
423 static inline void vlc_dictionary_clear( vlc_dictionary_t * p_dict,
424 void ( * pf_free )( void * p_data, void * p_obj ),
427 if( p_dict->p_entries )
429 for( int i = 0; i < p_dict->i_size; i++ )
431 vlc_dictionary_entry_t * p_current, * p_next;
432 p_current = p_dict->p_entries[i];
435 p_next = p_current->p_next;
436 if( pf_free != NULL )
437 ( * pf_free )( p_current->p_value, p_obj );
438 free( p_current->psz_key );
443 free( p_dict->p_entries );
444 p_dict->p_entries = NULL;
450 vlc_dictionary_has_key( const vlc_dictionary_t * p_dict, const char * psz_key )
452 if( !p_dict->p_entries )
455 int i_pos = DictHash( psz_key, p_dict->i_size );
456 return p_dict->p_entries[i_pos] != NULL;
460 vlc_dictionary_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_key )
462 if( !p_dict->p_entries )
463 return kVLCDictionaryNotFound;
465 int i_pos = DictHash( psz_key, p_dict->i_size );
466 vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos];
469 return kVLCDictionaryNotFound;
471 /* Make sure we return the right item. (Hash collision) */
473 if( !strcmp( psz_key, p_entry->psz_key ) )
474 return p_entry->p_value;
475 p_entry = p_entry->p_next;
478 return kVLCDictionaryNotFound;
482 vlc_dictionary_keys_count( const vlc_dictionary_t * p_dict )
484 vlc_dictionary_entry_t * p_entry;
487 if( !p_dict->p_entries )
490 for( i = 0; i < p_dict->i_size; i++ )
492 for( p_entry = p_dict->p_entries[i]; p_entry; p_entry = p_entry->p_next ) count++;
497 static inline char **
498 vlc_dictionary_all_keys( const vlc_dictionary_t * p_dict )
500 vlc_dictionary_entry_t * p_entry;
502 int i, count = vlc_dictionary_keys_count( p_dict );
504 ppsz_ret = (char**)malloc(sizeof(char *) * (count + 1));
505 if( unlikely(!ppsz_ret) )
509 for( i = 0; i < p_dict->i_size; i++ )
511 for( p_entry = p_dict->p_entries[i]; p_entry; p_entry = p_entry->p_next )
512 ppsz_ret[count++] = strdup( p_entry->psz_key );
514 ppsz_ret[count] = NULL;
519 __vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key,
520 void * p_value, bool rebuild )
522 if( !p_dict->p_entries )
523 vlc_dictionary_init( p_dict, 1 );
525 int i_pos = DictHash( psz_key, p_dict->i_size );
526 vlc_dictionary_entry_t * p_entry;
528 p_entry = (vlc_dictionary_entry_t *)malloc(sizeof(*p_entry));
529 p_entry->psz_key = strdup( psz_key );
530 p_entry->p_value = p_value;
531 p_entry->p_next = p_dict->p_entries[i_pos];
532 p_dict->p_entries[i_pos] = p_entry;
535 /* Count how many items there was */
537 for( count = 1; p_entry->p_next; count++ )
538 p_entry = p_entry->p_next;
539 if( count > 3 ) /* XXX: this need tuning */
541 /* Here it starts to be not good, rebuild a bigger dictionary */
542 struct vlc_dictionary_t new_dict;
543 int i_new_size = ( (p_dict->i_size+2) * 3) / 2; /* XXX: this need tuning */
545 vlc_dictionary_init( &new_dict, i_new_size );
546 for( i = 0; i < p_dict->i_size; i++ )
548 p_entry = p_dict->p_entries[i];
551 __vlc_dictionary_insert( &new_dict, p_entry->psz_key,
553 false /* To avoid multiple rebuild loop */);
554 p_entry = p_entry->p_next;
558 vlc_dictionary_clear( p_dict, NULL, NULL );
559 p_dict->i_size = new_dict.i_size;
560 p_dict->p_entries = new_dict.p_entries;
566 vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key, void * p_value )
568 __vlc_dictionary_insert( p_dict, psz_key, p_value, true );
572 vlc_dictionary_remove_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_key,
573 void ( * pf_free )( void * p_data, void * p_obj ),
576 if( !p_dict->p_entries )
579 int i_pos = DictHash( psz_key, p_dict->i_size );
580 vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos];
581 vlc_dictionary_entry_t * p_prev;
584 return; /* Not found, nothing to do */
589 if( !strcmp( psz_key, p_entry->psz_key ) )
591 if( pf_free != NULL )
592 ( * pf_free )( p_entry->p_value, p_obj );
594 p_dict->p_entries[i_pos] = p_entry->p_next;
596 p_prev->p_next = p_entry->p_next;
597 free( p_entry->psz_key );
602 p_entry = p_entry->p_next;
605 /* No key was found */
610 template <typename T>
611 void vlc_delete_all( T &container )
613 typename T::iterator it = container.begin();
614 while ( it != container.end() )