Fix typo in http interface
[vlc.git] / include / vlc_arrays.h
blob14db10b93eba1b0dfe51eae51190fe8defefd375
1 /*****************************************************************************
2 * vlc_arrays.h : Arrays and data structures handling
3 *****************************************************************************
4 * Copyright (C) 1999-2004 the VideoLAN team
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
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 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 General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, 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 #if defined( _MSC_VER ) && _MSC_VER < 1300 && !defined( UNDER_CE )
44 # define VLCCVP (void**) /* Work-around for broken compiler */
45 #else
46 # define VLCCVP
47 #endif
48 #define INSERT_ELEM( p_ar, i_oldsize, i_pos, elem ) \
49 do \
50 { \
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) ) \
55 { \
56 memmove( (p_ar) + (i_pos) + 1, (p_ar) + (i_pos), \
57 ((i_oldsize) - (i_pos)) * sizeof( *(p_ar) ) ); \
58 } \
59 (p_ar)[(i_pos)] = elem; \
60 (i_oldsize)++; \
61 } \
62 while( 0 )
64 #define REMOVE_ELEM( p_ar, i_size, i_pos ) \
65 do \
66 { \
67 if( (i_size) - (i_pos) - 1 ) \
68 { \
69 memmove( (p_ar) + (i_pos), \
70 (p_ar) + (i_pos) + 1, \
71 ((i_size) - (i_pos) - 1) * sizeof( *(p_ar) ) ); \
72 } \
73 if( i_size > 1 ) \
74 (p_ar) = realloc_down( p_ar, ((i_size) - 1) * sizeof( *(p_ar) ) );\
75 else \
76 { \
77 free( p_ar ); \
78 (p_ar) = NULL; \
79 } \
80 (i_size)--; \
81 } \
82 while( 0 )
84 #define TAB_INIT( count, tab ) \
85 do { \
86 (count) = 0; \
87 (tab) = NULL; \
88 } while(0)
90 #define TAB_CLEAN( count, tab ) \
91 do { \
92 free( tab ); \
93 (count)= 0; \
94 (tab)= NULL; \
95 } while(0)
97 #define TAB_APPEND_CAST( cast, count, tab, p ) \
98 do { \
99 if( (count) > 0 ) \
100 (tab) = cast realloc( tab, sizeof( void ** ) * ( (count) + 1 ) ); \
101 else \
102 (tab) = cast malloc( sizeof( void ** ) ); \
103 if( !(tab) ) abort(); \
104 (tab)[count] = (p); \
105 (count)++; \
106 } while(0)
108 #define TAB_APPEND( count, tab, p ) \
109 TAB_APPEND_CAST( , count, tab, p )
111 #define TAB_FIND( count, tab, p, index ) \
112 do { \
113 (index) = -1; \
114 for( int i = 0; i < (count); i++ ) \
115 if( (tab)[i] == (p) ) \
117 (index) = i; \
118 break; \
120 } while(0)
123 #define TAB_REMOVE( count, tab, p ) \
124 do { \
125 int i_index; \
126 TAB_FIND( count, tab, p, i_index ); \
127 if( i_index >= 0 ) \
129 if( (count) > 1 ) \
131 memmove( ((void**)(tab) + i_index), \
132 ((void**)(tab) + i_index+1), \
133 ( (count) - i_index - 1 ) * sizeof( void* ) );\
135 (count)--; \
136 if( (count) == 0 ) \
138 free( tab ); \
139 (tab) = NULL; \
142 } while(0)
144 #define TAB_INSERT_CAST( cast, count, tab, p, index ) do { \
145 if( (count) > 0 ) \
146 (tab) = cast realloc( tab, sizeof( void ** ) * ( (count) + 1 ) ); \
147 else \
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); \
155 (count)++; \
156 } while(0)
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 ) \
171 do { \
172 int low = 0, high = count - 1; \
173 answer = -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 ) \
178 low = mid + 1; \
179 else if ( mid_val > key ) \
180 high = mid -1; \
181 else \
183 answer = mid; break; \
186 } while(0)
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]))
210 /* API */
211 #define DECL_ARRAY(type) struct { \
212 int i_alloc; \
213 int i_size; \
214 type *p_elems; \
217 #define TYPEDEF_ARRAY(type, name) typedef DECL_ARRAY(type) name;
219 #define ARRAY_INIT(array) \
220 do { \
221 (array).i_alloc = 0; \
222 (array).i_size = 0; \
223 (array).p_elems = NULL; \
224 } while(0)
226 #define ARRAY_RESET(array) \
227 do { \
228 (array).i_alloc = 0; \
229 (array).i_size = 0; \
230 free( (array).p_elems ); (array).p_elems = NULL; \
231 } while(0)
233 #define ARRAY_APPEND(array, elem) \
234 do { \
235 _ARRAY_GROW1(array); \
236 (array).p_elems[(array).i_size] = elem; \
237 (array).i_size++; \
238 } while(0)
240 #define ARRAY_INSERT(array,elem,pos) \
241 do { \
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; \
248 (array).i_size++; \
249 } while(0)
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) \
258 do { \
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) ); \
264 (array).i_size--; \
265 _ARRAY_SHRINK(array); \
266 } while(0)
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 ) { \
274 int fe_idx; \
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
287 int i_count;
288 void ** pp_elems;
289 } 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 );
306 return ret;
309 static inline void vlc_array_destroy( vlc_array_t * p_array )
311 if( !p_array )
312 return;
313 vlc_array_clear( p_array );
314 free( p_array );
318 /* Read */
319 static inline int
320 vlc_array_count( vlc_array_t * p_array )
322 return p_array->i_count;
325 static inline void *
326 vlc_array_item_at_index( vlc_array_t * p_array, int i_index )
328 return p_array->pp_elems[i_index];
331 static inline int
332 vlc_array_index_of_item( vlc_array_t * p_array, void * item )
334 int i;
335 for( i = 0; i < p_array->i_count; i++)
337 if( p_array->pp_elems[i] == item )
338 return i;
340 return -1;
343 /* Write */
344 static inline void
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 );
350 static inline void
351 vlc_array_append( vlc_array_t * p_array, void * p_elem )
353 vlc_array_insert( p_array, p_elem, p_array->i_count );
356 static inline void
357 vlc_array_remove( vlc_array_t * p_array, int i_index )
359 if( i_index >= 0 )
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* ) );
367 p_array->i_count--;
368 if( p_array->i_count == 0 )
370 free( p_array->pp_elems );
371 p_array->pp_elems = NULL;
377 /************************************************************************
378 * Dictionaries
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 )
387 uint64_t i_hash = 0;
388 if( psz_string )
390 while( *psz_string )
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
402 char * psz_key;
403 void * p_value;
404 struct vlc_dictionary_entry_t * p_next;
405 } vlc_dictionary_entry_t;
407 typedef struct vlc_dictionary_t
409 int i_size;
410 vlc_dictionary_entry_t ** p_entries;
411 } vlc_dictionary_t;
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;
419 if( i_size > 0 )
421 p_dict->p_entries = (vlc_dictionary_entry_t **)calloc( i_size, sizeof(*p_dict->p_entries) );
422 if( !p_dict->p_entries )
423 i_size = 0;
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 ),
430 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];
438 while( p_current )
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 );
444 free( p_current );
445 p_current = p_next;
448 free( p_dict->p_entries );
449 p_dict->p_entries = NULL;
451 p_dict->i_size = 0;
456 static inline void *
457 vlc_dictionary_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_key )
459 if( !p_dict->p_entries )
460 return kVLCDictionaryNotFound;
462 int i_pos = DictHash( psz_key, p_dict->i_size );
463 vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos];
465 if( !p_entry )
466 return kVLCDictionaryNotFound;
468 /* Make sure we return the right item. (Hash collision) */
469 do {
470 if( !strcmp( psz_key, p_entry->psz_key ) )
471 return p_entry->p_value;
472 p_entry = p_entry->p_next;
473 } while( p_entry );
475 return kVLCDictionaryNotFound;
478 static inline int
479 vlc_dictionary_keys_count( const vlc_dictionary_t * p_dict )
481 vlc_dictionary_entry_t * p_entry;
482 int i, count = 0;
484 if( !p_dict->p_entries )
485 return 0;
487 for( i = 0; i < p_dict->i_size; i++ )
489 for( p_entry = p_dict->p_entries[i]; p_entry; p_entry = p_entry->p_next ) count++;
491 return count;
494 static inline char **
495 vlc_dictionary_all_keys( const vlc_dictionary_t * p_dict )
497 vlc_dictionary_entry_t * p_entry;
498 char ** ppsz_ret;
499 int i, count = vlc_dictionary_keys_count( p_dict );
501 ppsz_ret = (char**)malloc(sizeof(char *) * (count + 1));
503 count = 0;
504 for( i = 0; i < p_dict->i_size; i++ )
506 for( p_entry = p_dict->p_entries[i]; p_entry; p_entry = p_entry->p_next )
507 ppsz_ret[count++] = strdup( p_entry->psz_key );
509 ppsz_ret[count] = NULL;
510 return ppsz_ret;
513 static inline void
514 __vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key,
515 void * p_value, bool rebuild )
517 if( !p_dict->p_entries )
518 vlc_dictionary_init( p_dict, 1 );
520 int i_pos = DictHash( psz_key, p_dict->i_size );
521 vlc_dictionary_entry_t * p_entry;
523 p_entry = (vlc_dictionary_entry_t *)malloc(sizeof(*p_entry));
524 p_entry->psz_key = strdup( psz_key );
525 p_entry->p_value = p_value;
526 p_entry->p_next = p_dict->p_entries[i_pos];
527 p_dict->p_entries[i_pos] = p_entry;
528 if( rebuild )
530 /* Count how many items there was */
531 int count;
532 for( count = 1; p_entry->p_next; count++ )
533 p_entry = p_entry->p_next;
534 if( count > 3 ) /* XXX: this need tuning */
536 /* Here it starts to be not good, rebuild a bigger dictionary */
537 struct vlc_dictionary_t new_dict;
538 int i_new_size = ( (p_dict->i_size+2) * 3) / 2; /* XXX: this need tuning */
539 int i;
540 vlc_dictionary_init( &new_dict, i_new_size );
541 for( i = 0; i < p_dict->i_size; i++ )
543 p_entry = p_dict->p_entries[i];
544 while( p_entry )
546 __vlc_dictionary_insert( &new_dict, p_entry->psz_key,
547 p_entry->p_value,
548 false /* To avoid multiple rebuild loop */);
549 p_entry = p_entry->p_next;
553 vlc_dictionary_clear( p_dict, NULL, NULL );
554 p_dict->i_size = new_dict.i_size;
555 p_dict->p_entries = new_dict.p_entries;
560 static inline void
561 vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key, void * p_value )
563 __vlc_dictionary_insert( p_dict, psz_key, p_value, true );
566 static inline void
567 vlc_dictionary_remove_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_key,
568 void ( * pf_free )( void * p_data, void * p_obj ),
569 void * p_obj )
571 if( !p_dict->p_entries )
572 return;
574 int i_pos = DictHash( psz_key, p_dict->i_size );
575 vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos];
576 vlc_dictionary_entry_t * p_prev;
578 if( !p_entry )
579 return; /* Not found, nothing to do */
581 /* Hash collision */
582 p_prev = NULL;
583 do {
584 if( !strcmp( psz_key, p_entry->psz_key ) )
586 if( pf_free != NULL )
587 ( * pf_free )( p_entry->p_value, p_obj );
588 if( !p_prev )
589 p_dict->p_entries[i_pos] = p_entry->p_next;
590 else
591 p_prev->p_next = p_entry->p_next;
592 free( p_entry->psz_key );
593 free( p_entry );
594 return;
596 p_prev = p_entry;
597 p_entry = p_entry->p_next;
598 } while( p_entry );
600 /* No key was found */
603 #ifdef __cplusplus
604 // C++ helpers
605 template <typename T>
606 void vlc_delete_all( T &container )
608 typename T::iterator it = container.begin();
609 while ( it != container.end() )
611 delete *it;
612 ++it;
614 container.clear();
617 #endif
619 #endif