Rework dicts as macros for type-independance
[vlc.git] / include / vlc_arrays.h
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 $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          ClĂ©ment Stenac <zorglub@videolan.org>
9  *
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.
14  *
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.
19  *
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
24 #if !defined( __LIBVLC__ )
25   #error You are not libvlc or one of its plugins. You cannot include this file
26 #endif
27
28 #ifndef _VLC_ARRAYS_H_
29 #define _VLC_ARRAYS_H_
30
31 /**
32  * Simple dynamic array handling. Array is realloced at each insert/removal
33  */
34 #if defined( _MSC_VER ) && _MSC_VER < 1300 && !defined( UNDER_CE )
35 #   define VLCCVP (void**) /* Work-around for broken compiler */
36 #else
37 #   define VLCCVP
38 #endif
39 #define INSERT_ELEM( p_ar, i_oldsize, i_pos, elem )                           \
40     do                                                                        \
41     {                                                                         \
42         if( !i_oldsize ) (p_ar) = NULL;                                       \
43         (p_ar) = VLCCVP realloc( p_ar, ((i_oldsize) + 1) * sizeof(*(p_ar)) ); \
44         if( (i_oldsize) - (i_pos) )                                           \
45         {                                                                     \
46             memmove( (p_ar) + (i_pos) + 1, (p_ar) + (i_pos),                  \
47                      ((i_oldsize) - (i_pos)) * sizeof( *(p_ar) ) );           \
48         }                                                                     \
49         (p_ar)[i_pos] = elem;                                                 \
50         (i_oldsize)++;                                                        \
51     }                                                                         \
52     while( 0 )
53
54 #define REMOVE_ELEM( p_ar, i_oldsize, i_pos )                                 \
55     do                                                                        \
56     {                                                                         \
57         if( (i_oldsize) - (i_pos) - 1 )                                       \
58         {                                                                     \
59             memmove( (p_ar) + (i_pos),                                        \
60                      (p_ar) + (i_pos) + 1,                                    \
61                      ((i_oldsize) - (i_pos) - 1) * sizeof( *(p_ar) ) );       \
62         }                                                                     \
63         if( i_oldsize > 1 )                                                   \
64         {                                                                     \
65             (p_ar) = realloc( p_ar, ((i_oldsize) - 1) * sizeof( *(p_ar) ) );  \
66         }                                                                     \
67         else                                                                  \
68         {                                                                     \
69             free( p_ar );                                                     \
70             (p_ar) = NULL;                                                    \
71         }                                                                     \
72         (i_oldsize)--;                                                        \
73     }                                                                         \
74     while( 0 )
75
76
77 #define TAB_APPEND( count, tab, p )             \
78     if( (count) > 0 )                           \
79     {                                           \
80         (tab) = realloc( tab, sizeof( void ** ) * ( (count) + 1 ) ); \
81     }                                           \
82     else                                        \
83     {                                           \
84         (tab) = malloc( sizeof( void ** ) );    \
85     }                                           \
86     (tab)[count] = (p);        \
87     (count)++
88
89 #define TAB_FIND( count, tab, p, index )        \
90     {                                           \
91         int _i_;                                \
92         (index) = -1;                           \
93         for( _i_ = 0; _i_ < (count); _i_++ )    \
94         {                                       \
95             if( (tab)[_i_] == (p) )  \
96             {                                   \
97                 (index) = _i_;                  \
98                 break;                          \
99             }                                   \
100         }                                       \
101     }
102
103 #define TAB_REMOVE( count, tab, p )             \
104     {                                           \
105         int _i_index_;                          \
106         TAB_FIND( count, tab, p, _i_index_ );   \
107         if( _i_index_ >= 0 )                    \
108         {                                       \
109             if( (count) > 1 )                     \
110             {                                   \
111                 memmove( ((void**)(tab) + _i_index_),    \
112                          ((void**)(tab) + _i_index_+1),  \
113                          ( (count) - _i_index_ - 1 ) * sizeof( void* ) );\
114             }                                   \
115             (count)--;                          \
116             if( (count) == 0 )                  \
117             {                                   \
118                 free( tab );                    \
119                 (tab) = NULL;                   \
120             }                                   \
121         }                                       \
122     }
123
124 /**
125  * Binary search in a sorted array. The key must be comparable by < and >
126  * \param entries array of entries
127  * \param count number of entries
128  * \param elem key to check within an entry (like .id, or ->i_id)
129  * \param zetype type of the key
130  * \param key value of the key
131  * \param answer index of answer within the array. -1 if not found
132  */
133 #define BSEARCH( entries, count, elem, zetype, key, answer ) {  \
134     int low = 0, high = count - 1;   \
135     answer = -1; \
136     while( low <= high ) {\
137         int mid = (low + high ) / 2; /* Just don't care about 2^30 tables */ \
138         zetype mid_val = entries[mid] elem;\
139         if( mid_val < key ) \
140             low = mid + 1; \
141         else if ( mid_val > key ) \
142             high = mid -1;  \
143         else    \
144         {   \
145             answer = mid;  break;   \
146         }\
147     } \
148 }
149
150 /************************************************************************
151  * Dictionaries
152  ************************************************************************/
153
154 /* This function is not intended to be crypto-secure, we only want it to be
155  * fast and not suck too much. This one is pretty fast and did 0 collisions
156  * in wenglish's dictionary.
157  */
158 static inline uint64_t DictHash( const char *psz_string, int i_int )
159 {
160     uint64_t i_hash = 0;
161     if( psz_string )
162     {
163         while( *psz_string )
164         {
165             i_hash += *psz_string++;
166             i_hash += i_hash << 10;
167             i_hash ^= i_hash >> 8;
168         }
169     }
170     return i_hash + ( (uint64_t)i_int << 32 );
171 }
172
173 #define DICT_TYPE(name,type)                                                  \
174     typedef struct dict_entry_##name##_t {                                    \
175         int i_int;                                                            \
176         char *psz_string;                                                     \
177         uint64_t i_hash;                                                      \
178         type data;                                                            \
179     } dict_entry_##name##_t;                                                  \
180     typedef struct dict_##name##_t {                                          \
181         dict_entry_##name##_t *p_entries;                                     \
182         int i_entries;                                                        \
183     } dict_##name##_t;
184
185 #define DICT_NEW( p_dict ) {                                                  \
186     p_dict = malloc( sizeof(int)+sizeof(void*) );                             \
187     p_dict->i_entries = 0;                                                    \
188     p_dict->p_entries = NULL;                                                 \
189 }
190
191 #define DICT_CLEAR( zdict ) {                                                 \
192     int _i_dict = 0;                                                          \
193     for ( _i_dict = 0; _i_dict < zdict->i_entries; _i_dict++ )                \
194     {                                                                         \
195         FREE( zdict->p_entries[_i_dict].psz_string );                         \
196     }                                                                         \
197     FREE( zdict->p_entries );                                                 \
198     free( zdict );                                                            \
199 }
200
201 #define DICT_INSERT( zdict, zint, zstring, zdata ) {                          \
202     uint64_t i_hash = DictHash( (zstring), (zint) );                          \
203     int i_new;                                                                \
204     /* Find a free slot */                                                    \
205     if( zdict->i_entries == 0 || i_hash <= zdict->p_entries[0].i_hash )       \
206         i_new = 0;                                                            \
207     else if( i_hash >= zdict->p_entries[zdict->i_entries-1].i_hash )          \
208         i_new = zdict->i_entries;\
209     else                                                                      \
210     {                                                                         \
211         int i_low = 0, i_high = zdict->i_entries - 1;                         \
212         while( i_high - i_low > 1 )                                           \
213         {                                                                     \
214             int i_mid = (i_low + i_high)/2;                                   \
215             fprintf(stderr, "Low %i, high %i\n", i_low, i_high);              \
216             if( zdict->p_entries[i_mid].i_hash < i_hash ) {                   \
217                 i_low = i_mid;                                                \
218             } else if( zdict->p_entries[i_mid].i_hash > i_hash ) {            \
219                 i_high = i_mid;                                               \
220             }                                                                 \
221         }                                                                     \
222         if( zdict->p_entries[i_low].i_hash < i_hash )                         \
223             i_new = i_high;                                                   \
224         else                                                                  \
225             i_new = i_low;                                                    \
226     }                                                                         \
227     zdict->p_entries = realloc( zdict->p_entries, (zdict->i_entries + 1) *    \
228         ( sizeof(zdata) + sizeof(int) + sizeof(void*) + sizeof(uint64_t) ) ); \
229     zdict->i_entries++;                                                       \
230     if( i_new != zdict->i_entries -1 )                                        \
231         memmove( &zdict->p_entries[i_new+1], &zdict->p_entries[i_new],        \
232          ( zdict->i_entries - i_new - 1 ) *                                   \
233          ( sizeof(zdata) + sizeof(int) + sizeof(void*) + sizeof(uint64_t) ) );\
234                                                                               \
235     zdict->p_entries[i_new].i_hash = i_hash;                                  \
236     zdict->p_entries[i_new].i_int = (zint);                                   \
237     if( (zstring) ) {                                                         \
238         zdict->p_entries[i_new].psz_string = strdup( (zstring) );             \
239     } else {                                                                  \
240         zdict->p_entries[i_new].psz_string = NULL;                            \
241     }                                                                         \
242     zdict->p_entries[i_new].data = zdata;                                     \
243 }
244
245 #define DICT_LOOKUP( zdict, zint, zstring, answer ) do {                      \
246     uint64_t i_hash;                                                          \
247     int i, i_pos;                                                             \
248     vlc_bool_t b_found = VLC_FALSE;                                           \
249     if( zdict->i_entries == 0 ) {                                             \
250         answer = -1;                                                          \
251         break;                                                                \
252     }                                                                         \
253                                                                               \
254     i_hash = DictHash( (zstring), (zint) );                                   \
255     BSEARCH( zdict->p_entries, zdict->i_entries, .i_hash, uint64_t,           \
256              i_hash, i_pos );                                                 \
257     if( i_pos == -1 ) {                                                       \
258         answer = -1;                                                          \
259         break;                                                                \
260     }                                                                         \
261                                                                               \
262     /* Hash found, let's check it looks like the entry */                     \
263     if( !strcmp( (zstring), zdict->p_entries[i_pos].psz_string ) ) {          \
264         answer = i_pos;                                                       \
265         break;                                                                \
266     }                                                                         \
267                                                                               \
268     /* Hash collision! This should be very rare, but we cannot guarantee      \
269      * it will never happen. Just do an exhaustive search amongst all         \
270      * entries with the same hash. */                                         \
271     for( i = i_pos - 1 ; i > 0 && i_hash == zdict->p_entries[i].i_hash ; i-- )\
272     {                                                                         \
273         if( !strcmp( (zstring), zdict->p_entries[i].psz_string ) &&           \
274                    zdict->p_entries[i].i_int == (zint) ) {                    \
275             b_found = VLC_TRUE;                                               \
276             answer = i;                                                       \
277             break;                                                            \
278         }                                                                     \
279     }                                                                         \
280     if( b_found == VLC_TRUE )                                                 \
281         break;                                                                \
282     for( i = i_pos + 1 ; i < zdict->i_entries &&                              \
283                          i_hash == zdict->p_entries[i].i_hash ; i++ )         \
284     {                                                                         \
285          if( !strcmp( (zstring), zdict->p_entries[i].psz_string ) &&          \
286                       zdict->p_entries[i].i_int == (zint) ) {                 \
287             b_found = VLC_TRUE;                                               \
288             answer = i;                                                       \
289             break;                                                            \
290         }                                                                     \
291     }                                                                         \
292     /* Hash found, but entry not found (quite strange !) */                   \
293     assert( 0 );                                                              \
294 } while(0)
295
296 #define DICT_GET( zdict, i_int, psz_string, answer ) {                        \
297     int int_answer;                                                           \
298     DICT_LOOKUP( zdict, i_int, psz_string, int_answer );                      \
299     if( int_answer >=  0 )                                                    \
300         answer = zdict->p_entries[int_answer].data;                           \
301 }
302
303 /************************************************************************
304  * Dynamic arrays with progressive allocation
305  ************************************************************************/
306
307 /* Internal functions */
308 #define _ARRAY_ALLOC(array, newsize) {                                      \
309     array.i_alloc = newsize;                                                \
310     array.p_elems = VLCCVP realloc( array.p_elems, array.i_alloc *          \
311                                     sizeof(*array.p_elems) );               \
312     assert(array.p_elems);                                                  \
313 }
314
315 #define _ARRAY_GROW1(array) {                                               \
316     if( array.i_alloc < 10 )                                                \
317         _ARRAY_ALLOC(array, 10 )                                            \
318     else if( array.i_alloc == array.i_size )                                \
319         _ARRAY_ALLOC(array, (int)(array.i_alloc * 1.5) )                    \
320 }
321
322 #define _ARRAY_GROW(array,additional) {                                     \
323      int i_first = array.i_alloc;                                           \
324      while( array.i_alloc - i_first < additional )                          \
325      {                                                                      \
326          if( array.i_alloc < 10 )                                           \
327             _ARRAY_ALLOC(array, 10 )                                        \
328         else if( array.i_alloc == array.i_size )                            \
329             _ARRAY_ALLOC(array, (int)(array.i_alloc * 1.5) )                \
330         else break;                                                         \
331      }                                                                      \
332 }
333
334 #define _ARRAY_SHRINK(array) {                                              \
335     if( array.i_size > 10 && array.i_size < (int)(array.i_alloc / 1.5) ) {  \
336         _ARRAY_ALLOC(array, array.i_size + 5);                              \
337     }                                                                       \
338 }
339
340
341 /* API */
342 #define DECL_ARRAY(type) struct {                                           \
343     int i_alloc;                                                            \
344     int i_size;                                                             \
345     type *p_elems;                                                          \
346 }
347
348 #define TYPEDEF_ARRAY(type, name) typedef DECL_ARRAY(type) name;
349
350 #define ARRAY_INIT(array)                                                   \
351     array.i_alloc = 0;                                                      \
352     array.i_size = 0;                                                       \
353     array.p_elems = NULL;
354
355 #define ARRAY_RESET(array)                                                  \
356     array.i_alloc = 0;                                                      \
357     array.i_size = 0;                                                       \
358     free( array.p_elems ); array.p_elems = NULL;
359
360 #define ARRAY_APPEND(array, elem) {                                         \
361     _ARRAY_GROW1(array);                                                    \
362     array.p_elems[array.i_size] = elem;                                     \
363     array.i_size++;                                                         \
364 }
365
366 #define ARRAY_INSERT(array,elem,pos) {                                      \
367     _ARRAY_GROW1(array);                                                    \
368     if( array.i_size - pos ) {                                              \
369         memmove( array.p_elems + pos + 1, array.p_elems + pos,              \
370                  (array.i_size-pos) * sizeof(*array.p_elems) );             \
371     }                                                                       \
372     array.p_elems[pos] = elem;                                              \
373     array.i_size++;                                                         \
374 }
375
376 #define ARRAY_REMOVE(array,pos) {                                           \
377     if( array.i_size - (pos) - 1 )                                          \
378     {                                                                       \
379         memmove( array.p_elems + pos, array.p_elems + pos + 1,              \
380                  ( array.i_size - pos - 1 ) *sizeof(*array.p_elems) );      \
381     }                                                                       \
382     array.i_size--;                                                         \
383     _ARRAY_SHRINK(array);                                                   \
384 }
385
386 #define ARRAY_VAL(array, pos) array.p_elems[pos]
387
388 #define ARRAY_BSEARCH(array, elem, zetype, key, answer) \
389     BSEARCH( array.p_elems, array.i_size, elem, zetype, key, answer)
390
391 #define FOREACH_ARRAY( item, array ) { \
392     int fe_idx; \
393     for( fe_idx = 0 ; fe_idx < array.i_size ; fe_idx++ ) \
394     { \
395         item = array.p_elems[fe_idx];
396
397 #define FOREACH_END() } }
398
399 #endif