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