1 /*****************************************************************************
2 * vlc_arrays.h : Arrays and data structures handling
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VLC authors and VideoLAN
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 *****************************************************************************/
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
;
41 * Simple dynamic array handling. Array is realloced at each insert/removal
43 #if defined( _MSC_VER ) && _MSC_VER < 1300 && !defined( UNDER_CE )
44 # define VLCCVP (void**) /* Work-around for broken compiler */
48 #define INSERT_ELEM( p_ar, i_oldsize, i_pos, elem ) \
51 if( !(i_oldsize) ) (p_ar) = NULL; \
52 (p_ar) = VLCCVP realloc( p_ar, ((i_oldsize) + 1) * sizeof(*(p_ar)) ); \
53 if( !(p_ar) ) abort(); \
54 if( (i_oldsize) - (i_pos) ) \
56 memmove( (p_ar) + (i_pos) + 1, (p_ar) + (i_pos), \
57 ((i_oldsize) - (i_pos)) * sizeof( *(p_ar) ) ); \
59 (p_ar)[(i_pos)] = elem; \
64 #define REMOVE_ELEM( p_ar, i_size, i_pos ) \
67 if( (i_size) - (i_pos) - 1 ) \
69 memmove( (p_ar) + (i_pos), \
70 (p_ar) + (i_pos) + 1, \
71 ((i_size) - (i_pos) - 1) * sizeof( *(p_ar) ) ); \
74 (p_ar) = realloc_down( p_ar, ((i_size) - 1) * sizeof( *(p_ar) ) );\
84 #define TAB_INIT( count, tab ) \
90 #define TAB_CLEAN( count, tab ) \
97 #define TAB_APPEND_CAST( cast, count, tab, p ) \
100 (tab) = cast realloc( tab, sizeof( void ** ) * ( (count) + 1 ) ); \
102 (tab) = cast malloc( sizeof( void ** ) ); \
103 if( !(tab) ) abort(); \
104 (tab)[count] = (p); \
108 #define TAB_APPEND( count, tab, p ) \
109 TAB_APPEND_CAST( , count, tab, p )
111 #define TAB_FIND( count, tab, p, index ) \
114 for( int i = 0; i < (count); i++ ) \
115 if( (tab)[i] == (p) ) \
123 #define TAB_REMOVE( count, tab, p ) \
126 TAB_FIND( count, tab, p, i_index ); \
131 memmove( ((void**)(tab) + i_index), \
132 ((void**)(tab) + i_index+1), \
133 ( (count) - i_index - 1 ) * sizeof( void* ) );\
144 #define TAB_INSERT_CAST( cast, count, tab, p, index ) do { \
146 (tab) = cast realloc( tab, sizeof( void ** ) * ( (count) + 1 ) ); \
148 (tab) = cast malloc( sizeof( void ** ) ); \
149 if( !(tab) ) abort(); \
150 if( (count) - (index) > 0 ) \
151 memmove( (void**)(tab) + (index) + 1, \
152 (void**)(tab) + (index), \
153 ((count) - (index)) * sizeof(*(tab)) );\
154 (tab)[(index)] = (p); \
158 #define TAB_INSERT( count, tab, p, index ) \
159 TAB_INSERT_CAST( , count, tab, p, index )
162 * Binary search in a sorted array. The key must be comparable by < and >
163 * \param entries array of entries
164 * \param count number of entries
165 * \param elem key to check within an entry (like .id, or ->i_id)
166 * \param zetype type of the key
167 * \param key value of the key
168 * \param answer index of answer within the array. -1 if not found
170 #define BSEARCH( entries, count, elem, zetype, key, answer ) \
172 int low = 0, high = count - 1; \
174 while( low <= high ) {\
175 int mid = (low + high ) / 2; /* Just don't care about 2^30 tables */ \
176 zetype mid_val = entries[mid] elem;\
177 if( mid_val < key ) \
179 else if ( mid_val > key ) \
183 answer = mid; break; \
189 /************************************************************************
190 * Dynamic arrays with progressive allocation
191 ************************************************************************/
193 /* Internal functions */
194 #define _ARRAY_ALLOC(array, newsize) { \
195 (array).i_alloc = newsize; \
196 (array).p_elems = VLCCVP realloc( (array).p_elems, (array).i_alloc * \
197 sizeof(*(array).p_elems) ); \
198 if( !(array).p_elems ) abort(); \
201 #define _ARRAY_GROW1(array) { \
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) ) \
208 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
211 #define DECL_ARRAY(type) struct { \
217 #define TYPEDEF_ARRAY(type, name) typedef DECL_ARRAY(type) name;
219 #define ARRAY_INIT(array) \
221 (array).i_alloc = 0; \
222 (array).i_size = 0; \
223 (array).p_elems = NULL; \
226 #define ARRAY_RESET(array) \
228 (array).i_alloc = 0; \
229 (array).i_size = 0; \
230 free( (array).p_elems ); (array).p_elems = NULL; \
233 #define ARRAY_APPEND(array, elem) \
235 _ARRAY_GROW1(array); \
236 (array).p_elems[(array).i_size] = elem; \
240 #define ARRAY_INSERT(array,elem,pos) \
242 _ARRAY_GROW1(array); \
243 if( (array).i_size - pos ) { \
244 memmove( (array).p_elems + pos + 1, (array).p_elems + pos, \
245 ((array).i_size-pos) * sizeof(*(array).p_elems) ); \
247 (array).p_elems[pos] = elem; \
251 #define _ARRAY_SHRINK(array) { \
252 if( (array).i_size > 10 && (array).i_size < (int)((array).i_alloc / 1.5) ) { \
253 _ARRAY_ALLOC(array, (array).i_size + 5); \
257 #define ARRAY_REMOVE(array,pos) \
259 if( (array).i_size - (pos) - 1 ) \
261 memmove( (array).p_elems + pos, (array).p_elems + pos + 1, \
262 ( (array).i_size - pos - 1 ) *sizeof(*(array).p_elems) ); \
265 _ARRAY_SHRINK(array); \
268 #define ARRAY_VAL(array, pos) array.p_elems[pos]
270 #define ARRAY_BSEARCH(array, elem, zetype, key, answer) \
271 BSEARCH( (array).p_elems, (array).i_size, elem, zetype, key, answer)
273 #define FOREACH_ARRAY( item, array ) { \
275 for( fe_idx = 0 ; fe_idx < (array).i_size ; fe_idx++ ) \
277 item = (array).p_elems[fe_idx];
279 #define FOREACH_END() } }
282 /************************************************************************
283 * Dynamic arrays with progressive allocation (Preferred API)
284 ************************************************************************/
285 typedef struct vlc_array_t
291 static inline void vlc_array_init( vlc_array_t
* p_array
)
293 memset( p_array
, 0, sizeof(vlc_array_t
) );
296 static inline void vlc_array_clear( vlc_array_t
* p_array
)
298 free( p_array
->pp_elems
);
299 memset( p_array
, 0, sizeof(vlc_array_t
) );
302 static inline vlc_array_t
* vlc_array_new( void )
304 vlc_array_t
* ret
= (vlc_array_t
*)malloc( sizeof(vlc_array_t
) );
305 if( ret
) vlc_array_init( ret
);
309 static inline void vlc_array_destroy( vlc_array_t
* p_array
)
313 vlc_array_clear( p_array
);
320 vlc_array_count( vlc_array_t
* p_array
)
322 return p_array
->i_count
;
326 vlc_array_item_at_index( vlc_array_t
* p_array
, int i_index
)
328 return p_array
->pp_elems
[i_index
];
332 vlc_array_index_of_item( vlc_array_t
* p_array
, void * item
)
335 for( i
= 0; i
< p_array
->i_count
; i
++)
337 if( p_array
->pp_elems
[i
] == item
)
345 vlc_array_insert( vlc_array_t
* p_array
, void * p_elem
, int i_index
)
347 TAB_INSERT_CAST( (void **), p_array
->i_count
, p_array
->pp_elems
, p_elem
, i_index
);
351 vlc_array_append( vlc_array_t
* p_array
, void * p_elem
)
353 vlc_array_insert( p_array
, p_elem
, p_array
->i_count
);
357 vlc_array_remove( vlc_array_t
* p_array
, int i_index
)
361 if( p_array
->i_count
> 1 )
363 memmove( p_array
->pp_elems
+ i_index
,
364 p_array
->pp_elems
+ i_index
+1,
365 ( p_array
->i_count
- i_index
- 1 ) * sizeof( void* ) );
368 if( p_array
->i_count
== 0 )
370 free( p_array
->pp_elems
);
371 p_array
->pp_elems
= NULL
;
377 /************************************************************************
379 ************************************************************************/
381 /* This function is not intended to be crypto-secure, we only want it to be
382 * fast and not suck too much. This one is pretty fast and did 0 collisions
383 * in wenglish's dictionary.
385 static inline uint64_t DictHash( const char *psz_string
, int hashsize
)
392 i_hash
+= *psz_string
++;
393 i_hash
+= i_hash
<< 10;
394 i_hash
^= i_hash
>> 8;
397 return i_hash
% hashsize
;
400 typedef struct vlc_dictionary_entry_t
404 struct vlc_dictionary_entry_t
* p_next
;
405 } vlc_dictionary_entry_t
;
407 typedef struct vlc_dictionary_t
410 vlc_dictionary_entry_t
** p_entries
;
413 static void * const kVLCDictionaryNotFound
= NULL
;
415 static inline void vlc_dictionary_init( vlc_dictionary_t
* p_dict
, int i_size
)
417 p_dict
->p_entries
= NULL
;
421 p_dict
->p_entries
= (vlc_dictionary_entry_t
**)calloc( i_size
, sizeof(*p_dict
->p_entries
) );
422 if( !p_dict
->p_entries
)
425 p_dict
->i_size
= i_size
;
428 static inline void vlc_dictionary_clear( vlc_dictionary_t
* p_dict
,
429 void ( * pf_free
)( void * p_data
, void * p_obj
),
432 if( p_dict
->p_entries
)
434 for( int i
= 0; i
< p_dict
->i_size
; i
++ )
436 vlc_dictionary_entry_t
* p_current
, * p_next
;
437 p_current
= p_dict
->p_entries
[i
];
440 p_next
= p_current
->p_next
;
441 if( pf_free
!= NULL
)
442 ( * pf_free
)( p_current
->p_value
, p_obj
);
443 free( p_current
->psz_key
);
448 free( p_dict
->p_entries
);
449 p_dict
->p_entries
= NULL
;
455 vlc_dictionary_has_key( const vlc_dictionary_t
* p_dict
, const char * psz_key
)
457 if( !p_dict
->p_entries
)
460 int i_pos
= DictHash( psz_key
, p_dict
->i_size
);
461 return p_dict
->p_entries
[i_pos
] != NULL
;
465 vlc_dictionary_value_for_key( const vlc_dictionary_t
* p_dict
, const char * psz_key
)
467 if( !p_dict
->p_entries
)
468 return kVLCDictionaryNotFound
;
470 int i_pos
= DictHash( psz_key
, p_dict
->i_size
);
471 vlc_dictionary_entry_t
* p_entry
= p_dict
->p_entries
[i_pos
];
474 return kVLCDictionaryNotFound
;
476 /* Make sure we return the right item. (Hash collision) */
478 if( !strcmp( psz_key
, p_entry
->psz_key
) )
479 return p_entry
->p_value
;
480 p_entry
= p_entry
->p_next
;
483 return kVLCDictionaryNotFound
;
487 vlc_dictionary_keys_count( const vlc_dictionary_t
* p_dict
)
489 vlc_dictionary_entry_t
* p_entry
;
492 if( !p_dict
->p_entries
)
495 for( i
= 0; i
< p_dict
->i_size
; i
++ )
497 for( p_entry
= p_dict
->p_entries
[i
]; p_entry
; p_entry
= p_entry
->p_next
) count
++;
502 static inline char **
503 vlc_dictionary_all_keys( const vlc_dictionary_t
* p_dict
)
505 vlc_dictionary_entry_t
* p_entry
;
507 int i
, count
= vlc_dictionary_keys_count( p_dict
);
509 ppsz_ret
= (char**)malloc(sizeof(char *) * (count
+ 1));
510 if( unlikely(!ppsz_ret
) )
514 for( i
= 0; i
< p_dict
->i_size
; i
++ )
516 for( p_entry
= p_dict
->p_entries
[i
]; p_entry
; p_entry
= p_entry
->p_next
)
517 ppsz_ret
[count
++] = strdup( p_entry
->psz_key
);
519 ppsz_ret
[count
] = NULL
;
524 __vlc_dictionary_insert( vlc_dictionary_t
* p_dict
, const char * psz_key
,
525 void * p_value
, bool rebuild
)
527 if( !p_dict
->p_entries
)
528 vlc_dictionary_init( p_dict
, 1 );
530 int i_pos
= DictHash( psz_key
, p_dict
->i_size
);
531 vlc_dictionary_entry_t
* p_entry
;
533 p_entry
= (vlc_dictionary_entry_t
*)malloc(sizeof(*p_entry
));
534 p_entry
->psz_key
= strdup( psz_key
);
535 p_entry
->p_value
= p_value
;
536 p_entry
->p_next
= p_dict
->p_entries
[i_pos
];
537 p_dict
->p_entries
[i_pos
] = p_entry
;
540 /* Count how many items there was */
542 for( count
= 1; p_entry
->p_next
; count
++ )
543 p_entry
= p_entry
->p_next
;
544 if( count
> 3 ) /* XXX: this need tuning */
546 /* Here it starts to be not good, rebuild a bigger dictionary */
547 struct vlc_dictionary_t new_dict
;
548 int i_new_size
= ( (p_dict
->i_size
+2) * 3) / 2; /* XXX: this need tuning */
550 vlc_dictionary_init( &new_dict
, i_new_size
);
551 for( i
= 0; i
< p_dict
->i_size
; i
++ )
553 p_entry
= p_dict
->p_entries
[i
];
556 __vlc_dictionary_insert( &new_dict
, p_entry
->psz_key
,
558 false /* To avoid multiple rebuild loop */);
559 p_entry
= p_entry
->p_next
;
563 vlc_dictionary_clear( p_dict
, NULL
, NULL
);
564 p_dict
->i_size
= new_dict
.i_size
;
565 p_dict
->p_entries
= new_dict
.p_entries
;
571 vlc_dictionary_insert( vlc_dictionary_t
* p_dict
, const char * psz_key
, void * p_value
)
573 __vlc_dictionary_insert( p_dict
, psz_key
, p_value
, true );
577 vlc_dictionary_remove_value_for_key( const vlc_dictionary_t
* p_dict
, const char * psz_key
,
578 void ( * pf_free
)( void * p_data
, void * p_obj
),
581 if( !p_dict
->p_entries
)
584 int i_pos
= DictHash( psz_key
, p_dict
->i_size
);
585 vlc_dictionary_entry_t
* p_entry
= p_dict
->p_entries
[i_pos
];
586 vlc_dictionary_entry_t
* p_prev
;
589 return; /* Not found, nothing to do */
594 if( !strcmp( psz_key
, p_entry
->psz_key
) )
596 if( pf_free
!= NULL
)
597 ( * pf_free
)( p_entry
->p_value
, p_obj
);
599 p_dict
->p_entries
[i_pos
] = p_entry
->p_next
;
601 p_prev
->p_next
= p_entry
->p_next
;
602 free( p_entry
->psz_key
);
607 p_entry
= p_entry
->p_next
;
610 /* No key was found */
615 template <typename T
>
616 void vlc_delete_all( T
&container
)
618 typename
T::iterator it
= container
.begin();
619 while ( it
!= container
.end() )