1 /*****************************************************************************
2 * vlc_arrays.h : Arrays and data structures handling
3 *****************************************************************************
4 * Copyright (C) 1999-2004 the VideoLAN team
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.
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 #if defined( _MSC_VER ) && _MSC_VER < 1300 && !defined( UNDER_CE )
44 # define VLCCVP (void**) /* Work-around for broken compiler */
48 #define INSERT_ELEM( p_ar, i_oldsize, i_pos, elem ) \
51 if( !i_oldsize ) (p_ar) = NULL; \
52 (p_ar) = VLCCVP realloc( p_ar, ((i_oldsize) + 1) * sizeof(*(p_ar)) ); \
53 if( (i_oldsize) - (i_pos) ) \
55 memmove( (p_ar) + (i_pos) + 1, (p_ar) + (i_pos), \
56 ((i_oldsize) - (i_pos)) * sizeof( *(p_ar) ) ); \
58 (p_ar)[i_pos] = elem; \
63 #define REMOVE_ELEM( p_ar, i_size, i_pos ) \
66 if( (i_size) - (i_pos) - 1 ) \
68 memmove( (p_ar) + (i_pos), \
69 (p_ar) + (i_pos) + 1, \
70 ((i_size) - (i_pos) - 1) * sizeof( *(p_ar) ) ); \
73 (p_ar) = realloc_down( p_ar, ((i_size) - 1) * sizeof( *(p_ar) ) );\
83 #define TAB_INIT( count, tab ) \
89 #define TAB_CLEAN( count, tab ) \
96 #define TAB_APPEND_CAST( cast, count, tab, p ) \
99 (tab) = cast realloc( tab, sizeof( void ** ) * ( (count) + 1 ) ); \
101 (tab) = cast malloc( sizeof( void ** ) ); \
102 (tab)[count] = (p); \
106 #define TAB_APPEND( count, tab, p ) \
107 TAB_APPEND_CAST( , count, tab, p )
108 #define TAB_APPEND_CPP( type, count, tab, p ) \
109 TAB_APPEND_CAST( (type**), count, tab, p )
111 #define TAB_FIND( count, tab, p, index ) \
115 for( _i_ = 0; _i_ < (count); _i_++ ) \
117 if( (tab)[_i_] == (p) ) \
126 #define TAB_REMOVE( count, tab, p ) \
129 TAB_FIND( count, tab, p, _i_index_ ); \
130 if( _i_index_ >= 0 ) \
134 memmove( ((void**)(tab) + _i_index_), \
135 ((void**)(tab) + _i_index_+1), \
136 ( (count) - _i_index_ - 1 ) * sizeof( void* ) );\
147 #define TAB_INSERT_CAST( cast, count, tab, p, index ) do { \
149 (tab) = cast realloc( tab, sizeof( void ** ) * ( (count) + 1 ) ); \
151 (tab) = cast malloc( sizeof( void ** ) ); \
152 if( (count) - (index) > 0 ) \
153 memmove( (void**)(tab) + (index) + 1, \
154 (void**)(tab) + (index), \
155 ((count) - (index)) * sizeof(*(tab)) );\
156 (tab)[(index)] = (p); \
160 #define TAB_INSERT( count, tab, p, index ) \
161 TAB_INSERT_CAST( , count, tab, p, index )
164 * Binary search in a sorted array. The key must be comparable by < and >
165 * \param entries array of entries
166 * \param count number of entries
167 * \param elem key to check within an entry (like .id, or ->i_id)
168 * \param zetype type of the key
169 * \param key value of the key
170 * \param answer index of answer within the array. -1 if not found
172 #define BSEARCH( entries, count, elem, zetype, key, answer ) \
174 int low = 0, high = count - 1; \
176 while( low <= high ) {\
177 int mid = (low + high ) / 2; /* Just don't care about 2^30 tables */ \
178 zetype mid_val = entries[mid] elem;\
179 if( mid_val < key ) \
181 else if ( mid_val > key ) \
185 answer = mid; break; \
191 /************************************************************************
192 * Dynamic arrays with progressive allocation
193 ************************************************************************/
195 /* Internal functions */
196 #define _ARRAY_ALLOC(array, newsize) { \
197 (array).i_alloc = newsize; \
198 (array).p_elems = VLCCVP realloc( (array).p_elems, (array).i_alloc * \
199 sizeof(*(array).p_elems) ); \
202 #define _ARRAY_GROW1(array) { \
203 if( (array).i_alloc < 10 ) \
204 _ARRAY_ALLOC(array, 10 ) \
205 else if( (array).i_alloc == (array).i_size ) \
206 _ARRAY_ALLOC(array, (int)(array.i_alloc * 1.5) ) \
209 #define _ARRAY_GROW(array,additional) { \
210 int i_first = (array).i_alloc; \
211 while( (array).i_alloc - i_first < additional ) \
213 if( (array).i_alloc < 10 ) \
214 _ARRAY_ALLOC(array, 10 ) \
215 else if( (array).i_alloc == (array).i_size ) \
216 _ARRAY_ALLOC(array, (int)((array).i_alloc * 1.5) ) \
221 #define _ARRAY_SHRINK(array) { \
222 if( (array).i_size > 10 && (array).i_size < (int)((array).i_alloc / 1.5) ) { \
223 _ARRAY_ALLOC(array, (array).i_size + 5); \
227 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
230 #define DECL_ARRAY(type) struct { \
236 #define TYPEDEF_ARRAY(type, name) typedef DECL_ARRAY(type) name;
238 #define ARRAY_INIT(array) \
240 (array).i_alloc = 0; \
241 (array).i_size = 0; \
242 (array).p_elems = NULL; \
245 #define ARRAY_RESET(array) \
247 (array).i_alloc = 0; \
248 (array).i_size = 0; \
249 free( (array).p_elems ); (array).p_elems = NULL; \
252 #define ARRAY_APPEND(array, elem) \
254 _ARRAY_GROW1(array); \
255 (array).p_elems[(array).i_size] = elem; \
259 #define ARRAY_INSERT(array,elem,pos) \
261 _ARRAY_GROW1(array); \
262 if( (array).i_size - pos ) { \
263 memmove( (array).p_elems + pos + 1, (array).p_elems + pos, \
264 ((array).i_size-pos) * sizeof(*(array).p_elems) ); \
266 (array).p_elems[pos] = elem; \
270 #define ARRAY_REMOVE(array,pos) \
272 if( (array).i_size - (pos) - 1 ) \
274 memmove( (array).p_elems + pos, (array).p_elems + pos + 1, \
275 ( (array).i_size - pos - 1 ) *sizeof(*(array).p_elems) ); \
278 _ARRAY_SHRINK(array); \
281 #define ARRAY_VAL(array, pos) array.p_elems[pos]
283 #define ARRAY_BSEARCH(array, elem, zetype, key, answer) \
284 BSEARCH( (array).p_elems, (array).i_size, elem, zetype, key, answer)
286 #define FOREACH_ARRAY( item, array ) { \
288 for( fe_idx = 0 ; fe_idx < (array).i_size ; fe_idx++ ) \
290 item = (array).p_elems[fe_idx];
292 #define FOREACH_END() } }
295 /************************************************************************
296 * Dynamic arrays with progressive allocation (Preferred API)
297 ************************************************************************/
298 typedef struct vlc_array_t
304 static inline void vlc_array_init( vlc_array_t * p_array )
306 memset( p_array, 0, sizeof(vlc_array_t) );
309 static inline void vlc_array_clear( vlc_array_t * p_array )
311 free( p_array->pp_elems );
312 memset( p_array, 0, sizeof(vlc_array_t) );
315 static inline vlc_array_t * vlc_array_new( void )
317 vlc_array_t * ret = (vlc_array_t *)malloc( sizeof(vlc_array_t) );
318 if( ret ) vlc_array_init( ret );
322 static inline void vlc_array_destroy( vlc_array_t * p_array )
326 vlc_array_clear( p_array );
333 vlc_array_count( vlc_array_t * p_array )
335 return p_array->i_count;
339 vlc_array_item_at_index( vlc_array_t * p_array, int i_index )
341 return p_array->pp_elems[i_index];
345 vlc_array_index_of_item( vlc_array_t * p_array, void * item )
348 for( i = 0; i < p_array->i_count; i++)
350 if( p_array->pp_elems[i] == item )
358 vlc_array_insert( vlc_array_t * p_array, void * p_elem, int i_index )
360 TAB_INSERT_CAST( (void **), p_array->i_count, p_array->pp_elems, p_elem, i_index );
364 vlc_array_append( vlc_array_t * p_array, void * p_elem )
366 vlc_array_insert( p_array, p_elem, p_array->i_count );
370 vlc_array_remove( vlc_array_t * p_array, int i_index )
374 if( p_array->i_count > 1 )
376 memmove( p_array->pp_elems + i_index,
377 p_array->pp_elems + i_index+1,
378 ( p_array->i_count - i_index - 1 ) * sizeof( void* ) );
381 if( p_array->i_count == 0 )
383 free( p_array->pp_elems );
384 p_array->pp_elems = NULL;
390 /************************************************************************
392 ************************************************************************/
394 /* This function is not intended to be crypto-secure, we only want it to be
395 * fast and not suck too much. This one is pretty fast and did 0 collisions
396 * in wenglish's dictionary.
398 static inline uint64_t DictHash( const char *psz_string, int hashsize )
405 i_hash += *psz_string++;
406 i_hash += i_hash << 10;
407 i_hash ^= i_hash >> 8;
410 return i_hash % hashsize;
413 typedef struct vlc_dictionary_entry_t
417 struct vlc_dictionary_entry_t * p_next;
418 } vlc_dictionary_entry_t;
420 typedef struct vlc_dictionary_t
423 vlc_dictionary_entry_t ** p_entries;
426 static void * const kVLCDictionaryNotFound = NULL;
428 static inline void vlc_dictionary_init( vlc_dictionary_t * p_dict, int i_size )
430 p_dict->p_entries = NULL;
434 p_dict->p_entries = (vlc_dictionary_entry_t **)calloc( i_size, sizeof(*p_dict->p_entries) );
435 if( !p_dict->p_entries )
438 p_dict->i_size = i_size;
441 static inline void vlc_dictionary_clear( vlc_dictionary_t * p_dict,
442 void ( * pf_free )( void * p_data, void * p_obj ),
445 if( p_dict->p_entries )
447 for( int i = 0; i < p_dict->i_size; i++ )
449 vlc_dictionary_entry_t * p_current, * p_next;
450 p_current = p_dict->p_entries[i];
453 p_next = p_current->p_next;
454 if( pf_free != NULL )
455 ( * pf_free )( p_current->p_value, p_obj );
456 free( p_current->psz_key );
461 free( p_dict->p_entries );
462 p_dict->p_entries = NULL;
470 vlc_dictionary_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_key )
472 if( !p_dict->p_entries )
473 return kVLCDictionaryNotFound;
475 int i_pos = DictHash( psz_key, p_dict->i_size );
476 vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos];
479 return kVLCDictionaryNotFound;
481 /* Make sure we return the right item. (Hash collision) */
483 if( !strcmp( psz_key, p_entry->psz_key ) )
484 return p_entry->p_value;
485 p_entry = p_entry->p_next;
488 return kVLCDictionaryNotFound;
492 vlc_dictionary_keys_count( const vlc_dictionary_t * p_dict )
494 vlc_dictionary_entry_t * p_entry;
497 if( !p_dict->p_entries )
500 for( i = 0; i < p_dict->i_size; i++ )
502 for( p_entry = p_dict->p_entries[i]; p_entry; p_entry = p_entry->p_next ) count++;
507 static inline char **
508 vlc_dictionary_all_keys( const vlc_dictionary_t * p_dict )
510 vlc_dictionary_entry_t * p_entry;
512 int i, count = vlc_dictionary_keys_count( p_dict );
514 ppsz_ret = (char**)malloc(sizeof(char *) * (count + 1));
517 for( i = 0; i < p_dict->i_size; i++ )
519 for( p_entry = p_dict->p_entries[i]; p_entry; p_entry = p_entry->p_next )
520 ppsz_ret[count++] = strdup( p_entry->psz_key );
522 ppsz_ret[count] = NULL;
527 __vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key,
528 void * p_value, bool rebuild )
530 if( !p_dict->p_entries )
531 vlc_dictionary_init( p_dict, 1 );
533 int i_pos = DictHash( psz_key, p_dict->i_size );
534 vlc_dictionary_entry_t * p_entry;
536 p_entry = (vlc_dictionary_entry_t *)malloc(sizeof(*p_entry));
537 p_entry->psz_key = strdup( psz_key );
538 p_entry->p_value = p_value;
539 p_entry->p_next = p_dict->p_entries[i_pos];
540 p_dict->p_entries[i_pos] = p_entry;
543 /* Count how many items there was */
545 for( count = 1; p_entry->p_next; count++ )
546 p_entry = p_entry->p_next;
547 if( count > 3 ) /* XXX: this need tuning */
549 /* Here it starts to be not good, rebuild a bigger dictionary */
550 struct vlc_dictionary_t new_dict;
551 int i_new_size = ( (p_dict->i_size+2) * 3) / 2; /* XXX: this need tuning */
553 vlc_dictionary_init( &new_dict, i_new_size );
554 for( i = 0; i < p_dict->i_size; i++ )
556 p_entry = p_dict->p_entries[i];
559 __vlc_dictionary_insert( &new_dict, p_entry->psz_key,
561 false /* To avoid multiple rebuild loop */);
562 p_entry = p_entry->p_next;
566 vlc_dictionary_clear( p_dict, NULL, NULL );
567 p_dict->i_size = new_dict.i_size;
568 p_dict->p_entries = new_dict.p_entries;
574 vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key, void * p_value )
576 __vlc_dictionary_insert( p_dict, psz_key, p_value, true );
580 vlc_dictionary_remove_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_key,
581 void ( * pf_free )( void * p_data, void * p_obj ),
584 if( !p_dict->p_entries )
587 int i_pos = DictHash( psz_key, p_dict->i_size );
588 vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos];
589 vlc_dictionary_entry_t * p_prev;
592 return; /* Not found, nothing to do */
597 if( !strcmp( psz_key, p_entry->psz_key ) )
599 if( pf_free != NULL )
600 ( * pf_free )( p_entry->p_value, p_obj );
602 p_dict->p_entries[i_pos] = p_entry->p_next;
604 p_prev->p_next = p_entry->p_next;
605 free( p_entry->psz_key );
610 p_entry = p_entry->p_next;
613 /* No key was found */