src/cx/list.h

changeset 1305
c34a72d8e104
parent 1293
a8d86a951d0b
equal deleted inserted replaced
1304:57e062a4bb05 1305:c34a72d8e104
201 bool backward 201 bool backward
202 ); 202 );
203 }; 203 };
204 204
205 /** 205 /**
206 * Common type for all list implementations.
207 */
208 typedef struct cx_list_s CxList;
209
210 /**
211 * A shared instance of an empty list.
212 *
213 * Writing to that list is not allowed.
214 *
215 * You can use this is a placeholder for initializing CxList pointers
216 * for which you do not want to reserve memory right from the beginning.
217 */
218 cx_attr_export
219 extern CxList *const cxEmptyList;
220
221 /**
206 * Default implementation of an array insert. 222 * Default implementation of an array insert.
207 * 223 *
208 * This function uses the element insert function for each element of the array. 224 * This function uses the element insert function for each element of the array.
209 * 225 *
210 * Use this in your own list class if you do not want to implement an optimized 226 * Use this in your own list class if you do not want to implement an optimized
334 cx_compare_func comparator, 350 cx_compare_func comparator,
335 size_t elem_size 351 size_t elem_size
336 ); 352 );
337 353
338 /** 354 /**
339 * Common type for all list implementations.
340 */
341 typedef struct cx_list_s CxList;
342
343 /**
344 * Returns the number of elements currently stored in the list. 355 * Returns the number of elements currently stored in the list.
345 * 356 *
346 * @param list the list 357 * @param list the list
347 * @return the number of currently stored elements 358 * @return the number of currently stored elements
348 */ 359 */
789 /** 800 /**
790 * Returns an iterator pointing to the first item of the list. 801 * Returns an iterator pointing to the first item of the list.
791 * 802 *
792 * The returned iterator is position-aware. 803 * The returned iterator is position-aware.
793 * 804 *
794 * If the list is empty, a past-the-end iterator will be returned. 805 * If the list is empty or @c NULL, a past-the-end iterator will be returned.
795 * 806 *
796 * @param list the list 807 * @param list the list
797 * @return a new iterator 808 * @return a new iterator
798 */ 809 */
799 cx_attr_nonnull
800 cx_attr_nodiscard 810 cx_attr_nodiscard
801 static inline CxIterator cxListIterator(const CxList *list) { 811 static inline CxIterator cxListIterator(const CxList *list) {
812 if (list == NULL) list = cxEmptyList;
802 return list->cl->iterator(list, 0, false); 813 return list->cl->iterator(list, 0, false);
803 } 814 }
804 815
805 /** 816 /**
806 * Returns a mutating iterator pointing to the first item of the list. 817 * Returns a mutating iterator pointing to the first item of the list.
807 * 818 *
808 * The returned iterator is position-aware. 819 * The returned iterator is position-aware.
809 * 820 *
810 * If the list is empty, a past-the-end iterator will be returned. 821 * If the list is empty or @c NULL, a past-the-end iterator will be returned.
811 * 822 *
812 * @param list the list 823 * @param list the list
813 * @return a new iterator 824 * @return a new iterator
814 */ 825 */
815 cx_attr_nonnull
816 cx_attr_nodiscard 826 cx_attr_nodiscard
817 static inline CxIterator cxListMutIterator(CxList *list) { 827 static inline CxIterator cxListMutIterator(CxList *list) {
828 if (list == NULL) list = cxEmptyList;
818 return cxListMutIteratorAt(list, 0); 829 return cxListMutIteratorAt(list, 0);
819 } 830 }
820 831
821 832
822 /** 833 /**
823 * Returns a backwards iterator pointing to the last item of the list. 834 * Returns a backwards iterator pointing to the last item of the list.
824 * 835 *
825 * The returned iterator is position-aware. 836 * The returned iterator is position-aware.
826 * 837 *
827 * If the list is empty, a past-the-end iterator will be returned. 838 * If the list is empty or @c NULL, a past-the-end iterator will be returned.
828 * 839 *
829 * @param list the list 840 * @param list the list
830 * @return a new iterator 841 * @return a new iterator
831 */ 842 */
832 cx_attr_nonnull
833 cx_attr_nodiscard 843 cx_attr_nodiscard
834 static inline CxIterator cxListBackwardsIterator(const CxList *list) { 844 static inline CxIterator cxListBackwardsIterator(const CxList *list) {
845 if (list == NULL) list = cxEmptyList;
835 return list->cl->iterator(list, list->collection.size - 1, true); 846 return list->cl->iterator(list, list->collection.size - 1, true);
836 } 847 }
837 848
838 /** 849 /**
839 * Returns a mutating backwards iterator pointing to the last item of the list. 850 * Returns a mutating backwards iterator pointing to the last item of the list.
840 * 851 *
841 * The returned iterator is position-aware. 852 * The returned iterator is position-aware.
842 * 853 *
843 * If the list is empty, a past-the-end iterator will be returned. 854 * If the list is empty or @c NULL, a past-the-end iterator will be returned.
844 * 855 *
845 * @param list the list 856 * @param list the list
846 * @return a new iterator 857 * @return a new iterator
847 */ 858 */
848 cx_attr_nonnull
849 cx_attr_nodiscard 859 cx_attr_nodiscard
850 static inline CxIterator cxListMutBackwardsIterator(CxList *list) { 860 static inline CxIterator cxListMutBackwardsIterator(CxList *list) {
861 if (list == NULL) list = cxEmptyList;
851 return cxListMutBackwardsIteratorAt(list, list->collection.size - 1); 862 return cxListMutBackwardsIteratorAt(list, list->collection.size - 1);
852 } 863 }
853 864
854 /** 865 /**
855 * Returns the index of the first element that equals @p elem. 866 * Returns the index of the first element that equals @p elem.
980 * @param list the list which shall be freed 991 * @param list the list which shall be freed
981 */ 992 */
982 cx_attr_export 993 cx_attr_export
983 void cxListFree(CxList *list); 994 void cxListFree(CxList *list);
984 995
985 /**
986 * A shared instance of an empty list.
987 *
988 * Writing to that list is not allowed.
989 *
990 * You can use this is a placeholder for initializing CxList pointers
991 * for which you do not want to reserve memory right from the beginning.
992 */
993 cx_attr_export
994 extern CxList *const cxEmptyList;
995
996 996
997 #ifdef __cplusplus 997 #ifdef __cplusplus
998 } // extern "C" 998 } // extern "C"
999 #endif 999 #endif
1000 1000

mercurial