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