vlc_arrays.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * vlc_arrays.h : Arrays and data structures handling
00003  *****************************************************************************
00004  * Copyright (C) 1999-2004 the VideoLAN team
00005  * $Id: 390c1b6d9df4d275619b9c62945d352e063b4229 $
00006  *
00007  * Authors: Samuel Hocevar <sam@zoy.org>
00008  *          Clément Stenac <zorglub@videolan.org>
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
00023  *****************************************************************************/
00024 
00025 #ifndef VLC_ARRAYS_H_
00026 #define VLC_ARRAYS_H_
00027 
00028 /**
00029  * \file
00030  * This file defines functions, structures and macros for handling arrays in vlc
00031  */
00032 
00033 /* realloc() that never fails *if* downsizing */
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  * Simple dynamic array handling. Array is realloced at each insert/removal
00042  */
00043 #if defined( _MSC_VER ) && _MSC_VER < 1300 && !defined( UNDER_CE )
00044 #   define VLCCVP (void**) /* Work-around for broken compiler */
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 #define TAB_APPEND_CPP( type, count, tab, p )   \
00111     TAB_APPEND_CAST( (type**), count, tab, p )
00112 
00113 #define TAB_FIND( count, tab, p, index )        \
00114   do {                                          \
00115         int _i_;                                \
00116         (index) = -1;                           \
00117         for( _i_ = 0; _i_ < (count); _i_++ )    \
00118         {                                       \
00119             if( (tab)[_i_] == (p) )             \
00120             {                                   \
00121                 (index) = _i_;                  \
00122                 break;                          \
00123             }                                   \
00124         }                                       \
00125   } while(0)
00126 
00127 
00128 #define TAB_REMOVE( count, tab, p )             \
00129   do {                                          \
00130         int _i_index_;                          \
00131         TAB_FIND( count, tab, p, _i_index_ );   \
00132         if( _i_index_ >= 0 )                    \
00133         {                                       \
00134             if( (count) > 1 )                   \
00135             {                                   \
00136                 memmove( ((void**)(tab) + _i_index_),    \
00137                          ((void**)(tab) + _i_index_+1),  \
00138                          ( (count) - _i_index_ - 1 ) * sizeof( void* ) );\
00139             }                                   \
00140             (count)--;                          \
00141             if( (count) == 0 )                  \
00142             {                                   \
00143                 free( tab );                    \
00144                 (tab) = NULL;                   \
00145             }                                   \
00146         }                                       \
00147   } while(0)
00148 
00149 #define TAB_INSERT_CAST( cast, count, tab, p, index ) do { \
00150     if( (count) > 0 )                           \
00151         (tab) = cast realloc( tab, sizeof( void ** ) * ( (count) + 1 ) ); \
00152     else                                        \
00153         (tab) = cast malloc( sizeof( void ** ) );       \
00154     if( !(tab) ) abort();                       \
00155     if( (count) - (index) > 0 )                 \
00156         memmove( (void**)(tab) + (index) + 1,   \
00157                  (void**)(tab) + (index),       \
00158                  ((count) - (index)) * sizeof(*(tab)) );\
00159     (tab)[(index)] = (p);                       \
00160     (count)++;                                  \
00161 } while(0)
00162 
00163 #define TAB_INSERT( count, tab, p, index )      \
00164     TAB_INSERT_CAST( , count, tab, p, index )
00165 
00166 /**
00167  * Binary search in a sorted array. The key must be comparable by < and >
00168  * \param entries array of entries
00169  * \param count number of entries
00170  * \param elem key to check within an entry (like .id, or ->i_id)
00171  * \param zetype type of the key
00172  * \param key value of the key
00173  * \param answer index of answer within the array. -1 if not found
00174  */
00175 #define BSEARCH( entries, count, elem, zetype, key, answer ) \
00176    do {  \
00177     int low = 0, high = count - 1;   \
00178     answer = -1; \
00179     while( low <= high ) {\
00180         int mid = (low + high ) / 2; /* Just don't care about 2^30 tables */ \
00181         zetype mid_val = entries[mid] elem;\
00182         if( mid_val < key ) \
00183             low = mid + 1; \
00184         else if ( mid_val > key ) \
00185             high = mid -1;  \
00186         else    \
00187         {   \
00188             answer = mid;  break;   \
00189         }\
00190     } \
00191  } while(0)
00192 
00193 
00194 /************************************************************************
00195  * Dynamic arrays with progressive allocation
00196  ************************************************************************/
00197 
00198 /* Internal functions */
00199 #define _ARRAY_ALLOC(array, newsize) {                                      \
00200     (array).i_alloc = newsize;                                              \
00201     (array).p_elems = VLCCVP realloc( (array).p_elems, (array).i_alloc *    \
00202                                     sizeof(*(array).p_elems) );             \
00203     if( !(array).p_elems ) abort();                                         \
00204 }
00205 
00206 #define _ARRAY_GROW1(array) {                                               \
00207     if( (array).i_alloc < 10 )                                              \
00208         _ARRAY_ALLOC(array, 10 )                                            \
00209     else if( (array).i_alloc == (array).i_size )                            \
00210         _ARRAY_ALLOC(array, (int)(array.i_alloc * 1.5) )                    \
00211 }
00212 
00213 #define _ARRAY_GROW(array,additional) {                                     \
00214      int i_first = (array).i_alloc;                                         \
00215      while( (array).i_alloc - i_first < additional )                        \
00216      {                                                                      \
00217          if( (array).i_alloc < 10 )                                         \
00218             _ARRAY_ALLOC(array, 10 )                                        \
00219         else if( (array).i_alloc == (array).i_size )                        \
00220             _ARRAY_ALLOC(array, (int)((array).i_alloc * 1.5) )              \
00221         else break;                                                         \
00222      }                                                                      \
00223 }
00224 
00225 #define _ARRAY_SHRINK(array) {                                              \
00226     if( (array).i_size > 10 && (array).i_size < (int)((array).i_alloc / 1.5) ) {  \
00227         _ARRAY_ALLOC(array, (array).i_size + 5);                            \
00228     }                                                                       \
00229 }
00230 
00231 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
00232 
00233 /* API */
00234 #define DECL_ARRAY(type) struct {                                           \
00235     int i_alloc;                                                            \
00236     int i_size;                                                             \
00237     type *p_elems;                                                          \
00238 }
00239 
00240 #define TYPEDEF_ARRAY(type, name) typedef DECL_ARRAY(type) name;
00241 
00242 #define ARRAY_INIT(array)                                                   \
00243   do {                                                                      \
00244     (array).i_alloc = 0;                                                    \
00245     (array).i_size = 0;                                                     \
00246     (array).p_elems = NULL;                                                 \
00247   } while(0)
00248 
00249 #define ARRAY_RESET(array)                                                  \
00250   do {                                                                      \
00251     (array).i_alloc = 0;                                                    \
00252     (array).i_size = 0;                                                     \
00253     free( (array).p_elems ); (array).p_elems = NULL;                        \
00254   } while(0)
00255 
00256 #define ARRAY_APPEND(array, elem)                                           \
00257   do {                                                                      \
00258     _ARRAY_GROW1(array);                                                    \
00259     (array).p_elems[(array).i_size] = elem;                                 \
00260     (array).i_size++;                                                       \
00261   } while(0)
00262 
00263 #define ARRAY_INSERT(array,elem,pos)                                        \
00264   do {                                                                      \
00265     _ARRAY_GROW1(array);                                                    \
00266     if( (array).i_size - pos ) {                                            \
00267         memmove( (array).p_elems + pos + 1, (array).p_elems + pos,          \
00268                  ((array).i_size-pos) * sizeof(*(array).p_elems) );         \
00269     }                                                                       \
00270     (array).p_elems[pos] = elem;                                            \
00271     (array).i_size++;                                                       \
00272   } while(0)
00273 
00274 #define ARRAY_REMOVE(array,pos)                                             \
00275   do {                                                                      \
00276     if( (array).i_size - (pos) - 1 )                                        \
00277     {                                                                       \
00278         memmove( (array).p_elems + pos, (array).p_elems + pos + 1,          \
00279                  ( (array).i_size - pos - 1 ) *sizeof(*(array).p_elems) );  \
00280     }                                                                       \
00281     (array).i_size--;                                                       \
00282     _ARRAY_SHRINK(array);                                                   \
00283   } while(0)
00284 
00285 #define ARRAY_VAL(array, pos) array.p_elems[pos]
00286 
00287 #define ARRAY_BSEARCH(array, elem, zetype, key, answer) \
00288     BSEARCH( (array).p_elems, (array).i_size, elem, zetype, key, answer)
00289 
00290 #define FOREACH_ARRAY( item, array ) { \
00291     int fe_idx; \
00292     for( fe_idx = 0 ; fe_idx < (array).i_size ; fe_idx++ ) \
00293     { \
00294         item = (array).p_elems[fe_idx];
00295 
00296 #define FOREACH_END() } }
00297 
00298 
00299 /************************************************************************
00300  * Dynamic arrays with progressive allocation (Preferred API)
00301  ************************************************************************/
00302 typedef struct vlc_array_t
00303 {
00304     int i_count;
00305     void ** pp_elems;
00306 } vlc_array_t;
00307 
00308 static inline void vlc_array_init( vlc_array_t * p_array )
00309 {
00310     memset( p_array, 0, sizeof(vlc_array_t) );
00311 }
00312 
00313 static inline void vlc_array_clear( vlc_array_t * p_array )
00314 {
00315     free( p_array->pp_elems );
00316     memset( p_array, 0, sizeof(vlc_array_t) );
00317 }
00318 
00319 static inline vlc_array_t * vlc_array_new( void )
00320 {
00321     vlc_array_t * ret = (vlc_array_t *)malloc( sizeof(vlc_array_t) );
00322     if( ret ) vlc_array_init( ret );
00323     return ret;
00324 }
00325 
00326 static inline void vlc_array_destroy( vlc_array_t * p_array )
00327 {
00328     if( !p_array )
00329         return;
00330     vlc_array_clear( p_array );
00331     free( p_array );
00332 }
00333 
00334 
00335 /* Read */
00336 static inline int
00337 vlc_array_count( vlc_array_t * p_array )
00338 {
00339     return p_array->i_count;
00340 }
00341 
00342 static inline void *
00343 vlc_array_item_at_index( vlc_array_t * p_array, int i_index )
00344 {
00345     return p_array->pp_elems[i_index];
00346 }
00347 
00348 static inline int
00349 vlc_array_index_of_item( vlc_array_t * p_array, void * item )
00350 {
00351     int i;
00352     for( i = 0; i < p_array->i_count; i++)
00353     {
00354         if( p_array->pp_elems[i] == item )
00355             return i;
00356     }
00357     return -1;
00358 }
00359 
00360 /* Write */
00361 static inline void
00362 vlc_array_insert( vlc_array_t * p_array, void * p_elem, int i_index )
00363 {
00364     TAB_INSERT_CAST( (void **), p_array->i_count, p_array->pp_elems, p_elem, i_index );
00365 }
00366 
00367 static inline void
00368 vlc_array_append( vlc_array_t * p_array, void * p_elem )
00369 {
00370     vlc_array_insert( p_array, p_elem, p_array->i_count );
00371 }
00372 
00373 static inline void
00374 vlc_array_remove( vlc_array_t * p_array, int i_index )
00375 {
00376     if( i_index >= 0 )
00377     {
00378         if( p_array->i_count > 1 )
00379         {
00380             memmove( p_array->pp_elems + i_index,
00381                      p_array->pp_elems + i_index+1,
00382                      ( p_array->i_count - i_index - 1 ) * sizeof( void* ) );
00383         }
00384         p_array->i_count--;
00385         if( p_array->i_count == 0 )
00386         {
00387             free( p_array->pp_elems );
00388             p_array->pp_elems = NULL;
00389         }
00390     }
00391 }
00392 
00393 
00394 /************************************************************************
00395  * Dictionaries
00396  ************************************************************************/
00397 
00398 /* This function is not intended to be crypto-secure, we only want it to be
00399  * fast and not suck too much. This one is pretty fast and did 0 collisions
00400  * in wenglish's dictionary.
00401  */
00402 static inline uint64_t DictHash( const char *psz_string, int hashsize )
00403 {
00404     uint64_t i_hash = 0;
00405     if( psz_string )
00406     {
00407         while( *psz_string )
00408         {
00409             i_hash += *psz_string++;
00410             i_hash += i_hash << 10;
00411             i_hash ^= i_hash >> 8;
00412         }
00413     }
00414     return i_hash % hashsize;
00415 }
00416 
00417 typedef struct vlc_dictionary_entry_t
00418 {
00419     char *   psz_key;
00420     void *   p_value;
00421     struct vlc_dictionary_entry_t * p_next;
00422 } vlc_dictionary_entry_t;
00423 
00424 typedef struct vlc_dictionary_t
00425 {
00426     int i_size;
00427     vlc_dictionary_entry_t ** p_entries;
00428 } vlc_dictionary_t;
00429 
00430 static void * const kVLCDictionaryNotFound = NULL;
00431 
00432 static inline void vlc_dictionary_init( vlc_dictionary_t * p_dict, int i_size )
00433 {
00434     p_dict->p_entries = NULL;
00435 
00436     if( i_size > 0 )
00437     {
00438         p_dict->p_entries = (vlc_dictionary_entry_t **)calloc( i_size, sizeof(*p_dict->p_entries) );
00439         if( !p_dict->p_entries )
00440             i_size = 0;
00441     }
00442     p_dict->i_size = i_size;
00443 }
00444 
00445 static inline void vlc_dictionary_clear( vlc_dictionary_t * p_dict,
00446                                          void ( * pf_free )( void * p_data, void * p_obj ),
00447                                          void * p_obj )
00448 {
00449     if( p_dict->p_entries )
00450     {
00451         for( int i = 0; i < p_dict->i_size; i++ )
00452         {
00453             vlc_dictionary_entry_t * p_current, * p_next;
00454             p_current = p_dict->p_entries[i];
00455             while( p_current )
00456             {
00457                 p_next = p_current->p_next;
00458                 if( pf_free != NULL )
00459                     ( * pf_free )( p_current->p_value, p_obj );
00460                 free( p_current->psz_key );
00461                 free( p_current );
00462                 p_current = p_next;
00463             }
00464         }
00465         free( p_dict->p_entries );
00466         p_dict->p_entries = NULL;
00467     }
00468     p_dict->i_size = 0;
00469 }
00470 
00471 
00472 
00473 static inline void *
00474 vlc_dictionary_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_key )
00475 {
00476     if( !p_dict->p_entries )
00477         return kVLCDictionaryNotFound;
00478 
00479     int i_pos = DictHash( psz_key, p_dict->i_size );
00480     vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos];
00481 
00482     if( !p_entry )
00483         return kVLCDictionaryNotFound;
00484 
00485     /* Make sure we return the right item. (Hash collision) */
00486     do {
00487         if( !strcmp( psz_key, p_entry->psz_key ) )
00488             return p_entry->p_value;
00489         p_entry = p_entry->p_next;
00490     } while( p_entry );
00491 
00492     return kVLCDictionaryNotFound;
00493 }
00494 
00495 static inline int
00496 vlc_dictionary_keys_count( const vlc_dictionary_t * p_dict )
00497 {
00498     vlc_dictionary_entry_t * p_entry;
00499     int i, count = 0;
00500 
00501     if( !p_dict->p_entries )
00502         return 0;
00503 
00504     for( i = 0; i < p_dict->i_size; i++ )
00505     {
00506         for( p_entry = p_dict->p_entries[i]; p_entry; p_entry = p_entry->p_next ) count++;
00507     }
00508     return count;
00509 }
00510 
00511 static inline char **
00512 vlc_dictionary_all_keys( const vlc_dictionary_t * p_dict )
00513 {
00514     vlc_dictionary_entry_t * p_entry;
00515     char ** ppsz_ret;
00516     int i, count = vlc_dictionary_keys_count( p_dict );
00517 
00518     ppsz_ret = (char**)malloc(sizeof(char *) * (count + 1));
00519 
00520     count = 0;
00521     for( i = 0; i < p_dict->i_size; i++ )
00522     {
00523         for( p_entry = p_dict->p_entries[i]; p_entry; p_entry = p_entry->p_next )
00524             ppsz_ret[count++] = strdup( p_entry->psz_key );
00525     }
00526     ppsz_ret[count] = NULL;
00527     return ppsz_ret;
00528 }
00529 
00530 static inline void
00531 __vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key,
00532                          void * p_value, bool rebuild )
00533 {
00534     if( !p_dict->p_entries )
00535         vlc_dictionary_init( p_dict, 1 );
00536 
00537     int i_pos = DictHash( psz_key, p_dict->i_size );
00538     vlc_dictionary_entry_t * p_entry;
00539 
00540     p_entry = (vlc_dictionary_entry_t *)malloc(sizeof(*p_entry));
00541     p_entry->psz_key = strdup( psz_key );
00542     p_entry->p_value = p_value;
00543     p_entry->p_next = p_dict->p_entries[i_pos];
00544     p_dict->p_entries[i_pos] = p_entry;
00545     if( rebuild )
00546     {
00547         /* Count how many items there was */
00548         int count;
00549         for( count = 1; p_entry->p_next; count++ )
00550             p_entry = p_entry->p_next;
00551         if( count > 3 ) /* XXX: this need tuning */
00552         {
00553             /* Here it starts to be not good, rebuild a bigger dictionary */
00554             struct vlc_dictionary_t new_dict;
00555             int i_new_size = ( (p_dict->i_size+2) * 3) / 2; /* XXX: this need tuning */
00556             int i;
00557             vlc_dictionary_init( &new_dict, i_new_size );
00558             for( i = 0; i < p_dict->i_size; i++ )
00559             {
00560                 p_entry = p_dict->p_entries[i];
00561                 while( p_entry )
00562                 {
00563                     __vlc_dictionary_insert( &new_dict, p_entry->psz_key,
00564                                              p_entry->p_value,
00565                                              false /* To avoid multiple rebuild loop */);
00566                     p_entry = p_entry->p_next;
00567                 }
00568             }
00569 
00570             vlc_dictionary_clear( p_dict, NULL, NULL );
00571             p_dict->i_size = new_dict.i_size;
00572             p_dict->p_entries = new_dict.p_entries;
00573         }
00574     }
00575 }
00576 
00577 static inline void
00578 vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key, void * p_value )
00579 {
00580     __vlc_dictionary_insert( p_dict, psz_key, p_value, true );
00581 }
00582 
00583 static inline void
00584 vlc_dictionary_remove_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_key,
00585                                      void ( * pf_free )( void * p_data, void * p_obj ),
00586                                      void * p_obj )
00587 {
00588     if( !p_dict->p_entries )
00589         return;
00590 
00591     int i_pos = DictHash( psz_key, p_dict->i_size );
00592     vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos];
00593     vlc_dictionary_entry_t * p_prev;
00594 
00595     if( !p_entry )
00596         return; /* Not found, nothing to do */
00597 
00598     /* Hash collision */
00599     p_prev = NULL;
00600     do {
00601         if( !strcmp( psz_key, p_entry->psz_key ) )
00602         {
00603             if( pf_free != NULL )
00604                 ( * pf_free )( p_entry->p_value, p_obj );
00605             if( !p_prev )
00606                 p_dict->p_entries[i_pos] = p_entry->p_next;
00607             else
00608                 p_prev->p_next = p_entry->p_next;
00609             free( p_entry->psz_key );
00610             free( p_entry );
00611             return;
00612         }
00613         p_prev = p_entry;
00614         p_entry = p_entry->p_next;
00615     } while( p_entry );
00616 
00617     /* No key was found */
00618 }
00619 
00620 #endif

Generated on Sat Nov 21 08:05:14 2009 for VLC by  doxygen 1.5.6