]> git.sesse.net Git - vlc/blob - include/vlc_arrays.h
Do not use assert in public headers
[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$
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     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
119 #define TAB_REMOVE( count, tab, p )             \
120   do {                                          \
121         int _i_index_;                          \
122         TAB_FIND( count, tab, p, _i_index_ );   \
123         if( _i_index_ >= 0 )                    \
124         {                                       \
125             if( (count) > 1 )                   \
126             {                                   \
127                 memmove( ((void**)(tab) + _i_index_),    \
128                          ((void**)(tab) + _i_index_+1),  \
129                          ( (count) - _i_index_ - 1 ) * sizeof( void* ) );\
130             }                                   \
131             (count)--;                          \
132             if( (count) == 0 )                  \
133             {                                   \
134                 free( tab );                    \
135                 (tab) = NULL;                   \
136             }                                   \
137         }                                       \
138   } while(0)
139
140 #define TAB_INSERT_CAST( cast, count, tab, p, index ) do { \
141     if( (count) > 0 )                           \
142         (tab) = cast realloc( tab, sizeof( void ** ) * ( (count) + 1 ) ); \
143     else                                        \
144         (tab) = cast malloc( sizeof( void ** ) );       \
145     if( (count) - (index) > 0 )                 \
146         memmove( (void**)(tab) + (index) + 1,   \
147                  (void**)(tab) + (index),       \
148                  ((count) - (index)) * sizeof(*(tab)) );\
149     (tab)[(index)] = (p);                       \
150     (count)++;                                  \
151 } while(0)
152
153 #define TAB_INSERT( count, tab, p, index )      \
154     TAB_INSERT_CAST( , count, tab, p, index )
155
156 /**
157  * Binary search in a sorted array. The key must be comparable by < and >
158  * \param entries array of entries
159  * \param count number of entries
160  * \param elem key to check within an entry (like .id, or ->i_id)
161  * \param zetype type of the key
162  * \param key value of the key
163  * \param answer index of answer within the array. -1 if not found
164  */
165 #define BSEARCH( entries, count, elem, zetype, key, answer ) \
166    do {  \
167     int low = 0, high = count - 1;   \
168     answer = -1; \
169     while( low <= high ) {\
170         int mid = (low + high ) / 2; /* Just don't care about 2^30 tables */ \
171         zetype mid_val = entries[mid] elem;\
172         if( mid_val < key ) \
173             low = mid + 1; \
174         else if ( mid_val > key ) \
175             high = mid -1;  \
176         else    \
177         {   \
178             answer = mid;  break;   \
179         }\
180     } \
181  } while(0)
182
183
184 /************************************************************************
185  * Dynamic arrays with progressive allocation
186  ************************************************************************/
187
188 /* Internal functions */
189 #define _ARRAY_ALLOC(array, newsize) {                                      \
190     array.i_alloc = newsize;                                                \
191     array.p_elems = VLCCVP realloc( array.p_elems, array.i_alloc *          \
192                                     sizeof(*array.p_elems) );               \
193 }
194
195 #define _ARRAY_GROW1(array) {                                               \
196     if( array.i_alloc < 10 )                                                \
197         _ARRAY_ALLOC(array, 10 )                                            \
198     else if( array.i_alloc == array.i_size )                                \
199         _ARRAY_ALLOC(array, (int)(array.i_alloc * 1.5) )                    \
200 }
201
202 #define _ARRAY_GROW(array,additional) {                                     \
203      int i_first = array.i_alloc;                                           \
204      while( array.i_alloc - i_first < additional )                          \
205      {                                                                      \
206          if( array.i_alloc < 10 )                                           \
207             _ARRAY_ALLOC(array, 10 )                                        \
208         else if( array.i_alloc == array.i_size )                            \
209             _ARRAY_ALLOC(array, (int)(array.i_alloc * 1.5) )                \
210         else break;                                                         \
211      }                                                                      \
212 }
213
214 #define _ARRAY_SHRINK(array) {                                              \
215     if( array.i_size > 10 && array.i_size < (int)(array.i_alloc / 1.5) ) {  \
216         _ARRAY_ALLOC(array, array.i_size + 5);                              \
217     }                                                                       \
218 }
219
220 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
221
222 /* API */
223 #define DECL_ARRAY(type) struct {                                           \
224     int i_alloc;                                                            \
225     int i_size;                                                             \
226     type *p_elems;                                                          \
227 }
228
229 #define TYPEDEF_ARRAY(type, name) typedef DECL_ARRAY(type) name;
230
231 #define ARRAY_INIT(array)                                                   \
232     array.i_alloc = 0;                                                      \
233     array.i_size = 0;                                                       \
234     array.p_elems = NULL;
235
236 #define ARRAY_RESET(array)                                                  \
237     array.i_alloc = 0;                                                      \
238     array.i_size = 0;                                                       \
239     free( array.p_elems ); array.p_elems = NULL;
240
241 #define ARRAY_APPEND(array, elem) {                                         \
242     _ARRAY_GROW1(array);                                                    \
243     array.p_elems[array.i_size] = elem;                                     \
244     array.i_size++;                                                         \
245 }
246
247 #define ARRAY_INSERT(array,elem,pos) {                                      \
248     _ARRAY_GROW1(array);                                                    \
249     if( array.i_size - pos ) {                                              \
250         memmove( array.p_elems + pos + 1, array.p_elems + pos,              \
251                  (array.i_size-pos) * sizeof(*array.p_elems) );             \
252     }                                                                       \
253     array.p_elems[pos] = elem;                                              \
254     array.i_size++;                                                         \
255 }
256
257 #define ARRAY_REMOVE(array,pos) {                                           \
258     if( array.i_size - (pos) - 1 )                                          \
259     {                                                                       \
260         memmove( array.p_elems + pos, array.p_elems + pos + 1,              \
261                  ( array.i_size - pos - 1 ) *sizeof(*array.p_elems) );      \
262     }                                                                       \
263     array.i_size--;                                                         \
264     _ARRAY_SHRINK(array);                                                   \
265 }
266
267 #define ARRAY_VAL(array, pos) array.p_elems[pos]
268
269 #define ARRAY_BSEARCH(array, elem, zetype, key, answer) \
270     BSEARCH( array.p_elems, array.i_size, elem, zetype, key, answer)
271
272 #define FOREACH_ARRAY( item, array ) { \
273     int fe_idx; \
274     for( fe_idx = 0 ; fe_idx < array.i_size ; fe_idx++ ) \
275     { \
276         item = array.p_elems[fe_idx];
277
278 #define FOREACH_END() } }
279
280
281 /************************************************************************
282  * Dynamic arrays with progressive allocation (Preferred API)
283  ************************************************************************/
284 typedef struct vlc_array_t
285 {
286     int i_count;
287     void ** pp_elems;
288 } vlc_array_t;
289
290 static inline void vlc_array_init( vlc_array_t * p_array )
291 {
292     memset( p_array, 0, sizeof(vlc_array_t) );
293 }
294
295 static inline void vlc_array_clear( vlc_array_t * p_array )
296 {
297     free( p_array->pp_elems );
298     memset( p_array, 0, sizeof(vlc_array_t) );
299 }
300
301 static inline vlc_array_t * vlc_array_new( void )
302 {
303     vlc_array_t * ret = (vlc_array_t *)malloc( sizeof(vlc_array_t) );
304     if( ret ) vlc_array_init( ret );
305     return ret;
306 }
307
308 static inline void vlc_array_destroy( vlc_array_t * p_array )
309 {
310     if( !p_array )
311         return;
312     vlc_array_clear( p_array );
313     free( p_array );
314 }
315
316
317 /* Read */
318 static inline int
319 vlc_array_count( vlc_array_t * p_array )
320 {
321     return p_array->i_count;
322 }
323
324 static inline void *
325 vlc_array_item_at_index( vlc_array_t * p_array, int i_index )
326 {
327     return p_array->pp_elems[i_index];
328 }
329
330 static inline int
331 vlc_array_index_of_item( vlc_array_t * p_array, void * item )
332 {
333     int i;
334     for( i = 0; i < p_array->i_count; i++)
335     {
336         if( p_array->pp_elems[i] == item )
337             return i;
338     }
339     return -1;
340 }
341
342 /* Write */
343 static inline void
344 vlc_array_insert( vlc_array_t * p_array, void * p_elem, int i_index )
345 {
346     TAB_INSERT_CAST( (void **), p_array->i_count, p_array->pp_elems, p_elem, i_index );
347 }
348
349 static inline void
350 vlc_array_append( vlc_array_t * p_array, void * p_elem )
351 {
352     vlc_array_insert( p_array, p_elem, p_array->i_count );
353 }
354
355 static inline void
356 vlc_array_remove( vlc_array_t * p_array, int i_index )
357 {
358     if( i_index >= 0 )
359     {
360         if( p_array->i_count > 1 )
361         {
362             memmove( p_array->pp_elems + i_index,
363                      p_array->pp_elems + i_index+1,
364                      ( p_array->i_count - i_index - 1 ) * sizeof( void* ) );
365         }
366         p_array->i_count--;
367         if( p_array->i_count == 0 )
368         {
369             free( p_array->pp_elems );
370             p_array->pp_elems = NULL;
371         }
372     }
373 }
374
375
376 /************************************************************************
377  * Dictionaries
378  ************************************************************************/
379
380 /* This function is not intended to be crypto-secure, we only want it to be
381  * fast and not suck too much. This one is pretty fast and did 0 collisions
382  * in wenglish's dictionary.
383  */
384 static inline uint64_t DictHash( const char *psz_string, int hashsize )
385 {
386     uint64_t i_hash = 0;
387     if( psz_string )
388     {
389         while( *psz_string )
390         {
391             i_hash += *psz_string++;
392             i_hash += i_hash << 10;
393             i_hash ^= i_hash >> 8;
394         }
395     }
396     return i_hash % hashsize;
397 }
398
399 struct vlc_dictionary_entry_t
400 {
401     char *   psz_key;
402     void *   p_value;
403     struct vlc_dictionary_entry_t * p_next;
404 };
405
406 typedef struct vlc_dictionary_t
407 {
408     int i_size;
409     struct vlc_dictionary_entry_t ** p_entries;
410 } vlc_dictionary_t;
411
412 static void * const kVLCDictionaryNotFound = NULL;
413
414 static inline void vlc_dictionary_init( vlc_dictionary_t * p_dict, int i_size )
415 {
416     if( i_size > 0 )
417     {
418         p_dict->p_entries = (struct vlc_dictionary_entry_t **)malloc(sizeof(struct vlc_dictionary_entry_t *) * i_size);
419         memset( p_dict->p_entries, 0, sizeof(struct vlc_dictionary_entry_t *) * i_size );
420     }
421     else
422         p_dict->p_entries = NULL;
423     p_dict->i_size = i_size;
424 }
425
426 static inline void vlc_dictionary_clear( vlc_dictionary_t * p_dict )
427 {
428     int i;
429     struct vlc_dictionary_entry_t * p_current, * p_next;
430     if( p_dict->p_entries )
431     {
432         for( i = 0; i < p_dict->i_size; i++ )
433         {
434             p_current = p_dict->p_entries[i];
435             while( p_current )
436             {
437                 p_next = p_current->p_next;
438                 free( p_current->psz_key );
439                 free( p_current );
440                 p_current = p_next;
441             }
442         }
443         free( p_dict->p_entries );
444     }
445     p_dict->i_size = 0;
446 }
447
448
449
450 static inline void *
451 vlc_dictionary_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_key )
452 {
453     if( !p_dict->p_entries )
454         return kVLCDictionaryNotFound;
455
456     int i_pos = DictHash( psz_key, p_dict->i_size );
457     struct vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos];
458
459     if( !p_entry )
460         return kVLCDictionaryNotFound;
461
462     /* Make sure we return the right item. (Hash collision) */
463     do {
464         if( !strcmp( psz_key, p_entry->psz_key ) )
465             return p_entry->p_value;
466         p_entry = p_entry->p_next;
467     } while( p_entry );
468
469     return kVLCDictionaryNotFound;
470 }
471
472 static inline int
473 vlc_dictionary_keys_count( const vlc_dictionary_t * p_dict )
474 {
475     struct vlc_dictionary_entry_t * p_entry;
476     int i, count = 0;
477
478     if( !p_dict->p_entries )
479         return 0;
480
481     for( i = 0; i < p_dict->i_size; i++ )
482     {
483         for( p_entry = p_dict->p_entries[i]; p_entry; p_entry = p_entry->p_next ) count++;
484     }
485     return count;
486 }
487
488 static inline char **
489 vlc_dictionary_all_keys( const vlc_dictionary_t * p_dict )
490 {
491     struct vlc_dictionary_entry_t * p_entry;
492     char ** ppsz_ret;
493     int i, count = vlc_dictionary_keys_count( p_dict );
494
495     ppsz_ret = (char**)malloc(sizeof(char *) * (count + 1));
496
497     count = 0;
498     for( i = 0; i < p_dict->i_size; i++ )
499     {
500         for( p_entry = p_dict->p_entries[i]; p_entry; p_entry = p_entry->p_next )
501             ppsz_ret[count++] = strdup( p_entry->psz_key );
502     }
503     ppsz_ret[count] = NULL;
504     return ppsz_ret;
505 }
506
507 static inline void
508 __vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key,
509                          void * p_value, bool rebuild )
510 {
511     if( !p_dict->p_entries )
512         vlc_dictionary_init( p_dict, 1 );
513
514     int i_pos = DictHash( psz_key, p_dict->i_size );
515     struct vlc_dictionary_entry_t * p_entry;
516
517     p_entry = (struct vlc_dictionary_entry_t *)malloc(sizeof(struct vlc_dictionary_entry_t));
518     p_entry->psz_key = strdup( psz_key );
519     p_entry->p_value = p_value;
520     p_entry->p_next = p_dict->p_entries[i_pos];
521     p_dict->p_entries[i_pos] = p_entry;
522     if( rebuild )
523     {
524         /* Count how many items there was */
525         int count;
526         for( count = 1; p_entry->p_next; count++ ) p_entry = p_entry->p_next;
527         if( count > 3 ) /* XXX: this need tuning */
528         {
529             /* Here it starts to be not good, rebuild a bigger dictionary */
530             struct vlc_dictionary_t new_dict;
531             int i_new_size = ( (p_dict->i_size+2) * 3) / 2; /* XXX: this need tuning */
532             int i;
533             vlc_dictionary_init( &new_dict, i_new_size );
534             for( i = 0; i < p_dict->i_size; i++ )
535             {
536                 p_entry = p_dict->p_entries[i];
537                 while( p_entry )
538                 {
539                     __vlc_dictionary_insert( &new_dict, p_entry->psz_key,
540                                              p_entry->p_value,
541                                              0 /* To avoid multiple rebuild loop */);
542                     p_entry = p_entry->p_next;
543                 }
544             }
545
546             vlc_dictionary_clear( p_dict );
547             p_dict->i_size = new_dict.i_size;
548             p_dict->p_entries = new_dict.p_entries;
549         }
550     }
551 }
552
553 static inline void
554 vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key, void * p_value )
555 {
556     __vlc_dictionary_insert( p_dict, psz_key, p_value, 1 );
557 }
558
559 static inline void
560 vlc_dictionary_remove_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_key )
561 {
562     if( !p_dict->p_entries )
563         return;
564
565     int i_pos = DictHash( psz_key, p_dict->i_size );
566     struct vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos];
567     struct vlc_dictionary_entry_t * p_prev;
568
569     if( !p_entry )
570         return; /* Not found, nothing to do */
571
572     /* Hash collision */
573     p_prev = NULL;
574     do {
575         if( !strcmp( psz_key, p_entry->psz_key ) )
576         {
577             if( !p_prev )
578                 p_dict->p_entries[i_pos] = p_entry->p_next;
579             else
580                 p_prev->p_next = p_entry->p_next;
581             free( p_entry->psz_key );
582             free( p_entry );
583             return;
584         }
585         p_prev = p_entry;
586         p_entry = p_entry->p_next;
587     } while( p_entry );
588
589     /* No key was found */
590 }
591
592 #endif