src/cx/list.h

branch
docs/3.1
changeset 1164
148b7c7ccaf9
parent 1163
68ff0839bc6a
equal deleted inserted replaced
1148:8ff82697f2c3 1164:148b7c7ccaf9
163 ); 163 );
164 164
165 /** 165 /**
166 * Member function for finding and optionally removing an element. 166 * Member function for finding and optionally removing an element.
167 */ 167 */
168 ssize_t (*find_remove)( 168 size_t (*find_remove)(
169 struct cx_list_s *list, 169 struct cx_list_s *list,
170 const void *elem, 170 const void *elem,
171 bool remove 171 bool remove
172 ); 172 );
173 173
360 cx_attr_nonnull 360 cx_attr_nonnull
361 static inline int cxListAdd( 361 static inline int cxListAdd(
362 CxList *list, 362 CxList *list,
363 const void *elem 363 const void *elem
364 ) { 364 ) {
365 list->collection.sorted = false;
365 return list->cl->insert_element(list, list->collection.size, elem); 366 return list->cl->insert_element(list, list->collection.size, elem);
366 } 367 }
367 368
368 /** 369 /**
369 * Adds multiple items to the end of the list. 370 * Adds multiple items to the end of the list.
385 static inline size_t cxListAddArray( 386 static inline size_t cxListAddArray(
386 CxList *list, 387 CxList *list,
387 const void *array, 388 const void *array,
388 size_t n 389 size_t n
389 ) { 390 ) {
391 list->collection.sorted = false;
390 return list->cl->insert_array(list, list->collection.size, array, n); 392 return list->cl->insert_array(list, list->collection.size, array, n);
391 } 393 }
392 394
393 /** 395 /**
394 * Inserts an item at the specified index. 396 * Inserts an item at the specified index.
407 static inline int cxListInsert( 409 static inline int cxListInsert(
408 CxList *list, 410 CxList *list,
409 size_t index, 411 size_t index,
410 const void *elem 412 const void *elem
411 ) { 413 ) {
414 list->collection.sorted = false;
412 return list->cl->insert_element(list, index, elem); 415 return list->cl->insert_element(list, index, elem);
413 } 416 }
414 417
415 /** 418 /**
416 * Inserts an item into a sorted list. 419 * Inserts an item into a sorted list.
420 *
421 * If the list is not sorted already, the behavior is undefined.
417 * 422 *
418 * @param list the list 423 * @param list the list
419 * @param elem a pointer to the element to add 424 * @param elem a pointer to the element to add
420 * @retval zero success 425 * @retval zero success
421 * @retval non-zero memory allocation failure 426 * @retval non-zero memory allocation failure
423 cx_attr_nonnull 428 cx_attr_nonnull
424 static inline int cxListInsertSorted( 429 static inline int cxListInsertSorted(
425 CxList *list, 430 CxList *list,
426 const void *elem 431 const void *elem
427 ) { 432 ) {
433 list->collection.sorted = true; // guaranteed by definition
428 const void *data = list->collection.store_pointer ? &elem : elem; 434 const void *data = list->collection.store_pointer ? &elem : elem;
429 return list->cl->insert_sorted(list, data, 1) == 0; 435 return list->cl->insert_sorted(list, data, 1) == 0;
430 } 436 }
431 437
432 /** 438 /**
453 CxList *list, 459 CxList *list,
454 size_t index, 460 size_t index,
455 const void *array, 461 const void *array,
456 size_t n 462 size_t n
457 ) { 463 ) {
464 list->collection.sorted = false;
458 return list->cl->insert_array(list, index, array, n); 465 return list->cl->insert_array(list, index, array, n);
459 } 466 }
460 467
461 /** 468 /**
462 * Inserts a sorted array into a sorted list. 469 * Inserts a sorted array into a sorted list.
467 * If there is not enough memory to add all elements, the returned value is 474 * If there is not enough memory to add all elements, the returned value is
468 * less than @p n. 475 * less than @p n.
469 * 476 *
470 * If this list is storing pointers instead of objects @p array is expected to 477 * If this list is storing pointers instead of objects @p array is expected to
471 * be an array of pointers. 478 * be an array of pointers.
479 *
480 * If the list is not sorted already, the behavior is undefined.
472 * 481 *
473 * @param list the list 482 * @param list the list
474 * @param array a pointer to the elements to add 483 * @param array a pointer to the elements to add
475 * @param n the number of elements to add 484 * @param n the number of elements to add
476 * @return the number of added elements 485 * @return the number of added elements
479 static inline size_t cxListInsertSortedArray( 488 static inline size_t cxListInsertSortedArray(
480 CxList *list, 489 CxList *list,
481 const void *array, 490 const void *array,
482 size_t n 491 size_t n
483 ) { 492 ) {
493 list->collection.sorted = true; // guaranteed by definition
484 return list->cl->insert_sorted(list, array, n); 494 return list->cl->insert_sorted(list, array, n);
485 } 495 }
486 496
487 /** 497 /**
488 * Inserts an element after the current location of the specified iterator. 498 * Inserts an element after the current location of the specified iterator.
503 cx_attr_nonnull 513 cx_attr_nonnull
504 static inline int cxListInsertAfter( 514 static inline int cxListInsertAfter(
505 CxIterator *iter, 515 CxIterator *iter,
506 const void *elem 516 const void *elem
507 ) { 517 ) {
508 return ((struct cx_list_s *) iter->src_handle.m)->cl->insert_iter(iter, elem, 0); 518 CxList* list = iter->src_handle.m;
519 list->collection.sorted = false;
520 return list->cl->insert_iter(iter, elem, 0);
509 } 521 }
510 522
511 /** 523 /**
512 * Inserts an element before the current location of the specified iterator. 524 * Inserts an element before the current location of the specified iterator.
513 * 525 *
527 cx_attr_nonnull 539 cx_attr_nonnull
528 static inline int cxListInsertBefore( 540 static inline int cxListInsertBefore(
529 CxIterator *iter, 541 CxIterator *iter,
530 const void *elem 542 const void *elem
531 ) { 543 ) {
532 return ((struct cx_list_s *) iter->src_handle.m)->cl->insert_iter(iter, elem, 1); 544 CxList* list = iter->src_handle.m;
545 list->collection.sorted = false;
546 return list->cl->insert_iter(iter, elem, 1);
533 } 547 }
534 548
535 /** 549 /**
536 * Removes the element at the specified index. 550 * Removes the element at the specified index.
537 * 551 *
628 * 642 *
629 * @param list the list 643 * @param list the list
630 */ 644 */
631 cx_attr_nonnull 645 cx_attr_nonnull
632 static inline void cxListClear(CxList *list) { 646 static inline void cxListClear(CxList *list) {
647 list->collection.sorted = true; // empty lists are always sorted
633 list->cl->clear(list); 648 list->cl->clear(list);
634 } 649 }
635 650
636 /** 651 /**
637 * Swaps two items in the list. 652 * Swaps two items in the list.
650 static inline int cxListSwap( 665 static inline int cxListSwap(
651 CxList *list, 666 CxList *list,
652 size_t i, 667 size_t i,
653 size_t j 668 size_t j
654 ) { 669 ) {
670 list->collection.sorted = false;
655 return list->cl->swap(list, i, j); 671 return list->cl->swap(list, i, j);
656 } 672 }
657 673
658 /** 674 /**
659 * Returns a pointer to the element at the specified index. 675 * Returns a pointer to the element at the specified index.
817 * 833 *
818 * Determining equality is performed by the list's comparator function. 834 * Determining equality is performed by the list's comparator function.
819 * 835 *
820 * @param list the list 836 * @param list the list
821 * @param elem the element to find 837 * @param elem the element to find
822 * @return the index of the element or a negative 838 * @return the index of the element or the size of the list when the element is not found
823 * value when the element is not found 839 * @see cxListIndexValid()
824 */ 840 */
825 cx_attr_nonnull 841 cx_attr_nonnull
826 cx_attr_nodiscard 842 cx_attr_nodiscard
827 static inline ssize_t cxListFind( 843 static inline size_t cxListFind(
828 const CxList *list, 844 const CxList *list,
829 const void *elem 845 const void *elem
830 ) { 846 ) {
831 return list->cl->find_remove((CxList*)list, elem, false); 847 return list->cl->find_remove((CxList*)list, elem, false);
832 } 848 }
833 849
834 /** 850 /**
851 * Checks if the specified index is within bounds.
852 *
853 * @param list the list
854 * @param index the index
855 * @retval true if the index is within bounds
856 * @retval false if the index is out of bounds
857 */
858 cx_attr_nonnull
859 cx_attr_nodiscard
860 static inline bool cxListIndexValid(const CxList *list, size_t index) {
861 return index < list->collection.size;
862 }
863
864 /**
835 * Removes and returns the index of the first element that equals @p elem. 865 * Removes and returns the index of the first element that equals @p elem.
836 * 866 *
837 * Determining equality is performed by the list's comparator function. 867 * Determining equality is performed by the list's comparator function.
838 * 868 *
839 * @param list the list 869 * @param list the list
840 * @param elem the element to find and remove 870 * @param elem the element to find and remove
841 * @return the index of the now removed element or a negative 871 * @return the index of the now removed element or the list size
842 * value when the element is not found or could not be removed 872 * when the element is not found or could not be removed
843 */ 873 * @see cxListIndexValid()
844 cx_attr_nonnull 874 */
845 static inline ssize_t cxListFindRemove( 875 cx_attr_nonnull
876 static inline size_t cxListFindRemove(
846 CxList *list, 877 CxList *list,
847 const void *elem 878 const void *elem
848 ) { 879 ) {
849 return list->cl->find_remove(list, elem, true); 880 return list->cl->find_remove(list, elem, true);
850 } 881 }
857 * @param list the list 888 * @param list the list
858 */ 889 */
859 cx_attr_nonnull 890 cx_attr_nonnull
860 static inline void cxListSort(CxList *list) { 891 static inline void cxListSort(CxList *list) {
861 list->cl->sort(list); 892 list->cl->sort(list);
893 list->collection.sorted = true;
862 } 894 }
863 895
864 /** 896 /**
865 * Reverses the order of the items. 897 * Reverses the order of the items.
866 * 898 *
867 * @param list the list 899 * @param list the list
868 */ 900 */
869 cx_attr_nonnull 901 cx_attr_nonnull
870 static inline void cxListReverse(CxList *list) { 902 static inline void cxListReverse(CxList *list) {
903 // still sorted, but not according to the cmp_func
904 list->collection.sorted = false;
871 list->cl->reverse(list); 905 list->cl->reverse(list);
872 } 906 }
873 907
874 /** 908 /**
875 * Compares a list to another list of the same type. 909 * Compares a list to another list of the same type.

mercurial