macosx: Fix missing colon when copying log message
[vlc.git] / include / vlc_arrays.h
blob8c4419cb6114a32601446beb5bda3d71c83dc431
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 #define TAB_INIT( count, tab ) \
41 do { \
42 (count) = 0; \
43 (tab) = NULL; \
44 } while(0)
46 #define TAB_CLEAN( count, tab ) \
47 do { \
48 free( tab ); \
49 (count)= 0; \
50 (tab)= NULL; \
51 } while(0)
53 #define TAB_APPEND_CAST( cast, count, tab, p ) \
54 do { \
55 if( (count) > 0 ) \
56 (tab) = cast realloc( tab, sizeof( *(tab) ) * ( (count) + 1 ) ); \
57 else \
58 (tab) = cast malloc( sizeof( *(tab) ) ); \
59 if( !(tab) ) abort(); \
60 (tab)[count] = (p); \
61 (count)++; \
62 } while(0)
64 #define TAB_APPEND( count, tab, p ) \
65 TAB_APPEND_CAST( , count, tab, p )
67 #define TAB_FIND( count, tab, p, idx ) \
68 do { \
69 for( (idx) = 0; (idx) < (count); (idx)++ ) \
70 if( (tab)[(idx)] == (p) ) \
71 break; \
72 if( (idx) >= (count) ) \
73 (idx) = -1; \
74 } while(0)
77 #define TAB_ERASE( count, tab, index ) \
78 do { \
79 if( (count) > 1 ) \
80 memmove( (tab) + (index), \
81 (tab) + (index) + 1, \
82 ((count) - (index) - 1 ) * sizeof( *(tab) ) );\
83 (count)--; \
84 if( (count) == 0 ) \
85 { \
86 free( tab ); \
87 (tab) = NULL; \
88 } \
89 } while(0)
91 #define TAB_REMOVE( count, tab, p ) \
92 do { \
93 int i_index; \
94 TAB_FIND( count, tab, p, i_index ); \
95 if( i_index >= 0 ) \
96 TAB_ERASE( count, tab, i_index ); \
97 } while(0)
99 #define TAB_INSERT_CAST( cast, count, tab, p, index ) do { \
100 if( (count) > 0 ) \
101 (tab) = cast realloc( tab, sizeof( *(tab) ) * ( (count) + 1 ) ); \
102 else \
103 (tab) = cast malloc( sizeof( *(tab) ) ); \
104 if( !(tab) ) abort(); \
105 if( (count) - (index) > 0 ) \
106 memmove( (tab) + (index) + 1, \
107 (tab) + (index), \
108 ((count) - (index)) * sizeof( *(tab) ) );\
109 (tab)[(index)] = (p); \
110 (count)++; \
111 } while(0)
113 #define TAB_INSERT( count, tab, p, index ) \
114 TAB_INSERT_CAST( , count, tab, p, index )
117 * Binary search in a sorted array. The key must be comparable by < and >
118 * \param entries array of entries
119 * \param count number of entries
120 * \param elem key to check within an entry (like .id, or ->i_id)
121 * \param zetype type of the key
122 * \param key value of the key
123 * \param answer index of answer within the array. -1 if not found
125 #define BSEARCH( entries, count, elem, zetype, key, answer ) \
126 do { \
127 int low = 0, high = count - 1; \
128 answer = -1; \
129 while( low <= high ) {\
130 int mid = ((unsigned int)low + (unsigned int)high) >> 1;\
131 zetype mid_val = entries[mid] elem;\
132 if( mid_val < key ) \
133 low = mid + 1; \
134 else if ( mid_val > key ) \
135 high = mid -1; \
136 else \
138 answer = mid; break; \
141 } while(0)
144 /************************************************************************
145 * Dynamic arrays with progressive allocation
146 ************************************************************************/
148 /* Internal functions */
149 #define _ARRAY_ALLOC(array, newsize) { \
150 (array).i_alloc = newsize; \
151 (array).p_elems = realloc( (array).p_elems, (array).i_alloc * \
152 sizeof(*(array).p_elems) ); \
153 if( !(array).p_elems ) abort(); \
156 #define _ARRAY_GROW1(array) { \
157 if( (array).i_alloc < 10 ) \
158 _ARRAY_ALLOC(array, 10 ) \
159 else if( (array).i_alloc == (array).i_size ) \
160 _ARRAY_ALLOC(array, (int)((array).i_alloc * 1.5) ) \
163 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
165 /* API */
166 #define DECL_ARRAY(type) struct { \
167 int i_alloc; \
168 int i_size; \
169 type *p_elems; \
172 #define TYPEDEF_ARRAY(type, name) typedef DECL_ARRAY(type) name;
174 #define ARRAY_INIT(array) \
175 do { \
176 (array).i_alloc = 0; \
177 (array).i_size = 0; \
178 (array).p_elems = NULL; \
179 } while(0)
181 #define ARRAY_RESET(array) \
182 do { \
183 (array).i_alloc = 0; \
184 (array).i_size = 0; \
185 free( (array).p_elems ); (array).p_elems = NULL; \
186 } while(0)
188 #define ARRAY_APPEND(array, elem) \
189 do { \
190 _ARRAY_GROW1(array); \
191 (array).p_elems[(array).i_size] = elem; \
192 (array).i_size++; \
193 } while(0)
195 #define ARRAY_INSERT(array,elem,pos) \
196 do { \
197 _ARRAY_GROW1(array); \
198 if( (array).i_size - pos ) { \
199 memmove( (array).p_elems + pos + 1, (array).p_elems + pos, \
200 ((array).i_size-pos) * sizeof(*(array).p_elems) ); \
202 (array).p_elems[pos] = elem; \
203 (array).i_size++; \
204 } while(0)
206 #define _ARRAY_SHRINK(array) { \
207 if( (array).i_size > 10 && (array).i_size < (int)((array).i_alloc / 1.5) ) { \
208 _ARRAY_ALLOC(array, (array).i_size + 5); \
212 #define ARRAY_REMOVE(array,pos) \
213 do { \
214 if( (array).i_size - (pos) - 1 ) \
216 memmove( (array).p_elems + pos, (array).p_elems + pos + 1, \
217 ( (array).i_size - pos - 1 ) *sizeof(*(array).p_elems) ); \
219 (array).i_size--; \
220 _ARRAY_SHRINK(array); \
221 } while(0)
223 #define ARRAY_VAL(array, pos) array.p_elems[pos]
225 #define ARRAY_BSEARCH(array, elem, zetype, key, answer) \
226 BSEARCH( (array).p_elems, (array).i_size, elem, zetype, key, answer)
228 #define FOREACH_ARRAY( item, array ) { \
229 int fe_idx; \
230 for( fe_idx = 0 ; fe_idx < (array).i_size ; fe_idx++ ) \
232 item = (array).p_elems[fe_idx];
234 #define FOREACH_END() } }
237 /************************************************************************
238 * Dynamic arrays with progressive allocation (Preferred API)
239 ************************************************************************/
240 typedef struct vlc_array_t
242 size_t i_count;
243 void ** pp_elems;
244 } vlc_array_t;
246 static inline void vlc_array_init( vlc_array_t * p_array )
248 p_array->i_count = 0;
249 p_array->pp_elems = NULL;
252 static inline void vlc_array_clear( vlc_array_t * p_array )
254 free( p_array->pp_elems );
255 vlc_array_init( p_array );
258 /* Read */
259 static inline size_t vlc_array_count( vlc_array_t * p_array )
261 return p_array->i_count;
264 #if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
265 # define vlc_array_item_at_index(ar, idx) \
266 _Generic((ar), \
267 const vlc_array_t *: ((ar)->pp_elems[idx]), \
268 vlc_array_t *: ((ar)->pp_elems[idx]))
269 #elif defined (__cplusplus)
270 static inline void *vlc_array_item_at_index( vlc_array_t *ar, size_t idx )
272 return ar->pp_elems[idx];
275 static inline const void *vlc_array_item_at_index( const vlc_array_t *ar,
276 size_t idx )
278 return ar->pp_elems[idx];
280 #endif
282 static inline ssize_t vlc_array_index_of_item( const vlc_array_t *ar,
283 const void *elem )
285 for( size_t i = 0; i < ar->i_count; i++ )
287 if( ar->pp_elems[i] == elem )
288 return i;
290 return -1;
293 /* Write */
294 static inline void vlc_array_insert( vlc_array_t *ar, void *elem, int idx )
296 void **pp = (void **)realloc( ar->pp_elems,
297 sizeof( void * ) * (ar->i_count + 1) );
298 if( unlikely(pp == NULL) )
299 abort();
301 size_t tail = ar->i_count - idx;
302 if( tail > 0 )
303 memmove( pp + idx + 1, pp + idx, sizeof( void * ) * tail );
305 pp[idx] = elem;
306 ar->i_count++;
307 ar->pp_elems = pp;
310 static inline void vlc_array_append( vlc_array_t *ar, void *elem )
312 void **pp = (void **)realloc( ar->pp_elems,
313 sizeof( void * ) * (ar->i_count + 1) );
314 if( unlikely(pp == NULL) )
315 abort();
317 pp[ar->i_count++] = elem;
318 ar->pp_elems = pp;
321 static inline void vlc_array_remove( vlc_array_t *ar, size_t idx )
323 void **pp = ar->pp_elems;
324 size_t tail = ar->i_count - idx - 1;
326 if( tail > 0 )
327 memmove( pp + idx, pp + idx + 1, sizeof( void * ) * tail );
329 ar->i_count--;
331 if( ar->i_count > 0 )
333 pp = (void **)realloc( pp, sizeof( void * ) * ar->i_count );
334 if( likely(pp != NULL) )
335 ar->pp_elems = pp;
337 else
339 free( pp );
340 ar->pp_elems = NULL;
345 /************************************************************************
346 * Dictionaries
347 ************************************************************************/
349 /* This function is not intended to be crypto-secure, we only want it to be
350 * fast and not suck too much. This one is pretty fast and did 0 collisions
351 * in wenglish's dictionary.
353 static inline uint64_t DictHash( const char *psz_string, int hashsize )
355 uint64_t i_hash = 0;
356 if( psz_string )
358 while( *psz_string )
360 i_hash += *psz_string++;
361 i_hash += i_hash << 10;
362 i_hash ^= i_hash >> 8;
365 return i_hash % hashsize;
368 typedef struct vlc_dictionary_entry_t
370 char * psz_key;
371 void * p_value;
372 struct vlc_dictionary_entry_t * p_next;
373 } vlc_dictionary_entry_t;
375 typedef struct vlc_dictionary_t
377 int i_size;
378 vlc_dictionary_entry_t ** p_entries;
379 } vlc_dictionary_t;
381 static void * const kVLCDictionaryNotFound = NULL;
383 static inline void vlc_dictionary_init( vlc_dictionary_t * p_dict, int i_size )
385 p_dict->p_entries = NULL;
387 if( i_size > 0 )
389 p_dict->p_entries = (vlc_dictionary_entry_t **)calloc( i_size, sizeof(*p_dict->p_entries) );
390 if( !p_dict->p_entries )
391 i_size = 0;
393 p_dict->i_size = i_size;
396 static inline void vlc_dictionary_clear( vlc_dictionary_t * p_dict,
397 void ( * pf_free )( void * p_data, void * p_obj ),
398 void * p_obj )
400 if( p_dict->p_entries )
402 for( int i = 0; i < p_dict->i_size; i++ )
404 vlc_dictionary_entry_t * p_current, * p_next;
405 p_current = p_dict->p_entries[i];
406 while( p_current )
408 p_next = p_current->p_next;
409 if( pf_free != NULL )
410 ( * pf_free )( p_current->p_value, p_obj );
411 free( p_current->psz_key );
412 free( p_current );
413 p_current = p_next;
416 free( p_dict->p_entries );
417 p_dict->p_entries = NULL;
419 p_dict->i_size = 0;
422 static inline int
423 vlc_dictionary_has_key( const vlc_dictionary_t * p_dict, const char * psz_key )
425 if( !p_dict->p_entries )
426 return 0;
428 int i_pos = DictHash( psz_key, p_dict->i_size );
429 const vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos];
430 for( ; p_entry != NULL; p_entry = p_entry->p_next )
432 if( !strcmp( psz_key, p_entry->psz_key ) )
433 break;
435 return p_entry != NULL;
438 static inline void *
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];
447 if( !p_entry )
448 return kVLCDictionaryNotFound;
450 /* Make sure we return the right item. (Hash collision) */
451 do {
452 if( !strcmp( psz_key, p_entry->psz_key ) )
453 return p_entry->p_value;
454 p_entry = p_entry->p_next;
455 } while( p_entry );
457 return kVLCDictionaryNotFound;
460 static inline int
461 vlc_dictionary_keys_count( const vlc_dictionary_t * p_dict )
463 vlc_dictionary_entry_t * p_entry;
464 int i, count = 0;
466 if( !p_dict->p_entries )
467 return 0;
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++;
473 return count;
476 static inline bool
477 vlc_dictionary_is_empty( const vlc_dictionary_t * p_dict )
479 if( p_dict->p_entries )
480 for( int i = 0; i < p_dict->i_size; i++ )
481 if( p_dict->p_entries[i] )
482 return false;
483 return true;
486 static inline char **
487 vlc_dictionary_all_keys( const vlc_dictionary_t * p_dict )
489 vlc_dictionary_entry_t * p_entry;
490 char ** ppsz_ret;
491 int i, count = vlc_dictionary_keys_count( p_dict );
493 ppsz_ret = (char**)malloc(sizeof(char *) * (count + 1));
494 if( unlikely(!ppsz_ret) )
495 return NULL;
497 count = 0;
498 for( i = 0; i < p_dict->i_size; i++ )
500 for( p_entry = p_dict->p_entries[i]; p_entry; p_entry = p_entry->p_next )
501 ppsz_ret[count++] = strdup( p_entry->psz_key );
503 ppsz_ret[count] = NULL;
504 return ppsz_ret;
507 static inline void
508 vlc_dictionary_insert_impl_( vlc_dictionary_t * p_dict, const char * psz_key,
509 void * p_value, bool rebuild )
511 if( !p_dict->p_entries )
512 vlc_dictionary_init( p_dict, 1 );
514 int i_pos = DictHash( psz_key, p_dict->i_size );
515 vlc_dictionary_entry_t * p_entry;
517 p_entry = (vlc_dictionary_entry_t *)malloc(sizeof(*p_entry));
518 p_entry->psz_key = strdup( psz_key );
519 p_entry->p_value = p_value;
520 p_entry->p_next = p_dict->p_entries[i_pos];
521 p_dict->p_entries[i_pos] = p_entry;
522 if( rebuild )
524 /* Count how many items there was */
525 int count;
526 for( count = 1; p_entry->p_next; count++ )
527 p_entry = p_entry->p_next;
528 if( count > 3 ) /* XXX: this need tuning */
530 /* Here it starts to be not good, rebuild a bigger dictionary */
531 struct vlc_dictionary_t new_dict;
532 int i_new_size = ( (p_dict->i_size+2) * 3) / 2; /* XXX: this need tuning */
533 int i;
534 vlc_dictionary_init( &new_dict, i_new_size );
535 for( i = 0; i < p_dict->i_size; i++ )
537 p_entry = p_dict->p_entries[i];
538 while( p_entry )
540 vlc_dictionary_insert_impl_( &new_dict, p_entry->psz_key,
541 p_entry->p_value,
542 false /* To avoid multiple rebuild loop */);
543 p_entry = p_entry->p_next;
547 vlc_dictionary_clear( p_dict, NULL, NULL );
548 p_dict->i_size = new_dict.i_size;
549 p_dict->p_entries = new_dict.p_entries;
554 static inline void
555 vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key, void * p_value )
557 vlc_dictionary_insert_impl_( p_dict, psz_key, p_value, true );
560 static inline void
561 vlc_dictionary_remove_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_key,
562 void ( * pf_free )( void * p_data, void * p_obj ),
563 void * p_obj )
565 if( !p_dict->p_entries )
566 return;
568 int i_pos = DictHash( psz_key, p_dict->i_size );
569 vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos];
570 vlc_dictionary_entry_t * p_prev;
572 if( !p_entry )
573 return; /* Not found, nothing to do */
575 /* Hash collision */
576 p_prev = NULL;
577 do {
578 if( !strcmp( psz_key, p_entry->psz_key ) )
580 if( pf_free != NULL )
581 ( * pf_free )( p_entry->p_value, p_obj );
582 if( !p_prev )
583 p_dict->p_entries[i_pos] = p_entry->p_next;
584 else
585 p_prev->p_next = p_entry->p_next;
586 free( p_entry->psz_key );
587 free( p_entry );
588 return;
590 p_prev = p_entry;
591 p_entry = p_entry->p_next;
592 } while( p_entry );
594 /* No key was found */
597 #ifdef __cplusplus
598 // C++ helpers
599 template <typename T>
600 void vlc_delete_all( T &container )
602 typename T::iterator it = container.begin();
603 while ( it != container.end() )
605 delete *it;
606 ++it;
608 container.clear();
611 #endif
613 #endif