]> git.sesse.net Git - vlc/blob - include/vlc_arrays.h
Added missing const qualifier to vod_MediaControl.
[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( count, tab, p )             \
90   do {                                          \
91     if( (count) > 0 )                           \
92         (tab) = realloc( tab, sizeof( void ** ) * ( (count) + 1 ) ); \
93     else                                        \
94         (tab) = malloc( sizeof( void ** ) );    \
95     (tab)[count] = (p);                         \
96     (count)++;                                  \
97   } while(0)
98
99 #define TAB_FIND( count, tab, p, index )        \
100   do {                                          \
101         int _i_;                                \
102         (index) = -1;                           \
103         for( _i_ = 0; _i_ < (count); _i_++ )    \
104         {                                       \
105             if( (tab)[_i_] == (p) )             \
106             {                                   \
107                 (index) = _i_;                  \
108                 break;                          \
109             }                                   \
110         }                                       \
111   } while(0)
112
113 #define TAB_REMOVE( count, tab, p )             \
114   do {                                          \
115         int _i_index_;                          \
116         TAB_FIND( count, tab, p, _i_index_ );   \
117         if( _i_index_ >= 0 )                    \
118         {                                       \
119             if( (count) > 1 )                   \
120             {                                   \
121                 memmove( ((void**)(tab) + _i_index_),    \
122                          ((void**)(tab) + _i_index_+1),  \
123                          ( (count) - _i_index_ - 1 ) * sizeof( void* ) );\
124             }                                   \
125             (count)--;                          \
126             if( (count) == 0 )                  \
127             {                                   \
128                 free( tab );                    \
129                 (tab) = NULL;                   \
130             }                                   \
131         }                                       \
132   } while(0)
133
134 /**
135  * Binary search in a sorted array. The key must be comparable by < and >
136  * \param entries array of entries
137  * \param count number of entries
138  * \param elem key to check within an entry (like .id, or ->i_id)
139  * \param zetype type of the key
140  * \param key value of the key
141  * \param answer index of answer within the array. -1 if not found
142  */
143 #define BSEARCH( entries, count, elem, zetype, key, answer ) \
144    do {  \
145     int low = 0, high = count - 1;   \
146     answer = -1; \
147     while( low <= high ) {\
148         int mid = (low + high ) / 2; /* Just don't care about 2^30 tables */ \
149         zetype mid_val = entries[mid] elem;\
150         if( mid_val < key ) \
151             low = mid + 1; \
152         else if ( mid_val > key ) \
153             high = mid -1;  \
154         else    \
155         {   \
156             answer = mid;  break;   \
157         }\
158     } \
159  } while(0)
160
161 /************************************************************************
162  * Dictionaries
163  ************************************************************************/
164
165 /* This function is not intended to be crypto-secure, we only want it to be
166  * fast and not suck too much. This one is pretty fast and did 0 collisions
167  * in wenglish's dictionary.
168  */
169 static inline uint64_t DictHash( const char *psz_string, int i_int )
170 {
171     uint64_t i_hash = 0;
172     if( psz_string )
173     {
174         while( *psz_string )
175         {
176             i_hash += *psz_string++;
177             i_hash += i_hash << 10;
178             i_hash ^= i_hash >> 8;
179         }
180     }
181     return i_hash + ( (uint64_t)i_int << 32 );
182 }
183
184 #define DICT_TYPE(name,type)                                                  \
185     typedef struct dict_entry_##name##_t {                                    \
186         int i_int;                                                            \
187         char *psz_string;                                                     \
188         uint64_t i_hash;                                                      \
189         type data;                                                            \
190     } dict_entry_##name##_t;                                                  \
191     typedef struct dict_##name##_t {                                          \
192         dict_entry_##name##_t *p_entries;                                     \
193         int i_entries;                                                        \
194     } dict_##name##_t;
195
196 #define DICT_NEW( p_dict ) {                                                  \
197     p_dict = malloc( sizeof(int)+sizeof(void*) );                             \
198     p_dict->i_entries = 0;                                                    \
199     p_dict->p_entries = NULL;                                                 \
200 }
201
202 #define DICT_CLEAR( zdict ) {                                                 \
203     int _i_dict = 0;                                                          \
204     for ( _i_dict = 0; _i_dict < zdict->i_entries; _i_dict++ )                \
205     {                                                                         \
206         FREE( zdict->p_entries[_i_dict].psz_string );                         \
207     }                                                                         \
208     FREE( zdict->p_entries );                                                 \
209     free( zdict );                                                            \
210 }
211
212 #define DICT_INSERT( zdict, zint, zstring, zdata ) {                          \
213     uint64_t i_hash = DictHash( (zstring), (zint) );                          \
214     int i_new;                                                                \
215     /* Find a free slot */                                                    \
216     if( zdict->i_entries == 0 || i_hash <= zdict->p_entries[0].i_hash )       \
217         i_new = 0;                                                            \
218     else if( i_hash >= zdict->p_entries[zdict->i_entries-1].i_hash )          \
219         i_new = zdict->i_entries;\
220     else                                                                      \
221     {                                                                         \
222         int i_low = 0, i_high = zdict->i_entries - 1;                         \
223         while( i_high - i_low > 1 )                                           \
224         {                                                                     \
225             int i_mid = (i_low + i_high)/2;                                   \
226             fprintf(stderr, "Low %i, high %i\n", i_low, i_high);              \
227             if( zdict->p_entries[i_mid].i_hash < i_hash ) {                   \
228                 i_low = i_mid;                                                \
229             } else if( zdict->p_entries[i_mid].i_hash > i_hash ) {            \
230                 i_high = i_mid;                                               \
231             }                                                                 \
232         }                                                                     \
233         if( zdict->p_entries[i_low].i_hash < i_hash )                         \
234             i_new = i_high;                                                   \
235         else                                                                  \
236             i_new = i_low;                                                    \
237     }                                                                         \
238     zdict->p_entries = realloc( zdict->p_entries, (zdict->i_entries + 1) *    \
239         ( sizeof(zdata) + sizeof(int) + sizeof(void*) + sizeof(uint64_t) ) ); \
240     zdict->i_entries++;                                                       \
241     if( i_new != zdict->i_entries -1 )                                        \
242         memmove( &zdict->p_entries[i_new+1], &zdict->p_entries[i_new],        \
243          ( zdict->i_entries - i_new - 1 ) *                                   \
244          ( sizeof(zdata) + sizeof(int) + sizeof(void*) + sizeof(uint64_t) ) );\
245                                                                               \
246     zdict->p_entries[i_new].i_hash = i_hash;                                  \
247     zdict->p_entries[i_new].i_int = (zint);                                   \
248     if( (zstring) ) {                                                         \
249         zdict->p_entries[i_new].psz_string = strdup( (zstring) );             \
250     } else {                                                                  \
251         zdict->p_entries[i_new].psz_string = NULL;                            \
252     }                                                                         \
253     zdict->p_entries[i_new].data = zdata;                                     \
254 }
255
256 #define DICT_LOOKUP( zdict, zint, zstring, answer ) do {                      \
257     uint64_t i_hash;                                                          \
258     int i, i_pos;                                                             \
259     vlc_bool_t b_found = VLC_FALSE;                                           \
260     if( zdict->i_entries == 0 ) {                                             \
261         answer = -1;                                                          \
262         break;                                                                \
263     }                                                                         \
264                                                                               \
265     i_hash = DictHash( (zstring), (zint) );                                   \
266     BSEARCH( zdict->p_entries, zdict->i_entries, .i_hash, uint64_t,           \
267              i_hash, i_pos );                                                 \
268     if( i_pos == -1 ) {                                                       \
269         answer = -1;                                                          \
270         break;                                                                \
271     }                                                                         \
272                                                                               \
273     /* Hash found, let's check it looks like the entry */                     \
274     if( !strcmp( (zstring), zdict->p_entries[i_pos].psz_string ) ) {          \
275         answer = i_pos;                                                       \
276         break;                                                                \
277     }                                                                         \
278                                                                               \
279     /* Hash collision! This should be very rare, but we cannot guarantee      \
280      * it will never happen. Just do an exhaustive search amongst all         \
281      * entries with the same hash. */                                         \
282     for( i = i_pos - 1 ; i > 0 && i_hash == zdict->p_entries[i].i_hash ; i-- )\
283     {                                                                         \
284         if( !strcmp( (zstring), zdict->p_entries[i].psz_string ) &&           \
285                    zdict->p_entries[i].i_int == (zint) ) {                    \
286             b_found = VLC_TRUE;                                               \
287             answer = i;                                                       \
288             break;                                                            \
289         }                                                                     \
290     }                                                                         \
291     if( b_found == VLC_TRUE )                                                 \
292         break;                                                                \
293     for( i = i_pos + 1 ; i < zdict->i_entries &&                              \
294                          i_hash == zdict->p_entries[i].i_hash ; i++ )         \
295     {                                                                         \
296          if( !strcmp( (zstring), zdict->p_entries[i].psz_string ) &&          \
297                       zdict->p_entries[i].i_int == (zint) ) {                 \
298             b_found = VLC_TRUE;                                               \
299             answer = i;                                                       \
300             break;                                                            \
301         }                                                                     \
302     }                                                                         \
303     /* Hash found, but entry not found (quite strange !) */                   \
304     assert( 0 );                                                              \
305 } while(0)
306
307 #define DICT_GET( zdict, i_int, psz_string, answer ) {                        \
308     int int_answer;                                                           \
309     DICT_LOOKUP( zdict, i_int, psz_string, int_answer );                      \
310     if( int_answer >=  0 )                                                    \
311         answer = zdict->p_entries[int_answer].data;                           \
312 }
313
314 /************************************************************************
315  * Dynamic arrays with progressive allocation
316  ************************************************************************/
317
318 /* Internal functions */
319 #define _ARRAY_ALLOC(array, newsize) {                                      \
320     array.i_alloc = newsize;                                                \
321     array.p_elems = VLCCVP realloc( array.p_elems, array.i_alloc *          \
322                                     sizeof(*array.p_elems) );               \
323     assert(array.p_elems);                                                  \
324 }
325
326 #define _ARRAY_GROW1(array) {                                               \
327     if( array.i_alloc < 10 )                                                \
328         _ARRAY_ALLOC(array, 10 )                                            \
329     else if( array.i_alloc == array.i_size )                                \
330         _ARRAY_ALLOC(array, (int)(array.i_alloc * 1.5) )                    \
331 }
332
333 #define _ARRAY_GROW(array,additional) {                                     \
334      int i_first = array.i_alloc;                                           \
335      while( array.i_alloc - i_first < additional )                          \
336      {                                                                      \
337          if( array.i_alloc < 10 )                                           \
338             _ARRAY_ALLOC(array, 10 )                                        \
339         else if( array.i_alloc == array.i_size )                            \
340             _ARRAY_ALLOC(array, (int)(array.i_alloc * 1.5) )                \
341         else break;                                                         \
342      }                                                                      \
343 }
344
345 #define _ARRAY_SHRINK(array) {                                              \
346     if( array.i_size > 10 && array.i_size < (int)(array.i_alloc / 1.5) ) {  \
347         _ARRAY_ALLOC(array, array.i_size + 5);                              \
348     }                                                                       \
349 }
350
351
352 /* API */
353 #define DECL_ARRAY(type) struct {                                           \
354     int i_alloc;                                                            \
355     int i_size;                                                             \
356     type *p_elems;                                                          \
357 }
358
359 #define TYPEDEF_ARRAY(type, name) typedef DECL_ARRAY(type) name;
360
361 #define ARRAY_INIT(array)                                                   \
362     array.i_alloc = 0;                                                      \
363     array.i_size = 0;                                                       \
364     array.p_elems = NULL;
365
366 #define ARRAY_RESET(array)                                                  \
367     array.i_alloc = 0;                                                      \
368     array.i_size = 0;                                                       \
369     free( array.p_elems ); array.p_elems = NULL;
370
371 #define ARRAY_APPEND(array, elem) {                                         \
372     _ARRAY_GROW1(array);                                                    \
373     array.p_elems[array.i_size] = elem;                                     \
374     array.i_size++;                                                         \
375 }
376
377 #define ARRAY_INSERT(array,elem,pos) {                                      \
378     _ARRAY_GROW1(array);                                                    \
379     if( array.i_size - pos ) {                                              \
380         memmove( array.p_elems + pos + 1, array.p_elems + pos,              \
381                  (array.i_size-pos) * sizeof(*array.p_elems) );             \
382     }                                                                       \
383     array.p_elems[pos] = elem;                                              \
384     array.i_size++;                                                         \
385 }
386
387 #define ARRAY_REMOVE(array,pos) {                                           \
388     if( array.i_size - (pos) - 1 )                                          \
389     {                                                                       \
390         memmove( array.p_elems + pos, array.p_elems + pos + 1,              \
391                  ( array.i_size - pos - 1 ) *sizeof(*array.p_elems) );      \
392     }                                                                       \
393     array.i_size--;                                                         \
394     _ARRAY_SHRINK(array);                                                   \
395 }
396
397 #define ARRAY_VAL(array, pos) array.p_elems[pos]
398
399 #define ARRAY_BSEARCH(array, elem, zetype, key, answer) \
400     BSEARCH( array.p_elems, array.i_size, elem, zetype, key, answer)
401
402 #define FOREACH_ARRAY( item, array ) { \
403     int fe_idx; \
404     for( fe_idx = 0 ; fe_idx < array.i_size ; fe_idx++ ) \
405     { \
406         item = array.p_elems[fe_idx];
407
408 #define FOREACH_END() } }
409
410 #endif