Playlist
[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 #ifndef _VLC_ARRAYS_H_
25 #define _VLC_ARRAYS_H_
26
27 /**
28  * Simple dynamic array handling. Array is realloced at each insert/removal
29  */
30 #if defined( _MSC_VER ) && _MSC_VER < 1300 && !defined( UNDER_CE )
31 #   define VLCCVP (void**) /* Work-around for broken compiler */
32 #else
33 #   define VLCCVP
34 #endif
35 #define INSERT_ELEM( p_ar, i_oldsize, i_pos, elem )                           \
36     do                                                                        \
37     {                                                                         \
38         if( !i_oldsize ) (p_ar) = NULL;                                       \
39         (p_ar) = VLCCVP realloc( p_ar, ((i_oldsize) + 1) * sizeof(*(p_ar)) ); \
40         if( (i_oldsize) - (i_pos) )                                           \
41         {                                                                     \
42             memmove( (p_ar) + (i_pos) + 1, (p_ar) + (i_pos),                  \
43                      ((i_oldsize) - (i_pos)) * sizeof( *(p_ar) ) );           \
44         }                                                                     \
45         (p_ar)[i_pos] = elem;                                                 \
46         (i_oldsize)++;                                                        \
47     }                                                                         \
48     while( 0 )
49
50 #define REMOVE_ELEM( p_ar, i_oldsize, i_pos )                                 \
51     do                                                                        \
52     {                                                                         \
53         if( (i_oldsize) - (i_pos) - 1 )                                       \
54         {                                                                     \
55             memmove( (p_ar) + (i_pos),                                        \
56                      (p_ar) + (i_pos) + 1,                                    \
57                      ((i_oldsize) - (i_pos) - 1) * sizeof( *(p_ar) ) );       \
58         }                                                                     \
59         if( i_oldsize > 1 )                                                   \
60         {                                                                     \
61             (p_ar) = realloc( p_ar, ((i_oldsize) - 1) * sizeof( *(p_ar) ) );  \
62         }                                                                     \
63         else                                                                  \
64         {                                                                     \
65             free( p_ar );                                                     \
66             (p_ar) = NULL;                                                    \
67         }                                                                     \
68         (i_oldsize)--;                                                        \
69     }                                                                         \
70     while( 0 )
71
72
73 #define TAB_APPEND( count, tab, p )             \
74     if( (count) > 0 )                           \
75     {                                           \
76         (tab) = realloc( tab, sizeof( void ** ) * ( (count) + 1 ) ); \
77     }                                           \
78     else                                        \
79     {                                           \
80         (tab) = malloc( sizeof( void ** ) );    \
81     }                                           \
82     (tab)[count] = (p);        \
83     (count)++
84
85 #define TAB_FIND( count, tab, p, index )        \
86     {                                           \
87         int _i_;                                \
88         (index) = -1;                           \
89         for( _i_ = 0; _i_ < (count); _i_++ )    \
90         {                                       \
91             if( (tab)[_i_] == (p) )  \
92             {                                   \
93                 (index) = _i_;                  \
94                 break;                          \
95             }                                   \
96         }                                       \
97     }
98
99 #define TAB_REMOVE( count, tab, p )             \
100     {                                           \
101         int _i_index_;                          \
102         TAB_FIND( count, tab, p, _i_index_ );   \
103         if( _i_index_ >= 0 )                    \
104         {                                       \
105             if( (count) > 1 )                     \
106             {                                   \
107                 memmove( ((void**)(tab) + _i_index_),    \
108                          ((void**)(tab) + _i_index_+1),  \
109                          ( (count) - _i_index_ - 1 ) * sizeof( void* ) );\
110             }                                   \
111             (count)--;                          \
112             if( (count) == 0 )                  \
113             {                                   \
114                 free( tab );                    \
115                 (tab) = NULL;                   \
116             }                                   \
117         }                                       \
118     }
119
120 /**
121  * Binary search in a sorted array. The key must be comparable by < and >
122  * \param entries array of entries
123  * \param count number of entries
124  * \param elem key to check within an entry (like .id, or ->i_id)
125  * \param zetype type of the key
126  * \param key value of the key
127  * \param answer index of answer within the array. -1 if not found
128  */
129 #define BSEARCH( entries, count, elem, zetype, key, answer ) {  \
130     int low = 0, high = count - 1;   \
131     answer = -1; \
132     while( low <= high ) {\
133         int mid = (low + high ) / 2; /* Just don't care about 2^30 tables */ \
134         zetype mid_val = entries[mid] elem;\
135         if( mid_val < key ) \
136             low = mid + 1; \
137         else if ( mid_val > key ) \
138             high = mid -1;  \
139         else    \
140         {   \
141             answer = mid;  break;   \
142         }\
143     } \
144 }
145
146 /* Dictionnary handling */
147 struct dict_entry_t
148 {
149     int       i_int;
150     char     *psz_string;
151     uint64_t  i_hash;
152     void     *p_data;
153 };
154
155 struct dict_t
156 {
157     dict_entry_t *p_entries;
158     int i_entries;
159 };
160
161 VLC_EXPORT( dict_t *, vlc_DictNew, (void) );
162 VLC_EXPORT( void, vlc_DictClear, (dict_t * ) );
163 VLC_EXPORT( void, vlc_DictInsert, (dict_t *, int, const char *, void * ) );
164 VLC_EXPORT( void*, vlc_DictGet, (dict_t *, int, const char * ) );
165 VLC_EXPORT( int, vlc_DictLookup, (dict_t *, int, const char * ) );
166
167
168 /************************************************************************
169  * Dynamic arrays with progressive allocation
170  ************************************************************************/
171
172 /* Internal functions */
173 //printf("Realloc from %i to %i\n", array.i_alloc, newsize);
174 #define _ARRAY_ALLOC(array, newsize) {                                      \
175     array.i_alloc = newsize;                                                \
176     array.p_elems = VLCCVP realloc( array.p_elems, array.i_alloc *          \
177                                     sizeof(*array.p_elems) );               \
178     assert(array.p_elems);                                                  \
179 }
180
181 #define _ARRAY_GROW1(array) {                                               \
182     if( array.i_alloc < 10 )                                                \
183         _ARRAY_ALLOC(array, 10 )                                            \
184     else if( array.i_alloc == array.i_size )                                \
185         _ARRAY_ALLOC(array, (int)(array.i_alloc * 1.5) )                    \
186 }
187
188 #define _ARRAY_GROW(array,additional) {                                     \
189      int i_first = array.i_alloc;                                           \
190      while( array.i_alloc - i_first < additional )                          \
191      {                                                                      \
192          if( array.i_alloc < 10 )                                           \
193             _ARRAY_ALLOC(array, 10 )                                        \
194         else if( array.i_alloc == array.i_size )                            \
195             _ARRAY_ALLOC(array, (int)(array.i_alloc * 1.5) )                \
196         else break;                                                         \
197      }                                                                      \
198 }
199
200 #define _ARRAY_SHRINK(array) {                                              \
201     if( array.i_size > 10 && array.i_size < (int)(array.i_alloc / 1.5) ) {  \
202         _ARRAY_ALLOC(array, array.i_size + 5);                              \
203     }                                                                       \
204 }
205
206
207 /* API */
208 #define DECL_ARRAY(type) struct {                                           \
209     int i_alloc;                                                            \
210     int i_size;                                                             \
211     type *p_elems;                                                          \
212 }
213
214 #define TYPEDEF_ARRAY(type, name) typedef DECL_ARRAY(type) name;
215
216 #define ARRAY_INIT(array)                                                   \
217     array.i_alloc = 0;                                                      \
218     array.i_size = 0;                                                       \
219     array.p_elems = NULL;
220
221 #define ARRAY_RESET(array)                                                  \
222     array.i_alloc = 0;                                                      \
223     array.i_size = 0;                                                       \
224     free( array.p_elems ); array.p_elems = NULL;
225
226 #define ARRAY_APPEND(array, elem) {                                         \
227     _ARRAY_GROW1(array);                                                    \
228     array.p_elems[array.i_size] = elem;                                     \
229     array.i_size++;                                                         \
230 }
231
232 #define ARRAY_INSERT(array,elem,pos) {                                      \
233     _ARRAY_GROW1(array);                                                    \
234     if( array.i_size - pos ) {                                              \
235         memmove( array.p_elems + pos + 1, array.p_elems + pos,              \
236                  (array.i_size-pos) * sizeof(*array.p_elems) );             \
237     }                                                                       \
238     array.p_elems[pos] = elem;                                              \
239     array.i_size++;                                                         \
240 }
241
242 #define ARRAY_REMOVE(array,pos) {                                           \
243     if( array.i_size - (pos) - 1 )                                          \
244     {                                                                       \
245         memmove( array.p_elems + pos, array.p_elems + pos + 1,              \
246                  ( array.i_size - pos - 1 ) *sizeof(*array.p_elems) );      \
247     }                                                                       \
248     array.i_size--;                                                         \
249     _ARRAY_SHRINK(array);                                                   \
250 }
251
252 #define ARRAY_VAL(array, pos) array.p_elems[pos]
253
254 #define ARRAY_BSEARCH(array, elem, zetype, key, answer) \
255     BSEARCH( array.p_elems, array.i_size, elem, zetype, key, answer)
256
257 #define FOREACH_ARRAY( item, array ) { \
258     int fe_idx; \
259     for( fe_idx = 0 ; fe_idx < array.i_size ; fe_idx++ ) \
260     { \
261         item = array.p_elems[fe_idx];
262
263 #define FOREACH_END() } }
264
265 #endif