Logo AND Algorithmique Numérique Distribuée

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