Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New XBT module: file
[simgrid.git] / include / xbt / dynar.h
1 /* dynar - a generic dynamic array                                          */
2
3 /* Copyright (c) 2004-2007, 2009-2015. 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/base.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   * Note that if you use dynars to store pointed data, the
56   * xbt_dynar_search(), xbt_dynar_search_or_negative() and
57   * xbt_dynar_member() won't be for you. Instead of comparing
58   * your pointed elements, they compare the pointer to them. See
59   * the documentation of xbt_dynar_search() for more info.
60   * 
61   */
62 /** @defgroup XBT_dynar_cons Dynar constructor and destructor
63  *  @ingroup XBT_dynar
64  *
65  *  @{
66  */
67    /** \brief Dynar data type (opaque type) */
68 typedef struct xbt_dynar_s *xbt_dynar_t;
69
70
71 XBT_PUBLIC(xbt_dynar_t) xbt_dynar_new(const unsigned long elm_size,
72                                       void_f_pvoid_t const free_f);
73 XBT_PUBLIC(void) xbt_dynar_free(xbt_dynar_t * dynar);
74 XBT_PUBLIC(void) xbt_dynar_free_voidp(void *dynar);
75 XBT_PUBLIC(void) xbt_dynar_free_container(xbt_dynar_t * dynar);
76 XBT_PUBLIC(void) xbt_dynar_shrink(xbt_dynar_t dynar, int empty_slots);
77 XBT_PUBLIC(void) xbt_dynar_dump(xbt_dynar_t dynar);
78
79 /** @} */
80 /** @defgroup XBT_dynar_array Dynar as a regular array
81  *  @ingroup XBT_dynar
82  *
83  *  @{
84  */
85
86 XBT_PUBLIC(void) xbt_dynar_get_cpy(const xbt_dynar_t dynar,
87                                    const unsigned long idx,
88                                    void *const dst);
89 XBT_PUBLIC(void) xbt_dynar_set(xbt_dynar_t dynar, const int idx,
90                                const void *src);
91 XBT_PUBLIC(void) xbt_dynar_replace(xbt_dynar_t dynar,
92                                    const unsigned long idx,
93                                    const void *object);
94
95 XBT_PUBLIC(void) xbt_dynar_insert_at(xbt_dynar_t const dynar,
96                                      const int idx, const void *src);
97 XBT_PUBLIC(void) xbt_dynar_remove_at(xbt_dynar_t const dynar,
98                                      const int idx, void *const dst);
99 XBT_PUBLIC(void) xbt_dynar_remove_n_at(xbt_dynar_t const dynar,
100                                      const unsigned int n, const int idx);
101
102
103 XBT_PUBLIC(unsigned int) xbt_dynar_search(xbt_dynar_t const dynar, void *elem);
104 XBT_PUBLIC(signed int) xbt_dynar_search_or_negative(xbt_dynar_t const dynar, void *const 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_PUBLIC(void) xbt_dynar_three_way_partition(xbt_dynar_t const dynar,
109     int_f_pvoid_t color);
110 XBT_PUBLIC(int) xbt_dynar_compare(xbt_dynar_t d1, xbt_dynar_t d2,
111                                   int(*compar)(const void *, const void *));
112 XBT_PUBLIC(void *) xbt_dynar_to_array (xbt_dynar_t dynar);
113
114 /** @} */
115 /** @defgroup XBT_dynar_misc Dynar miscellaneous functions
116  *  @ingroup XBT_dynar
117  *
118  *  @{
119  */
120
121 XBT_PUBLIC(unsigned long) xbt_dynar_length(const xbt_dynar_t dynar);
122 XBT_PUBLIC(int) xbt_dynar_is_empty(const xbt_dynar_t dynar);
123 XBT_PUBLIC(void) xbt_dynar_reset(xbt_dynar_t const dynar);
124 XBT_PUBLIC(void) xbt_dynar_merge(xbt_dynar_t *d1, xbt_dynar_t *d2);
125
126 /** @} */
127 /** @defgroup XBT_dynar_perl Perl-like use of dynars
128  *  @ingroup XBT_dynar
129  *
130  *  @{
131  */
132
133 XBT_PUBLIC(void) xbt_dynar_push(xbt_dynar_t const dynar, const void *src);
134 XBT_PUBLIC(void) xbt_dynar_pop(xbt_dynar_t const dynar, void *const dst);
135 XBT_PUBLIC(void) xbt_dynar_unshift(xbt_dynar_t const dynar,
136                                    const void *src);
137 XBT_PUBLIC(void) xbt_dynar_shift(xbt_dynar_t const dynar, void *const dst);
138 XBT_PUBLIC(void) xbt_dynar_map(const xbt_dynar_t dynar,
139                                void_f_pvoid_t const op);
140
141 /** @} */
142 /** @defgroup XBT_dynar_ctn Direct manipulation to the dynars content
143  *  @ingroup XBT_dynar
144  *
145  *  Those functions do not retrieve the content, but only their address.
146  *
147  *  @{
148  */
149
150 XBT_PUBLIC(void *) xbt_dynar_set_at_ptr(const xbt_dynar_t dynar,
151                                         const unsigned long idx);
152 XBT_PUBLIC(void *) xbt_dynar_get_ptr(const xbt_dynar_t dynar,
153                                      const unsigned long idx);
154 XBT_PUBLIC(void *) xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar,
155                                            const int idx);
156 XBT_PUBLIC(void *) xbt_dynar_push_ptr(xbt_dynar_t const dynar);
157 XBT_PUBLIC(void *) xbt_dynar_pop_ptr(xbt_dynar_t const dynar);
158
159 /** @} */
160 /** @defgroup XBT_dynar_speed Speed optimized access to dynars of scalars
161  *  @ingroup XBT_dynar
162  *
163  *  While the other functions use a memcpy to retrieve the content into the
164  *  user provided area, those ones use a regular affectation. It only works
165  *  for scalar values, but should be a little faster.
166  *
167  *  @{
168  */
169
170   /** @brief Quick retrieval of scalar content 
171    *  @hideinitializer */
172 #  define xbt_dynar_get_as(dynar,idx,type) \
173           (*(type*)xbt_dynar_get_ptr((dynar),(idx)))
174 /** @brief Quick setting of scalar content
175  *  @hideinitializer */
176 #  define xbt_dynar_set_as(dynar,idx,type,val) \
177          (*(type*)xbt_dynar_set_at_ptr((dynar),(idx))) = val
178   /** @brief Quick retrieval of scalar content 
179    *  @hideinitializer */
180 #  define xbt_dynar_getlast_as(dynar,type) \
181           (*(type*)xbt_dynar_get_ptr((dynar),xbt_dynar_length(dynar)-1))
182   /** @brief Quick retrieval of scalar content 
183    *  @hideinitializer */
184 #  define xbt_dynar_getfirst_as(dynar,type) \
185           (*(type*)xbt_dynar_get_ptr((dynar),0))
186   /** @brief Quick insertion of scalar content 
187    *  @hideinitializer */
188 #  define xbt_dynar_insert_at_as(dynar,idx,type,value) \
189           *(type*)xbt_dynar_insert_at_ptr(dynar,idx)=value
190   /** @brief Quick insertion of scalar content 
191    *  @hideinitializer */
192 #  define xbt_dynar_push_as(dynar,type,value) \
193           *(type*)xbt_dynar_push_ptr(dynar)=value
194   /** @brief Quick removal of scalar content
195    *  @hideinitializer */
196 #  define xbt_dynar_pop_as(dynar,type) \
197            (*(type*)xbt_dynar_pop_ptr(dynar))
198
199 /** @} */
200 /** @defgroup XBT_dynar_cursor Cursors on dynar
201  *  @ingroup XBT_dynar
202  *
203  * Cursors are used to iterate over the structure. Never add elements to the 
204  * DynArr during the traversal. To remove elements, use the
205  * xbt_dynar_cursor_rm() function.
206  *
207  * Do not call these function directly, but only within the xbt_dynar_foreach
208  * macro.
209  *
210  *  @{
211  */
212
213 XBT_PUBLIC(void) xbt_dynar_cursor_rm(xbt_dynar_t dynar,
214                                      unsigned int *const cursor);
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 typedef struct xbt_dynar_s {
223   unsigned long size;
224   unsigned long used;
225   unsigned long elmsize;
226   void *data;
227   void_f_pvoid_t free_f;
228 } s_xbt_dynar_t;
229
230 static XBT_INLINE void
231 _xbt_dynar_cursor_first(const xbt_dynar_t dynar _XBT_GNUC_UNUSED,
232                         unsigned int *const cursor)
233 {
234   /* iterating over a NULL dynar is a no-op (but we don't want to have uninitialized counters) */
235
236   //XBT_DEBUG("Set cursor on %p to the first position", (void *) dynar);
237   *cursor = 0;
238 }
239
240 static XBT_INLINE int
241 _xbt_dynar_cursor_get(const xbt_dynar_t dynar,
242                       unsigned int idx, void *const dst)
243 {
244   if (!dynar) /* iterating over a NULL dynar is a no-op */
245     return FALSE;
246
247   if (idx >= dynar->used) {
248     //XBT_DEBUG("Cursor on %p already on last elem", (void *) dynar);
249     return FALSE;
250   }
251   //  XBT_DEBUG("Cash out cursor on %p at %u", (void *) dynar, *idx);
252
253   memcpy(dst, ((char *) dynar->data) + idx * dynar->elmsize,
254          dynar->elmsize);
255
256   return TRUE;
257 }
258
259
260
261 /** @brief Iterates over the whole dynar. 
262  * 
263  *  @param _dynar what to iterate over
264  *  @param _cursor an integer used as cursor
265  *  @param _data
266  *  @hideinitializer
267  *
268  * Here is an example of usage:
269  * \code
270 xbt_dynar_t dyn;
271 unsigned int cpt;
272 string *str;
273 xbt_dynar_foreach (dyn,cpt,str) {
274   printf("Seen %s\n",str);
275 }
276 \endcode
277  * 
278  * Note that underneath, that's a simple for loop with no real black
279  * magic involved. It's perfectly safe to interrupt a foreach with a
280  * break or a return statement. 
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 #ifndef __cplusplus
288 #define xbt_dynar_foreach_ptr(_dynar,_cursor,_ptr) \
289        for (_xbt_dynar_cursor_first(_dynar,&(_cursor))       ; \
290       (_ptr = _cursor < _dynar->used ? xbt_dynar_get_ptr(_dynar,_cursor) : NULL) ; \
291             (_cursor)++         )
292 #else
293 #define xbt_dynar_foreach_ptr(_dynar,_cursor,_ptr) \
294        for (_xbt_dynar_cursor_first(_dynar,&(_cursor))       ; \
295       (_ptr = _cursor < _dynar->used ? (decltype(_ptr)) xbt_dynar_get_ptr(_dynar,_cursor) : NULL) ; \
296             (_cursor)++         )
297 #endif
298 /** @} */
299
300 SG_END_DECL()
301 #endif                          /* _XBT_DYNAR_H */