00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef VLC_ARRAYS_H_
00026 #define VLC_ARRAYS_H_
00027
00028
00029
00030
00031
00032
00033
00034 static inline void *realloc_down( void *ptr, size_t size )
00035 {
00036 void *ret = realloc( ptr, size );
00037 return ret ? ret : ptr;
00038 }
00039
00040
00041
00042
00043 #if defined( _MSC_VER ) && _MSC_VER < 1300 && !defined( UNDER_CE )
00044 # define VLCCVP (void**)
00045 #else
00046 # define VLCCVP
00047 #endif
00048 #define INSERT_ELEM( p_ar, i_oldsize, i_pos, elem ) \
00049 do \
00050 { \
00051 if( !(i_oldsize) ) (p_ar) = NULL; \
00052 (p_ar) = VLCCVP realloc( p_ar, ((i_oldsize) + 1) * sizeof(*(p_ar)) ); \
00053 if( !(p_ar) ) abort(); \
00054 if( (i_oldsize) - (i_pos) ) \
00055 { \
00056 memmove( (p_ar) + (i_pos) + 1, (p_ar) + (i_pos), \
00057 ((i_oldsize) - (i_pos)) * sizeof( *(p_ar) ) ); \
00058 } \
00059 (p_ar)[(i_pos)] = elem; \
00060 (i_oldsize)++; \
00061 } \
00062 while( 0 )
00063
00064 #define REMOVE_ELEM( p_ar, i_size, i_pos ) \
00065 do \
00066 { \
00067 if( (i_size) - (i_pos) - 1 ) \
00068 { \
00069 memmove( (p_ar) + (i_pos), \
00070 (p_ar) + (i_pos) + 1, \
00071 ((i_size) - (i_pos) - 1) * sizeof( *(p_ar) ) ); \
00072 } \
00073 if( i_size > 1 ) \
00074 (p_ar) = realloc_down( p_ar, ((i_size) - 1) * sizeof( *(p_ar) ) );\
00075 else \
00076 { \
00077 free( p_ar ); \
00078 (p_ar) = NULL; \
00079 } \
00080 (i_size)--; \
00081 } \
00082 while( 0 )
00083
00084 #define TAB_INIT( count, tab ) \
00085 do { \
00086 (count) = 0; \
00087 (tab) = NULL; \
00088 } while(0)
00089
00090 #define TAB_CLEAN( count, tab ) \
00091 do { \
00092 free( tab ); \
00093 (count)= 0; \
00094 (tab)= NULL; \
00095 } while(0)
00096
00097 #define TAB_APPEND_CAST( cast, count, tab, p ) \
00098 do { \
00099 if( (count) > 0 ) \
00100 (tab) = cast realloc( tab, sizeof( void ** ) * ( (count) + 1 ) ); \
00101 else \
00102 (tab) = cast malloc( sizeof( void ** ) ); \
00103 if( !(tab) ) abort(); \
00104 (tab)[count] = (p); \
00105 (count)++; \
00106 } while(0)
00107
00108 #define TAB_APPEND( count, tab, p ) \
00109 TAB_APPEND_CAST( , count, tab, p )
00110
00111 #define TAB_FIND( count, tab, p, index ) \
00112 do { \
00113 (index) = -1; \
00114 for( int i = 0; i < (count); i++ ) \
00115 if( (tab)[i] == (p) ) \
00116 { \
00117 (index) = i; \
00118 break; \
00119 } \
00120 } while(0)
00121
00122
00123 #define TAB_REMOVE( count, tab, p ) \
00124 do { \
00125 int i_index; \
00126 TAB_FIND( count, tab, p, i_index ); \
00127 if( i_index >= 0 ) \
00128 { \
00129 if( (count) > 1 ) \
00130 { \
00131 memmove( ((void**)(tab) + i_index), \
00132 ((void**)(tab) + i_index+1), \
00133 ( (count) - i_index - 1 ) * sizeof( void* ) );\
00134 } \
00135 (count)--; \
00136 if( (count) == 0 ) \
00137 { \
00138 free( tab ); \
00139 (tab) = NULL; \
00140 } \
00141 } \
00142 } while(0)
00143
00144 #define TAB_INSERT_CAST( cast, count, tab, p, index ) do { \
00145 if( (count) > 0 ) \
00146 (tab) = cast realloc( tab, sizeof( void ** ) * ( (count) + 1 ) ); \
00147 else \
00148 (tab) = cast malloc( sizeof( void ** ) ); \
00149 if( !(tab) ) abort(); \
00150 if( (count) - (index) > 0 ) \
00151 memmove( (void**)(tab) + (index) + 1, \
00152 (void**)(tab) + (index), \
00153 ((count) - (index)) * sizeof(*(tab)) );\
00154 (tab)[(index)] = (p); \
00155 (count)++; \
00156 } while(0)
00157
00158 #define TAB_INSERT( count, tab, p, index ) \
00159 TAB_INSERT_CAST( , count, tab, p, index )
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 #define BSEARCH( entries, count, elem, zetype, key, answer ) \
00171 do { \
00172 int low = 0, high = count - 1; \
00173 answer = -1; \
00174 while( low <= high ) {\
00175 int mid = (low + high ) / 2; \
00176 zetype mid_val = entries[mid] elem;\
00177 if( mid_val < key ) \
00178 low = mid + 1; \
00179 else if ( mid_val > key ) \
00180 high = mid -1; \
00181 else \
00182 { \
00183 answer = mid; break; \
00184 }\
00185 } \
00186 } while(0)
00187
00188
00189
00190
00191
00192
00193
00194 #define _ARRAY_ALLOC(array, newsize) { \
00195 (array).i_alloc = newsize; \
00196 (array).p_elems = VLCCVP realloc( (array).p_elems, (array).i_alloc * \
00197 sizeof(*(array).p_elems) ); \
00198 if( !(array).p_elems ) abort(); \
00199 }
00200
00201 #define _ARRAY_GROW1(array) { \
00202 if( (array).i_alloc < 10 ) \
00203 _ARRAY_ALLOC(array, 10 ) \
00204 else if( (array).i_alloc == (array).i_size ) \
00205 _ARRAY_ALLOC(array, (int)(array.i_alloc * 1.5) ) \
00206 }
00207
00208 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
00209
00210
00211 #define DECL_ARRAY(type) struct { \
00212 int i_alloc; \
00213 int i_size; \
00214 type *p_elems; \
00215 }
00216
00217 #define TYPEDEF_ARRAY(type, name) typedef DECL_ARRAY(type) name;
00218
00219 #define ARRAY_INIT(array) \
00220 do { \
00221 (array).i_alloc = 0; \
00222 (array).i_size = 0; \
00223 (array).p_elems = NULL; \
00224 } while(0)
00225
00226 #define ARRAY_RESET(array) \
00227 do { \
00228 (array).i_alloc = 0; \
00229 (array).i_size = 0; \
00230 free( (array).p_elems ); (array).p_elems = NULL; \
00231 } while(0)
00232
00233 #define ARRAY_APPEND(array, elem) \
00234 do { \
00235 _ARRAY_GROW1(array); \
00236 (array).p_elems[(array).i_size] = elem; \
00237 (array).i_size++; \
00238 } while(0)
00239
00240 #define ARRAY_INSERT(array,elem,pos) \
00241 do { \
00242 _ARRAY_GROW1(array); \
00243 if( (array).i_size - pos ) { \
00244 memmove( (array).p_elems + pos + 1, (array).p_elems + pos, \
00245 ((array).i_size-pos) * sizeof(*(array).p_elems) ); \
00246 } \
00247 (array).p_elems[pos] = elem; \
00248 (array).i_size++; \
00249 } while(0)
00250
00251 #define _ARRAY_SHRINK(array) { \
00252 if( (array).i_size > 10 && (array).i_size < (int)((array).i_alloc / 1.5) ) { \
00253 _ARRAY_ALLOC(array, (array).i_size + 5); \
00254 } \
00255 }
00256
00257 #define ARRAY_REMOVE(array,pos) \
00258 do { \
00259 if( (array).i_size - (pos) - 1 ) \
00260 { \
00261 memmove( (array).p_elems + pos, (array).p_elems + pos + 1, \
00262 ( (array).i_size - pos - 1 ) *sizeof(*(array).p_elems) ); \
00263 } \
00264 (array).i_size--; \
00265 _ARRAY_SHRINK(array); \
00266 } while(0)
00267
00268 #define ARRAY_VAL(array, pos) array.p_elems[pos]
00269
00270 #define ARRAY_BSEARCH(array, elem, zetype, key, answer) \
00271 BSEARCH( (array).p_elems, (array).i_size, elem, zetype, key, answer)
00272
00273 #define FOREACH_ARRAY( item, array ) { \
00274 int fe_idx; \
00275 for( fe_idx = 0 ; fe_idx < (array).i_size ; fe_idx++ ) \
00276 { \
00277 item = (array).p_elems[fe_idx];
00278
00279 #define FOREACH_END() } }
00280
00281
00282
00283
00284
00285 typedef struct vlc_array_t
00286 {
00287 int i_count;
00288 void ** pp_elems;
00289 } vlc_array_t;
00290
00291 static inline void vlc_array_init( vlc_array_t * p_array )
00292 {
00293 memset( p_array, 0, sizeof(vlc_array_t) );
00294 }
00295
00296 static inline void vlc_array_clear( vlc_array_t * p_array )
00297 {
00298 free( p_array->pp_elems );
00299 memset( p_array, 0, sizeof(vlc_array_t) );
00300 }
00301
00302 static inline vlc_array_t * vlc_array_new( void )
00303 {
00304 vlc_array_t * ret = (vlc_array_t *)malloc( sizeof(vlc_array_t) );
00305 if( ret ) vlc_array_init( ret );
00306 return ret;
00307 }
00308
00309 static inline void vlc_array_destroy( vlc_array_t * p_array )
00310 {
00311 if( !p_array )
00312 return;
00313 vlc_array_clear( p_array );
00314 free( p_array );
00315 }
00316
00317
00318
00319 static inline int
00320 vlc_array_count( vlc_array_t * p_array )
00321 {
00322 return p_array->i_count;
00323 }
00324
00325 static inline void *
00326 vlc_array_item_at_index( vlc_array_t * p_array, int i_index )
00327 {
00328 return p_array->pp_elems[i_index];
00329 }
00330
00331 static inline int
00332 vlc_array_index_of_item( vlc_array_t * p_array, void * item )
00333 {
00334 int i;
00335 for( i = 0; i < p_array->i_count; i++)
00336 {
00337 if( p_array->pp_elems[i] == item )
00338 return i;
00339 }
00340 return -1;
00341 }
00342
00343
00344 static inline void
00345 vlc_array_insert( vlc_array_t * p_array, void * p_elem, int i_index )
00346 {
00347 TAB_INSERT_CAST( (void **), p_array->i_count, p_array->pp_elems, p_elem, i_index );
00348 }
00349
00350 static inline void
00351 vlc_array_append( vlc_array_t * p_array, void * p_elem )
00352 {
00353 vlc_array_insert( p_array, p_elem, p_array->i_count );
00354 }
00355
00356 static inline void
00357 vlc_array_remove( vlc_array_t * p_array, int i_index )
00358 {
00359 if( i_index >= 0 )
00360 {
00361 if( p_array->i_count > 1 )
00362 {
00363 memmove( p_array->pp_elems + i_index,
00364 p_array->pp_elems + i_index+1,
00365 ( p_array->i_count - i_index - 1 ) * sizeof( void* ) );
00366 }
00367 p_array->i_count--;
00368 if( p_array->i_count == 0 )
00369 {
00370 free( p_array->pp_elems );
00371 p_array->pp_elems = NULL;
00372 }
00373 }
00374 }
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385 static inline uint64_t DictHash( const char *psz_string, int hashsize )
00386 {
00387 uint64_t i_hash = 0;
00388 if( psz_string )
00389 {
00390 while( *psz_string )
00391 {
00392 i_hash += *psz_string++;
00393 i_hash += i_hash << 10;
00394 i_hash ^= i_hash >> 8;
00395 }
00396 }
00397 return i_hash % hashsize;
00398 }
00399
00400 typedef struct vlc_dictionary_entry_t
00401 {
00402 char * psz_key;
00403 void * p_value;
00404 struct vlc_dictionary_entry_t * p_next;
00405 } vlc_dictionary_entry_t;
00406
00407 typedef struct vlc_dictionary_t
00408 {
00409 int i_size;
00410 vlc_dictionary_entry_t ** p_entries;
00411 } vlc_dictionary_t;
00412
00413 static void * const kVLCDictionaryNotFound = NULL;
00414
00415 static inline void vlc_dictionary_init( vlc_dictionary_t * p_dict, int i_size )
00416 {
00417 p_dict->p_entries = NULL;
00418
00419 if( i_size > 0 )
00420 {
00421 p_dict->p_entries = (vlc_dictionary_entry_t **)calloc( i_size, sizeof(*p_dict->p_entries) );
00422 if( !p_dict->p_entries )
00423 i_size = 0;
00424 }
00425 p_dict->i_size = i_size;
00426 }
00427
00428 static inline void vlc_dictionary_clear( vlc_dictionary_t * p_dict,
00429 void ( * pf_free )( void * p_data, void * p_obj ),
00430 void * p_obj )
00431 {
00432 if( p_dict->p_entries )
00433 {
00434 for( int i = 0; i < p_dict->i_size; i++ )
00435 {
00436 vlc_dictionary_entry_t * p_current, * p_next;
00437 p_current = p_dict->p_entries[i];
00438 while( p_current )
00439 {
00440 p_next = p_current->p_next;
00441 if( pf_free != NULL )
00442 ( * pf_free )( p_current->p_value, p_obj );
00443 free( p_current->psz_key );
00444 free( p_current );
00445 p_current = p_next;
00446 }
00447 }
00448 free( p_dict->p_entries );
00449 p_dict->p_entries = NULL;
00450 }
00451 p_dict->i_size = 0;
00452 }
00453
00454 static inline int
00455 vlc_dictionary_has_key( const vlc_dictionary_t * p_dict, const char * psz_key )
00456 {
00457 if( !p_dict->p_entries )
00458 return 0;
00459
00460 int i_pos = DictHash( psz_key, p_dict->i_size );
00461 return p_dict->p_entries[i_pos] != NULL;
00462 }
00463
00464 static inline void *
00465 vlc_dictionary_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_key )
00466 {
00467 if( !p_dict->p_entries )
00468 return kVLCDictionaryNotFound;
00469
00470 int i_pos = DictHash( psz_key, p_dict->i_size );
00471 vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos];
00472
00473 if( !p_entry )
00474 return kVLCDictionaryNotFound;
00475
00476
00477 do {
00478 if( !strcmp( psz_key, p_entry->psz_key ) )
00479 return p_entry->p_value;
00480 p_entry = p_entry->p_next;
00481 } while( p_entry );
00482
00483 return kVLCDictionaryNotFound;
00484 }
00485
00486 static inline int
00487 vlc_dictionary_keys_count( const vlc_dictionary_t * p_dict )
00488 {
00489 vlc_dictionary_entry_t * p_entry;
00490 int i, count = 0;
00491
00492 if( !p_dict->p_entries )
00493 return 0;
00494
00495 for( i = 0; i < p_dict->i_size; i++ )
00496 {
00497 for( p_entry = p_dict->p_entries[i]; p_entry; p_entry = p_entry->p_next ) count++;
00498 }
00499 return count;
00500 }
00501
00502 static inline char **
00503 vlc_dictionary_all_keys( const vlc_dictionary_t * p_dict )
00504 {
00505 vlc_dictionary_entry_t * p_entry;
00506 char ** ppsz_ret;
00507 int i, count = vlc_dictionary_keys_count( p_dict );
00508
00509 ppsz_ret = (char**)malloc(sizeof(char *) * (count + 1));
00510
00511 count = 0;
00512 for( i = 0; i < p_dict->i_size; i++ )
00513 {
00514 for( p_entry = p_dict->p_entries[i]; p_entry; p_entry = p_entry->p_next )
00515 ppsz_ret[count++] = strdup( p_entry->psz_key );
00516 }
00517 ppsz_ret[count] = NULL;
00518 return ppsz_ret;
00519 }
00520
00521 static inline void
00522 __vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key,
00523 void * p_value, bool rebuild )
00524 {
00525 if( !p_dict->p_entries )
00526 vlc_dictionary_init( p_dict, 1 );
00527
00528 int i_pos = DictHash( psz_key, p_dict->i_size );
00529 vlc_dictionary_entry_t * p_entry;
00530
00531 p_entry = (vlc_dictionary_entry_t *)malloc(sizeof(*p_entry));
00532 p_entry->psz_key = strdup( psz_key );
00533 p_entry->p_value = p_value;
00534 p_entry->p_next = p_dict->p_entries[i_pos];
00535 p_dict->p_entries[i_pos] = p_entry;
00536 if( rebuild )
00537 {
00538
00539 int count;
00540 for( count = 1; p_entry->p_next; count++ )
00541 p_entry = p_entry->p_next;
00542 if( count > 3 )
00543 {
00544
00545 struct vlc_dictionary_t new_dict;
00546 int i_new_size = ( (p_dict->i_size+2) * 3) / 2;
00547 int i;
00548 vlc_dictionary_init( &new_dict, i_new_size );
00549 for( i = 0; i < p_dict->i_size; i++ )
00550 {
00551 p_entry = p_dict->p_entries[i];
00552 while( p_entry )
00553 {
00554 __vlc_dictionary_insert( &new_dict, p_entry->psz_key,
00555 p_entry->p_value,
00556 false );
00557 p_entry = p_entry->p_next;
00558 }
00559 }
00560
00561 vlc_dictionary_clear( p_dict, NULL, NULL );
00562 p_dict->i_size = new_dict.i_size;
00563 p_dict->p_entries = new_dict.p_entries;
00564 }
00565 }
00566 }
00567
00568 static inline void
00569 vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key, void * p_value )
00570 {
00571 __vlc_dictionary_insert( p_dict, psz_key, p_value, true );
00572 }
00573
00574 static inline void
00575 vlc_dictionary_remove_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_key,
00576 void ( * pf_free )( void * p_data, void * p_obj ),
00577 void * p_obj )
00578 {
00579 if( !p_dict->p_entries )
00580 return;
00581
00582 int i_pos = DictHash( psz_key, p_dict->i_size );
00583 vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos];
00584 vlc_dictionary_entry_t * p_prev;
00585
00586 if( !p_entry )
00587 return;
00588
00589
00590 p_prev = NULL;
00591 do {
00592 if( !strcmp( psz_key, p_entry->psz_key ) )
00593 {
00594 if( pf_free != NULL )
00595 ( * pf_free )( p_entry->p_value, p_obj );
00596 if( !p_prev )
00597 p_dict->p_entries[i_pos] = p_entry->p_next;
00598 else
00599 p_prev->p_next = p_entry->p_next;
00600 free( p_entry->psz_key );
00601 free( p_entry );
00602 return;
00603 }
00604 p_prev = p_entry;
00605 p_entry = p_entry->p_next;
00606 } while( p_entry );
00607
00608
00609 }
00610
00611 #ifdef __cplusplus
00612
00613 template <typename T>
00614 void vlc_delete_all( T &container )
00615 {
00616 typename T::iterator it = container.begin();
00617 while ( it != container.end() )
00618 {
00619 delete *it;
00620 ++it;
00621 }
00622 container.clear();
00623 }
00624
00625 #endif
00626
00627 #endif