src/cx/collection.h

changeset 1618
ef7cab6eb131
parent 1605
55b13f583356
equal deleted inserted replaced
1617:d4385f35f8b0 1618:ef7cab6eb131
56 /** 56 /**
57 * The allocator to use. 57 * The allocator to use.
58 */ 58 */
59 const CxAllocator *allocator; 59 const CxAllocator *allocator;
60 /** 60 /**
61 * The comparator function for the elements.
62 */
63 cx_compare_func cmpfunc;
64 /**
65 * The size of each element. 61 * The size of each element.
66 */ 62 */
67 size_t elem_size; 63 size_t elem_size;
68 /** 64 /**
69 * The number of currently stored elements. 65 * The number of currently stored elements.
70 */ 66 */
71 size_t size; 67 size_t size;
68 /**
69 * A two-argument comparator function for the elements.
70 */
71 cx_compare_func simple_cmp;
72 /**
73 * A three-argument comparator function for the elements.
74 * If specified, this function has precedence over the @c simple_cmp function.
75 */
76 cx_compare_func2 advanced_cmp;
77 /**
78 * A pointer to custom data for the @c advanced_cmp function
79 */
80 void *cmp_data;
72 /** 81 /**
73 * An optional simple destructor for the collection's elements. 82 * An optional simple destructor for the collection's elements.
74 * 83 *
75 * @attention Read the documentation of the particular collection implementation 84 * @attention Read the documentation of the particular collection implementation
76 * whether this destructor shall only destroy the contents or also free the memory. 85 * whether this destructor shall only destroy the contents or also free the memory.
137 * @retval true if this collection stores only pointers to data 146 * @retval true if this collection stores only pointers to data
138 * @retval false if this collection stores the actual element's data 147 * @retval false if this collection stores the actual element's data
139 */ 148 */
140 #define cxCollectionStoresPointers(c) ((c)->collection.store_pointer) 149 #define cxCollectionStoresPointers(c) ((c)->collection.store_pointer)
141 150
151
152 /**
153 * Convenience macro for adding indirection to an element if the collection is storing pointers.
154 *
155 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE
156 * @param elem the pointer that shall be taken the address from, if the collection is storing pointers
157 * @return if the collection is storing pointers, takes the address of @p elem, otherwise returns @p elem
158 */
159 #define cx_ref(c, elem) (cxCollectionStoresPointers(c) ? ((void*)&(elem)) : (elem))
160
161 /**
162 * Convenience macro for dereferencing an element if the collection is storing pointers.
163 *
164 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE
165 * @param elem a pointer to the collection element
166 * @return if the collection is storing pointers, dereferences @p elem, otherwise returns @p elem
167 */
168 #define cx_deref(c, elem) (cxCollectionStoresPointers(c) ? *((void**)(elem)) : (elem))
169
142 /** 170 /**
143 * Indicates whether the collection can guarantee that the stored elements are currently sorted. 171 * Indicates whether the collection can guarantee that the stored elements are currently sorted.
144 * 172 *
145 * This may return @c false even when the elements are sorted. 173 * This may return @c false even when the elements are sorted.
146 * It is totally up to the implementation of the collection when to check if the elements are sorted. 174 * It is totally up to the implementation of the collection when to check if the elements are sorted.
152 * @retval false if the order of elements is unknown 180 * @retval false if the order of elements is unknown
153 */ 181 */
154 #define cxCollectionSorted(c) ((c)->collection.sorted || (c)->collection.size == 0) 182 #define cxCollectionSorted(c) ((c)->collection.sorted || (c)->collection.size == 0)
155 183
156 /** 184 /**
157 * Sets the compare function for a collection. 185 * Sets a simple compare function for a collection.
158 * 186 *
159 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE 187 * Erases a possible advanced compare function.
160 * @param func (@c cx_compare_func) the compare function that shall be used by @c c 188 * If you want to set both, because you want to access the simple function
161 */ 189 * in your advanced function, you must set the simple function first.
162 #define cxSetCompareFunc(c, func) (c)->collection.cmpfunc = (cx_compare_func)(func) 190 *
191 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE
192 * @param func (@c cx_compare_func) the compare function
193 */
194 #define cxSetCompareFunc(c, func) \
195 (c)->collection.simple_cmp = (cx_compare_func)(func); \
196 (c)->collection.advanced_cmp = NULL
197
198 /**
199 * Sets an advanced compare function that supports custom data for a collection.
200 *
201 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE
202 * @param func (@c cx_compare_func2) the compare function
203 * @param data (@c void*) the pointer to custom data that is passed to the compare function
204 */
205 #define cxSetAdvancedCompareFunc(c, func, data) \
206 (c)->collection.advanced_cmp = (cx_compare_func2) func; \
207 (c)->collection.destructor_data = data
208
209 /**
210 * Invokes the simple comparator function for two elements.
211 *
212 * Usually only used by collection implementations. There should be no need
213 * to invoke this macro manually.
214 *
215 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE
216 * @param left (@c void*) pointer to data
217 * @param right (@c void*) pointer to data
218 */
219 #define cx_invoke_simple_compare_func(c, left, right) \
220 (c)->collection.simple_cmp(left, right)
221
222 /**
223 * Invokes the advanced comparator function for two elements.
224 *
225 * Usually only used by collection implementations. There should be no need
226 * to invoke this macro manually.
227 *
228 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE
229 * @param left (@c void*) pointer to data
230 * @param right (@c void*) pointer to data
231 */
232 #define cx_invoke_advanced_compare_func(c, left, right) \
233 (c)->collection.advanced_cmp(left, right, (c)->collection.cmp_data)
234
235
236 /**
237 * Invokes the configured comparator function for two elements.
238 *
239 * Usually only used by collection implementations. There should be no need
240 * to invoke this macro manually.
241 *
242 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE
243 * @param left (@c void*) pointer to data
244 * @param right (@c void*) pointer to data
245 */
246 #define cx_invoke_compare_func(c, left, right) \
247 (((c)->collection.advanced_cmp) ? \
248 cx_invoke_advanced_compare_func(c,left,right) : \
249 cx_invoke_simple_compare_func(c,left,right))
163 250
164 /** 251 /**
165 * Sets a simple destructor function for this collection. 252 * Sets a simple destructor function for this collection.
166 * 253 *
167 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE 254 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE
168 * @param destr the destructor function 255 * @param destr (@c cx_destructor_func) the destructor function
169 */ 256 */
170 #define cxSetDestructor(c, destr) \ 257 #define cxSetDestructor(c, destr) \
171 (c)->collection.simple_destructor = (cx_destructor_func) destr 258 (c)->collection.simple_destructor = (cx_destructor_func) destr
172 259
173 /** 260 /**
174 * Sets a simple destructor function for this collection. 261 * Sets an advanced destructor function for this collection.
175 * 262 *
176 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE 263 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE
177 * @param destr the destructor function 264 * @param destr (@c cx_destructor_func2) the destructor function
265 * @param data (@c void*) the additional data the advanced destructor is invoked with
178 */ 266 */
179 #define cxSetAdvancedDestructor(c, destr, data) \ 267 #define cxSetAdvancedDestructor(c, destr, data) \
180 (c)->collection.advanced_destructor = (cx_destructor_func2) destr; \ 268 (c)->collection.advanced_destructor = (cx_destructor_func2) destr; \
181 (c)->collection.destructor_data = data 269 (c)->collection.destructor_data = data
182 270

mercurial