src/cx/list.h

changeset 1315
b4c3e0b4c3d5
parent 1305
c34a72d8e104
child 1316
c41538edfcef
equal deleted inserted replaced
1314:25108c15e2d4 1315:b4c3e0b4c3d5
580 } 580 }
581 581
582 /** 582 /**
583 * Removes and returns the element at the specified index. 583 * Removes and returns the element at the specified index.
584 * 584 *
585 * No destructor is called and instead the element is copied to the 585 * No destructor is called, and instead the element is copied to the
586 * @p targetbuf which MUST be large enough to hold the removed element. 586 * @p targetbuf which MUST be large enough to hold the removed element.
587 * If the list is storing pointers, only the pointer is copied to @p targetbuf.
587 * 588 *
588 * @param list the list 589 * @param list the list
589 * @param index the index of the element 590 * @param index the index of the element
590 * @param targetbuf a buffer where to copy the element 591 * @param targetbuf a buffer where to copy the element
591 * @retval zero success 592 * @retval zero success
600 ) { 601 ) {
601 return list->cl->remove(list, index, 1, targetbuf) == 0; 602 return list->cl->remove(list, index, 1, targetbuf) == 0;
602 } 603 }
603 604
604 /** 605 /**
606 * Removes and returns the first element of the list.
607 *
608 * No destructor is called, and instead the element is copied to the
609 * @p targetbuf which MUST be large enough to hold the removed element.
610 * If the list is storing pointers, only the pointer is copied to @p targetbuf.
611 *
612 * @param list the list
613 * @param targetbuf a buffer where to copy the element
614 * @retval zero success
615 * @retval non-zero list is empty
616 * @see cxListPopFront()
617 * @see cxListRemoveAndGetLast()
618 */
619 cx_attr_nonnull
620 cx_attr_access_w(2)
621 static inline int cxListRemoveAndGetFirst(
622 CxList *list,
623 void *targetbuf
624 ) {
625 return list->cl->remove(list, 0, 1, targetbuf) == 0;
626 }
627
628 /**
629 * Removes and returns the first element of the list.
630 *
631 * Alias for cxListRemoveAndGetFirst().
632 *
633 * No destructor is called, and instead the element is copied to the
634 * @p targetbuf which MUST be large enough to hold the removed element.
635 * If the list is storing pointers, only the pointer is copied to @p targetbuf.
636 *
637 * @param list (@c CxList*) the list
638 * @param targetbuf (@c void*) a buffer where to copy the element
639 * @retval zero success
640 * @retval non-zero list is empty
641 * @see cxListRemoveAndGetFirst()
642 * @see cxListPop()
643 */
644 #define cxListPopFront(list, targetbuf) cxListRemoveAndGetFirst((list), (targetbuf))
645
646
647 /**
648 * Removes and returns the last element of the list.
649 *
650 * No destructor is called, and instead the element is copied to the
651 * @p targetbuf which MUST be large enough to hold the removed element.
652 * If the list is storing pointers, only the pointer is copied to @p targetbuf.
653 *
654 * @param list the list
655 * @param targetbuf a buffer where to copy the element
656 * @retval zero success
657 * @retval non-zero list is empty
658 */
659 cx_attr_nonnull
660 cx_attr_access_w(2)
661 static inline int cxListRemoveAndGetLast(
662 CxList *list,
663 void *targetbuf
664 ) {
665 // note: index may wrap - member function will catch that
666 return list->cl->remove(list, list->collection.size - 1, 1, targetbuf) == 0;
667 }
668
669 /**
670 * Removes and returns the last element of the list.
671 *
672 * Alias for cxListRemoveAndGetLast().
673 *
674 * No destructor is called, and instead the element is copied to the
675 * @p targetbuf which MUST be large enough to hold the removed element.
676 * If the list is storing pointers, only the pointer is copied to @p targetbuf.
677 *
678 * @param list (@c CxList*) the list
679 * @param targetbuf (@c void*) a buffer where to copy the element
680 * @retval zero success
681 * @retval non-zero list is empty
682 * @see cxListRemoveAndGetLast()
683 * @see cxListPopFront()
684 */
685 #define cxListPop(list, targetbuf) cxListRemoveAndGetLast((list), (targetbuf))
686
687 /**
605 * Removes multiple element starting at the specified index. 688 * Removes multiple element starting at the specified index.
606 * 689 *
607 * If an element destructor function is specified, it is called for each 690 * If an element destructor function is specified, it is called for each
608 * element. It is guaranteed that the destructor is called before removing 691 * element. It is guaranteed that the destructor is called before removing
609 * the element, however, due to possible optimizations it is neither guaranteed 692 * the element. However, due to possible optimizations, it is neither guaranteed
610 * that the destructors are invoked for all elements before starting to remove 693 * that the destructors are invoked for all elements before starting to remove
611 * them, nor that the element is removed immediately after the destructor call 694 * them, nor that the element is removed immediately after the destructor call
612 * before proceeding to the next element. 695 * before proceeding to the next element.
613 * 696 *
614 * @param list the list 697 * @param list the list
624 ) { 707 ) {
625 return list->cl->remove(list, index, num, NULL); 708 return list->cl->remove(list, index, num, NULL);
626 } 709 }
627 710
628 /** 711 /**
629 * Removes and returns multiple element starting at the specified index. 712 * Removes and returns multiple elements starting at the specified index.
630 * 713 *
631 * No destructor is called and instead the elements are copied to the 714 * No destructor is called, and instead the elements are copied to the
632 * @p targetbuf which MUST be large enough to hold all removed elements. 715 * @p targetbuf which MUST be large enough to hold all removed elements.
716 * If the list is storing pointers, @p targetbuf is expected to be an array of pointers.
633 * 717 *
634 * @param list the list 718 * @param list the list
635 * @param index the index of the element 719 * @param index the index of the element
636 * @param num the number of elements to remove 720 * @param num the number of elements to remove
637 * @param targetbuf a buffer where to copy the elements 721 * @param targetbuf a buffer where to copy the elements
663 } 747 }
664 748
665 /** 749 /**
666 * Swaps two items in the list. 750 * Swaps two items in the list.
667 * 751 *
668 * Implementations should only allocate temporary memory for the swap, if 752 * Implementations should only allocate temporary memory for the swap if
669 * it is necessary. 753 * it is necessary.
670 * 754 *
671 * @param list the list 755 * @param list the list
672 * @param i the index of the first element 756 * @param i the index of the first element
673 * @param j the index of the second element 757 * @param j the index of the second element
674 * @retval zero success 758 * @retval zero success
675 * @retval non-zero one of the indices is out of bounds 759 * @retval non-zero one of the indices is out of bounds,
676 * or the swap needed extra memory but allocation failed 760 * or the swap needed extra memory, but allocation failed
677 */ 761 */
678 cx_attr_nonnull 762 cx_attr_nonnull
679 static inline int cxListSwap( 763 static inline int cxListSwap(
680 CxList *list, 764 CxList *list,
681 size_t i, 765 size_t i,
685 return list->cl->swap(list, i, j); 769 return list->cl->swap(list, i, j);
686 } 770 }
687 771
688 /** 772 /**
689 * Returns a pointer to the element at the specified index. 773 * Returns a pointer to the element at the specified index.
774 *
775 * If the list is storing pointers, returns the pointer stored at the specified index.
690 * 776 *
691 * @param list the list 777 * @param list the list
692 * @param index the index of the element 778 * @param index the index of the element
693 * @return a pointer to the element or @c NULL if the index is out of bounds 779 * @return a pointer to the element or @c NULL if the index is out of bounds
694 */ 780 */
698 size_t index 784 size_t index
699 ) { 785 ) {
700 return list->cl->at(list, index); 786 return list->cl->at(list, index);
701 } 787 }
702 788
789 /**
790 * Returns a pointer to the first element.
791 *
792 * If the list is storing pointers, returns the first pointer stored in the list.
793 *
794 * @param list the list
795 * @return a pointer to the first element or @c NULL if the list is empty
796 */
797 cx_attr_nonnull
798 static inline void *cxListFirst(const CxList *list) {
799 return list->cl->at(list, 0);
800 }
801
802 /**
803 * Returns a pointer to the last element.
804 *
805 * If the list is storing pointers, returns the last pointer stored in the list.
806 *
807 * @param list the list
808 * @return a pointer to the last element or @c NULL if the list is empty
809 */
810 cx_attr_nonnull
811 static inline void *cxListLast(const CxList *list) {
812 return list->cl->at(list, list->collection.size - 1);
813 }
703 814
704 /** 815 /**
705 * Sets the element at the specified index in the list 816 * Sets the element at the specified index in the list
706 * 817 *
707 * @param list the list to set the element in 818 * @param list the list to set the element in

mercurial