aa19662d8dfecd0640072a878db58cc53b30d760
[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 /* Dictionnary handling */
151 struct dict_entry_t
152 {
153     int       i_int;
154     char     *psz_string;
155     uint64_t  i_hash;
156     void     *p_data;
157 };
158
159 struct dict_t
160 {
161     dict_entry_t *p_entries;
162     int i_entries;
163 };
164
165 VLC_EXPORT( dict_t *, vlc_DictNew, (void) );
166 VLC_EXPORT( void, vlc_DictClear, (dict_t * ) );
167 VLC_EXPORT( void, vlc_DictInsert, (dict_t *, int, const char *, void * ) );
168 VLC_EXPORT( void*, vlc_DictGet, (dict_t *, int, const char * ) );
169 VLC_EXPORT( int, vlc_DictLookup, (dict_t *, int, const char * ) );
170
171 /************************************************************************
172  * Dynamic arrays with progressive allocation
173  ************************************************************************/
174
175 /* Internal functions */
176 //printf("Realloc from %i to %i\n", array.i_alloc, newsize);
177 #define _ARRAY_ALLOC(array, newsize) {                                      \
178     array.i_alloc = newsize;                                                \
179     array.p_elems = VLCCVP realloc( array.p_elems, array.i_alloc *          \
180                                     sizeof(*array.p_elems) );               \
181     assert(array.p_elems);                                                  \
182 }
183
184 #define _ARRAY_GROW1(array) {                                               \
185     if( array.i_alloc < 10 )                                                \
186         _ARRAY_ALLOC(array, 10 )                                            \
187     else if( array.i_alloc == array.i_size )                                \
188         _ARRAY_ALLOC(array, (int)(array.i_alloc * 1.5) )                    \
189 }
190
191 #define _ARRAY_GROW(array,additional) {                                     \
192      int i_first = array.i_alloc;                                           \
193      while( array.i_alloc - i_first < additional )                          \
194      {                                                                      \
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) )                \
199         else break;                                                         \
200      }                                                                      \
201 }
202
203 #define _ARRAY_SHRINK(array) {                                              \
204     if( array.i_size > 10 && array.i_size < (int)(array.i_alloc / 1.5) ) {  \
205         _ARRAY_ALLOC(array, array.i_size + 5);                              \
206     }                                                                       \
207 }
208
209
210 /* API */
211 #define DECL_ARRAY(type) struct {                                           \
212     int i_alloc;                                                            \
213     int i_size;                                                             \
214     type *p_elems;                                                          \
215 }
216
217 #define TYPEDEF_ARRAY(type, name) typedef DECL_ARRAY(type) name;
218
219 #define ARRAY_INIT(array)                                                   \
220     array.i_alloc = 0;                                                      \
221     array.i_size = 0;                                                       \
222     array.p_elems = NULL;
223
224 #define ARRAY_RESET(array)                                                  \
225     array.i_alloc = 0;                                                      \
226     array.i_size = 0;                                                       \
227     free( array.p_elems ); array.p_elems = NULL;
228
229 #define ARRAY_APPEND(array, elem) {                                         \
230     _ARRAY_GROW1(array);                                                    \
231     array.p_elems[array.i_size] = elem;                                     \
232     array.i_size++;                                                         \
233 }
234
235 #define ARRAY_INSERT(array,elem,pos) {                                      \
236     _ARRAY_GROW1(array);                                                    \
237     if( array.i_size - pos ) {                                              \
238         memmove( array.p_elems + pos + 1, array.p_elems + pos,              \
239                  (array.i_size-pos) * sizeof(*array.p_elems) );             \
240     }                                                                       \
241     array.p_elems[pos] = elem;                                              \
242     array.i_size++;                                                         \
243 }
244
245 #define ARRAY_REMOVE(array,pos) {                                           \
246     if( array.i_size - (pos) - 1 )                                          \
247     {                                                                       \
248         memmove( array.p_elems + pos, array.p_elems + pos + 1,              \
249                  ( array.i_size - pos - 1 ) *sizeof(*array.p_elems) );      \
250     }                                                                       \
251     array.i_size--;                                                         \
252     _ARRAY_SHRINK(array);                                                   \
253 }
254
255 #define ARRAY_VAL(array, pos) array.p_elems[pos]
256
257 #define ARRAY_BSEARCH(array, elem, zetype, key, answer) \
258     BSEARCH( array.p_elems, array.i_size, elem, zetype, key, answer)
259
260 #define FOREACH_ARRAY( item, array ) { \
261     int fe_idx; \
262     for( fe_idx = 0 ; fe_idx < array.i_size ; fe_idx++ ) \
263     { \
264         item = array.p_elems[fe_idx];
265
266 #define FOREACH_END() } }
267
268 #endif