src/cx/compare.h

changeset 1618
ef7cab6eb131
parent 1426
3a89b31f0724
equal deleted inserted replaced
1617:d4385f35f8b0 1618:ef7cab6eb131
55 * pointer. 55 * pointer.
56 */ 56 */
57 typedef int (*cx_compare_func)(const void *left, const void *right); 57 typedef int (*cx_compare_func)(const void *left, const void *right);
58 58
59 /** 59 /**
60 * A comparator function comparing two arbitrary values.
61 *
62 * Functions with this signature allow specifying a pointer to custom data.
63 */
64 typedef int (*cx_compare_func2)(const void *left, const void *right, void *data);
65
66 /**
60 * Compares two integers of type int. 67 * Compares two integers of type int.
61 * 68 *
62 * @note the parameters deliberately have type @c void* to be 69 * @note the parameters deliberately have type @c void* to be
63 * compatible with #cx_compare_func without the need of a cast. 70 * compatible with #cx_compare_func without the need of a cast.
64 * 71 *
524 * @retval 0 if both arguments are equal 531 * @retval 0 if both arguments are equal
525 * @retval 1 if the left argument is greater than the right argument 532 * @retval 1 if the left argument is greater than the right argument
526 */ 533 */
527 cx_attr_nonnull cx_attr_nodiscard 534 cx_attr_nonnull cx_attr_nodiscard
528 CX_EXPORT int cx_cmp_ptr(const void *ptr1, const void *ptr2); 535 CX_EXPORT int cx_cmp_ptr(const void *ptr1, const void *ptr2);
536
537 /**
538 * A @c cx_compare_func2 compatible wrapper for @c memcmp().
539 *
540 * @param ptr1 pointer one
541 * @param ptr2 pointer two
542 * @param n (@c size_t*) a pointer to the length
543 * @return the result of @c memcmp()
544 */
545 cx_attr_nonnull cx_attr_nodiscard
546 CX_EXPORT int cx_acmp_memcmp(const void *ptr1, const void *ptr2, void *n);
547
548 /** Wraps a compare function for cx_acmp_wrap. */
549 typedef struct {
550 /** The wrapped compare function */
551 cx_compare_func cmp;
552 } cx_compare_func_wrapper;
553
554 /**
555 * A @c cx_compare_func2 wrapper for a @c cx_compare_func().
556 *
557 * This is not strictly compatible with a @c cx_compare_func2 because
558 * ISO C does not define conversions between function and object pointers.
559 *
560 * But it works on all tested platforms to cast a pointer to this function to
561 * a @c cx_compare_func2.
562 *
563 * @param ptr1 pointer one
564 * @param ptr2 pointer two
565 * @param cmp_wrapper a pointer to a @c cx_compare_func_wrapper
566 * @return the result of the invoked compare function
567 * @see cx_compare_func_wrapper_s
568 */
569 cx_attr_nonnull cx_attr_nodiscard
570 CX_EXPORT int cx_acmp_wrap(const void *ptr1, const void *ptr2, void* cmp_wrapper);
529 571
530 #ifdef __cplusplus 572 #ifdef __cplusplus
531 } // extern "C" 573 } // extern "C"
532 #endif 574 #endif
533 575

mercurial