Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
adapt the prototypes of the declaration to the prototypes of the definition
[simgrid.git] / include / xbt / dynar.h
1 /* $Id$ */
2
3 /* dynar - a generic dynamic array                                          */
4
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved.            */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #ifndef _XBT_DYNAR_H
11 #define _XBT_DYNAR_H
12
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 doxygen_string_cruft
44   * \skip function
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
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_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_PUBLIC(unsigned long) xbt_dynar_length(const xbt_dynar_t dynar);
74   XBT_PUBLIC(void)          xbt_dynar_reset(xbt_dynar_t const dynar);
75   XBT_PUBLIC(void)          xbt_dynar_shrink(xbt_dynar_t dynar, int empty_slots);
76
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, const int idx, void * const dst);
87   
88   XBT_PUBLIC(void) xbt_dynar_set(xbt_dynar_t dynar, const int idx, const void *src);
89   XBT_PUBLIC(void) xbt_dynar_replace(xbt_dynar_t dynar, const int idx, const void *object);
90
91   XBT_PUBLIC(void) xbt_dynar_insert_at(xbt_dynar_t  const dynar, const int  idx, const void *src);
92   XBT_PUBLIC(void) xbt_dynar_remove_at(xbt_dynar_t  const dynar, const int  idx, void * const dst);
93
94   XBT_PUBLIC(int)  xbt_dynar_search(xbt_dynar_t  const dynar, void *elem);
95   XBT_PUBLIC(int)  xbt_dynar_member(xbt_dynar_t  const dynar, void *elem);
96 /** @} */
97 /** @defgroup XBT_dynar_perl Perl-like use of dynars
98  *  @ingroup XBT_dynar
99  *
100  *  @{
101  */
102
103   XBT_PUBLIC(void) xbt_dynar_push    (xbt_dynar_t  const dynar, const void *src);
104   XBT_PUBLIC(void) xbt_dynar_pop     (xbt_dynar_t  const dynar, void *const dst);
105   XBT_PUBLIC(void) xbt_dynar_unshift (xbt_dynar_t  const dynar, const void *src);
106   XBT_PUBLIC(void) xbt_dynar_shift   (xbt_dynar_t  const dynar, void *const dst);
107   XBT_PUBLIC(void) xbt_dynar_map     (const xbt_dynar_t dynar, void_f_pvoid_t     * const op);
108
109 /** @} */
110 /** @defgroup XBT_dynar_ctn Direct manipulation to the dynars content
111  *  @ingroup XBT_dynar
112  *
113  *  Those functions do not retrieve the content, but only their address.
114  *
115  *  @{
116  */
117
118   XBT_PUBLIC(void*) xbt_dynar_get_ptr(const xbt_dynar_t dynar, const int idx);
119   XBT_PUBLIC(void*) xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar, const int idx);
120   XBT_PUBLIC(void*) xbt_dynar_push_ptr(xbt_dynar_t  const dynar);
121   XBT_PUBLIC(void*) xbt_dynar_pop_ptr(xbt_dynar_t  const dynar);
122
123 /** @} */
124 /** @defgroup XBT_dynar_speed Speed optimized access to dynars of scalars
125  *  @ingroup XBT_dynar
126  *
127  *  While the other functions use a memcpy to retrieve the content into the
128  *  user provided area, those ones use a regular affectation. It only works
129  *  for scalar values, but should be a little faster.
130  *
131  *  @{
132  */
133
134   /** @brief Quick retrieval of scalar content 
135    *  @hideinitializer */
136 #  define xbt_dynar_get_as(dynar,idx,type) \
137           (*(type*)xbt_dynar_get_ptr((dynar),(idx)))
138   /** @brief Quick retrieval of scalar content 
139    *  @hideinitializer */
140 #  define xbt_dynar_getlast_as(dynar,type) \
141           (*(type*)xbt_dynar_get_ptr((dynar),xbt_dynar_length(dynar)-1))
142   /** @brief Quick retrieval of scalar content 
143    *  @hideinitializer */
144 #  define xbt_dynar_getfirst_as(dynar,type) \
145           (*(type*)xbt_dynar_get_ptr((dynar),0))
146   /** @brief Quick insertion of scalar content 
147    *  @hideinitializer */
148 #  define xbt_dynar_insert_at_as(dynar,idx,type,value) \
149           *(type*)xbt_dynar_insert_at_ptr(dynar,idx)=value
150   /** @brief Quick insertion of scalar content 
151    *  @hideinitializer */
152 #  define xbt_dynar_push_as(dynar,type,value) \
153           *(type*)xbt_dynar_push_ptr(dynar)=value
154   /** @brief Quick insertion of scalar content 
155    *  @hideinitializer */
156 #  define xbt_dynar_pop_as(dynar,type) \
157            (*(type*)xbt_dynar_pop_ptr(dynar))
158
159 /** @} */
160 /** @defgroup XBT_dynar_cursor Cursors on dynar
161  *  @ingroup XBT_dynar
162  *
163  * Cursors are used to iterate over the structure. Never add elements to the 
164  * DynArr during the traversal. To remove elements, use the
165  * xbt_dynar_cursor_rm() function.
166  *
167  * Do not call these functions directly, but only the xbt_dynar_foreach macro.
168  * 
169  * For synchronized dynars, the dynar will be locked during the whole
170  * loop and it will get unlocked automatically if you traverse all
171  * elements. If you want to break the loop before the end, make sure
172  * to call xbt_dynar_cursor_unlock() before the <tt>break;</tt>
173  *
174  *  @{
175  */
176
177   XBT_PUBLIC(void) _xbt_dynar_cursor_first (const xbt_dynar_t dynar, int * const cursor);
178   XBT_PUBLIC(void) _xbt_dynar_cursor_step  (const xbt_dynar_t dynar, int * const cursor);
179   XBT_PUBLIC(int)  _xbt_dynar_cursor_get   (const xbt_dynar_t dynar, int * const cursor, 
180                                             void *whereto);
181   XBT_PUBLIC(void) xbt_dynar_cursor_rm(xbt_dynar_t dynar,
182                                        int         *const cursor);
183   XBT_PUBLIC(void) xbt_dynar_cursor_unlock(xbt_dynar_t dynar);
184
185 /** @brief Iterates over the whole dynar. 
186  * 
187  *  @param _dynar what to iterate over
188  *  @param _cursor an integer used as cursor
189  *  @param _data
190  *  @hideinitializer
191  *
192  * \note An example of usage:
193  * \code
194 xbt_dynar_t dyn;
195 int cpt;
196 string *str;
197 xbt_dynar_foreach (dyn,cpt,str) {
198   printf("Seen %s\n",str);
199 }
200 \endcode
201  */
202 #define xbt_dynar_foreach(_dynar,_cursor,_data) \
203        for (_xbt_dynar_cursor_first(_dynar,&(_cursor))      ; \
204             _xbt_dynar_cursor_get(_dynar,&(_cursor),&_data) ; \
205             _xbt_dynar_cursor_step(_dynar,&(_cursor))         )
206
207 /** @} */
208
209 SG_END_DECL()
210
211 #endif /* _XBT_DYNAR_H */