3 /* dynar - a generic dynamic array */
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved. */
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. */
13 #include "xbt/misc.h" /* SG_BEGIN_DECL */
14 #include "xbt/function_types.h"
18 /** @addtogroup XBT_dynar
19 * @brief DynArr are dynamically sized vector which may contain any type of variables.
21 * These are the SimGrid version of the dynamically size arrays, which all C programmer recode one day or another.
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).
28 * \section XBT_dynar_exscal Example with scalar
29 * \dontinclude dynar.c
36 * \until end_of_traversal
39 * \until xbt_dynar_free
41 * \section XBT_dynar_exptr Example with pointed data
43 * \skip test_dynar_string
51 * \skip end_of_doxygen
55 /** @defgroup XBT_dynar_cons Dynar constructor and destructor
60 /** \brief Dynar data type (opaque type) */
61 typedef struct xbt_dynar_s *xbt_dynar_t;
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_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);
72 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);
76 XBT_PUBLIC(void) xbt_dynar_dump(xbt_dynar_t dynar);
79 /** @defgroup XBT_dynar_array Dynar as a regular array
85 XBT_PUBLIC(void) xbt_dynar_get_cpy(const xbt_dynar_t dynar,
86 const unsigned long idx, void *const dst);
88 XBT_PUBLIC(void) xbt_dynar_set(xbt_dynar_t dynar, const int idx,
90 XBT_PUBLIC(void) xbt_dynar_replace(xbt_dynar_t dynar, const unsigned long idx,
93 XBT_PUBLIC(void) xbt_dynar_insert_at(xbt_dynar_t const dynar, const int idx,
95 XBT_PUBLIC(void) xbt_dynar_remove_at(xbt_dynar_t const dynar, const int idx,
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);
101 /** @defgroup XBT_dynar_perl Perl-like use of dynars
107 XBT_PUBLIC(void) xbt_dynar_push(xbt_dynar_t const dynar, const void *src);
108 XBT_PUBLIC(void) xbt_dynar_pop(xbt_dynar_t const dynar, void *const dst);
109 XBT_PUBLIC(void) xbt_dynar_unshift(xbt_dynar_t const dynar, const void *src);
110 XBT_PUBLIC(void) xbt_dynar_shift(xbt_dynar_t const dynar, void *const dst);
111 XBT_PUBLIC(void) xbt_dynar_map(const xbt_dynar_t dynar,
112 void_f_pvoid_t const op);
115 /** @defgroup XBT_dynar_ctn Direct manipulation to the dynars content
118 * Those functions do not retrieve the content, but only their address.
123 XBT_PUBLIC(void *) xbt_dynar_get_ptr(const xbt_dynar_t dynar,
124 const unsigned long idx);
125 XBT_PUBLIC(void *) xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar,
127 XBT_PUBLIC(void *) xbt_dynar_push_ptr(xbt_dynar_t const dynar);
128 XBT_PUBLIC(void *) xbt_dynar_pop_ptr(xbt_dynar_t const dynar);
131 /** @defgroup XBT_dynar_speed Speed optimized access to dynars of scalars
134 * While the other functions use a memcpy to retrieve the content into the
135 * user provided area, those ones use a regular affectation. It only works
136 * for scalar values, but should be a little faster.
141 /** @brief Quick retrieval of scalar content
142 * @hideinitializer */
143 # define xbt_dynar_get_as(dynar,idx,type) \
144 (*(type*)xbt_dynar_get_ptr((dynar),(idx)))
145 /** @brief Quick retrieval of scalar content
146 * @hideinitializer */
147 # define xbt_dynar_getlast_as(dynar,type) \
148 (*(type*)xbt_dynar_get_ptr((dynar),xbt_dynar_length(dynar)-1))
149 /** @brief Quick retrieval of scalar content
150 * @hideinitializer */
151 # define xbt_dynar_getfirst_as(dynar,type) \
152 (*(type*)xbt_dynar_get_ptr((dynar),0))
153 /** @brief Quick insertion of scalar content
154 * @hideinitializer */
155 # define xbt_dynar_insert_at_as(dynar,idx,type,value) \
156 *(type*)xbt_dynar_insert_at_ptr(dynar,idx)=value
157 /** @brief Quick insertion of scalar content
158 * @hideinitializer */
159 # define xbt_dynar_push_as(dynar,type,value) \
160 *(type*)xbt_dynar_push_ptr(dynar)=value
161 /** @brief Quick removal of scalar content
162 * @hideinitializer */
163 # define xbt_dynar_pop_as(dynar,type) \
164 (*(type*)xbt_dynar_pop_ptr(dynar))
167 /** @defgroup XBT_dynar_cursor Cursors on dynar
170 * Cursors are used to iterate over the structure. Never add elements to the
171 * DynArr during the traversal. To remove elements, use the
172 * xbt_dynar_cursor_rm() function.
174 * Do not call these functions directly, but only the xbt_dynar_foreach macro.
176 * For synchronized dynars, the dynar will be locked during the whole
177 * loop and it will get unlocked automatically if you traverse all
178 * elements. If you want to break the loop before the end, make sure
179 * to call xbt_dynar_cursor_unlock() before the <tt>break;</tt>
184 XBT_PUBLIC(void) _xbt_dynar_cursor_first(const xbt_dynar_t dynar,
185 unsigned int *const cursor);
186 XBT_PUBLIC(void) _xbt_dynar_cursor_step(const xbt_dynar_t dynar,
187 unsigned int *const cursor);
188 XBT_PUBLIC(int) _xbt_dynar_cursor_get(const xbt_dynar_t dynar,
189 unsigned int *const cursor,
191 XBT_PUBLIC(void) xbt_dynar_cursor_rm(xbt_dynar_t dynar,
192 unsigned int *const cursor);
193 XBT_PUBLIC(void) xbt_dynar_cursor_unlock(xbt_dynar_t dynar);
195 /** @brief Iterates over the whole dynar.
197 * @param _dynar what to iterate over
198 * @param _cursor an integer used as cursor
202 * \note An example of usage:
207 xbt_dynar_foreach (dyn,cpt,str) {
208 printf("Seen %s\n",str);
212 #define xbt_dynar_foreach(_dynar,_cursor,_data) \
213 for (_xbt_dynar_cursor_first(_dynar,&(_cursor)) ; \
214 _xbt_dynar_cursor_get(_dynar,&(_cursor),&_data) ; \
215 _xbt_dynar_cursor_step(_dynar,&(_cursor)) )
220 #endif /* _XBT_DYNAR_H */