]> git.sesse.net Git - vlc/blob - include/vlc_arrays.h
A bit of headers cleanup
[vlc] / 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  * Dynamic arrays with progressive allocation
169  ************************************************************************/
170
171 /* Internal functions */
172 //printf("Realloc from %i to %i\n", array.i_alloc, newsize);
173 #define _ARRAY_ALLOC(array, newsize) {                                      \
174     array.i_alloc = newsize;                                                \
175     array.p_elems = VLCCVP realloc( array.p_elems, array.i_alloc *          \
176                                     sizeof(*array.p_elems) );               \
177     assert(array.p_elems);                                                  \
178 }
179
180 #define _ARRAY_GROW1(array) {                                               \
181     if( array.i_alloc < 10 )                                                \
182         _ARRAY_ALLOC(array, 10 )                                            \
183     else if( array.i_alloc == array.i_size )                                \
184         _ARRAY_ALLOC(array, (int)(array.i_alloc * 1.5) )                    \
185 }
186
187 #define _ARRAY_GROW(array,additional) {                                     \
188      int i_first = array.i_alloc;                                           \
189      while( array.i_alloc - i_first < additional )                          \
190      {                                                                      \
191          if( array.i_alloc < 10 )                                           \
192             _ARRAY_ALLOC(array, 10 )                                        \
193         else if( array.i_alloc == array.i_size )                            \
194             _ARRAY_ALLOC(array, (int)(array.i_alloc * 1.5) )                \
195         else break;                                                         \
196      }                                                                      \
197 }
198
199 #define _ARRAY_SHRINK(array) {                                              \
200     if( array.i_size > 10 && array.i_size < (int)(array.i_alloc / 1.5) ) {  \
201         _ARRAY_ALLOC(array, array.i_size + 5);                              \
202     }                                                                       \
203 }
204
205
206 /* API */
207 #define DECL_ARRAY(type) struct {                                           \
208     int i_alloc;                                                            \
209     int i_size;                                                             \
210     type *p_elems;                                                          \
211 }
212
213 #define TYPEDEF_ARRAY(type, name) typedef DECL_ARRAY(type) name;
214
215 #define ARRAY_INIT(array)                                                   \
216     array.i_alloc = 0;                                                      \
217     array.i_size = 0;                                                       \
218     array.p_elems = NULL;
219
220 #define ARRAY_RESET(array)                                                  \
221     array.i_alloc = 0;                                                      \
222     array.i_size = 0;                                                       \
223     free( array.p_elems ); array.p_elems = NULL;
224
225 #define ARRAY_APPEND(array, elem) {                                         \
226     _ARRAY_GROW1(array);                                                    \
227     array.p_elems[array.i_size] = elem;                                     \
228     array.i_size++;                                                         \
229 }
230
231 #define ARRAY_INSERT(array,elem,pos) {                                      \
232     _ARRAY_GROW1(array);                                                    \
233     if( array.i_size - pos ) {                                              \
234         memmove( array.p_elems + pos + 1, array.p_elems + pos,              \
235                  (array.i_size-pos) * sizeof(*array.p_elems) );             \
236     }                                                                       \
237     array.p_elems[pos] = elem;                                              \
238     array.i_size++;                                                         \
239 }
240
241 #define ARRAY_REMOVE(array,pos) {                                           \
242     if( array.i_size - (pos) - 1 )                                          \
243     {                                                                       \
244         memmove( array.p_elems + pos, array.p_elems + pos + 1,              \
245                  ( array.i_size - pos - 1 ) *sizeof(*array.p_elems) );      \
246     }                                                                       \
247     array.i_size--;                                                         \
248     _ARRAY_SHRINK(array);                                                   \
249 }
250
251 #define ARRAY_VAL(array, pos) array.p_elems[pos]
252
253 #define ARRAY_BSEARCH(array, elem, zetype, key, answer) \
254     BSEARCH( array.p_elems, array.i_size, elem, zetype, key, answer)
255
256 #define FOREACH_ARRAY( item, array ) { \
257     int fe_idx; \
258     for( fe_idx = 0 ; fe_idx < array.i_size ; fe_idx++ ) \
259     { \
260         item = array.p_elems[fe_idx];
261
262 #define FOREACH_END() } }
263
264 #endif