1 /*****************************************************************************
2 * vlc_arrays.h : Arrays and data structures handling
3 *****************************************************************************
4 * Copyright (C) 1999-2004 the VideoLAN team
5 * $Id: vlc_playlist.h 17108 2006-10-15 15:28:34Z zorglub $
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. *****************************************************************************/
24 #if !defined( __LIBVLC__ )
25 #error You are not libvlc or one of its plugins. You cannot include this file
28 #ifndef _VLC_ARRAYS_H_
29 #define _VLC_ARRAYS_H_
34 * Simple dynamic array handling. Array is realloced at each insert/removal
36 #if defined( _MSC_VER ) && _MSC_VER < 1300 && !defined( UNDER_CE )
37 # define VLCCVP (void**) /* Work-around for broken compiler */
41 #define INSERT_ELEM( p_ar, i_oldsize, i_pos, elem ) \
44 if( !i_oldsize ) (p_ar) = NULL; \
45 (p_ar) = VLCCVP realloc( p_ar, ((i_oldsize) + 1) * sizeof(*(p_ar)) ); \
46 if( (i_oldsize) - (i_pos) ) \
48 memmove( (p_ar) + (i_pos) + 1, (p_ar) + (i_pos), \
49 ((i_oldsize) - (i_pos)) * sizeof( *(p_ar) ) ); \
51 (p_ar)[i_pos] = elem; \
56 #define REMOVE_ELEM( p_ar, i_oldsize, i_pos ) \
59 if( (i_oldsize) - (i_pos) - 1 ) \
61 memmove( (p_ar) + (i_pos), \
62 (p_ar) + (i_pos) + 1, \
63 ((i_oldsize) - (i_pos) - 1) * sizeof( *(p_ar) ) ); \
67 (p_ar) = realloc( p_ar, ((i_oldsize) - 1) * sizeof( *(p_ar) ) ); \
78 #define TAB_INIT( count, tab ) \
84 #define TAB_CLEAN( count, tab ) \
86 if( tab ) free( tab ); \
91 #define TAB_APPEND_CAST( cast, count, tab, p ) \
94 (tab) = cast realloc( tab, sizeof( void ** ) * ( (count) + 1 ) ); \
96 (tab) = cast malloc( sizeof( void ** ) ); \
101 #define TAB_APPEND( count, tab, p ) \
102 TAB_APPEND_CAST( , count, tab, p )
103 #define TAB_APPEND_CPP( type, count, tab, p ) \
104 TAB_APPEND_CAST( (type**), count, tab, p )
106 #define TAB_FIND( count, tab, p, index ) \
110 for( _i_ = 0; _i_ < (count); _i_++ ) \
112 if( (tab)[_i_] == (p) ) \
121 #define TAB_REMOVE( count, tab, p ) \
124 TAB_FIND( count, tab, p, _i_index_ ); \
125 if( _i_index_ >= 0 ) \
129 memmove( ((void**)(tab) + _i_index_), \
130 ((void**)(tab) + _i_index_+1), \
131 ( (count) - _i_index_ - 1 ) * sizeof( void* ) );\
142 #define TAB_INSERT_CAST( cast, count, tab, p, index ) do { \
144 (tab) = cast realloc( tab, sizeof( void ** ) * ( (count) + 1 ) ); \
146 (tab) = cast malloc( sizeof( void ** ) ); \
147 if( (count) - (index) > 0 ) \
148 memmove( (void**)(tab) + (index) + 1, \
149 (void**)(tab) + (index), \
150 ((count) - (index)) * sizeof(*(tab)) );\
151 (tab)[(index)] = (p); \
155 #define TAB_INSERT( count, tab, p, index ) \
156 TAB_INSERT_CAST( , count, tab, p, index )
159 * Binary search in a sorted array. The key must be comparable by < and >
160 * \param entries array of entries
161 * \param count number of entries
162 * \param elem key to check within an entry (like .id, or ->i_id)
163 * \param zetype type of the key
164 * \param key value of the key
165 * \param answer index of answer within the array. -1 if not found
167 #define BSEARCH( entries, count, elem, zetype, key, answer ) \
169 int low = 0, high = count - 1; \
171 while( low <= high ) {\
172 int mid = (low + high ) / 2; /* Just don't care about 2^30 tables */ \
173 zetype mid_val = entries[mid] elem;\
174 if( mid_val < key ) \
176 else if ( mid_val > key ) \
180 answer = mid; break; \
186 /************************************************************************
187 * Dynamic arrays with progressive allocation
188 ************************************************************************/
190 /* Internal functions */
191 #define _ARRAY_ALLOC(array, newsize) { \
192 array.i_alloc = newsize; \
193 array.p_elems = VLCCVP realloc( array.p_elems, array.i_alloc * \
194 sizeof(*array.p_elems) ); \
195 assert(array.p_elems); \
198 #define _ARRAY_GROW1(array) { \
199 if( array.i_alloc < 10 ) \
200 _ARRAY_ALLOC(array, 10 ) \
201 else if( array.i_alloc == array.i_size ) \
202 _ARRAY_ALLOC(array, (int)(array.i_alloc * 1.5) ) \
205 #define _ARRAY_GROW(array,additional) { \
206 int i_first = array.i_alloc; \
207 while( array.i_alloc - i_first < additional ) \
209 if( array.i_alloc < 10 ) \
210 _ARRAY_ALLOC(array, 10 ) \
211 else if( array.i_alloc == array.i_size ) \
212 _ARRAY_ALLOC(array, (int)(array.i_alloc * 1.5) ) \
217 #define _ARRAY_SHRINK(array) { \
218 if( array.i_size > 10 && array.i_size < (int)(array.i_alloc / 1.5) ) { \
219 _ARRAY_ALLOC(array, array.i_size + 5); \
225 #define DECL_ARRAY(type) struct { \
231 #define TYPEDEF_ARRAY(type, name) typedef DECL_ARRAY(type) name;
233 #define ARRAY_INIT(array) \
236 array.p_elems = NULL;
238 #define ARRAY_RESET(array) \
241 free( array.p_elems ); array.p_elems = NULL;
243 #define ARRAY_APPEND(array, elem) { \
244 _ARRAY_GROW1(array); \
245 array.p_elems[array.i_size] = elem; \
249 #define ARRAY_INSERT(array,elem,pos) { \
250 _ARRAY_GROW1(array); \
251 if( array.i_size - pos ) { \
252 memmove( array.p_elems + pos + 1, array.p_elems + pos, \
253 (array.i_size-pos) * sizeof(*array.p_elems) ); \
255 array.p_elems[pos] = elem; \
259 #define ARRAY_REMOVE(array,pos) { \
260 if( array.i_size - (pos) - 1 ) \
262 memmove( array.p_elems + pos, array.p_elems + pos + 1, \
263 ( array.i_size - pos - 1 ) *sizeof(*array.p_elems) ); \
266 _ARRAY_SHRINK(array); \
269 #define ARRAY_VAL(array, pos) array.p_elems[pos]
271 #define ARRAY_BSEARCH(array, elem, zetype, key, answer) \
272 BSEARCH( array.p_elems, array.i_size, elem, zetype, key, answer)
274 #define FOREACH_ARRAY( item, array ) { \
276 for( fe_idx = 0 ; fe_idx < array.i_size ; fe_idx++ ) \
278 item = array.p_elems[fe_idx];
280 #define FOREACH_END() } }
283 /************************************************************************
284 * Dynamic arrays with progressive allocation (Prefered API)
285 ************************************************************************/
286 typedef struct vlc_array_t
292 static inline void vlc_array_init( vlc_array_t
* p_array
)
294 memset( p_array
, 0, sizeof(vlc_array_t
) );
297 static inline void vlc_array_clear( vlc_array_t
* p_array
)
299 free( p_array
->pp_elems
);
300 memset( p_array
, 0, sizeof(vlc_array_t
) );
305 vlc_array_count( vlc_array_t
* p_array
)
307 return p_array
->i_count
;
311 vlc_array_object_at_index( vlc_array_t
* p_array
, int i_index
)
313 return p_array
->pp_elems
[i_index
];
318 vlc_array_insert( vlc_array_t
* p_array
, void * p_elem
, int i_index
)
320 TAB_INSERT_CAST( (void **), p_array
->i_count
, p_array
->pp_elems
, p_elem
, i_index
);
324 vlc_array_append( vlc_array_t
* p_array
, void * p_elem
)
326 vlc_array_insert( p_array
, p_elem
, p_array
->i_count
);
330 vlc_array_remove( vlc_array_t
* p_array
, int i_index
)
334 if( p_array
->i_count
> 1 )
336 memmove( p_array
->pp_elems
+ i_index
,
337 p_array
->pp_elems
+ i_index
+1,
338 ( p_array
->i_count
- i_index
- 1 ) * sizeof( void* ) );
341 if( p_array
->i_count
== 0 )
343 free( p_array
->pp_elems
);
344 p_array
->pp_elems
= NULL
;
350 /************************************************************************
352 ************************************************************************/
354 /* This function is not intended to be crypto-secure, we only want it to be
355 * fast and not suck too much. This one is pretty fast and did 0 collisions
356 * in wenglish's dictionary.
358 static inline uint64_t DictHash( const char *psz_string
, int hashsize
)
365 i_hash
+= *psz_string
++;
366 i_hash
+= i_hash
<< 10;
367 i_hash
^= i_hash
>> 8;
370 return i_hash
% hashsize
;
373 struct vlc_dictionary_entry_t
377 struct vlc_dictionary_entry_t
* p_next
;
380 typedef struct vlc_dictionary_t
383 struct vlc_dictionary_entry_t
** p_entries
;
386 static void * const kVLCDictionaryNotFound
= NULL
;
388 static inline void vlc_dictionary_init( vlc_dictionary_t
* p_dict
, int i_size
)
392 p_dict
->p_entries
= (struct vlc_dictionary_entry_t
**)malloc(sizeof(struct vlc_dictionary_entry_t
*) * i_size
);
393 assert( p_dict
->p_entries
);
394 memset( p_dict
->p_entries
, 0, sizeof(struct vlc_dictionary_entry_t
*) * i_size
);
397 p_dict
->p_entries
= NULL
;
398 p_dict
->i_size
= i_size
;
401 static inline void vlc_dictionary_clear( vlc_dictionary_t
* p_dict
)
404 struct vlc_dictionary_entry_t
* p_current
, * p_next
;
405 for( i
= 0; i
< p_dict
->i_size
; i
++ )
407 p_current
= p_dict
->p_entries
[i
];
410 p_next
= p_dict
->p_entries
[i
]->p_next
;
411 free( p_dict
->p_entries
[i
]->psz_key
);
416 free( p_dict
->p_entries
);
423 vlc_dictionary_value_for_key( const vlc_dictionary_t
* p_dict
, const char * psz_key
)
425 if( !p_dict
->p_entries
)
426 return kVLCDictionaryNotFound
;
428 int i_pos
= DictHash( psz_key
, p_dict
->i_size
);
429 struct vlc_dictionary_entry_t
* p_entry
= p_dict
->p_entries
[i_pos
];
431 if( p_entry
&& !p_entry
->p_next
)
432 return p_entry
->p_value
;
435 return kVLCDictionaryNotFound
;
439 if( !strcmp( psz_key
, p_entry
->psz_key
) )
440 return p_entry
->p_value
;
441 p_entry
= p_entry
->p_next
;
444 return kVLCDictionaryNotFound
;
448 vlc_dictionary_keys_count( const vlc_dictionary_t
* p_dict
)
450 struct vlc_dictionary_entry_t
* p_entry
;
452 for( i
= 0; i
< p_dict
->i_size
; i
++ )
454 for( p_entry
= p_dict
->p_entries
[i
]; p_entry
; p_entry
= p_entry
->p_next
) count
++;
459 static inline char **
460 vlc_dictionary_all_keys( const vlc_dictionary_t
* p_dict
)
462 struct vlc_dictionary_entry_t
* p_entry
;
464 int i
, count
= vlc_dictionary_keys_count( p_dict
);
466 ppsz_ret
= (char**)malloc(sizeof(char *) * (count
+ 1));
470 for( i
= 0; i
< p_dict
->i_size
; i
++ )
472 for( p_entry
= p_dict
->p_entries
[i
]; p_entry
; p_entry
= p_entry
->p_next
)
474 ppsz_ret
[count
++] = strdup( p_entry
->psz_key
);
478 ppsz_ret
[count
] = NULL
;
483 __vlc_dictionary_insert( vlc_dictionary_t
* p_dict
, const char * psz_key
,
484 void * p_value
, vlc_bool_t rebuild
)
486 if( !p_dict
->p_entries
)
487 vlc_dictionary_init( p_dict
, 1 );
489 int i_pos
= DictHash( psz_key
, p_dict
->i_size
);
490 struct vlc_dictionary_entry_t
* p_entry
= p_dict
->p_entries
[i_pos
];
494 p_entry
= p_dict
->p_entries
[i_pos
] = (struct vlc_dictionary_entry_t
*)malloc(
495 sizeof(struct vlc_dictionary_entry_t
));
498 p_entry
->psz_key
= strdup( psz_key
);
499 assert( p_entry
->psz_key
);
500 p_entry
->p_value
= p_value
;
501 p_dict
->p_entries
[i_pos
]->p_next
= NULL
;
504 if( p_entry
->p_value
== kVLCDictionaryNotFound
)
506 /* This one is fine, just high jack */
507 p_entry
->psz_key
= strdup( psz_key
);
508 p_entry
->p_value
= p_value
;
512 /* Hash collision here */
513 p_entry
= (struct vlc_dictionary_entry_t
*)malloc(sizeof(struct vlc_dictionary_entry_t
));
515 p_entry
->psz_key
= strdup( psz_key
);
516 p_entry
->p_value
= p_value
;
517 p_entry
->p_next
= p_dict
->p_entries
[i_pos
];
518 p_dict
->p_entries
[i_pos
] = p_entry
;
522 /* Count how many items there was */
524 for( count
= 1; p_entry
->p_next
; count
++ ) p_entry
= p_entry
->p_next
;
525 if( count
> 3 ) /* XXX: this need tuning */
527 /* Here it starts to be not good, rebuild a bigger dictionary */
528 struct vlc_dictionary_t new_dict
;
529 int i_new_size
= ( (p_dict
->i_size
+2) * 3) / 2; /* XXX: this need tuning */
532 vlc_dictionary_init( &new_dict
, i_new_size
);
533 for( i
= 0; i
< p_dict
->i_size
; i
++ )
535 p_entry
= p_dict
->p_entries
[i
];
538 __vlc_dictionary_insert( &new_dict
, p_entry
->psz_key
,
540 0 /* To avoid multiple rebuild loop */);
541 p_entry
= p_entry
->p_next
;
544 vlc_dictionary_clear( p_dict
);
545 p_dict
->i_size
= new_dict
.i_size
;
546 p_dict
->p_entries
= new_dict
.p_entries
;
552 vlc_dictionary_insert( vlc_dictionary_t
* p_dict
, const char * psz_key
, void * p_value
)
554 __vlc_dictionary_insert( p_dict
, psz_key
, p_value
, 1 );
558 vlc_dictionary_remove_value_for_key( const vlc_dictionary_t
* p_dict
, const char * psz_key
)
560 if( !p_dict
->p_entries
)
563 int i_pos
= DictHash( psz_key
, p_dict
->i_size
);
564 struct vlc_dictionary_entry_t
* p_entry
= p_dict
->p_entries
[i_pos
];
565 struct vlc_dictionary_entry_t
* p_prev
;
568 return; /* Not found, nothing to do */
570 if( !p_entry
->p_next
)
572 free( p_entry
->psz_key
);
574 p_dict
->p_entries
[i_pos
] = NULL
;
581 if( !strcmp( psz_key
, p_entry
->psz_key
) )
584 p_dict
->p_entries
[i_pos
] = p_entry
->p_next
;
586 p_prev
->p_next
= p_entry
->p_next
;
587 free( p_entry
->psz_key
);
592 p_entry
= p_entry
->p_next
;
595 /* No key was found */