vlc_arrays.h: Fix a bug in vlc_dictionary_remove_value_for_key, thanks to funman.
[vlc.git] / 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 /************************************************************************
186  * Dynamic arrays with progressive allocation
187  ************************************************************************/
188
189 /* Internal functions */
190 #define _ARRAY_ALLOC(array, newsize) {                                      \
191     array.i_alloc = newsize;                                                \
192     array.p_elems = VLCCVP realloc( array.p_elems, array.i_alloc *          \
193                                     sizeof(*array.p_elems) );               \
194     assert(array.p_elems);                                                  \
195 }
196
197 #define _ARRAY_GROW1(array) {                                               \
198     if( array.i_alloc < 10 )                                                \
199         _ARRAY_ALLOC(array, 10 )                                            \
200     else if( array.i_alloc == array.i_size )                                \
201         _ARRAY_ALLOC(array, (int)(array.i_alloc * 1.5) )                    \
202 }
203
204 #define _ARRAY_GROW(array,additional) {                                     \
205      int i_first = array.i_alloc;                                           \
206      while( array.i_alloc - i_first < additional )                          \
207      {                                                                      \
208          if( array.i_alloc < 10 )                                           \
209             _ARRAY_ALLOC(array, 10 )                                        \
210         else if( array.i_alloc == array.i_size )                            \
211             _ARRAY_ALLOC(array, (int)(array.i_alloc * 1.5) )                \
212         else break;                                                         \
213      }                                                                      \
214 }
215
216 #define _ARRAY_SHRINK(array) {                                              \
217     if( array.i_size > 10 && array.i_size < (int)(array.i_alloc / 1.5) ) {  \
218         _ARRAY_ALLOC(array, array.i_size + 5);                              \
219     }                                                                       \
220 }
221
222
223 /* API */
224 #define DECL_ARRAY(type) struct {                                           \
225     int i_alloc;                                                            \
226     int i_size;                                                             \
227     type *p_elems;                                                          \
228 }
229
230 #define TYPEDEF_ARRAY(type, name) typedef DECL_ARRAY(type) name;
231
232 #define ARRAY_INIT(array)                                                   \
233     array.i_alloc = 0;                                                      \
234     array.i_size = 0;                                                       \
235     array.p_elems = NULL;
236
237 #define ARRAY_RESET(array)                                                  \
238     array.i_alloc = 0;                                                      \
239     array.i_size = 0;                                                       \
240     free( array.p_elems ); array.p_elems = NULL;
241
242 #define ARRAY_APPEND(array, elem) {                                         \
243     _ARRAY_GROW1(array);                                                    \
244     array.p_elems[array.i_size] = elem;                                     \
245     array.i_size++;                                                         \
246 }
247
248 #define ARRAY_INSERT(array,elem,pos) {                                      \
249     _ARRAY_GROW1(array);                                                    \
250     if( array.i_size - pos ) {                                              \
251         memmove( array.p_elems + pos + 1, array.p_elems + pos,              \
252                  (array.i_size-pos) * sizeof(*array.p_elems) );             \
253     }                                                                       \
254     array.p_elems[pos] = elem;                                              \
255     array.i_size++;                                                         \
256 }
257
258 #define ARRAY_REMOVE(array,pos) {                                           \
259     if( array.i_size - (pos) - 1 )                                          \
260     {                                                                       \
261         memmove( array.p_elems + pos, array.p_elems + pos + 1,              \
262                  ( array.i_size - pos - 1 ) *sizeof(*array.p_elems) );      \
263     }                                                                       \
264     array.i_size--;                                                         \
265     _ARRAY_SHRINK(array);                                                   \
266 }
267
268 #define ARRAY_VAL(array, pos) array.p_elems[pos]
269
270 #define ARRAY_BSEARCH(array, elem, zetype, key, answer) \
271     BSEARCH( array.p_elems, array.i_size, elem, zetype, key, answer)
272
273 #define FOREACH_ARRAY( item, array ) { \
274     int fe_idx; \
275     for( fe_idx = 0 ; fe_idx < array.i_size ; fe_idx++ ) \
276     { \
277         item = array.p_elems[fe_idx];
278
279 #define FOREACH_END() } }
280
281 /************************************************************************
282  * Dictionaries
283  ************************************************************************/
284
285 /* This function is not intended to be crypto-secure, we only want it to be
286  * fast and not suck too much. This one is pretty fast and did 0 collisions
287  * in wenglish's dictionary.
288  */
289 static inline uint64_t DictHash( const char *psz_string, int hashsize )
290 {
291     uint64_t i_hash = 0;
292     if( psz_string )
293     {
294         while( *psz_string )
295         {
296             i_hash += *psz_string++;
297             i_hash += i_hash << 10;
298             i_hash ^= i_hash >> 8;
299         }
300     }
301     return i_hash % hashsize;
302 }
303
304 struct vlc_dictionary_entry_t
305 {
306     char *   psz_key;
307     void *   p_value;
308     struct vlc_dictionary_entry_t * p_next;
309 };
310
311 typedef struct vlc_dictionary_t
312 {
313     int i_size;
314     struct vlc_dictionary_entry_t ** p_entries;
315 } vlc_dictionary_t;
316
317 static void * const kVLCDictionaryNotFound = NULL;
318
319 static inline void vlc_dictionary_init( vlc_dictionary_t * p_dict, int i_size )
320 {
321     if( i_size > 0 )
322     {
323         p_dict->p_entries = (struct vlc_dictionary_entry_t **)malloc(sizeof(struct vlc_dictionary_entry_t *) * i_size);
324         assert( p_dict->p_entries );
325         memset( p_dict->p_entries, 0, sizeof(struct vlc_dictionary_entry_t *) * i_size );
326     }
327     else
328         p_dict->p_entries = NULL;
329     p_dict->i_size = i_size;
330 }
331
332 static inline void vlc_dictionary_clear( vlc_dictionary_t * p_dict )
333 {
334     int i;
335     struct vlc_dictionary_entry_t * p_current, * p_next;
336     for( i = 0; i < p_dict->i_size; i++ )
337     {
338         p_current = p_dict->p_entries[i];
339         while( p_current )
340         {
341             p_next = p_dict->p_entries[i]->p_next;
342             free( p_dict->p_entries[i]->psz_key );
343             free( p_current );
344             p_current = p_next;
345         }
346     }
347     free( p_dict->p_entries );
348     p_dict->i_size = 0;
349 }
350
351
352
353 static inline void *
354 vlc_dictionary_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_key )
355 {
356     if( !p_dict->p_entries )
357         return kVLCDictionaryNotFound;
358
359     int i_pos = DictHash( psz_key, p_dict->i_size );
360     struct vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos];
361
362     if( p_entry && !p_entry->p_next )
363         return p_entry->p_value;
364
365     if( !p_entry )
366         return kVLCDictionaryNotFound;
367
368     /* Hash collision */        
369     do {
370         if( !strcmp( psz_key, p_entry->psz_key ) )
371             return p_entry->p_value;
372         p_entry = p_entry->p_next;
373     } while( p_entry );
374
375     return kVLCDictionaryNotFound;
376 }
377
378 static inline int
379 vlc_dictionary_keys_count( const vlc_dictionary_t * p_dict )
380 {
381     struct vlc_dictionary_entry_t * p_entry;    
382     int i, count = 0;
383     for( i = 0; i < p_dict->i_size; i++ )
384     {
385         for( p_entry = p_dict->p_entries[i]; p_entry; p_entry = p_entry->p_next ) count++;
386     }
387     return count;
388 }
389
390 static inline char **
391 vlc_dictionary_all_keys( const vlc_dictionary_t * p_dict )
392 {
393     struct vlc_dictionary_entry_t * p_entry;
394     char ** ppsz_ret;
395     int i, count = vlc_dictionary_keys_count( p_dict );
396
397     ppsz_ret = (char**)malloc(sizeof(char *) * (count + 1));
398     assert( ppsz_ret );
399     
400     count = 0;
401     for( i = 0; i < p_dict->i_size; i++ )
402     {
403         for( p_entry = p_dict->p_entries[i]; p_entry; p_entry = p_entry->p_next )
404         {
405             ppsz_ret[count++] = strdup( p_entry->psz_key );
406             assert( ppsz_ret );
407         }
408     }
409     ppsz_ret[count] = NULL;
410     return ppsz_ret;
411 }
412
413 static inline void
414 __vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key,
415                          void * p_value, vlc_bool_t rebuild )
416 {
417     if( !p_dict->p_entries )
418         vlc_dictionary_init( p_dict, 1 );
419
420     int i_pos = DictHash( psz_key, p_dict->i_size );
421     struct vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos];
422
423     if( !p_entry )
424     {
425         p_entry = p_dict->p_entries[i_pos] = (struct vlc_dictionary_entry_t *)malloc(
426                 sizeof(struct vlc_dictionary_entry_t));
427         assert( p_entry );
428
429         p_entry->psz_key = strdup( psz_key );
430         assert( p_entry->psz_key );
431         p_entry->p_value = p_value;
432         p_dict->p_entries[i_pos]->p_next = NULL;
433         return;
434     }
435     if( p_entry->p_value == kVLCDictionaryNotFound )
436     {
437         /* This one is fine, just high jack */
438         p_entry->psz_key = strdup( psz_key );
439         p_entry->p_value = p_value;
440         return;
441     }
442
443     /* Hash collision here */
444     p_entry = (struct vlc_dictionary_entry_t *)malloc(sizeof(struct vlc_dictionary_entry_t));
445     assert( p_entry );
446     p_entry->psz_key = strdup( psz_key );
447     p_entry->p_value = p_value;
448     p_entry->p_next = p_dict->p_entries[i_pos];
449     p_dict->p_entries[i_pos] = p_entry;
450
451     if( rebuild )
452     {
453         /* Count how many items there was */
454         int count;
455         for( count = 1; p_entry->p_next; count++ ) p_entry = p_entry->p_next;
456         if( count > 3 ) /* XXX: this need tuning */
457         {
458             /* Here it starts to be not good, rebuild a bigger dictionary */
459             struct vlc_dictionary_t new_dict;
460             int i_new_size = ( (p_dict->i_size+2) * 3) / 2; /* XXX: this need tuning */
461             int i;
462  
463             vlc_dictionary_init( &new_dict, i_new_size );
464             for( i = 0; i < p_dict->i_size; i++ )
465             {
466                 p_entry = p_dict->p_entries[i];
467                 while( p_entry );
468                 {
469                     __vlc_dictionary_insert( &new_dict, p_entry->psz_key,
470                                              p_entry->p_value,
471                                              0 /* To avoid multiple rebuild loop */);
472                     p_entry = p_entry->p_next;
473                 } 
474             }
475             vlc_dictionary_clear( p_dict );
476             p_dict->i_size = new_dict.i_size;
477             p_dict->p_entries= new_dict.p_entries;
478         }
479     }
480 }
481
482 static inline void
483 vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key, void * p_value )
484 {
485     __vlc_dictionary_insert( p_dict, psz_key, p_value, 1 );
486 }
487
488 static inline void
489 vlc_dictionary_remove_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_key )
490 {
491     if( !p_dict->p_entries )
492         return;
493
494     int i_pos = DictHash( psz_key, p_dict->i_size );
495     struct vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos];
496     struct vlc_dictionary_entry_t * p_prev;
497
498     if( !p_entry )
499         return; /* Not found, nothing to do */
500
501     if( !p_entry->p_next )
502     {
503         free( p_entry->psz_key );
504         free( p_entry );
505         p_dict->p_entries[i_pos] = NULL;
506         return;
507     }
508
509     /* Hash collision */
510     p_prev = NULL;
511     do {
512         if( !strcmp( psz_key, p_entry->psz_key ) )
513         {
514             if( !p_prev )
515                 p_dict->p_entries[i_pos] = p_entry->p_next;
516             else
517                 p_prev->p_next = p_entry->p_next;
518             free( p_entry->psz_key );
519             free( p_entry );
520             return;
521         }
522         p_prev = p_entry;
523         p_entry = p_entry->p_next;
524     } while( p_entry );
525
526     /* No key was found */
527 }
528
529 #endif