1 # Compare Functions
2
3 The `compare.h` header file contains a collection of compare functions for various primitive types.
4
5 They come in two flavors:
6 - prefixed with `cx_vcmp` they are taking the values directly as arguments
7 - prefixed with `cx_cmp` the signature is designed to be compatible with the `cx_compare_func` function pointer type.
8
9 ## Examples
10
11 In the following example we use `cx_cmp_int32` as compare function for a `CxList` of `int32_t` values.
12
13 ```C
14 CxList *list = cxArrayListCreate(
15 cxDefaultAllocator, // use the default allocator
16 cx_cmp_int32, // the compare function for the elements
17 sizeof(int32_t), // the size of one element
18 256 // reseve space for 256 elements
19 );
20 ```
21
22 In the next example we simply want to compare two `double` values with rounding tolerance.
23 Note how the use of the `cx_vcmp` flavour makes it unnecessary to store the literal in a variable just to be able to take an address.
24 ```C
25 double x = ...
26
27 if (0 == cx_vcmp(x, 47.11)) {
28 // do something when equal (except tolerance)
29 }
30 ```
31
32 ## List of Functions
33
34 ```C
35 #include <cx/compare.h>
36
37 // Value Flavour
38 int cx_vcmp_double(double a, double b);
39 int cx_vcmp_float(float a, float b);
40 int cx_vcmp_int(int a, int b);
41 int cx_vcmp_int16(int16_t a, int16_t b);
42 int cx_vcmp_int32(int32_t a, int32_t b));
43 int cx_vcmp_int64(int64_t a, int64_t b);
44 int cx_vcmp_intptr(intptr_t a, intptr_t b);
45 int cx_vcmp_longint(long int a, long int b);
46 int cx_vcmp_longlong(long long a, long long b);
47 int cx_vcmp_uint(unsigned int a, unsigned int b);
48 int cx_vcmp_uint16(uint16_t a, uint16_t b);
49 int cx_vcmp_uint32(uint32_t a, uint32_t b);
50 int cx_vcmp_uint64(uint64_t a, uint64_t b);
51 int cx_vcmp_size(size_t a, size_t b);
52 int cx_vcmp_uintptr(uintptr_t a, uintptr_t b);
53 int cx_vcmp_ulongint(unsigned long int a, unsigned long int b);
54 int cx_vcmp_ulonglong(unsigned long long a, unsigned long long b);
55
56 // Pointer Flavour
57 // (unspecified types make them compatible with cx_compare_func)
58 int cx_cmp_ptr(const void *a, const void *b);
59 int cx_cmp_double(const void *a, const void *b);
60 int cx_cmp_float(const void *a, const void *b);
61 int cx_cmp_int(const void *a, const void *b);
62 int cx_cmp_int16(const void *a, const void *b);
63 int cx_cmp_int32(const void *a, const void *b);
64 int cx_cmp_int64(const void *a, const void *b);
65 int cx_cmp_intptr(const void *a, const void *b);
66 int cx_cmp_longint(const void *a, const void *b);
67 int cx_cmp_longlong(const void *a, const void *b);
68 int cx_cmp_uint(const void *a, const void *b);
69 int cx_cmp_uint16(const void *a, const void *b);
70 int cx_cmp_uint32(const void *a, const void *b);
71 int cx_cmp_uint64(const void *a, const void *b);
72 int cx_cmp_size(const void *a, const void *b);
73 int cx_cmp_uintptr(const void *a, const void *b);
74 int cx_cmp_ulongint(const void *a, const void *b);
75 int cx_cmp_ulonglong(const void *a, const void *b);
76 ```
77
78 <seealso>
79 <category ref="apidoc">
80 <a href="https://ucx.sourceforge.io/api/compare_8h.html">compare.h</a>
81 </category>
82 </seealso>
83