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