src/cx/list.h

changeset 876
f4ce7df9cff0
parent 875
ee84ac776cbc
child 882
f8ca6e6c0d48
equal deleted inserted replaced
875:ee84ac776cbc 876:f4ce7df9cff0
96 void const *data, 96 void const *data,
97 size_t n 97 size_t n
98 ); 98 );
99 99
100 /** 100 /**
101 * Member function for inserting sorted elements into a sorted list.
102 *
103 * @see cx_list_default_insert_sorted()
104 */
105 size_t (*insert_sorted)(
106 struct cx_list_s *list,
107 void const *sorted_data,
108 size_t n
109 );
110
111 /**
101 * Member function for inserting an element relative to an iterator position. 112 * Member function for inserting an element relative to an iterator position.
102 */ 113 */
103 int (*insert_iter)( 114 int (*insert_iter)(
104 struct cx_iterator_s *iter, 115 struct cx_iterator_s *iter,
105 void const *elem, 116 void const *elem,
198 void const *data, 209 void const *data,
199 size_t n 210 size_t n
200 ); 211 );
201 212
202 /** 213 /**
214 * Default implementation of a sorted insert.
215 *
216 * This function uses the array insert function to insert consecutive groups
217 * of sorted data.
218 *
219 * The source data \em must already be sorted wrt. the list's compare function.
220 *
221 * Use this in your own list class if you do not want to implement an optimized
222 * version for your list.
223 *
224 * @param list the list
225 * @param sorted_data a pointer to the array of pre-sorted data to insert
226 * @param n the number of elements to insert
227 * @return the number of elements actually inserted
228 */
229 __attribute__((__nonnull__))
230 size_t cx_list_default_insert_sorted(
231 struct cx_list_s *list,
232 void const *sorted_data,
233 size_t n
234 );
235
236 /**
203 * Default unoptimized sort implementation. 237 * Default unoptimized sort implementation.
204 * 238 *
205 * This function will copy all data to an array, sort the array with standard 239 * This function will copy all data to an array, sort the array with standard
206 * qsort, and then copy the data back to the list memory. 240 * qsort, and then copy the data back to the list memory.
207 * 241 *
343 ) { 377 ) {
344 return list->cl->insert_element(list, index, elem); 378 return list->cl->insert_element(list, index, elem);
345 } 379 }
346 380
347 /** 381 /**
382 * Inserts an item into a sorted list.
383 *
384 * @param list the list
385 * @param elem a pointer to the element to add
386 * @return zero on success, non-zero on memory allocation failure
387 */
388 __attribute__((__nonnull__))
389 static inline int cxListInsertSorted(
390 CxList *list,
391 void const *elem
392 ) {
393 void const *data = list->collection.store_pointer ? &elem : elem;
394 return list->cl->insert_sorted(list, data, 1) == 0;
395 }
396
397 /**
348 * Inserts multiple items to the list at the specified index. 398 * Inserts multiple items to the list at the specified index.
349 * If \p index equals the list size, this is effectively cxListAddArray(). 399 * If \p index equals the list size, this is effectively cxListAddArray().
350 * 400 *
351 * This method is usually more efficient than invoking cxListInsert() 401 * This method is usually more efficient than invoking cxListInsert()
352 * multiple times. 402 * multiple times.
369 size_t index, 419 size_t index,
370 void const *array, 420 void const *array,
371 size_t n 421 size_t n
372 ) { 422 ) {
373 return list->cl->insert_array(list, index, array, n); 423 return list->cl->insert_array(list, index, array, n);
424 }
425
426 /**
427 * Inserts a sorted array into a sorted list.
428 *
429 * This method is usually more efficient than inserting each element separately,
430 * because consecutive chunks of sorted data are inserted in one pass.
431 *
432 * If there is not enough memory to add all elements, the returned value is
433 * less than \p n.
434 *
435 * If this list is storing pointers instead of objects \p array is expected to
436 * be an array of pointers.
437 *
438 * @param list the list
439 * @param array a pointer to the elements to add
440 * @param n the number of elements to add
441 * @return the number of added elements
442 */
443 __attribute__((__nonnull__))
444 static inline size_t cxListInsertSortedArray(
445 CxList *list,
446 void const *array,
447 size_t n
448 ) {
449 return list->cl->insert_sorted(list, array, n);
374 } 450 }
375 451
376 /** 452 /**
377 * Inserts an element after the current location of the specified iterator. 453 * Inserts an element after the current location of the specified iterator.
378 * 454 *

mercurial