Contribs: fix win32 static linkage
[vlc.git] / include / vlc_arrays.h
blob886a7f93895ab76a948744f4aa5fd83fcf8216d3
1 /*****************************************************************************
2 * vlc_arrays.h : Arrays and data structures handling
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Clément Stenac <zorglub@videolan.org>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #ifndef VLC_ARRAYS_H_
26 #define VLC_ARRAYS_H_
28 /**
29 * \file
30 * This file defines functions, structures and macros for handling arrays in vlc
33 /* realloc() that never fails *if* downsizing */
34 static inline void *realloc_down( void *ptr, size_t size )
36 void *ret = realloc( ptr, size );
37 return ret ? ret : ptr;
40 /**
41 * Simple dynamic array handling. Array is realloced at each insert/removal
43 #define INSERT_ELEM( p_ar, i_oldsize, i_pos, elem ) \
44 do \
45 { \
46 if( !(i_oldsize) ) (p_ar) = NULL; \
47 (p_ar) = realloc( p_ar, ((i_oldsize) + 1) * sizeof(*(p_ar)) ); \
48 if( !(p_ar) ) abort(); \
49 if( (i_oldsize) - (i_pos) ) \
50 { \
51 memmove( (p_ar) + (i_pos) + 1, (p_ar) + (i_pos), \
52 ((i_oldsize) - (i_pos)) * sizeof( *(p_ar) ) ); \
53 } \
54 (p_ar)[(i_pos)] = elem; \
55 (i_oldsize)++; \
56 } \
57 while( 0 )
59 #define REMOVE_ELEM( p_ar, i_size, i_pos ) \
60 do \
61 { \
62 if( (i_size) - (i_pos) - 1 ) \
63 { \
64 memmove( (p_ar) + (i_pos), \
65 (p_ar) + (i_pos) + 1, \
66 ((i_size) - (i_pos) - 1) * sizeof( *(p_ar) ) ); \
67 } \
68 if( i_size > 1 ) \
69 (p_ar) = realloc_down( p_ar, ((i_size) - 1) * sizeof( *(p_ar) ) );\
70 else \
71 { \
72 free( p_ar ); \
73 (p_ar) = NULL; \
74 } \
75 (i_size)--; \
76 } \
77 while( 0 )
79 #define TAB_INIT( count, tab ) \
80 do { \
81 (count) = 0; \
82 (tab) = NULL; \
83 } while(0)
85 #define TAB_CLEAN( count, tab ) \
86 do { \
87 free( tab ); \
88 (count)= 0; \
89 (tab)= NULL; \
90 } while(0)
92 #define TAB_APPEND_CAST( cast, count, tab, p ) \
93 do { \
94 if( (count) > 0 ) \
95 (tab) = cast realloc( tab, sizeof( *(tab) ) * ( (count) + 1 ) ); \
96 else \
97 (tab) = cast malloc( sizeof( *(tab) ) ); \
98 if( !(tab) ) abort(); \
99 (tab)[count] = (p); \
100 (count)++; \
101 } while(0)
103 #define TAB_APPEND( count, tab, p ) \
104 TAB_APPEND_CAST( , count, tab, p )
106 #define TAB_FIND( count, tab, p, idx ) \
107 do { \
108 for( (idx) = 0; (idx) < (count); (idx)++ ) \
109 if( (tab)[(idx)] == (p) ) \
110 break; \
111 if( (idx) >= (count) ) \
112 (idx) = -1; \
113 } while(0)
116 #define TAB_ERASE( count, tab, index ) \
117 do { \
118 if( (count) > 1 ) \
120 memmove( ((void**)(tab) + index), \
121 ((void**)(tab) + index+1), \
122 ( (count) - index - 1 ) * sizeof( *(tab) ) );\
124 (count)--; \
125 if( (count) == 0 ) \
127 free( tab ); \
128 (tab) = NULL; \
130 } while(0)
132 #define TAB_REMOVE( count, tab, p ) \
133 do { \
134 int i_index; \
135 TAB_FIND( count, tab, p, i_index ); \
136 if( i_index >= 0 ) \
137 TAB_ERASE( count, tab, i_index ); \
138 } while(0)
140 #define TAB_INSERT_CAST( cast, count, tab, p, index ) do { \
141 if( (count) > 0 ) \
142 (tab) = cast realloc( tab, sizeof( *(tab) ) * ( (count) + 1 ) ); \
143 else \
144 (tab) = cast malloc( sizeof( *(tab) ) ); \
145 if( !(tab) ) abort(); \
146 if( (count) - (index) > 0 ) \
147 memmove( (void**)(tab) + (index) + 1, \
148 (void**)(tab) + (index), \
149 ((count) - (index)) * sizeof(*(tab)) );\
150 (tab)[(index)] = (p); \
151 (count)++; \
152 } while(0)
154 #define TAB_INSERT( count, tab, p, index ) \
155 TAB_INSERT_CAST( , count, tab, p, index )
158 * Binary search in a sorted array. The key must be comparable by < and >
159 * \param entries array of entries
160 * \param count number of entries
161 * \param elem key to check within an entry (like .id, or ->i_id)
162 * \param zetype type of the key
163 * \param key value of the key
164 * \param answer index of answer within the array. -1 if not found
166 #define BSEARCH( entries, count, elem, zetype, key, answer ) \
167 do { \
168 int low = 0, high = count - 1; \
169 answer = -1; \
170 while( low <= high ) {\
171 int mid = (low + high ) / 2; /* Just don't care about 2^30 tables */ \
172 zetype mid_val = entries[mid] elem;\
173 if( mid_val < key ) \
174 low = mid + 1; \
175 else if ( mid_val > key ) \
176 high = mid -1; \
177 else \
179 answer = mid; break; \
182 } while(0)
185 /************************************************************************
186 * Dynamic arrays with progressive allocation
187 ************************************************************************/
189 /* Internal functions */
190 #define _ARRAY_ALLOC(array, newsize) { \
191 (array).i_alloc = newsize; \
192 (array).p_elems = realloc( (array).p_elems, (array).i_alloc * \
193 sizeof(*(array).p_elems) ); \
194 if( !(array).p_elems ) abort(); \
197 #define _ARRAY_GROW1(array) { \
198 if( (array).i_alloc < 10 ) \
199 _ARRAY_ALLOC(array, 10 ) \
200 else if( (array).i_alloc == (array).i_size ) \
201 _ARRAY_ALLOC(array, (int)(array.i_alloc * 1.5) ) \
204 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
206 /* API */
207 #define DECL_ARRAY(type) struct { \
208 int i_alloc; \
209 int i_size; \
210 type *p_elems; \
213 #define TYPEDEF_ARRAY(type, name) typedef DECL_ARRAY(type) name;
215 #define ARRAY_INIT(array) \
216 do { \
217 (array).i_alloc = 0; \
218 (array).i_size = 0; \
219 (array).p_elems = NULL; \
220 } while(0)
222 #define ARRAY_RESET(array) \
223 do { \
224 (array).i_alloc = 0; \
225 (array).i_size = 0; \
226 free( (array).p_elems ); (array).p_elems = NULL; \
227 } while(0)
229 #define ARRAY_APPEND(array, elem) \
230 do { \
231 _ARRAY_GROW1(array); \
232 (array).p_elems[(array).i_size] = elem; \
233 (array).i_size++; \
234 } while(0)
236 #define ARRAY_INSERT(array,elem,pos) \
237 do { \
238 _ARRAY_GROW1(array); \
239 if( (array).i_size - pos ) { \
240 memmove( (array).p_elems + pos + 1, (array).p_elems + pos, \
241 ((array).i_size-pos) * sizeof(*(array).p_elems) ); \
243 (array).p_elems[pos] = elem; \
244 (array).i_size++; \
245 } while(0)
247 #define _ARRAY_SHRINK(array) { \
248 if( (array).i_size > 10 && (array).i_size < (int)((array).i_alloc / 1.5) ) { \
249 _ARRAY_ALLOC(array, (array).i_size + 5); \
253 #define ARRAY_REMOVE(array,pos) \
254 do { \
255 if( (array).i_size - (pos) - 1 ) \
257 memmove( (array).p_elems + pos, (array).p_elems + pos + 1, \
258 ( (array).i_size - pos - 1 ) *sizeof(*(array).p_elems) ); \
260 (array).i_size--; \
261 _ARRAY_SHRINK(array); \
262 } while(0)
264 #define ARRAY_VAL(array, pos) array.p_elems[pos]
266 #define ARRAY_BSEARCH(array, elem, zetype, key, answer) \
267 BSEARCH( (array).p_elems, (array).i_size, elem, zetype, key, answer)
269 #define FOREACH_ARRAY( item, array ) { \
270 int fe_idx; \
271 for( fe_idx = 0 ; fe_idx < (array).i_size ; fe_idx++ ) \
273 item = (array).p_elems[fe_idx];
275 #define FOREACH_END() } }
278 /************************************************************************
279 * Dynamic arrays with progressive allocation (Preferred API)
280 ************************************************************************/
281 typedef struct vlc_array_t
283 int i_count;
284 void ** pp_elems;
285 } vlc_array_t;
287 static inline void vlc_array_init( vlc_array_t * p_array )
289 memset( p_array, 0, sizeof(vlc_array_t) );
292 static inline void vlc_array_clear( vlc_array_t * p_array )
294 free( p_array->pp_elems );
295 memset( p_array, 0, sizeof(vlc_array_t) );
298 static inline vlc_array_t * vlc_array_new( void )
300 vlc_array_t * ret = (vlc_array_t *)malloc( sizeof(vlc_array_t) );
301 if( ret ) vlc_array_init( ret );
302 return ret;
305 static inline void vlc_array_destroy( vlc_array_t * p_array )
307 if( !p_array )
308 return;
309 vlc_array_clear( p_array );
310 free( p_array );
314 /* Read */
315 static inline int
316 vlc_array_count( vlc_array_t * p_array )
318 return p_array->i_count;
321 static inline void *
322 vlc_array_item_at_index( vlc_array_t * p_array, int i_index )
324 return p_array->pp_elems[i_index];
327 static inline int
328 vlc_array_index_of_item( vlc_array_t * p_array, void * item )
330 int i;
331 for( i = 0; i < p_array->i_count; i++)
333 if( p_array->pp_elems[i] == item )
334 return i;
336 return -1;
339 /* Write */
340 static inline void
341 vlc_array_insert( vlc_array_t * p_array, void * p_elem, int i_index )
343 TAB_INSERT_CAST( (void **), p_array->i_count, p_array->pp_elems, p_elem, i_index );
346 static inline void
347 vlc_array_append( vlc_array_t * p_array, void * p_elem )
349 vlc_array_insert( p_array, p_elem, p_array->i_count );
352 static inline void
353 vlc_array_remove( vlc_array_t * p_array, int i_index )
355 if( i_index >= 0 )
357 if( p_array->i_count > 1 )
359 memmove( p_array->pp_elems + i_index,
360 p_array->pp_elems + i_index+1,
361 ( p_array->i_count - i_index - 1 ) * sizeof( void* ) );
363 p_array->i_count--;
364 if( p_array->i_count == 0 )
366 free( p_array->pp_elems );
367 p_array->pp_elems = NULL;
373 /************************************************************************
374 * Dictionaries
375 ************************************************************************/
377 /* This function is not intended to be crypto-secure, we only want it to be
378 * fast and not suck too much. This one is pretty fast and did 0 collisions
379 * in wenglish's dictionary.
381 static inline uint64_t DictHash( const char *psz_string, int hashsize )
383 uint64_t i_hash = 0;
384 if( psz_string )
386 while( *psz_string )
388 i_hash += *psz_string++;
389 i_hash += i_hash << 10;
390 i_hash ^= i_hash >> 8;
393 return i_hash % hashsize;
396 typedef struct vlc_dictionary_entry_t
398 char * psz_key;
399 void * p_value;
400 struct vlc_dictionary_entry_t * p_next;
401 } vlc_dictionary_entry_t;
403 typedef struct vlc_dictionary_t
405 int i_size;
406 vlc_dictionary_entry_t ** p_entries;
407 } vlc_dictionary_t;
409 static void * const kVLCDictionaryNotFound = NULL;
411 static inline void vlc_dictionary_init( vlc_dictionary_t * p_dict, int i_size )
413 p_dict->p_entries = NULL;
415 if( i_size > 0 )
417 p_dict->p_entries = (vlc_dictionary_entry_t **)calloc( i_size, sizeof(*p_dict->p_entries) );
418 if( !p_dict->p_entries )
419 i_size = 0;
421 p_dict->i_size = i_size;
424 static inline void vlc_dictionary_clear( vlc_dictionary_t * p_dict,
425 void ( * pf_free )( void * p_data, void * p_obj ),
426 void * p_obj )
428 if( p_dict->p_entries )
430 for( int i = 0; i < p_dict->i_size; i++ )
432 vlc_dictionary_entry_t * p_current, * p_next;
433 p_current = p_dict->p_entries[i];
434 while( p_current )
436 p_next = p_current->p_next;
437 if( pf_free != NULL )
438 ( * pf_free )( p_current->p_value, p_obj );
439 free( p_current->psz_key );
440 free( p_current );
441 p_current = p_next;
444 free( p_dict->p_entries );
445 p_dict->p_entries = NULL;
447 p_dict->i_size = 0;
450 static inline int
451 vlc_dictionary_has_key( const vlc_dictionary_t * p_dict, const char * psz_key )
453 if( !p_dict->p_entries )
454 return 0;
456 int i_pos = DictHash( psz_key, p_dict->i_size );
457 return p_dict->p_entries[i_pos] != NULL;
460 static inline void *
461 vlc_dictionary_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_key )
463 if( !p_dict->p_entries )
464 return kVLCDictionaryNotFound;
466 int i_pos = DictHash( psz_key, p_dict->i_size );
467 vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos];
469 if( !p_entry )
470 return kVLCDictionaryNotFound;
472 /* Make sure we return the right item. (Hash collision) */
473 do {
474 if( !strcmp( psz_key, p_entry->psz_key ) )
475 return p_entry->p_value;
476 p_entry = p_entry->p_next;
477 } while( p_entry );
479 return kVLCDictionaryNotFound;
482 static inline int
483 vlc_dictionary_keys_count( const vlc_dictionary_t * p_dict )
485 vlc_dictionary_entry_t * p_entry;
486 int i, count = 0;
488 if( !p_dict->p_entries )
489 return 0;
491 for( i = 0; i < p_dict->i_size; i++ )
493 for( p_entry = p_dict->p_entries[i]; p_entry; p_entry = p_entry->p_next ) count++;
495 return count;
498 static inline char **
499 vlc_dictionary_all_keys( const vlc_dictionary_t * p_dict )
501 vlc_dictionary_entry_t * p_entry;
502 char ** ppsz_ret;
503 int i, count = vlc_dictionary_keys_count( p_dict );
505 ppsz_ret = (char**)malloc(sizeof(char *) * (count + 1));
506 if( unlikely(!ppsz_ret) )
507 return NULL;
509 count = 0;
510 for( i = 0; i < p_dict->i_size; i++ )
512 for( p_entry = p_dict->p_entries[i]; p_entry; p_entry = p_entry->p_next )
513 ppsz_ret[count++] = strdup( p_entry->psz_key );
515 ppsz_ret[count] = NULL;
516 return ppsz_ret;
519 static inline void
520 __vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key,
521 void * p_value, bool rebuild )
523 if( !p_dict->p_entries )
524 vlc_dictionary_init( p_dict, 1 );
526 int i_pos = DictHash( psz_key, p_dict->i_size );
527 vlc_dictionary_entry_t * p_entry;
529 p_entry = (vlc_dictionary_entry_t *)malloc(sizeof(*p_entry));
530 p_entry->psz_key = strdup( psz_key );
531 p_entry->p_value = p_value;
532 p_entry->p_next = p_dict->p_entries[i_pos];
533 p_dict->p_entries[i_pos] = p_entry;
534 if( rebuild )
536 /* Count how many items there was */
537 int count;
538 for( count = 1; p_entry->p_next; count++ )
539 p_entry = p_entry->p_next;
540 if( count > 3 ) /* XXX: this need tuning */
542 /* Here it starts to be not good, rebuild a bigger dictionary */
543 struct vlc_dictionary_t new_dict;
544 int i_new_size = ( (p_dict->i_size+2) * 3) / 2; /* XXX: this need tuning */
545 int i;
546 vlc_dictionary_init( &new_dict, i_new_size );
547 for( i = 0; i < p_dict->i_size; i++ )
549 p_entry = p_dict->p_entries[i];
550 while( p_entry )
552 __vlc_dictionary_insert( &new_dict, p_entry->psz_key,
553 p_entry->p_value,
554 false /* To avoid multiple rebuild loop */);
555 p_entry = p_entry->p_next;
559 vlc_dictionary_clear( p_dict, NULL, NULL );
560 p_dict->i_size = new_dict.i_size;
561 p_dict->p_entries = new_dict.p_entries;
566 static inline void
567 vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key, void * p_value )
569 __vlc_dictionary_insert( p_dict, psz_key, p_value, true );
572 static inline void
573 vlc_dictionary_remove_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_key,
574 void ( * pf_free )( void * p_data, void * p_obj ),
575 void * p_obj )
577 if( !p_dict->p_entries )
578 return;
580 int i_pos = DictHash( psz_key, p_dict->i_size );
581 vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos];
582 vlc_dictionary_entry_t * p_prev;
584 if( !p_entry )
585 return; /* Not found, nothing to do */
587 /* Hash collision */
588 p_prev = NULL;
589 do {
590 if( !strcmp( psz_key, p_entry->psz_key ) )
592 if( pf_free != NULL )
593 ( * pf_free )( p_entry->p_value, p_obj );
594 if( !p_prev )
595 p_dict->p_entries[i_pos] = p_entry->p_next;
596 else
597 p_prev->p_next = p_entry->p_next;
598 free( p_entry->psz_key );
599 free( p_entry );
600 return;
602 p_prev = p_entry;
603 p_entry = p_entry->p_next;
604 } while( p_entry );
606 /* No key was found */
609 #ifdef __cplusplus
610 // C++ helpers
611 template <typename T>
612 void vlc_delete_all( T &container )
614 typename T::iterator it = container.begin();
615 while ( it != container.end() )
617 delete *it;
618 ++it;
620 container.clear();
623 #endif
625 #endif