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$
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 #ifndef VLC_ARRAYS_H_
00025 #define VLC_ARRAYS_H_
00026 
00027 /**
00028  * Simple dynamic array handling. Array is realloced at each insert/removal
00029  */
00030 #if defined( _MSC_VER ) && _MSC_VER < 1300 && !defined( UNDER_CE )
00031 #   define VLCCVP (void**) /* Work-around for broken compiler */
00032 #else
00033 #   define VLCCVP
00034 #endif
00035 #define INSERT_ELEM( p_ar, i_oldsize, i_pos, elem )                           \
00036     do                                                                        \
00037     {                                                                         \
00038         if( !i_oldsize ) (p_ar) = NULL;                                       \
00039         (p_ar) = VLCCVP realloc( p_ar, ((i_oldsize) + 1) * sizeof(*(p_ar)) ); \
00040         if( (i_oldsize) - (i_pos) )                                           \
00041         {                                                                     \
00042             memmove( (p_ar) + (i_pos) + 1, (p_ar) + (i_pos),                  \
00043                      ((i_oldsize) - (i_pos)) * sizeof( *(p_ar) ) );           \
00044         }                                                                     \
00045         (p_ar)[i_pos] = elem;                                                 \
00046         (i_oldsize)++;                                                        \
00047     }                                                                         \
00048     while( 0 )
00049 
00050 #define REMOVE_ELEM( p_ar, i_oldsize, i_pos )                                 \
00051     do                                                                        \
00052     {                                                                         \
00053         if( (i_oldsize) - (i_pos) - 1 )                                       \
00054         {                                                                     \
00055             memmove( (p_ar) + (i_pos),                                        \
00056                      (p_ar) + (i_pos) + 1,                                    \
00057                      ((i_oldsize) - (i_pos) - 1) * sizeof( *(p_ar) ) );       \
00058         }                                                                     \
00059         if( i_oldsize > 1 )                                                   \
00060         {                                                                     \
00061             (p_ar) = realloc( p_ar, ((i_oldsize) - 1) * sizeof( *(p_ar) ) );  \
00062         }                                                                     \
00063         else                                                                  \
00064         {                                                                     \
00065             free( p_ar );                                                     \
00066             (p_ar) = NULL;                                                    \
00067         }                                                                     \
00068         (i_oldsize)--;                                                        \
00069     }                                                                         \
00070     while( 0 )
00071 
00072 #define TAB_INIT( count, tab )                  \
00073   do {                                          \
00074     (count) = 0;                                \
00075     (tab) = NULL;                               \
00076   } while(0)
00077 
00078 #define TAB_CLEAN( count, tab )                 \
00079   do {                                          \
00080     free( tab );                                \
00081     (count)= 0;                                 \
00082     (tab)= NULL;                                \
00083   } while(0)
00084 
00085 #define TAB_APPEND_CAST( cast, count, tab, p )             \
00086   do {                                          \
00087     if( (count) > 0 )                           \
00088         (tab) = cast realloc( tab, sizeof( void ** ) * ( (count) + 1 ) ); \
00089     else                                        \
00090         (tab) = cast malloc( sizeof( void ** ) );    \
00091     (tab)[count] = (p);                         \
00092     (count)++;                                  \
00093   } while(0)
00094 
00095 #define TAB_APPEND( count, tab, p )             \
00096     TAB_APPEND_CAST( , count, tab, p )
00097 #define TAB_APPEND_CPP( type, count, tab, p )   \
00098     TAB_APPEND_CAST( (type**), count, tab, p )
00099 
00100 #define TAB_FIND( count, tab, p, index )        \
00101   do {                                          \
00102         int _i_;                                \
00103         (index) = -1;                           \
00104         for( _i_ = 0; _i_ < (count); _i_++ )    \
00105         {                                       \
00106             if( (tab)[_i_] == (p) )             \
00107             {                                   \
00108                 (index) = _i_;                  \
00109                 break;                          \
00110             }                                   \
00111         }                                       \
00112   } while(0)
00113 
00114 
00115 #define TAB_REMOVE( count, tab, p )             \
00116   do {                                          \
00117         int _i_index_;                          \
00118         TAB_FIND( count, tab, p, _i_index_ );   \
00119         if( _i_index_ >= 0 )                    \
00120         {                                       \
00121             if( (count) > 1 )                   \
00122             {                                   \
00123                 memmove( ((void**)(tab) + _i_index_),    \
00124                          ((void**)(tab) + _i_index_+1),  \
00125                          ( (count) - _i_index_ - 1 ) * sizeof( void* ) );\
00126             }                                   \
00127             (count)--;                          \
00128             if( (count) == 0 )                  \
00129             {                                   \
00130                 free( tab );                    \
00131                 (tab) = NULL;                   \
00132             }                                   \
00133         }                                       \
00134   } while(0)
00135 
00136 #define TAB_INSERT_CAST( cast, count, tab, p, index ) do { \
00137     if( (count) > 0 )                           \
00138         (tab) = cast realloc( tab, sizeof( void ** ) * ( (count) + 1 ) ); \
00139     else                                        \
00140         (tab) = cast malloc( sizeof( void ** ) );       \
00141     if( (count) - (index) > 0 )                 \
00142         memmove( (void**)(tab) + (index) + 1,   \
00143                  (void**)(tab) + (index),       \
00144                  ((count) - (index)) * sizeof(*(tab)) );\
00145     (tab)[(index)] = (p);                       \
00146     (count)++;                                  \
00147 } while(0)
00148 
00149 #define TAB_INSERT( count, tab, p, index )      \
00150     TAB_INSERT_CAST( , count, tab, p, index )
00151 
00152 /**
00153  * Binary search in a sorted array. The key must be comparable by < and >
00154  * \param entries array of entries
00155  * \param count number of entries
00156  * \param elem key to check within an entry (like .id, or ->i_id)
00157  * \param zetype type of the key
00158  * \param key value of the key
00159  * \param answer index of answer within the array. -1 if not found
00160  */
00161 #define BSEARCH( entries, count, elem, zetype, key, answer ) \
00162    do {  \
00163     int low = 0, high = count - 1;   \
00164     answer = -1; \
00165     while( low <= high ) {\
00166         int mid = (low + high ) / 2; /* Just don't care about 2^30 tables */ \
00167         zetype mid_val = entries[mid] elem;\
00168         if( mid_val < key ) \
00169             low = mid + 1; \
00170         else if ( mid_val > key ) \
00171             high = mid -1;  \
00172         else    \
00173         {   \
00174             answer = mid;  break;   \
00175         }\
00176     } \
00177  } while(0)
00178 
00179 
00180 /************************************************************************
00181  * Dynamic arrays with progressive allocation
00182  ************************************************************************/
00183 
00184 /* Internal functions */
00185 #define _ARRAY_ALLOC(array, newsize) {                                      \
00186     array.i_alloc = newsize;                                                \
00187     array.p_elems = VLCCVP realloc( array.p_elems, array.i_alloc *          \
00188                                     sizeof(*array.p_elems) );               \
00189 }
00190 
00191 #define _ARRAY_GROW1(array) {                                               \
00192     if( array.i_alloc < 10 )                                                \
00193         _ARRAY_ALLOC(array, 10 )                                            \
00194     else if( array.i_alloc == array.i_size )                                \
00195         _ARRAY_ALLOC(array, (int)(array.i_alloc * 1.5) )                    \
00196 }
00197 
00198 #define _ARRAY_GROW(array,additional) {                                     \
00199      int i_first = array.i_alloc;                                           \
00200      while( array.i_alloc - i_first < additional )                          \
00201      {                                                                      \
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         else break;                                                         \
00207      }                                                                      \
00208 }
00209 
00210 #define _ARRAY_SHRINK(array) {                                              \
00211     if( array.i_size > 10 && array.i_size < (int)(array.i_alloc / 1.5) ) {  \
00212         _ARRAY_ALLOC(array, array.i_size + 5);                              \
00213     }                                                                       \
00214 }
00215 
00216 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
00217 
00218 /* API */
00219 #define DECL_ARRAY(type) struct {                                           \
00220     int i_alloc;                                                            \
00221     int i_size;                                                             \
00222     type *p_elems;                                                          \
00223 }
00224 
00225 #define TYPEDEF_ARRAY(type, name) typedef DECL_ARRAY(type) name;
00226 
00227 #define ARRAY_INIT(array)                                                   \
00228     array.i_alloc = 0;                                                      \
00229     array.i_size = 0;                                                       \
00230     array.p_elems = NULL;
00231 
00232 #define ARRAY_RESET(array)                                                  \
00233     array.i_alloc = 0;                                                      \
00234     array.i_size = 0;                                                       \
00235     free( array.p_elems ); array.p_elems = NULL;
00236 
00237 #define ARRAY_APPEND(array, elem) {                                         \
00238     _ARRAY_GROW1(array);                                                    \
00239     array.p_elems[array.i_size] = elem;                                     \
00240     array.i_size++;                                                         \
00241 }
00242 
00243 #define ARRAY_INSERT(array,elem,pos) {                                      \
00244     _ARRAY_GROW1(array);                                                    \
00245     if( array.i_size - pos ) {                                              \
00246         memmove( array.p_elems + pos + 1, array.p_elems + pos,              \
00247                  (array.i_size-pos) * sizeof(*array.p_elems) );             \
00248     }                                                                       \
00249     array.p_elems[pos] = elem;                                              \
00250     array.i_size++;                                                         \
00251 }
00252 
00253 #define ARRAY_REMOVE(array,pos) {                                           \
00254     if( array.i_size - (pos) - 1 )                                          \
00255     {                                                                       \
00256         memmove( array.p_elems + pos, array.p_elems + pos + 1,              \
00257                  ( array.i_size - pos - 1 ) *sizeof(*array.p_elems) );      \
00258     }                                                                       \
00259     array.i_size--;                                                         \
00260     _ARRAY_SHRINK(array);                                                   \
00261 }
00262 
00263 #define ARRAY_VAL(array, pos) array.p_elems[pos]
00264 
00265 #define ARRAY_BSEARCH(array, elem, zetype, key, answer) \
00266     BSEARCH( array.p_elems, array.i_size, elem, zetype, key, answer)
00267 
00268 #define FOREACH_ARRAY( item, array ) { \
00269     int fe_idx; \
00270     for( fe_idx = 0 ; fe_idx < array.i_size ; fe_idx++ ) \
00271     { \
00272         item = array.p_elems[fe_idx];
00273 
00274 #define FOREACH_END() } }
00275 
00276 
00277 /************************************************************************
00278  * Dynamic arrays with progressive allocation (Preferred API)
00279  ************************************************************************/
00280 typedef struct vlc_array_t
00281 {
00282     int i_count;
00283     void ** pp_elems;
00284 } vlc_array_t;
00285 
00286 static inline void vlc_array_init( vlc_array_t * p_array )
00287 {
00288     memset( p_array, 0, sizeof(vlc_array_t) );
00289 }
00290 
00291 static inline void vlc_array_clear( vlc_array_t * p_array )
00292 {
00293     free( p_array->pp_elems );
00294     memset( p_array, 0, sizeof(vlc_array_t) );
00295 }
00296 
00297 static inline vlc_array_t * vlc_array_new( void )
00298 {
00299     vlc_array_t * ret = (vlc_array_t *)malloc( sizeof(vlc_array_t) );
00300     if( ret ) vlc_array_init( ret );
00301     return ret;
00302 }
00303 
00304 static inline void vlc_array_destroy( vlc_array_t * p_array )
00305 {
00306     if( !p_array )
00307         return;
00308     vlc_array_clear( p_array );
00309     free( p_array );
00310 }
00311 
00312 
00313 /* Read */
00314 static inline int
00315 vlc_array_count( vlc_array_t * p_array )
00316 {
00317     return p_array->i_count;
00318 }
00319 
00320 static inline void *
00321 vlc_array_item_at_index( vlc_array_t * p_array, int i_index )
00322 {
00323     return p_array->pp_elems[i_index];
00324 }
00325 
00326 static inline int
00327 vlc_array_index_of_item( vlc_array_t * p_array, void * item )
00328 {
00329     int i;
00330     for( i = 0; i < p_array->i_count; i++)
00331     {
00332         if( p_array->pp_elems[i] == item )
00333             return i;
00334     }
00335     return -1;
00336 }
00337 
00338 /* Write */
00339 static inline void
00340 vlc_array_insert( vlc_array_t * p_array, void * p_elem, int i_index )
00341 {
00342     TAB_INSERT_CAST( (void **), p_array->i_count, p_array->pp_elems, p_elem, i_index );
00343 }
00344 
00345 static inline void
00346 vlc_array_append( vlc_array_t * p_array, void * p_elem )
00347 {
00348     vlc_array_insert( p_array, p_elem, p_array->i_count );
00349 }
00350 
00351 static inline void
00352 vlc_array_remove( vlc_array_t * p_array, int i_index )
00353 {
00354     if( i_index >= 0 )
00355     {
00356         if( p_array->i_count > 1 )
00357         {
00358             memmove( p_array->pp_elems + i_index,
00359                      p_array->pp_elems + i_index+1,
00360                      ( p_array->i_count - i_index - 1 ) * sizeof( void* ) );
00361         }
00362         p_array->i_count--;
00363         if( p_array->i_count == 0 )
00364         {
00365             free( p_array->pp_elems );
00366             p_array->pp_elems = NULL;
00367         }
00368     }
00369 }
00370 
00371 
00372 /************************************************************************
00373  * Dictionaries
00374  ************************************************************************/
00375 
00376 /* This function is not intended to be crypto-secure, we only want it to be
00377  * fast and not suck too much. This one is pretty fast and did 0 collisions
00378  * in wenglish's dictionary.
00379  */
00380 static inline uint64_t DictHash( const char *psz_string, int hashsize )
00381 {
00382     uint64_t i_hash = 0;
00383     if( psz_string )
00384     {
00385         while( *psz_string )
00386         {
00387             i_hash += *psz_string++;
00388             i_hash += i_hash << 10;
00389             i_hash ^= i_hash >> 8;
00390         }
00391     }
00392     return i_hash % hashsize;
00393 }
00394 
00395 struct vlc_dictionary_entry_t
00396 {
00397     char *   psz_key;
00398     void *   p_value;
00399     struct vlc_dictionary_entry_t * p_next;
00400 };
00401 
00402 typedef struct vlc_dictionary_t
00403 {
00404     int i_size;
00405     struct vlc_dictionary_entry_t ** p_entries;
00406 } vlc_dictionary_t;
00407 
00408 static void * const kVLCDictionaryNotFound = NULL;
00409 
00410 static inline void vlc_dictionary_init( vlc_dictionary_t * p_dict, int i_size )
00411 {
00412     if( i_size > 0 )
00413     {
00414         p_dict->p_entries = (struct vlc_dictionary_entry_t **)malloc(sizeof(struct vlc_dictionary_entry_t *) * i_size);
00415         memset( p_dict->p_entries, 0, sizeof(struct vlc_dictionary_entry_t *) * i_size );
00416     }
00417     else
00418         p_dict->p_entries = NULL;
00419     p_dict->i_size = i_size;
00420 }
00421 
00422 static inline void vlc_dictionary_clear( vlc_dictionary_t * p_dict )
00423 {
00424     int i;
00425     struct vlc_dictionary_entry_t * p_current, * p_next;
00426     if( p_dict->p_entries )
00427     {
00428         for( i = 0; i < p_dict->i_size; i++ )
00429         {
00430             p_current = p_dict->p_entries[i];
00431             while( p_current )
00432             {
00433                 p_next = p_current->p_next;
00434                 free( p_current->psz_key );
00435                 free( p_current );
00436                 p_current = p_next;
00437             }
00438         }
00439         free( p_dict->p_entries );
00440     }
00441     p_dict->i_size = 0;
00442 }
00443 
00444 
00445 
00446 static inline void *
00447 vlc_dictionary_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_key )
00448 {
00449     if( !p_dict->p_entries )
00450         return kVLCDictionaryNotFound;
00451 
00452     int i_pos = DictHash( psz_key, p_dict->i_size );
00453     struct vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos];
00454 
00455     if( !p_entry )
00456         return kVLCDictionaryNotFound;
00457 
00458     /* Make sure we return the right item. (Hash collision) */
00459     do {
00460         if( !strcmp( psz_key, p_entry->psz_key ) )
00461             return p_entry->p_value;
00462         p_entry = p_entry->p_next;
00463     } while( p_entry );
00464 
00465     return kVLCDictionaryNotFound;
00466 }
00467 
00468 static inline int
00469 vlc_dictionary_keys_count( const vlc_dictionary_t * p_dict )
00470 {
00471     struct vlc_dictionary_entry_t * p_entry;
00472     int i, count = 0;
00473 
00474     if( !p_dict->p_entries )
00475         return 0;
00476 
00477     for( i = 0; i < p_dict->i_size; i++ )
00478     {
00479         for( p_entry = p_dict->p_entries[i]; p_entry; p_entry = p_entry->p_next ) count++;
00480     }
00481     return count;
00482 }
00483 
00484 static inline char **
00485 vlc_dictionary_all_keys( const vlc_dictionary_t * p_dict )
00486 {
00487     struct vlc_dictionary_entry_t * p_entry;
00488     char ** ppsz_ret;
00489     int i, count = vlc_dictionary_keys_count( p_dict );
00490 
00491     ppsz_ret = (char**)malloc(sizeof(char *) * (count + 1));
00492 
00493     count = 0;
00494     for( i = 0; i < p_dict->i_size; i++ )
00495     {
00496         for( p_entry = p_dict->p_entries[i]; p_entry; p_entry = p_entry->p_next )
00497             ppsz_ret[count++] = strdup( p_entry->psz_key );
00498     }
00499     ppsz_ret[count] = NULL;
00500     return ppsz_ret;
00501 }
00502 
00503 static inline void
00504 __vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key,
00505                          void * p_value, bool rebuild )
00506 {
00507     if( !p_dict->p_entries )
00508         vlc_dictionary_init( p_dict, 1 );
00509 
00510     int i_pos = DictHash( psz_key, p_dict->i_size );
00511     struct vlc_dictionary_entry_t * p_entry;
00512 
00513     p_entry = (struct vlc_dictionary_entry_t *)malloc(sizeof(struct vlc_dictionary_entry_t));
00514     p_entry->psz_key = strdup( psz_key );
00515     p_entry->p_value = p_value;
00516     p_entry->p_next = p_dict->p_entries[i_pos];
00517     p_dict->p_entries[i_pos] = p_entry;
00518     if( rebuild )
00519     {
00520         /* Count how many items there was */
00521         int count;
00522         for( count = 1; p_entry->p_next; count++ ) p_entry = p_entry->p_next;
00523         if( count > 3 ) /* XXX: this need tuning */
00524         {
00525             /* Here it starts to be not good, rebuild a bigger dictionary */
00526             struct vlc_dictionary_t new_dict;
00527             int i_new_size = ( (p_dict->i_size+2) * 3) / 2; /* XXX: this need tuning */
00528             int i;
00529             vlc_dictionary_init( &new_dict, i_new_size );
00530             for( i = 0; i < p_dict->i_size; i++ )
00531             {
00532                 p_entry = p_dict->p_entries[i];
00533                 while( p_entry )
00534                 {
00535                     __vlc_dictionary_insert( &new_dict, p_entry->psz_key,
00536                                              p_entry->p_value,
00537                                              0 /* To avoid multiple rebuild loop */);
00538                     p_entry = p_entry->p_next;
00539                 }
00540             }
00541 
00542             vlc_dictionary_clear( p_dict );
00543             p_dict->i_size = new_dict.i_size;
00544             p_dict->p_entries = new_dict.p_entries;
00545         }
00546     }
00547 }
00548 
00549 static inline void
00550 vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key, void * p_value )
00551 {
00552     __vlc_dictionary_insert( p_dict, psz_key, p_value, 1 );
00553 }
00554 
00555 static inline void
00556 vlc_dictionary_remove_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_key )
00557 {
00558     if( !p_dict->p_entries )
00559         return;
00560 
00561     int i_pos = DictHash( psz_key, p_dict->i_size );
00562     struct vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos];
00563     struct vlc_dictionary_entry_t * p_prev;
00564 
00565     if( !p_entry )
00566         return; /* Not found, nothing to do */
00567 
00568     /* Hash collision */
00569     p_prev = NULL;
00570     do {
00571         if( !strcmp( psz_key, p_entry->psz_key ) )
00572         {
00573             if( !p_prev )
00574                 p_dict->p_entries[i_pos] = p_entry->p_next;
00575             else
00576                 p_prev->p_next = p_entry->p_next;
00577             free( p_entry->psz_key );
00578             free( p_entry );
00579             return;
00580         }
00581         p_prev = p_entry;
00582         p_entry = p_entry->p_next;
00583     } while( p_entry );
00584 
00585     /* No key was found */
00586 }
00587 
00588 #endif

Generated on Wed Aug 13 08:02:37 2008 for VLC by  doxygen 1.5.1