]> git.sesse.net Git - vlc/blob - include/vlc_arrays.h
Fixed vlc_meta_SetXXX macros
[vlc] / include / vlc_arrays.h
1 /*****************************************************************************
2  * vlc_arrays.h : Arrays and data structures handling
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
5  * $Id: vlc_playlist.h 17108 2006-10-15 15:28:34Z zorglub $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          ClĂ©ment Stenac <zorglub@videolan.org>
9  *
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.
14  *
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.
19  *
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
24 #if !defined( __LIBVLC__ )
25   #error You are not libvlc or one of its plugins. You cannot include this file
26 #endif
27
28 #ifndef _VLC_ARRAYS_H_
29 #define _VLC_ARRAYS_H_
30
31 /**
32  * Simple dynamic array handling. Array is realloced at each insert/removal
33  */
34 #if defined( _MSC_VER ) && _MSC_VER < 1300 && !defined( UNDER_CE )
35 #   define VLCCVP (void**) /* Work-around for broken compiler */
36 #else
37 #   define VLCCVP
38 #endif
39 #define INSERT_ELEM( p_ar, i_oldsize, i_pos, elem )                           \
40     do                                                                        \
41     {                                                                         \
42         if( !i_oldsize ) (p_ar) = NULL;                                       \
43         (p_ar) = VLCCVP realloc( p_ar, ((i_oldsize) + 1) * sizeof(*(p_ar)) ); \
44         if( (i_oldsize) - (i_pos) )                                           \
45         {                                                                     \
46             memmove( (p_ar) + (i_pos) + 1, (p_ar) + (i_pos),                  \
47                      ((i_oldsize) - (i_pos)) * sizeof( *(p_ar) ) );           \
48         }                                                                     \
49         (p_ar)[i_pos] = elem;                                                 \
50         (i_oldsize)++;                                                        \
51     }                                                                         \
52     while( 0 )
53
54 #define REMOVE_ELEM( p_ar, i_oldsize, i_pos )                                 \
55     do                                                                        \
56     {                                                                         \
57         if( (i_oldsize) - (i_pos) - 1 )                                       \
58         {                                                                     \
59             memmove( (p_ar) + (i_pos),                                        \
60                      (p_ar) + (i_pos) + 1,                                    \
61                      ((i_oldsize) - (i_pos) - 1) * sizeof( *(p_ar) ) );       \
62         }                                                                     \
63         if( i_oldsize > 1 )                                                   \
64         {                                                                     \
65             (p_ar) = realloc( p_ar, ((i_oldsize) - 1) * sizeof( *(p_ar) ) );  \
66         }                                                                     \
67         else                                                                  \
68         {                                                                     \
69             free( p_ar );                                                     \
70             (p_ar) = NULL;                                                    \
71         }                                                                     \
72         (i_oldsize)--;                                                        \
73     }                                                                         \
74     while( 0 )
75
76 #define TAB_INIT( count, tab )                  \
77   do {                                          \
78     (count) = 0;                                \
79     (tab) = NULL;                               \
80   } while(0)
81
82 #define TAB_CLEAN( count, tab )                 \
83   do {                                          \
84     if( tab ) free( tab );                      \
85     (count)= 0;                                 \
86     (tab)= NULL;                                \
87   } while(0)
88
89 #define TAB_APPEND_CAST( cast, count, tab, p )             \
90   do {                                          \
91     if( (count) > 0 )                           \
92         (tab) = cast realloc( tab, sizeof( void ** ) * ( (count) + 1 ) ); \
93     else                                        \
94         (tab) = cast malloc( sizeof( void ** ) );    \
95     (tab)[count] = (p);                         \
96     (count)++;                                  \
97   } while(0)
98
99 #define TAB_APPEND( count, tab, p )             \
100     TAB_APPEND_CAST( , count, tab, p )
101 #define TAB_APPEND_CPP( type, count, tab, p )   \
102     TAB_APPEND_CAST( (type**), count, tab, p )
103
104 #define TAB_FIND( count, tab, p, index )        \
105   do {                                          \
106         int _i_;                                \
107         (index) = -1;                           \
108         for( _i_ = 0; _i_ < (count); _i_++ )    \
109         {                                       \
110             if( (tab)[_i_] == (p) )             \
111             {                                   \
112                 (index) = _i_;                  \
113                 break;                          \
114             }                                   \
115         }                                       \
116   } while(0)
117
118 #define TAB_REMOVE( count, tab, p )             \
119   do {                                          \
120         int _i_index_;                          \
121         TAB_FIND( count, tab, p, _i_index_ );   \
122         if( _i_index_ >= 0 )                    \
123         {                                       \
124             if( (count) > 1 )                   \
125             {                                   \
126                 memmove( ((void**)(tab) + _i_index_),    \
127                          ((void**)(tab) + _i_index_+1),  \
128                          ( (count) - _i_index_ - 1 ) * sizeof( void* ) );\
129             }                                   \
130             (count)--;                          \
131             if( (count) == 0 )                  \
132             {                                   \
133                 free( tab );                    \
134                 (tab) = NULL;                   \
135             }                                   \
136         }                                       \
137   } while(0)
138
139 #define TAB_INSERT_CAST( cast, count, tab, p, index ) do { \
140     if( (count) > 0 )                           \
141         (tab) = cast realloc( tab, sizeof( void ** ) * ( (count) + 1 ) ); \
142     else                                        \
143         (tab) = cast malloc( sizeof( void ** ) );       \
144     if( (count) - (index) > 0 )                 \
145         memmove( (void**)(tab) + (index) + 1,   \
146                  (void**)(tab) + (index),       \
147                  ((count) - (index)) * sizeof(*(tab)) );\
148     (tab)[(index)] = (p);                       \
149     (count)++;                                  \
150 } while(0)
151
152 #define TAB_INSERT( count, tab, p, index )      \
153     TAB_INSERT_CAST( , count, tab, p, index )
154
155 /**
156  * Binary search in a sorted array. The key must be comparable by < and >
157  * \param entries array of entries
158  * \param count number of entries
159  * \param elem key to check within an entry (like .id, or ->i_id)
160  * \param zetype type of the key
161  * \param key value of the key
162  * \param answer index of answer within the array. -1 if not found
163  */
164 #define BSEARCH( entries, count, elem, zetype, key, answer ) \
165    do {  \
166     int low = 0, high = count - 1;   \
167     answer = -1; \
168     while( low <= high ) {\
169         int mid = (low + high ) / 2; /* Just don't care about 2^30 tables */ \
170         zetype mid_val = entries[mid] elem;\
171         if( mid_val < key ) \
172             low = mid + 1; \
173         else if ( mid_val > key ) \
174             high = mid -1;  \
175         else    \
176         {   \
177             answer = mid;  break;   \
178         }\
179     } \
180  } while(0)
181
182 /************************************************************************
183  * Dictionaries
184  ************************************************************************/
185
186 /* This function is not intended to be crypto-secure, we only want it to be
187  * fast and not suck too much. This one is pretty fast and did 0 collisions
188  * in wenglish's dictionary.
189  */
190 static inline uint64_t DictHash( const char *psz_string, int i_int )
191 {
192     uint64_t i_hash = 0;
193     if( psz_string )
194     {
195         while( *psz_string )
196         {
197             i_hash += *psz_string++;
198             i_hash += i_hash << 10;
199             i_hash ^= i_hash >> 8;
200         }
201     }
202     return i_hash + ( (uint64_t)i_int << 32 );
203 }
204
205 #define DICT_TYPE(name,type)                                                  \
206     typedef struct dict_entry_##name##_t {                                    \
207         int i_int;                                                            \
208         char *psz_string;                                                     \
209         uint64_t i_hash;                                                      \
210         type data;                                                            \
211     } dict_entry_##name##_t;                                                  \
212     typedef struct dict_##name##_t {                                          \
213         dict_entry_##name##_t *p_entries;                                     \
214         int i_entries;                                                        \
215     } dict_##name##_t;
216
217 #define DICT_NEW( p_dict ) {                                                  \
218     p_dict = malloc( sizeof(int)+sizeof(void*) );                             \
219     p_dict->i_entries = 0;                                                    \
220     p_dict->p_entries = NULL;                                                 \
221 }
222
223 #define DICT_CLEAR( zdict ) {                                                 \
224     int _i_dict = 0;                                                          \
225     for ( _i_dict = 0; _i_dict < zdict->i_entries; _i_dict++ )                \
226     {                                                                         \
227         FREE( zdict->p_entries[_i_dict].psz_string );                         \
228     }                                                                         \
229     FREE( zdict->p_entries );                                                 \
230     free( zdict );                                                            \
231 }
232
233 #define DICT_INSERT( zdict, zint, zstring, zdata ) {                          \
234     uint64_t i_hash = DictHash( (zstring), (zint) );                          \
235     int i_new;                                                                \
236     /* Find a free slot */                                                    \
237     if( zdict->i_entries == 0 || i_hash <= zdict->p_entries[0].i_hash )       \
238         i_new = 0;                                                            \
239     else if( i_hash >= zdict->p_entries[zdict->i_entries-1].i_hash )          \
240         i_new = zdict->i_entries;\
241     else                                                                      \
242     {                                                                         \
243         int i_low = 0, i_high = zdict->i_entries - 1;                         \
244         while( i_high - i_low > 1 )                                           \
245         {                                                                     \
246             int i_mid = (i_low + i_high)/2;                                   \
247             fprintf(stderr, "Low %i, high %i\n", i_low, i_high);              \
248             if( zdict->p_entries[i_mid].i_hash < i_hash ) {                   \
249                 i_low = i_mid;                                                \
250             } else if( zdict->p_entries[i_mid].i_hash > i_hash ) {            \
251                 i_high = i_mid;                                               \
252             }                                                                 \
253         }                                                                     \
254         if( zdict->p_entries[i_low].i_hash < i_hash )                         \
255             i_new = i_high;                                                   \
256         else                                                                  \
257             i_new = i_low;                                                    \
258     }                                                                         \
259     zdict->p_entries = realloc( zdict->p_entries, (zdict->i_entries + 1) *    \
260         ( sizeof(zdata) + sizeof(int) + sizeof(void*) + sizeof(uint64_t) ) ); \
261     zdict->i_entries++;                                                       \
262     if( i_new != zdict->i_entries -1 )                                        \
263         memmove( &zdict->p_entries[i_new+1], &zdict->p_entries[i_new],        \
264          ( zdict->i_entries - i_new - 1 ) *                                   \
265          ( sizeof(zdata) + sizeof(int) + sizeof(void*) + sizeof(uint64_t) ) );\
266                                                                               \
267     zdict->p_entries[i_new].i_hash = i_hash;                                  \
268     zdict->p_entries[i_new].i_int = (zint);                                   \
269     if( (zstring) ) {                                                         \
270         zdict->p_entries[i_new].psz_string = strdup( (zstring) );             \
271     } else {                                                                  \
272         zdict->p_entries[i_new].psz_string = NULL;                            \
273     }                                                                         \
274     zdict->p_entries[i_new].data = zdata;                                     \
275 }
276
277 #define DICT_LOOKUP( zdict, zint, zstring, answer ) do {                      \
278     uint64_t i_hash;                                                          \
279     int i, i_pos;                                                             \
280     vlc_bool_t b_found = VLC_FALSE;                                           \
281     if( zdict->i_entries == 0 ) {                                             \
282         answer = -1;                                                          \
283         break;                                                                \
284     }                                                                         \
285                                                                               \
286     i_hash = DictHash( (zstring), (zint) );                                   \
287     BSEARCH( zdict->p_entries, zdict->i_entries, .i_hash, uint64_t,           \
288              i_hash, i_pos );                                                 \
289     if( i_pos == -1 ) {                                                       \
290         answer = -1;                                                          \
291         break;                                                                \
292     }                                                                         \
293                                                                               \
294     /* Hash found, let's check it looks like the entry */                     \
295     if( !strcmp( (zstring), zdict->p_entries[i_pos].psz_string ) ) {          \
296         answer = i_pos;                                                       \
297         break;                                                                \
298     }                                                                         \
299                                                                               \
300     /* Hash collision! This should be very rare, but we cannot guarantee      \
301      * it will never happen. Just do an exhaustive search amongst all         \
302      * entries with the same hash. */                                         \
303     for( i = i_pos - 1 ; i > 0 && i_hash == zdict->p_entries[i].i_hash ; i-- )\
304     {                                                                         \
305         if( !strcmp( (zstring), zdict->p_entries[i].psz_string ) &&           \
306                    zdict->p_entries[i].i_int == (zint) ) {                    \
307             b_found = VLC_TRUE;                                               \
308             answer = i;                                                       \
309             break;                                                            \
310         }                                                                     \
311     }                                                                         \
312     if( b_found == VLC_TRUE )                                                 \
313         break;                                                                \
314     for( i = i_pos + 1 ; i < zdict->i_entries &&                              \
315                          i_hash == zdict->p_entries[i].i_hash ; i++ )         \
316     {                                                                         \
317          if( !strcmp( (zstring), zdict->p_entries[i].psz_string ) &&          \
318                       zdict->p_entries[i].i_int == (zint) ) {                 \
319             b_found = VLC_TRUE;                                               \
320             answer = i;                                                       \
321             break;                                                            \
322         }                                                                     \
323     }                                                                         \
324     /* Hash found, but entry not found (quite strange !) */                   \
325     assert( 0 );                                                              \
326 } while(0)
327
328 #define DICT_GET( zdict, i_int, psz_string, answer ) {                        \
329     int int_answer;                                                           \
330     DICT_LOOKUP( zdict, i_int, psz_string, int_answer );                      \
331     if( int_answer >=  0 )                                                    \
332         answer = zdict->p_entries[int_answer].data;                           \
333 }
334
335 /************************************************************************
336  * Dynamic arrays with progressive allocation
337  ************************************************************************/
338
339 /* Internal functions */
340 #define _ARRAY_ALLOC(array, newsize) {                                      \
341     array.i_alloc = newsize;                                                \
342     array.p_elems = VLCCVP realloc( array.p_elems, array.i_alloc *          \
343                                     sizeof(*array.p_elems) );               \
344     assert(array.p_elems);                                                  \
345 }
346
347 #define _ARRAY_GROW1(array) {                                               \
348     if( array.i_alloc < 10 )                                                \
349         _ARRAY_ALLOC(array, 10 )                                            \
350     else if( array.i_alloc == array.i_size )                                \
351         _ARRAY_ALLOC(array, (int)(array.i_alloc * 1.5) )                    \
352 }
353
354 #define _ARRAY_GROW(array,additional) {                                     \
355      int i_first = array.i_alloc;                                           \
356      while( array.i_alloc - i_first < additional )                          \
357      {                                                                      \
358          if( array.i_alloc < 10 )                                           \
359             _ARRAY_ALLOC(array, 10 )                                        \
360         else if( array.i_alloc == array.i_size )                            \
361             _ARRAY_ALLOC(array, (int)(array.i_alloc * 1.5) )                \
362         else break;                                                         \
363      }                                                                      \
364 }
365
366 #define _ARRAY_SHRINK(array) {                                              \
367     if( array.i_size > 10 && array.i_size < (int)(array.i_alloc / 1.5) ) {  \
368         _ARRAY_ALLOC(array, array.i_size + 5);                              \
369     }                                                                       \
370 }
371
372
373 /* API */
374 #define DECL_ARRAY(type) struct {                                           \
375     int i_alloc;                                                            \
376     int i_size;                                                             \
377     type *p_elems;                                                          \
378 }
379
380 #define TYPEDEF_ARRAY(type, name) typedef DECL_ARRAY(type) name;
381
382 #define ARRAY_INIT(array)                                                   \
383     array.i_alloc = 0;                                                      \
384     array.i_size = 0;                                                       \
385     array.p_elems = NULL;
386
387 #define ARRAY_RESET(array)                                                  \
388     array.i_alloc = 0;                                                      \
389     array.i_size = 0;                                                       \
390     free( array.p_elems ); array.p_elems = NULL;
391
392 #define ARRAY_APPEND(array, elem) {                                         \
393     _ARRAY_GROW1(array);                                                    \
394     array.p_elems[array.i_size] = elem;                                     \
395     array.i_size++;                                                         \
396 }
397
398 #define ARRAY_INSERT(array,elem,pos) {                                      \
399     _ARRAY_GROW1(array);                                                    \
400     if( array.i_size - pos ) {                                              \
401         memmove( array.p_elems + pos + 1, array.p_elems + pos,              \
402                  (array.i_size-pos) * sizeof(*array.p_elems) );             \
403     }                                                                       \
404     array.p_elems[pos] = elem;                                              \
405     array.i_size++;                                                         \
406 }
407
408 #define ARRAY_REMOVE(array,pos) {                                           \
409     if( array.i_size - (pos) - 1 )                                          \
410     {                                                                       \
411         memmove( array.p_elems + pos, array.p_elems + pos + 1,              \
412                  ( array.i_size - pos - 1 ) *sizeof(*array.p_elems) );      \
413     }                                                                       \
414     array.i_size--;                                                         \
415     _ARRAY_SHRINK(array);                                                   \
416 }
417
418 #define ARRAY_VAL(array, pos) array.p_elems[pos]
419
420 #define ARRAY_BSEARCH(array, elem, zetype, key, answer) \
421     BSEARCH( array.p_elems, array.i_size, elem, zetype, key, answer)
422
423 #define FOREACH_ARRAY( item, array ) { \
424     int fe_idx; \
425     for( fe_idx = 0 ; fe_idx < array.i_size ; fe_idx++ ) \
426     { \
427         item = array.p_elems[fe_idx];
428
429 #define FOREACH_END() } }
430
431 #endif