Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add a version of gridpp.xml with its tool.
[simgrid.git] / include / xbt / dynar.h
1 /* dynar - a generic dynamic array                                          */
2
3 /* Copyright (c) 2004, 2005, 2006, 2007, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #ifndef _XBT_DYNAR_H
10 #define _XBT_DYNAR_H
11
12 #include <string.h>             /* memcpy */
13
14 #include "xbt/misc.h"           /* SG_BEGIN_DECL */
15 #include "xbt/function_types.h"
16
17 SG_BEGIN_DECL()
18
19 /** @addtogroup XBT_dynar
20   * @brief DynArr are dynamically sized vector which may contain any type of variables.
21   *
22   * These are the SimGrid version of the dynamically size arrays, which all C programmer recode one day or another.
23   *  
24   * For performance concerns, the content of DynArr must be homogeneous (in
25   * contrary to dictionnaries -- see the \ref XBT_dict section). You thus
26   * have to provide the function which will be used to free the content at
27   * structure creation (of type void_f_ppvoid_t or void_f_pvoid_t).
28   *
29   * \section XBT_dynar_exscal Example with scalar
30   * \dontinclude dynar.c
31   *
32   * \skip Vars_decl
33   * \skip dyn
34   * \until iptr
35   * \skip Populate_ints
36   * \skip dyn
37   * \until end_of_traversal
38   * \skip shifting
39   * \skip val
40   * \until xbt_dynar_free
41   *
42   * \section XBT_dynar_exptr Example with pointed data
43   * 
44   * \skip test_dynar_string
45   * \skip dynar_t
46   * \until s2
47   * \skip Populate_str
48   * \skip dyn
49   * \until }
50   * \skip macro
51   * \until dynar_free
52   * \skip end_of_doxygen
53   * \until }
54   *
55   */
56 /** @defgroup XBT_dynar_cons Dynar constructor and destructor
57  *  @ingroup XBT_dynar
58  *
59  *  @{
60  */
61    /** \brief Dynar data type (opaque type) */
62 typedef struct xbt_dynar_s *xbt_dynar_t;
63
64
65 XBT_PUBLIC(xbt_dynar_t) xbt_dynar_new(const unsigned long elm_size,
66                                       void_f_pvoid_t const free_f);
67 XBT_PUBLIC(xbt_dynar_t) xbt_dynar_new_sync(const unsigned long elm_size,
68                                            void_f_pvoid_t const free_f);
69 XBT_INLINE XBT_PUBLIC(void) xbt_dynar_free(xbt_dynar_t * dynar);
70 XBT_PUBLIC(void) xbt_dynar_free_voidp(void *dynar);
71 XBT_PUBLIC(void) xbt_dynar_free_container(xbt_dynar_t * dynar);
72 XBT_PUBLIC(void) xbt_dynar_shrink(xbt_dynar_t dynar, int empty_slots);
73 XBT_PUBLIC(void) xbt_dynar_dump(xbt_dynar_t dynar);
74
75 /** @} */
76 /** @defgroup XBT_dynar_array Dynar as a regular array
77  *  @ingroup XBT_dynar
78  *
79  *  @{
80  */
81
82 XBT_PUBLIC(void) xbt_dynar_get_cpy(const xbt_dynar_t dynar,
83                                    const unsigned long idx,
84                                    void *const dst);
85 XBT_PUBLIC(void) xbt_dynar_set(xbt_dynar_t dynar, const int idx,
86                                const void *src);
87 XBT_PUBLIC(void) xbt_dynar_replace(xbt_dynar_t dynar,
88                                    const unsigned long idx,
89                                    const void *object);
90
91 XBT_PUBLIC(void) xbt_dynar_insert_at(xbt_dynar_t const dynar,
92                                      const int idx, const void *src);
93 XBT_PUBLIC(void) xbt_dynar_remove_at(xbt_dynar_t const dynar,
94                                      const int idx, void *const dst);
95
96 XBT_PUBLIC(unsigned int) xbt_dynar_search(xbt_dynar_t const dynar,
97                                           void *elem);
98 XBT_PUBLIC(int) xbt_dynar_member(xbt_dynar_t const dynar, void *elem);
99 XBT_PUBLIC(void) xbt_dynar_sort(xbt_dynar_t const dynar,
100                                 int_f_cpvoid_cpvoid_t compar_fn);
101 XBT_INLINE XBT_PUBLIC(int) xbt_dynar_compare(xbt_dynar_t d1, xbt_dynar_t d2,
102                                         int(*compar)(const void *, const void *));
103 XBT_INLINE XBT_PUBLIC(void *) xbt_dynar_to_array (xbt_dynar_t dynar);
104
105 /** @} */
106 /** @defgroup XBT_dynar_misc Dynar miscellaneous functions
107  *  @ingroup XBT_dynar
108  *
109  *  @{
110  */
111
112 XBT_INLINE XBT_PUBLIC(unsigned long) xbt_dynar_length(const xbt_dynar_t
113                                                       dynar);
114 XBT_INLINE XBT_PUBLIC(int) xbt_dynar_is_empty(const xbt_dynar_t dynar);
115 XBT_PUBLIC(void) xbt_dynar_reset(xbt_dynar_t const dynar);
116
117
118 /** @} */
119 /** @defgroup XBT_dynar_perl Perl-like use of dynars
120  *  @ingroup XBT_dynar
121  *
122  *  @{
123  */
124
125 XBT_INLINE XBT_PUBLIC(void) xbt_dynar_push(xbt_dynar_t const dynar,
126                                            const void *src);
127 XBT_INLINE XBT_PUBLIC(void) xbt_dynar_pop(xbt_dynar_t const dynar,
128                                           void *const dst);
129 XBT_PUBLIC(void) xbt_dynar_unshift(xbt_dynar_t const dynar,
130                                    const void *src);
131 XBT_PUBLIC(void) xbt_dynar_shift(xbt_dynar_t const dynar, void *const dst);
132 XBT_PUBLIC(void) xbt_dynar_map(const xbt_dynar_t dynar,
133                                void_f_pvoid_t const op);
134
135 /** @} */
136 /** @defgroup XBT_dynar_ctn Direct manipulation to the dynars content
137  *  @ingroup XBT_dynar
138  *
139  *  Those functions do not retrieve the content, but only their address.
140  *
141  *  @{
142  */
143
144 XBT_INLINE XBT_PUBLIC(void *) xbt_dynar_set_at_ptr(const xbt_dynar_t dynar,
145                                                    const unsigned long idx);
146 XBT_INLINE XBT_PUBLIC(void *) xbt_dynar_get_ptr(const xbt_dynar_t dynar,
147                                                 const unsigned long idx);
148 XBT_PUBLIC(void *) xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar,
149                                            const int idx);
150 XBT_PUBLIC(void *) xbt_dynar_push_ptr(xbt_dynar_t const dynar);
151 XBT_PUBLIC(void *) xbt_dynar_pop_ptr(xbt_dynar_t const dynar);
152
153 /** @} */
154 /** @defgroup XBT_dynar_speed Speed optimized access to dynars of scalars
155  *  @ingroup XBT_dynar
156  *
157  *  While the other functions use a memcpy to retrieve the content into the
158  *  user provided area, those ones use a regular affectation. It only works
159  *  for scalar values, but should be a little faster.
160  *
161  *  @{
162  */
163
164   /** @brief Quick retrieval of scalar content 
165    *  @hideinitializer */
166 #  define xbt_dynar_get_as(dynar,idx,type) \
167           (*(type*)xbt_dynar_get_ptr((dynar),(idx)))
168 /** @brief Quick setting of scalar content
169  *  @hideinitializer */
170 #  define xbt_dynar_set_as(dynar,idx,type,val) \
171          (*(type*)xbt_dynar_set_at_ptr((dynar),(idx))) = val
172   /** @brief Quick retrieval of scalar content 
173    *  @hideinitializer */
174 #  define xbt_dynar_getlast_as(dynar,type) \
175           (*(type*)xbt_dynar_get_ptr((dynar),xbt_dynar_length(dynar)-1))
176   /** @brief Quick retrieval of scalar content 
177    *  @hideinitializer */
178 #  define xbt_dynar_getfirst_as(dynar,type) \
179           (*(type*)xbt_dynar_get_ptr((dynar),0))
180   /** @brief Quick insertion of scalar content 
181    *  @hideinitializer */
182 #  define xbt_dynar_insert_at_as(dynar,idx,type,value) \
183           *(type*)xbt_dynar_insert_at_ptr(dynar,idx)=value
184   /** @brief Quick insertion of scalar content 
185    *  @hideinitializer */
186 #  define xbt_dynar_push_as(dynar,type,value) \
187           *(type*)xbt_dynar_push_ptr(dynar)=value
188   /** @brief Quick removal of scalar content
189    *  @hideinitializer */
190 #  define xbt_dynar_pop_as(dynar,type) \
191            (*(type*)xbt_dynar_pop_ptr(dynar))
192
193 /** @} */
194 /** @defgroup XBT_dynar_cursor Cursors on dynar
195  *  @ingroup XBT_dynar
196  *
197  * Cursors are used to iterate over the structure. Never add elements to the 
198  * DynArr during the traversal. To remove elements, use the
199  * xbt_dynar_cursor_rm() function.
200  *
201  * Do not call these functions directly, but only the xbt_dynar_foreach macro.
202  * 
203  * For synchronized dynars, the dynar will be locked during the whole
204  * loop and it will get unlocked automatically if you traverse all
205  * elements. If you want to break the loop before the end, make sure
206  * to call xbt_dynar_cursor_unlock() before the <tt>break;</tt>
207  *
208  *  @{
209  */
210
211 XBT_INLINE XBT_PUBLIC(void) xbt_dynar_cursor_rm(xbt_dynar_t dynar,
212                                                 unsigned int *const
213                                                 cursor);
214 XBT_PUBLIC(void) xbt_dynar_cursor_unlock(xbt_dynar_t dynar);
215
216 /* do not use this structure internals directly, but use the public interface
217  * This was made public to allow:
218  *  - the inlining of the foreach elements
219  *  - sending such beasts over the network
220  */
221
222 #include "xbt/synchro_core.h"
223 typedef struct xbt_dynar_s {
224   unsigned long size;
225   unsigned long used;
226   unsigned long elmsize;
227   void *data;
228   void_f_pvoid_t free_f;
229   xbt_mutex_t mutex;
230 } s_xbt_dynar_t;
231
232 static XBT_INLINE void
233 _xbt_dynar_cursor_first(const xbt_dynar_t dynar,
234                         unsigned int *const cursor)
235 {
236   /* don't test for dynar!=NULL. The segfault would tell us */
237   if (dynar->mutex)             /* ie _dynar_lock(dynar) but not public */
238     xbt_mutex_acquire(dynar->mutex);
239
240   //XBT_DEBUG("Set cursor on %p to the first position", (void *) dynar);
241   *cursor = 0;
242 }
243
244 static XBT_INLINE int
245 _xbt_dynar_cursor_get(const xbt_dynar_t dynar,
246                       unsigned int idx, void *const dst)
247 {
248
249   if (idx >= dynar->used) {
250     //XBT_DEBUG("Cursor on %p already on last elem", (void *) dynar);
251     if (dynar->mutex)           /* unlock */
252       xbt_mutex_release(dynar->mutex);
253     return FALSE;
254   }
255   //  XBT_DEBUG("Cash out cursor on %p at %u", (void *) dynar, *idx);
256
257   memcpy(dst, ((char *) dynar->data) + idx * dynar->elmsize,
258          dynar->elmsize);
259
260   return TRUE;
261 }
262
263
264
265 /** @brief Iterates over the whole dynar. 
266  * 
267  *  @param _dynar what to iterate over
268  *  @param _cursor an integer used as cursor
269  *  @param _data
270  *  @hideinitializer
271  *
272  * \note An example of usage:
273  * \code
274 xbt_dynar_t dyn;
275 unsigned int cpt;
276 string *str;
277 xbt_dynar_foreach (dyn,cpt,str) {
278   printf("Seen %s\n",str);
279 }
280 \endcode
281  */
282 #define xbt_dynar_foreach(_dynar,_cursor,_data) \
283        for (_xbt_dynar_cursor_first(_dynar,&(_cursor))      ; \
284             _xbt_dynar_cursor_get(_dynar,_cursor,&_data) ; \
285             (_cursor)++         )
286
287 /** @} */
288
289 SG_END_DECL()
290 #endif                          /* _XBT_DYNAR_H */