| 283 * |
285 * |
| 284 * Only use this function if you are creating your own list implementation. |
286 * Only use this function if you are creating your own list implementation. |
| 285 * The purpose of this function is to be called in the initialization code |
287 * The purpose of this function is to be called in the initialization code |
| 286 * of your list to set certain members correctly. |
288 * of your list to set certain members correctly. |
| 287 * |
289 * |
| 288 * This is particularly important when you want your list to support |
290 * This is particularly useful when you want your list to support |
| 289 * #CX_STORE_POINTERS as @p elem_size. This function will wrap the list |
291 * #CX_STORE_POINTERS as @p elem_size. |
| 290 * class accordingly and make sure that you can implement your list as if |
|
| 291 * it was only storing objects, and the wrapper will automatically enable |
|
| 292 * the feature of storing pointers. |
|
| 293 * |
292 * |
| 294 * @par Example |
293 * @par Example |
| 295 * |
294 * |
| 296 * @code |
295 * @code |
| 297 * CxList *myCustomListCreate( |
296 * CxList *myCustomListCreate( |
| 298 * const CxAllocator *allocator, |
297 * const CxAllocator *allocator, |
| 299 * cx_compare_func comparator, |
|
| 300 * size_t elem_size |
298 * size_t elem_size |
| 301 * ) { |
299 * ) { |
| 302 * if (allocator == NULL) { |
300 * if (allocator == NULL) { |
| 303 * allocator = cxDefaultAllocator; |
301 * allocator = cxDefaultAllocator; |
| 304 * } |
302 * } |
| 305 * |
303 * |
| 306 * MyCustomList *list = cxCalloc(allocator, 1, sizeof(MyCustomList)); |
304 * MyCustomList *list = cxZalloc(allocator, sizeof(MyCustomList)); |
| 307 * if (list == NULL) return NULL; |
305 * if (list == NULL) return NULL; |
| 308 * |
306 * |
| 309 * // initialize |
307 * // initialize |
| 310 * cx_list_init((CxList*)list, &my_custom_list_class, |
308 * cx_list_init((CxList*)list, &my_custom_list_class, |
| 311 * allocator, comparator, elem_size); |
309 * allocator, elem_size); |
| 312 * |
310 * |
| 313 * // ... some more custom stuff ... |
311 * // ... some more custom stuff ... |
| 314 * |
312 * |
| 315 * return (CxList *) list; |
313 * return (CxList *) list; |
| 316 * } |
314 * } |
| 317 * @endcode |
315 * @endcode |
| 318 * |
316 * |
| 319 * @param list the list to initialize |
317 * @param list the list to initialize |
| 320 * @param cl the list class |
318 * @param cl the list class |
| 321 * @param allocator the allocator for the elements |
319 * @param allocator the allocator for the elements |
| 322 * @param comparator a compare function for the elements |
|
| 323 * @param elem_size the size of one element |
320 * @param elem_size the size of one element |
| 324 */ |
321 */ |
| 325 cx_attr_nonnull_arg(1, 2, 3) |
322 cx_attr_nonnull_arg(1, 2, 3) |
| 326 CX_EXPORT void cx_list_init(struct cx_list_s *list, |
323 CX_EXPORT void cx_list_init(struct cx_list_s *list, |
| 327 struct cx_list_class_s *cl, const struct cx_allocator_s *allocator, |
324 struct cx_list_class_s *cl, const struct cx_allocator_s *allocator, |
| 328 cx_compare_func comparator, size_t elem_size); |
325 size_t elem_size); |
| |
326 |
| |
327 /** |
| |
328 * A @c cx_compare_func2 compatible wrapper for the compare functions of a list. |
| |
329 * |
| |
330 * @param left first element |
| |
331 * @param right second element |
| |
332 * @param list the list which is comparing the elements |
| |
333 * @return the comparison result |
| |
334 */ |
| |
335 cx_attr_nonnull |
| |
336 CX_EXPORT int cx_list_compare_wrapper( |
| |
337 const void *left, const void *right, void *list); |
| 329 |
338 |
| 330 /** |
339 /** |
| 331 * Returns the number of elements currently stored in the list. |
340 * Returns the number of elements currently stored in the list. |
| 332 * |
341 * |
| 333 * @param list the list |
342 * @param list the list |
| 976 * @param clone_func the clone function for the elements |
985 * @param clone_func the clone function for the elements |
| 977 * @param clone_allocator the allocator that is passed to the clone function |
986 * @param clone_allocator the allocator that is passed to the clone function |
| 978 * @param data optional additional data that is passed to the clone function |
987 * @param data optional additional data that is passed to the clone function |
| 979 * @retval zero when all elements were successfully cloned |
988 * @retval zero when all elements were successfully cloned |
| 980 * @retval non-zero when an allocation error occurred |
989 * @retval non-zero when an allocation error occurred |
| 981 * @see cxListUnion() |
990 * @see cxListCloneShallow() |
| 982 */ |
991 */ |
| 983 cx_attr_nonnull_arg(1, 2, 3) |
992 cx_attr_nonnull_arg(1, 2, 3) |
| 984 CX_EXPORT int cxListClone(CxList *dst, const CxList *src, |
993 CX_EXPORT int cxListClone(CxList *dst, const CxList *src, |
| 985 cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data); |
994 cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data); |
| 986 |
995 |
| 999 * @param clone_func the clone function for the elements |
1008 * @param clone_func the clone function for the elements |
| 1000 * @param clone_allocator the allocator that is passed to the clone function |
1009 * @param clone_allocator the allocator that is passed to the clone function |
| 1001 * @param data optional additional data that is passed to the clone function |
1010 * @param data optional additional data that is passed to the clone function |
| 1002 * @retval zero when the elements were successfully cloned |
1011 * @retval zero when the elements were successfully cloned |
| 1003 * @retval non-zero when an allocation error occurred |
1012 * @retval non-zero when an allocation error occurred |
| |
1013 * @see cxListDifferenceShallow() |
| 1004 */ |
1014 */ |
| 1005 cx_attr_nonnull_arg(1, 2, 3, 4) |
1015 cx_attr_nonnull_arg(1, 2, 3, 4) |
| 1006 CX_EXPORT int cxListDifference(CxList *dst, |
1016 CX_EXPORT int cxListDifference(CxList *dst, |
| 1007 const CxList *minuend, const CxList *subtrahend, |
1017 const CxList *minuend, const CxList *subtrahend, |
| 1008 cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data); |
1018 cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data); |
| 1022 * @param clone_func the clone function for the elements |
1032 * @param clone_func the clone function for the elements |
| 1023 * @param clone_allocator the allocator that is passed to the clone function |
1033 * @param clone_allocator the allocator that is passed to the clone function |
| 1024 * @param data optional additional data that is passed to the clone function |
1034 * @param data optional additional data that is passed to the clone function |
| 1025 * @retval zero when the elements were successfully cloned |
1035 * @retval zero when the elements were successfully cloned |
| 1026 * @retval non-zero when an allocation error occurred |
1036 * @retval non-zero when an allocation error occurred |
| |
1037 * @see cxListIntersectionShallow() |
| 1027 */ |
1038 */ |
| 1028 cx_attr_nonnull_arg(1, 2, 3, 4) |
1039 cx_attr_nonnull_arg(1, 2, 3, 4) |
| 1029 CX_EXPORT int cxListIntersection(CxList *dst, const CxList *src, const CxList *other, |
1040 CX_EXPORT int cxListIntersection(CxList *dst, const CxList *src, const CxList *other, |
| 1030 cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data); |
1041 cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data); |
| 1031 |
1042 |
| 1046 * @param clone_func the clone function for the elements |
1057 * @param clone_func the clone function for the elements |
| 1047 * @param clone_allocator the allocator that is passed to the clone function |
1058 * @param clone_allocator the allocator that is passed to the clone function |
| 1048 * @param data optional additional data that is passed to the clone function |
1059 * @param data optional additional data that is passed to the clone function |
| 1049 * @retval zero when the elements were successfully cloned |
1060 * @retval zero when the elements were successfully cloned |
| 1050 * @retval non-zero when an allocation error occurred |
1061 * @retval non-zero when an allocation error occurred |
| 1051 * @see cxListClone() |
1062 * @see cxListUnionShallow() |
| 1052 */ |
1063 */ |
| 1053 cx_attr_nonnull_arg(1, 2, 3, 4) |
1064 cx_attr_nonnull_arg(1, 2, 3, 4) |
| 1054 CX_EXPORT int cxListUnion(CxList *dst, const CxList *src, const CxList *other, |
1065 CX_EXPORT int cxListUnion(CxList *dst, const CxList *src, const CxList *other, |
| 1055 cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data); |
1066 cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data); |
| 1056 |
1067 |
| |
1068 /** |
| |
1069 * Performs a shallow clone of one list into another. |
| |
1070 * |
| |
1071 * This function uses the default allocator, if needed, and performs |
| |
1072 * shallow clones with @c memcpy(). |
| |
1073 * |
| |
1074 * If the destination list already contains elements, the cloned elements |
| |
1075 * are appended to that list. |
| |
1076 * |
| |
1077 * @attention If the cloned elements need to be destroyed by a destructor |
| |
1078 * function, you must make sure that the destination list also uses this |
| |
1079 * destructor function. |
| |
1080 * |
| |
1081 * @param dst the destination list |
| |
1082 * @param src the source list |
| |
1083 * @retval zero when all elements were successfully cloned |
| |
1084 * @retval non-zero when an allocation error occurred |
| |
1085 * @see cxListClone() |
| |
1086 */ |
| |
1087 cx_attr_nonnull |
| |
1088 CX_EXPORT int cxListCloneShallow(CxList *dst, const CxList *src); |
| |
1089 |
| |
1090 /** |
| |
1091 * Clones elements from a list only if they are not present in another list. |
| |
1092 * |
| |
1093 * This function uses the default allocator, if needed, and performs |
| |
1094 * shallow clones with @c memcpy(). |
| |
1095 * |
| |
1096 * If the @p minuend does not contain duplicates, this is equivalent to adding |
| |
1097 * the set difference to the destination list. |
| |
1098 * |
| |
1099 * This function is optimized for the case when both the @p minuend and the |
| |
1100 * @p subtrahend are sorted. |
| |
1101 * |
| |
1102 * @param dst the destination list |
| |
1103 * @param minuend the list to subtract elements from |
| |
1104 * @param subtrahend the elements that shall be subtracted |
| |
1105 * @retval zero when the elements were successfully cloned |
| |
1106 * @retval non-zero when an allocation error occurred |
| |
1107 * @see cxListDifference() |
| |
1108 */ |
| |
1109 cx_attr_nonnull |
| |
1110 CX_EXPORT int cxListDifferenceShallow(CxList *dst, |
| |
1111 const CxList *minuend, const CxList *subtrahend); |
| |
1112 |
| |
1113 /** |
| |
1114 * Clones elements from a list only if they are also present in another list. |
| |
1115 * |
| |
1116 * This function uses the default allocator, if needed, and performs |
| |
1117 * shallow clones with @c memcpy(). |
| |
1118 * |
| |
1119 * This function is optimized for the case when both the @p src and the |
| |
1120 * @p other list are sorted. |
| |
1121 * |
| |
1122 * If the destination list already contains elements, the intersection is appended |
| |
1123 * to that list. |
| |
1124 * |
| |
1125 * @param dst the destination list |
| |
1126 * @param src the list to clone the elements from |
| |
1127 * @param other the list to check the elements for existence |
| |
1128 * @retval zero when the elements were successfully cloned |
| |
1129 * @retval non-zero when an allocation error occurred |
| |
1130 * @see cxListIntersection() |
| |
1131 */ |
| |
1132 cx_attr_nonnull |
| |
1133 CX_EXPORT int cxListIntersectionShallow(CxList *dst, const CxList *src, const CxList *other); |
| |
1134 |
| |
1135 /** |
| |
1136 * Performs a deep clone of one list into another, skipping duplicates. |
| |
1137 * |
| |
1138 * This function uses the default allocator, if needed, and performs |
| |
1139 * shallow clones with @c memcpy(). |
| |
1140 * |
| |
1141 * This function is optimized for the case when both the @p src and the |
| |
1142 * @p other list are sorted. |
| |
1143 * In that case, the union will also be sorted. |
| |
1144 * |
| |
1145 * If the destination list already contains elements, the union is appended |
| |
1146 * to that list. In that case the destination is not necessarily sorted. |
| |
1147 * |
| |
1148 * @param dst the destination list |
| |
1149 * @param src the primary source list |
| |
1150 * @param other the other list, where elements are only cloned from |
| |
1151 * when they are not in @p src |
| |
1152 * @retval zero when the elements were successfully cloned |
| |
1153 * @retval non-zero when an allocation error occurred |
| |
1154 * @see cxListUnion() |
| |
1155 */ |
| |
1156 cx_attr_nonnull |
| |
1157 CX_EXPORT int cxListUnionShallow(CxList *dst, const CxList *src, const CxList *other); |
| |
1158 |
| |
1159 /** |
| |
1160 * Asks the list to reserve enough memory for a given total number of elements. |
| |
1161 * |
| |
1162 * List implementations are free to choose if reserving memory upfront makes |
| |
1163 * sense. |
| |
1164 * For example, array-based implementations usually do support reserving memory |
| |
1165 * for additional elements while linked lists usually don't. |
| |
1166 * |
| |
1167 * @note When the requested capacity is smaller than the current size, |
| |
1168 * this function returns zero without performing any action. |
| |
1169 * |
| |
1170 * @param list the list |
| |
1171 * @param capacity the expected total number of elements |
| |
1172 * @retval zero on success or when overallocation is not supported |
| |
1173 * @retval non-zero when an allocation error occurred |
| |
1174 * @see cxListShrink() |
| |
1175 */ |
| |
1176 cx_attr_nonnull |
| |
1177 CX_EXPORT int cxListReserve(CxList *list, size_t capacity); |
| |
1178 |
| |
1179 /** |
| |
1180 * Advises the list to free any overallocated memory. |
| |
1181 * |
| |
1182 * Lists that do not support overallocation simply return zero. |
| |
1183 * |
| |
1184 * This function usually returns zero, except for very special and custom |
| |
1185 * list and/or allocator implementations where freeing memory can fail. |
| |
1186 * |
| |
1187 * @param list the list |
| |
1188 * @return usually zero |
| |
1189 */ |
| |
1190 cx_attr_nonnull |
| |
1191 CX_EXPORT int cxListShrink(CxList *list); |
| 1057 |
1192 |
| 1058 #ifdef __cplusplus |
1193 #ifdef __cplusplus |
| 1059 } // extern "C" |
1194 } // extern "C" |
| 1060 #endif |
1195 #endif |
| 1061 |
1196 |