src/ucx/cx/list.h

changeset 438
22eca559aded
parent 415
d938228c382e
child 490
d218607f5a7e
equal deleted inserted replaced
437:545010bc5e71 438:22eca559aded
128 struct cx_list_s *list, 128 struct cx_list_s *list,
129 void const *elem 129 void const *elem
130 ); 130 );
131 131
132 /** 132 /**
133 * Member function for adding multiple elements.
134 */
135 size_t (*add_array)(
136 struct cx_list_s *list,
137 void const *array,
138 size_t n
139 );
140
141 /**
133 * Member function for inserting an element. 142 * Member function for inserting an element.
134 */ 143 */
135 int (*insert)( 144 int (*insert)(
136 struct cx_list_s *list, 145 struct cx_list_s *list,
137 size_t index, 146 size_t index,
140 149
141 /** 150 /**
142 * Member function for inserting an element relative to an iterator position. 151 * Member function for inserting an element relative to an iterator position.
143 */ 152 */
144 int (*insert_iter)( 153 int (*insert_iter)(
145 struct cx_iterator_s *iter, 154 struct cx_mut_iterator_s *iter,
146 void const *elem, 155 void const *elem,
147 int prepend 156 int prepend
148 ); 157 );
149 158
150 /** 159 /**
191 200
192 /** 201 /**
193 * Returns an iterator pointing to the specified index. 202 * Returns an iterator pointing to the specified index.
194 */ 203 */
195 struct cx_iterator_s (*iterator)( 204 struct cx_iterator_s (*iterator)(
205 struct cx_list_s const *list,
206 size_t index
207 );
208
209 /**
210 * Returns a mutating iterator pointing to the specified index.
211 */
212 struct cx_mut_iterator_s (*mut_iterator)(
196 struct cx_list_s *list, 213 struct cx_list_s *list,
197 size_t index 214 size_t index
198 ); 215 );
199 }; 216 };
200 217
207 * Adds an item to the end of the list. 224 * Adds an item to the end of the list.
208 * 225 *
209 * @param list the list 226 * @param list the list
210 * @param elem a pointer to the element to add 227 * @param elem a pointer to the element to add
211 * @return zero on success, non-zero on memory allocation failure 228 * @return zero on success, non-zero on memory allocation failure
229 * @see cxListAddArray()
212 */ 230 */
213 __attribute__((__nonnull__)) 231 __attribute__((__nonnull__))
214 static inline int cxListAdd( 232 static inline int cxListAdd(
215 CxList *list, 233 CxList *list,
216 void const *elem 234 void const *elem
217 ) { 235 ) {
218 return list->cl->add(list, elem); 236 return list->cl->add(list, elem);
237 }
238
239 /**
240 * Adds multiple items to the end of the list.
241 *
242 * This method is more efficient than invoking cxListAdd() multiple times.
243 *
244 * If there is not enough memory to add all elements, the returned value is
245 * less than \p n.
246 *
247 * @param list the list
248 * @param array a pointer to the elements to add
249 * @param n the number of elements to add
250 * @return the number of added elements
251 */
252 __attribute__((__nonnull__))
253 static inline size_t cxListAddArray(
254 CxList *list,
255 void const *array,
256 size_t n
257 ) {
258 return list->cl->add_array(list, array, n);
219 } 259 }
220 260
221 /** 261 /**
222 * Inserts an item at the specified index. 262 * Inserts an item at the specified index.
223 * 263 *
255 * @see cxListInsert() 295 * @see cxListInsert()
256 * @see cxListInsertBefore() 296 * @see cxListInsertBefore()
257 */ 297 */
258 __attribute__((__nonnull__)) 298 __attribute__((__nonnull__))
259 static inline int cxListInsertAfter( 299 static inline int cxListInsertAfter(
260 CxIterator *iter, 300 CxMutIterator *iter,
261 void const *elem 301 void const *elem
262 ) { 302 ) {
263 return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 0); 303 return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 0);
264 } 304 }
265 305
278 * @see cxListInsert() 318 * @see cxListInsert()
279 * @see cxListInsertAfter() 319 * @see cxListInsertAfter()
280 */ 320 */
281 __attribute__((__nonnull__)) 321 __attribute__((__nonnull__))
282 static inline int cxListInsertBefore( 322 static inline int cxListInsertBefore(
283 CxIterator *iter, 323 CxMutIterator *iter,
284 void const *elem 324 void const *elem
285 ) { 325 ) {
286 return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 1); 326 return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 1);
287 } 327 }
288 328
326 * @param index the index where the iterator shall point at 366 * @param index the index where the iterator shall point at
327 * @return a new iterator 367 * @return a new iterator
328 */ 368 */
329 __attribute__((__nonnull__, __warn_unused_result__)) 369 __attribute__((__nonnull__, __warn_unused_result__))
330 static inline CxIterator cxListIterator( 370 static inline CxIterator cxListIterator(
331 CxList *list, 371 CxList const *list,
332 size_t index 372 size_t index
333 ) { 373 ) {
334 return list->cl->iterator(list, index); 374 return list->cl->iterator(list, index);
335 } 375 }
336 376
337 /** 377 /**
378 * Returns a mutating iterator pointing to the item at the specified index.
379 *
380 * The returned iterator is position-aware.
381 *
382 * If the index is out of range, a past-the-end iterator will be returned.
383 *
384 * @param list the list
385 * @param index the index where the iterator shall point at
386 * @return a new iterator
387 */
388 __attribute__((__nonnull__, __warn_unused_result__))
389 static inline CxMutIterator cxListMutIterator(
390 CxList *list,
391 size_t index
392 ) {
393 return list->cl->mut_iterator(list, index);
394 }
395
396 /**
338 * Returns an iterator pointing to the first item of the list. 397 * Returns an iterator pointing to the first item of the list.
339 * 398 *
340 * The returned iterator is position-aware. 399 * The returned iterator is position-aware.
341 * 400 *
342 * If the list is empty, a past-the-end iterator will be returned. 401 * If the list is empty, a past-the-end iterator will be returned.
343 * 402 *
344 * @param list the list 403 * @param list the list
345 * @return a new iterator 404 * @return a new iterator
346 */ 405 */
347 __attribute__((__nonnull__, __warn_unused_result__)) 406 __attribute__((__nonnull__, __warn_unused_result__))
348 static inline CxIterator cxListBegin(CxList *list) { 407 static inline CxIterator cxListBegin(CxList const *list) {
349 return list->cl->iterator(list, 0); 408 return list->cl->iterator(list, 0);
409 }
410
411 /**
412 * Returns a mutating iterator pointing to the first item of the list.
413 *
414 * The returned iterator is position-aware.
415 *
416 * If the list is empty, a past-the-end iterator will be returned.
417 *
418 * @param list the list
419 * @return a new iterator
420 */
421 __attribute__((__nonnull__, __warn_unused_result__))
422 static inline CxMutIterator cxListBeginMut(CxList *list) {
423 return list->cl->mut_iterator(list, 0);
350 } 424 }
351 425
352 /** 426 /**
353 * Returns the index of the first element that equals \p elem. 427 * Returns the index of the first element that equals \p elem.
354 * 428 *
358 * @param elem the element to find 432 * @param elem the element to find
359 * @return the index of the element or \c (size+1) if the element is not found 433 * @return the index of the element or \c (size+1) if the element is not found
360 */ 434 */
361 __attribute__((__nonnull__)) 435 __attribute__((__nonnull__))
362 static inline size_t cxListFind( 436 static inline size_t cxListFind(
363 CxList *list, 437 CxList const *list,
364 void const *elem 438 void const *elem
365 ) { 439 ) {
366 return list->cl->find(list, elem); 440 return list->cl->find(list, elem);
367 } 441 }
368 442
389 } 463 }
390 464
391 /** 465 /**
392 * Compares a list to another list of the same type. 466 * Compares a list to another list of the same type.
393 * 467 *
394 * First, the list sizes are compared. If they match, the lists are compared element-wise. 468 * First, the list sizes are compared.
469 * If they match, the lists are compared element-wise.
395 * 470 *
396 * @param list the list 471 * @param list the list
397 * @param other the list to compare to 472 * @param other the list to compare to
398 * @return zero, if both lists are equal element wise, negative if the first list is smaller, zero if the first list is larger 473 * @return zero, if both lists are equal element wise,
399 */ 474 * negative if the first list is smaller, positive if the first list is larger
400 __attribute__((__nonnull__)) 475 */
401 static inline int cxListCompare( 476 __attribute__((__nonnull__))
402 CxList *list, 477 int cxListCompare(
403 CxList *other 478 CxList const *list,
404 ) { 479 CxList const *other
405 return list->cl->compare(list, other); 480 );
406 }
407 481
408 /** 482 /**
409 * Deallocates the memory of the specified list structure. 483 * Deallocates the memory of the specified list structure.
410 * 484 *
411 * Also calls content a destructor function, depending on the configuration 485 * Also calls content a destructor function, depending on the configuration
417 */ 491 */
418 __attribute__((__nonnull__)) 492 __attribute__((__nonnull__))
419 void cxListDestroy(CxList *list); 493 void cxListDestroy(CxList *list);
420 494
421 #ifdef __cplusplus 495 #ifdef __cplusplus
422 } /* extern "C" */ 496 } // extern "C"
423 #endif 497 #endif
424 498
425 #endif /* UCX_LIST_H */ 499 #endif // UCX_LIST_H

mercurial