34 static inline void *realloc_down(
void *ptr,
size_t size )
36 void *ret = realloc( ptr, size );
37 return ret ? ret : ptr;
43 #define INSERT_ELEM( p_ar, i_oldsize, i_pos, elem ) \
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) ) \
51 memmove( (p_ar) + (i_pos) + 1, (p_ar) + (i_pos), \
52 ((i_oldsize) - (i_pos)) * sizeof( *(p_ar) ) ); \
54 (p_ar)[(i_pos)] = elem; \
59 #define REMOVE_ELEM( p_ar, i_size, i_pos ) \
62 if( (i_size) - (i_pos) - 1 ) \
64 memmove( (p_ar) + (i_pos), \
65 (p_ar) + (i_pos) + 1, \
66 ((i_size) - (i_pos) - 1) * sizeof( *(p_ar) ) ); \
69 (p_ar) = realloc_down( p_ar, ((i_size) - 1) * sizeof( *(p_ar) ) );\
79 #define TAB_INIT( count, tab ) \
85 #define TAB_CLEAN( count, tab ) \
92 #define TAB_APPEND_CAST( cast, count, tab, p ) \
95 (tab) = cast realloc( tab, sizeof( void ** ) * ( (count) + 1 ) ); \
97 (tab) = cast malloc( sizeof( void ** ) ); \
98 if( !(tab) ) abort(); \
103 #define TAB_APPEND( count, tab, p ) \
104 TAB_APPEND_CAST( , count, tab, p )
106 #define TAB_FIND( count, tab, p, idx ) \
108 for( (idx) = 0; (idx) < (count); (idx)++ ) \
109 if( (tab)[(idx)] == (p) ) \
111 if( (idx) >= (count) ) \
116 #define TAB_REMOVE( count, tab, p ) \
119 TAB_FIND( count, tab, p, i_index ); \
124 memmove( ((void**)(tab) + i_index), \
125 ((void**)(tab) + i_index+1), \
126 ( (count) - i_index - 1 ) * sizeof( void* ) );\
137 #define TAB_INSERT_CAST( cast, count, tab, p, index ) do { \
139 (tab) = cast realloc( tab, sizeof( void ** ) * ( (count) + 1 ) ); \
141 (tab) = cast malloc( sizeof( void ** ) ); \
142 if( !(tab) ) abort(); \
143 if( (count) - (index) > 0 ) \
144 memmove( (void**)(tab) + (index) + 1, \
145 (void**)(tab) + (index), \
146 ((count) - (index)) * sizeof(*(tab)) );\
147 (tab)[(index)] = (p); \
151 #define TAB_INSERT( count, tab, p, index ) \
152 TAB_INSERT_CAST( , count, tab, p, index )
163 #define BSEARCH( entries, count, elem, zetype, key, answer ) \
165 int low = 0, high = count - 1; \
167 while( low <= high ) {\
168 int mid = (low + high ) / 2; \
169 zetype mid_val = entries[mid] elem;\
170 if( mid_val < key ) \
172 else if ( mid_val > key ) \
176 answer = mid; break; \
187 #define _ARRAY_ALLOC(array, newsize) { \
188 (array).i_alloc = newsize; \
189 (array).p_elems = realloc( (array).p_elems, (array).i_alloc * \
190 sizeof(*(array).p_elems) ); \
191 if( !(array).p_elems ) abort(); \
194 #define _ARRAY_GROW1(array) { \
195 if( (array).i_alloc < 10 ) \
196 _ARRAY_ALLOC(array, 10 ) \
197 else if( (array).i_alloc == (array).i_size ) \
198 _ARRAY_ALLOC(array, (int)(array.i_alloc * 1.5) ) \
201 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
204 #define DECL_ARRAY(type) struct { \
210 #define TYPEDEF_ARRAY(type, name) typedef DECL_ARRAY(type) name;
212 #define ARRAY_INIT(array) \
214 (array).i_alloc = 0; \
215 (array).i_size = 0; \
216 (array).p_elems = NULL; \
219 #define ARRAY_RESET(array) \
221 (array).i_alloc = 0; \
222 (array).i_size = 0; \
223 free( (array).p_elems ); (array).p_elems = NULL; \
226 #define ARRAY_APPEND(array, elem) \
228 _ARRAY_GROW1(array); \
229 (array).p_elems[(array).i_size] = elem; \
233 #define ARRAY_INSERT(array,elem,pos) \
235 _ARRAY_GROW1(array); \
236 if( (array).i_size - pos ) { \
237 memmove( (array).p_elems + pos + 1, (array).p_elems + pos, \
238 ((array).i_size-pos) * sizeof(*(array).p_elems) ); \
240 (array).p_elems[pos] = elem; \
244 #define _ARRAY_SHRINK(array) { \
245 if( (array).i_size > 10 && (array).i_size < (int)((array).i_alloc / 1.5) ) { \
246 _ARRAY_ALLOC(array, (array).i_size + 5); \
250 #define ARRAY_REMOVE(array,pos) \
252 if( (array).i_size - (pos) - 1 ) \
254 memmove( (array).p_elems + pos, (array).p_elems + pos + 1, \
255 ( (array).i_size - pos - 1 ) *sizeof(*(array).p_elems) ); \
258 _ARRAY_SHRINK(array); \
261 #define ARRAY_VAL(array, pos) array.p_elems[pos]
263 #define ARRAY_BSEARCH(array, elem, zetype, key, answer) \
264 BSEARCH( (array).p_elems, (array).i_size, elem, zetype, key, answer)
266 #define FOREACH_ARRAY( item, array ) { \
268 for( fe_idx = 0 ; fe_idx < (array).i_size ; fe_idx++ ) \
270 item = (array).p_elems[fe_idx];
272 #define FOREACH_END() } }
284 static inline void vlc_array_init(
vlc_array_t * p_array )
289 static inline void vlc_array_clear(
vlc_array_t * p_array )
291 free( p_array->pp_elems );
298 if( ret ) vlc_array_init( ret );
302 static inline void vlc_array_destroy(
vlc_array_t * p_array )
306 vlc_array_clear( p_array );
315 return p_array->i_count;
319 vlc_array_item_at_index(
vlc_array_t * p_array,
int i_index )
321 return p_array->pp_elems[i_index];
325 vlc_array_index_of_item(
vlc_array_t * p_array,
void * item )
328 for( i = 0; i < p_array->i_count; i++)
330 if( p_array->pp_elems[i] == item )
338 vlc_array_insert(
vlc_array_t * p_array,
void * p_elem,
int i_index )
340 TAB_INSERT_CAST( (
void **), p_array->i_count, p_array->pp_elems, p_elem, i_index );
344 vlc_array_append(
vlc_array_t * p_array,
void * p_elem )
346 vlc_array_insert( p_array, p_elem, p_array->i_count );
350 vlc_array_remove(
vlc_array_t * p_array,
int i_index )
354 if( p_array->i_count > 1 )
356 memmove( p_array->pp_elems + i_index,
357 p_array->pp_elems + i_index+1,
358 ( p_array->i_count - i_index - 1 ) *
sizeof(
void* ) );
361 if( p_array->i_count == 0 )
363 free( p_array->pp_elems );
364 p_array->pp_elems = NULL;
378 static inline uint64_t DictHash(
const char *psz_string,
int hashsize )
385 i_hash += *psz_string++;
386 i_hash += i_hash << 10;
387 i_hash ^= i_hash >> 8;
390 return i_hash % hashsize;
406 static void *
const kVLCDictionaryNotFound = NULL;
408 static inline void vlc_dictionary_init(
vlc_dictionary_t * p_dict,
int i_size )
410 p_dict->p_entries = NULL;
415 if( !p_dict->p_entries )
418 p_dict->i_size = i_size;
422 void ( * pf_free )(
void * p_data,
void * p_obj ),
425 if( p_dict->p_entries )
427 for(
int i = 0; i < p_dict->i_size; i++ )
430 p_current = p_dict->p_entries[i];
433 p_next = p_current->p_next;
434 if( pf_free != NULL )
435 ( * pf_free )( p_current->p_value, p_obj );
436 free( p_current->psz_key );
441 free( p_dict->p_entries );
442 p_dict->p_entries = NULL;
448 vlc_dictionary_has_key(
const vlc_dictionary_t * p_dict,
const char * psz_key )
450 if( !p_dict->p_entries )
453 int i_pos = DictHash( psz_key, p_dict->i_size );
454 return p_dict->p_entries[i_pos] != NULL;
458 vlc_dictionary_value_for_key(
const vlc_dictionary_t * p_dict,
const char * psz_key )
460 if( !p_dict->p_entries )
461 return kVLCDictionaryNotFound;
463 int i_pos = DictHash( psz_key, p_dict->i_size );
467 return kVLCDictionaryNotFound;
471 if( !strcmp( psz_key, p_entry->psz_key ) )
472 return p_entry->p_value;
473 p_entry = p_entry->p_next;
476 return kVLCDictionaryNotFound;
485 if( !p_dict->p_entries )
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 ) count++;
495 static inline char **
500 int i, count = vlc_dictionary_keys_count( p_dict );
502 ppsz_ret = (
char**)malloc(
sizeof(
char *) * (count + 1));
503 if( unlikely(!ppsz_ret) )
507 for( i = 0; i < p_dict->i_size; i++ )
509 for( p_entry = p_dict->p_entries[i]; p_entry; p_entry = p_entry->p_next )
510 ppsz_ret[count++] = strdup( p_entry->psz_key );
512 ppsz_ret[count] = NULL;
518 void * p_value,
bool rebuild )
520 if( !p_dict->p_entries )
521 vlc_dictionary_init( p_dict, 1 );
523 int i_pos = DictHash( psz_key, p_dict->i_size );
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;
535 for( count = 1; p_entry->p_next; count++ )
536 p_entry = p_entry->p_next;
541 int i_new_size = ( (p_dict->i_size+2) * 3) / 2;
543 vlc_dictionary_init( &new_dict, i_new_size );
544 for( i = 0; i < p_dict->i_size; i++ )
546 p_entry = p_dict->p_entries[i];
549 __vlc_dictionary_insert( &new_dict, p_entry->psz_key,
552 p_entry = p_entry->p_next;
556 vlc_dictionary_clear( p_dict, NULL, NULL );
557 p_dict->i_size = new_dict.i_size;
558 p_dict->p_entries = new_dict.p_entries;
564 vlc_dictionary_insert(
vlc_dictionary_t * p_dict,
const char * psz_key,
void * p_value )
566 __vlc_dictionary_insert( p_dict, psz_key, p_value,
true );
570 vlc_dictionary_remove_value_for_key(
const vlc_dictionary_t * p_dict,
const char * psz_key,
571 void ( * pf_free )(
void * p_data,
void * p_obj ),
574 if( !p_dict->p_entries )
577 int i_pos = DictHash( psz_key, p_dict->i_size );
587 if( !strcmp( psz_key, p_entry->psz_key ) )
589 if( pf_free != NULL )
590 ( * pf_free )( p_entry->p_value, p_obj );
592 p_dict->p_entries[i_pos] = p_entry->p_next;
594 p_prev->p_next = p_entry->p_next;
595 free( p_entry->psz_key );
600 p_entry = p_entry->p_next;
608 template <
typename T>
609 void vlc_delete_all( T &container )
611 typename T::iterator it = container.begin();
612 while ( it != container.end() )
Definition: vlc_arrays.h:278
Definition: vlc_arrays.h:393
Definition: vlc_arrays.h:400