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
;
40 #define TAB_INIT( count, tab ) \
46 #define TAB_CLEAN( count, tab ) \
53 #define TAB_APPEND_CAST( cast, count, tab, p ) \
56 (tab) = cast realloc( tab, sizeof( *(tab) ) * ( (count) + 1 ) ); \
58 (tab) = cast malloc( sizeof( *(tab) ) ); \
59 if( !(tab) ) abort(); \
64 #define TAB_APPEND( count, tab, p ) \
65 TAB_APPEND_CAST( , count, tab, p )
67 #define TAB_FIND( count, tab, p, idx ) \
69 for( (idx) = 0; (idx) < (count); (idx)++ ) \
70 if( (tab)[(idx)] == (p) ) \
72 if( (idx) >= (count) ) \
77 #define TAB_ERASE( count, tab, index ) \
80 memmove( (tab) + (index), \
81 (tab) + (index) + 1, \
82 ((count) - (index) - 1 ) * sizeof( *(tab) ) );\
91 #define TAB_REMOVE( count, tab, p ) \
94 TAB_FIND( count, tab, p, i_index ); \
96 TAB_ERASE( count, tab, i_index ); \
99 #define TAB_INSERT_CAST( cast, count, tab, p, index ) do { \
101 (tab) = cast realloc( tab, sizeof( *(tab) ) * ( (count) + 1 ) ); \
103 (tab) = cast malloc( sizeof( *(tab) ) ); \
104 if( !(tab) ) abort(); \
105 if( (count) - (index) > 0 ) \
106 memmove( (tab) + (index) + 1, \
108 ((count) - (index)) * sizeof( *(tab) ) );\
109 (tab)[(index)] = (p); \
113 #define TAB_INSERT( count, tab, p, index ) \
114 TAB_INSERT_CAST( , count, tab, p, index )
116 #define INSERT_ELEM( p_ar, i_oldsize, i_pos, elem ) \
117 TAB_INSERT( i_oldsize, p_ar, elem, i_pos )
119 #define REMOVE_ELEM( p_ar, i_size, i_pos ) \
120 TAB_ERASE( i_size, p_ar, i_pos )
123 * Binary search in a sorted array. The key must be comparable by < and >
124 * \param entries array of entries
125 * \param count number of entries
126 * \param elem key to check within an entry (like .id, or ->i_id)
127 * \param zetype type of the key
128 * \param key value of the key
129 * \param answer index of answer within the array. -1 if not found
131 #define BSEARCH( entries, count, elem, zetype, key, answer ) \
133 int low = 0, high = count - 1; \
135 while( low <= high ) {\
136 int mid = ((unsigned int)low + (unsigned int)high) >> 1;\
137 zetype mid_val = entries[mid] elem;\
138 if( mid_val < key ) \
140 else if ( mid_val > key ) \
144 answer = mid; break; \
150 /************************************************************************
151 * Dynamic arrays with progressive allocation
152 ************************************************************************/
154 /* Internal functions */
155 #define _ARRAY_ALLOC(array, newsize) { \
156 (array).i_alloc = newsize; \
157 (array).p_elems = realloc( (array).p_elems, (array).i_alloc * \
158 sizeof(*(array).p_elems) ); \
159 if( !(array).p_elems ) abort(); \
162 #define _ARRAY_GROW1(array) { \
163 if( (array).i_alloc < 10 ) \
164 _ARRAY_ALLOC(array, 10 ) \
165 else if( (array).i_alloc == (array).i_size ) \
166 _ARRAY_ALLOC(array, (int)(array.i_alloc * 1.5) ) \
169 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
172 #define DECL_ARRAY(type) struct { \
178 #define TYPEDEF_ARRAY(type, name) typedef DECL_ARRAY(type) name;
180 #define ARRAY_INIT(array) \
182 (array).i_alloc = 0; \
183 (array).i_size = 0; \
184 (array).p_elems = NULL; \
187 #define ARRAY_RESET(array) \
189 (array).i_alloc = 0; \
190 (array).i_size = 0; \
191 free( (array).p_elems ); (array).p_elems = NULL; \
194 #define ARRAY_APPEND(array, elem) \
196 _ARRAY_GROW1(array); \
197 (array).p_elems[(array).i_size] = elem; \
201 #define ARRAY_INSERT(array,elem,pos) \
203 _ARRAY_GROW1(array); \
204 if( (array).i_size - pos ) { \
205 memmove( (array).p_elems + pos + 1, (array).p_elems + pos, \
206 ((array).i_size-pos) * sizeof(*(array).p_elems) ); \
208 (array).p_elems[pos] = elem; \
212 #define _ARRAY_SHRINK(array) { \
213 if( (array).i_size > 10 && (array).i_size < (int)((array).i_alloc / 1.5) ) { \
214 _ARRAY_ALLOC(array, (array).i_size + 5); \
218 #define ARRAY_REMOVE(array,pos) \
220 if( (array).i_size - (pos) - 1 ) \
222 memmove( (array).p_elems + pos, (array).p_elems + pos + 1, \
223 ( (array).i_size - pos - 1 ) *sizeof(*(array).p_elems) ); \
226 _ARRAY_SHRINK(array); \
229 #define ARRAY_VAL(array, pos) array.p_elems[pos]
231 #define ARRAY_BSEARCH(array, elem, zetype, key, answer) \
232 BSEARCH( (array).p_elems, (array).i_size, elem, zetype, key, answer)
234 #define FOREACH_ARRAY( item, array ) { \
236 for( fe_idx = 0 ; fe_idx < (array).i_size ; fe_idx++ ) \
238 item = (array).p_elems[fe_idx];
240 #define FOREACH_END() } }
243 /************************************************************************
244 * Dynamic arrays with progressive allocation (Preferred API)
245 ************************************************************************/
246 typedef struct vlc_array_t
252 static inline void vlc_array_init( vlc_array_t
* p_array
)
254 p_array
->i_count
= 0;
255 p_array
->pp_elems
= NULL
;
258 static inline void vlc_array_clear( vlc_array_t
* p_array
)
260 free( p_array
->pp_elems
);
261 vlc_array_init( p_array
);
265 static inline size_t vlc_array_count( vlc_array_t
* p_array
)
267 return p_array
->i_count
;
270 #if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
271 # define vlc_array_item_at_index(ar, idx) \
273 const vlc_array_t *: ((ar)->pp_elems[idx]), \
274 vlc_array_t *: ((ar)->pp_elems[idx]))
275 #elif defined (__cplusplus)
276 static inline void *vlc_array_item_at_index( vlc_array_t
*ar
, size_t idx
)
278 return ar
->pp_elems
[idx
];
281 static inline const void *vlc_array_item_at_index( const vlc_array_t
*ar
,
284 return ar
->pp_elems
[idx
];
288 static inline ssize_t
vlc_array_index_of_item( const vlc_array_t
*ar
,
291 for( size_t i
= 0; i
< ar
->i_count
; i
++ )
293 if( ar
->pp_elems
[i
] == elem
)
300 static inline void vlc_array_insert( vlc_array_t
*ar
, void *elem
, int idx
)
302 void **pp
= (void **)realloc( ar
->pp_elems
,
303 sizeof( void * ) * (ar
->i_count
+ 1) );
304 if( unlikely(pp
== NULL
) )
307 size_t tail
= ar
->i_count
- idx
;
309 memmove( pp
+ idx
+ 1, pp
+ idx
, sizeof( void * ) * tail
);
316 static inline void vlc_array_append( vlc_array_t
*ar
, void *elem
)
318 void **pp
= (void **)realloc( ar
->pp_elems
,
319 sizeof( void * ) * (ar
->i_count
+ 1) );
320 if( unlikely(pp
== NULL
) )
323 pp
[ar
->i_count
++] = elem
;
327 static inline void vlc_array_remove( vlc_array_t
*ar
, size_t idx
)
329 void **pp
= ar
->pp_elems
;
330 size_t tail
= ar
->i_count
- idx
- 1;
333 memmove( pp
+ idx
, pp
+ idx
+ 1, sizeof( void * ) * tail
);
337 if( ar
->i_count
> 0 )
339 pp
= (void **)realloc( pp
, sizeof( void * ) * ar
->i_count
);
340 if( likely(pp
!= NULL
) )
351 /************************************************************************
353 ************************************************************************/
355 /* This function is not intended to be crypto-secure, we only want it to be
356 * fast and not suck too much. This one is pretty fast and did 0 collisions
357 * in wenglish's dictionary.
359 static inline uint64_t DictHash( const char *psz_string
, int hashsize
)
366 i_hash
+= *psz_string
++;
367 i_hash
+= i_hash
<< 10;
368 i_hash
^= i_hash
>> 8;
371 return i_hash
% hashsize
;
374 typedef struct vlc_dictionary_entry_t
378 struct vlc_dictionary_entry_t
* p_next
;
379 } vlc_dictionary_entry_t
;
381 typedef struct vlc_dictionary_t
384 vlc_dictionary_entry_t
** p_entries
;
387 static void * const kVLCDictionaryNotFound
= NULL
;
389 static inline void vlc_dictionary_init( vlc_dictionary_t
* p_dict
, int i_size
)
391 p_dict
->p_entries
= NULL
;
395 p_dict
->p_entries
= (vlc_dictionary_entry_t
**)calloc( i_size
, sizeof(*p_dict
->p_entries
) );
396 if( !p_dict
->p_entries
)
399 p_dict
->i_size
= i_size
;
402 static inline void vlc_dictionary_clear( vlc_dictionary_t
* p_dict
,
403 void ( * pf_free
)( void * p_data
, void * p_obj
),
406 if( p_dict
->p_entries
)
408 for( int i
= 0; i
< p_dict
->i_size
; i
++ )
410 vlc_dictionary_entry_t
* p_current
, * p_next
;
411 p_current
= p_dict
->p_entries
[i
];
414 p_next
= p_current
->p_next
;
415 if( pf_free
!= NULL
)
416 ( * pf_free
)( p_current
->p_value
, p_obj
);
417 free( p_current
->psz_key
);
422 free( p_dict
->p_entries
);
423 p_dict
->p_entries
= NULL
;
429 vlc_dictionary_has_key( const vlc_dictionary_t
* p_dict
, const char * psz_key
)
431 if( !p_dict
->p_entries
)
434 int i_pos
= DictHash( psz_key
, p_dict
->i_size
);
435 return p_dict
->p_entries
[i_pos
] != NULL
;
439 vlc_dictionary_value_for_key( const vlc_dictionary_t
* p_dict
, const char * psz_key
)
441 if( !p_dict
->p_entries
)
442 return kVLCDictionaryNotFound
;
444 int i_pos
= DictHash( psz_key
, p_dict
->i_size
);
445 vlc_dictionary_entry_t
* p_entry
= p_dict
->p_entries
[i_pos
];
448 return kVLCDictionaryNotFound
;
450 /* Make sure we return the right item. (Hash collision) */
452 if( !strcmp( psz_key
, p_entry
->psz_key
) )
453 return p_entry
->p_value
;
454 p_entry
= p_entry
->p_next
;
457 return kVLCDictionaryNotFound
;
461 vlc_dictionary_keys_count( const vlc_dictionary_t
* p_dict
)
463 vlc_dictionary_entry_t
* p_entry
;
466 if( !p_dict
->p_entries
)
469 for( i
= 0; i
< p_dict
->i_size
; i
++ )
471 for( p_entry
= p_dict
->p_entries
[i
]; p_entry
; p_entry
= p_entry
->p_next
) count
++;
476 static inline char **
477 vlc_dictionary_all_keys( const vlc_dictionary_t
* p_dict
)
479 vlc_dictionary_entry_t
* p_entry
;
481 int i
, count
= vlc_dictionary_keys_count( p_dict
);
483 ppsz_ret
= (char**)malloc(sizeof(char *) * (count
+ 1));
484 if( unlikely(!ppsz_ret
) )
488 for( i
= 0; i
< p_dict
->i_size
; i
++ )
490 for( p_entry
= p_dict
->p_entries
[i
]; p_entry
; p_entry
= p_entry
->p_next
)
491 ppsz_ret
[count
++] = strdup( p_entry
->psz_key
);
493 ppsz_ret
[count
] = NULL
;
498 __vlc_dictionary_insert( vlc_dictionary_t
* p_dict
, const char * psz_key
,
499 void * p_value
, bool rebuild
)
501 if( !p_dict
->p_entries
)
502 vlc_dictionary_init( p_dict
, 1 );
504 int i_pos
= DictHash( psz_key
, p_dict
->i_size
);
505 vlc_dictionary_entry_t
* p_entry
;
507 p_entry
= (vlc_dictionary_entry_t
*)malloc(sizeof(*p_entry
));
508 p_entry
->psz_key
= strdup( psz_key
);
509 p_entry
->p_value
= p_value
;
510 p_entry
->p_next
= p_dict
->p_entries
[i_pos
];
511 p_dict
->p_entries
[i_pos
] = p_entry
;
514 /* Count how many items there was */
516 for( count
= 1; p_entry
->p_next
; count
++ )
517 p_entry
= p_entry
->p_next
;
518 if( count
> 3 ) /* XXX: this need tuning */
520 /* Here it starts to be not good, rebuild a bigger dictionary */
521 struct vlc_dictionary_t new_dict
;
522 int i_new_size
= ( (p_dict
->i_size
+2) * 3) / 2; /* XXX: this need tuning */
524 vlc_dictionary_init( &new_dict
, i_new_size
);
525 for( i
= 0; i
< p_dict
->i_size
; i
++ )
527 p_entry
= p_dict
->p_entries
[i
];
530 __vlc_dictionary_insert( &new_dict
, p_entry
->psz_key
,
532 false /* To avoid multiple rebuild loop */);
533 p_entry
= p_entry
->p_next
;
537 vlc_dictionary_clear( p_dict
, NULL
, NULL
);
538 p_dict
->i_size
= new_dict
.i_size
;
539 p_dict
->p_entries
= new_dict
.p_entries
;
545 vlc_dictionary_insert( vlc_dictionary_t
* p_dict
, const char * psz_key
, void * p_value
)
547 __vlc_dictionary_insert( p_dict
, psz_key
, p_value
, true );
551 vlc_dictionary_remove_value_for_key( const vlc_dictionary_t
* p_dict
, const char * psz_key
,
552 void ( * pf_free
)( void * p_data
, void * p_obj
),
555 if( !p_dict
->p_entries
)
558 int i_pos
= DictHash( psz_key
, p_dict
->i_size
);
559 vlc_dictionary_entry_t
* p_entry
= p_dict
->p_entries
[i_pos
];
560 vlc_dictionary_entry_t
* p_prev
;
563 return; /* Not found, nothing to do */
568 if( !strcmp( psz_key
, p_entry
->psz_key
) )
570 if( pf_free
!= NULL
)
571 ( * pf_free
)( p_entry
->p_value
, p_obj
);
573 p_dict
->p_entries
[i_pos
] = p_entry
->p_next
;
575 p_prev
->p_next
= p_entry
->p_next
;
576 free( p_entry
->psz_key
);
581 p_entry
= p_entry
->p_next
;
584 /* No key was found */
589 template <typename T
>
590 void vlc_delete_all( T
&container
)
592 typename
T::iterator it
= container
.begin();
593 while ( it
!= container
.end() )