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