Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
aa03316140c1791e0941507f39238f68a325bcd7
[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
73 XBT_INLINE XBT_PUBLIC(unsigned long) xbt_dynar_length(const xbt_dynar_t
74                                                       dynar);
75 XBT_INLINE XBT_PUBLIC(int) xbt_dynar_is_empty(const xbt_dynar_t dynar);
76 XBT_PUBLIC(void) xbt_dynar_reset(xbt_dynar_t const dynar);
77 XBT_PUBLIC(void) xbt_dynar_shrink(xbt_dynar_t dynar, int empty_slots);
78
79 XBT_PUBLIC(void) xbt_dynar_dump(xbt_dynar_t dynar);
80
81 /** @} */
82 /** @defgroup XBT_dynar_array Dynar as a regular array
83  *  @ingroup XBT_dynar
84  *
85  *  @{
86  */
87
88 XBT_PUBLIC(void) xbt_dynar_get_cpy(const xbt_dynar_t dynar,
89                                    const unsigned long idx,
90                                    void *const dst);
91
92 XBT_PUBLIC(void) xbt_dynar_set(xbt_dynar_t dynar, const int idx,
93                                const void *src);
94 XBT_PUBLIC(void) xbt_dynar_replace(xbt_dynar_t dynar,
95                                    const unsigned long idx,
96                                    const void *object);
97
98 XBT_PUBLIC(void) xbt_dynar_insert_at(xbt_dynar_t const dynar,
99                                      const int idx, const void *src);
100 XBT_PUBLIC(void) xbt_dynar_remove_at(xbt_dynar_t const dynar,
101                                      const int idx, void *const dst);
102
103 XBT_PUBLIC(unsigned int) xbt_dynar_search(xbt_dynar_t const dynar,
104                                           void *elem);
105 XBT_PUBLIC(int) xbt_dynar_member(xbt_dynar_t const dynar, void *elem);
106 XBT_PUBLIC(void) xbt_dynar_sort(xbt_dynar_t const dynar,
107                                 int_f_cpvoid_cpvoid_t compar_fn);
108 XBT_INLINE int xbt_dynar_compare(xbt_dynar_t d1, xbt_dynar_t d2,
109                                         int(*compar)(const void *, const void *));
110
111 /** @} */
112 /** @defgroup XBT_dynar_perl Perl-like use of dynars
113  *  @ingroup XBT_dynar
114  *
115  *  @{
116  */
117
118 XBT_INLINE XBT_PUBLIC(void) xbt_dynar_push(xbt_dynar_t const dynar,
119                                            const void *src);
120 XBT_INLINE XBT_PUBLIC(void) xbt_dynar_pop(xbt_dynar_t const dynar,
121                                           void *const dst);
122 XBT_PUBLIC(void) xbt_dynar_unshift(xbt_dynar_t const dynar,
123                                    const void *src);
124 XBT_PUBLIC(void) xbt_dynar_shift(xbt_dynar_t const dynar, void *const dst);
125 XBT_PUBLIC(void) xbt_dynar_map(const xbt_dynar_t dynar,
126                                void_f_pvoid_t const op);
127
128 /** @} */
129 /** @defgroup XBT_dynar_ctn Direct manipulation to the dynars content
130  *  @ingroup XBT_dynar
131  *
132  *  Those functions do not retrieve the content, but only their address.
133  *
134  *  @{
135  */
136
137 XBT_INLINE XBT_PUBLIC(void *) xbt_dynar_get_ptr(const xbt_dynar_t dynar,
138                                                 const unsigned long idx);
139 XBT_PUBLIC(void *) xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar,
140                                            const int idx);
141 XBT_PUBLIC(void *) xbt_dynar_push_ptr(xbt_dynar_t const dynar);
142 XBT_PUBLIC(void *) xbt_dynar_pop_ptr(xbt_dynar_t const dynar);
143
144 /** @} */
145 /** @defgroup XBT_dynar_speed Speed optimized access to dynars of scalars
146  *  @ingroup XBT_dynar
147  *
148  *  While the other functions use a memcpy to retrieve the content into the
149  *  user provided area, those ones use a regular affectation. It only works
150  *  for scalar values, but should be a little faster.
151  *
152  *  @{
153  */
154
155   /** @brief Quick retrieval of scalar content 
156    *  @hideinitializer */
157 #  define xbt_dynar_get_as(dynar,idx,type) \
158           (*(type*)xbt_dynar_get_ptr((dynar),(idx)))
159   /** @brief Quick retrieval of scalar content 
160    *  @hideinitializer */
161 #  define xbt_dynar_getlast_as(dynar,type) \
162           (*(type*)xbt_dynar_get_ptr((dynar),xbt_dynar_length(dynar)-1))
163   /** @brief Quick retrieval of scalar content 
164    *  @hideinitializer */
165 #  define xbt_dynar_getfirst_as(dynar,type) \
166           (*(type*)xbt_dynar_get_ptr((dynar),0))
167   /** @brief Quick insertion of scalar content 
168    *  @hideinitializer */
169 #  define xbt_dynar_insert_at_as(dynar,idx,type,value) \
170           *(type*)xbt_dynar_insert_at_ptr(dynar,idx)=value
171   /** @brief Quick insertion of scalar content 
172    *  @hideinitializer */
173 #  define xbt_dynar_push_as(dynar,type,value) \
174           *(type*)xbt_dynar_push_ptr(dynar)=value
175   /** @brief Quick removal of scalar content
176    *  @hideinitializer */
177 #  define xbt_dynar_pop_as(dynar,type) \
178            (*(type*)xbt_dynar_pop_ptr(dynar))
179
180 /** @} */
181 /** @defgroup XBT_dynar_cursor Cursors on dynar
182  *  @ingroup XBT_dynar
183  *
184  * Cursors are used to iterate over the structure. Never add elements to the 
185  * DynArr during the traversal. To remove elements, use the
186  * xbt_dynar_cursor_rm() function.
187  *
188  * Do not call these functions directly, but only the xbt_dynar_foreach macro.
189  * 
190  * For synchronized dynars, the dynar will be locked during the whole
191  * loop and it will get unlocked automatically if you traverse all
192  * elements. If you want to break the loop before the end, make sure
193  * to call xbt_dynar_cursor_unlock() before the <tt>break;</tt>
194  *
195  *  @{
196  */
197
198 XBT_INLINE XBT_PUBLIC(void) xbt_dynar_cursor_rm(xbt_dynar_t dynar,
199                                                 unsigned int *const
200                                                 cursor);
201 XBT_PUBLIC(void) xbt_dynar_cursor_unlock(xbt_dynar_t dynar);
202
203 /* do not use this structure internals directly, but use the public interface
204  * This was made public to allow:
205  *  - the inlining of the foreach elements
206  *  - sending such beasts over the network
207  */
208
209 #include "xbt/synchro_core.h"
210 typedef struct xbt_dynar_s {
211   unsigned long size;
212   unsigned long used;
213   unsigned long elmsize;
214   void *data;
215   void_f_pvoid_t free_f;
216   xbt_mutex_t mutex;
217 } s_xbt_dynar_t;
218
219 static XBT_INLINE void
220 _xbt_dynar_cursor_first(const xbt_dynar_t dynar,
221                         unsigned int *const cursor)
222 {
223   /* don't test for dynar!=NULL. The segfault would tell us */
224   if (dynar->mutex)             /* ie _dynar_lock(dynar) but not public */
225     xbt_mutex_acquire(dynar->mutex);
226
227   //DEBUG1("Set cursor on %p to the first position", (void *) dynar);
228   *cursor = 0;
229 }
230
231 static XBT_INLINE int
232 _xbt_dynar_cursor_get(const xbt_dynar_t dynar,
233                       unsigned int idx, void *const dst)
234 {
235
236   if (idx >= dynar->used) {
237     //DEBUG1("Cursor on %p already on last elem", (void *) dynar);
238     if (dynar->mutex)           /* unlock */
239       xbt_mutex_release(dynar->mutex);
240     return FALSE;
241   }
242   //  DEBUG2("Cash out cursor on %p at %u", (void *) dynar, *idx);
243
244   memcpy(dst, ((char *) dynar->data) + idx * dynar->elmsize,
245          dynar->elmsize);
246
247   return TRUE;
248 }
249
250
251
252 /** @brief Iterates over the whole dynar. 
253  * 
254  *  @param _dynar what to iterate over
255  *  @param _cursor an integer used as cursor
256  *  @param _data
257  *  @hideinitializer
258  *
259  * \note An example of usage:
260  * \code
261 xbt_dynar_t dyn;
262 unsigned int cpt;
263 string *str;
264 xbt_dynar_foreach (dyn,cpt,str) {
265   printf("Seen %s\n",str);
266 }
267 \endcode
268  */
269 #define xbt_dynar_foreach(_dynar,_cursor,_data) \
270        for (_xbt_dynar_cursor_first(_dynar,&(_cursor))      ; \
271             _xbt_dynar_cursor_get(_dynar,_cursor,&_data) ; \
272             (_cursor)++         )
273
274 /** @} */
275
276 SG_END_DECL()
277 #endif                          /* _XBT_DYNAR_H */