| 1 # Compare Functions |
1 # Compare Functions |
| 2 |
2 |
| 3 The `compare.h` header file contains a collection of compare functions for various primitive types. |
3 The `compare.h` header file contains a collection of compare functions for various primitive types. |
| 4 |
4 |
| 5 They come in two flavors: |
5 They come in three flavors: |
| 6 - prefixed with `cx_vcmp` they are taking the values directly as arguments |
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. |
7 - prefixed with `cx_cmp` the signature is designed to be compatible with the `cx_compare_func` function pointer type. |
| |
8 - prefixed with `cx_ccmp` the signature is designed to be compatible with the `cx_compare_func2` function pointer type. |
| 8 |
9 |
| 9 ## Examples |
10 ## Examples |
| 10 |
11 |
| 11 In the following example we use `cx_cmp_int32` as compare function for a `CxList` of `int32_t` values. |
12 In the following example we use `cx_cmp_int32` as compare function for a `CxList` of `int32_t` values. |
| 12 |
13 |
| 13 ```C |
14 ```C |
| 14 CxList *list = cxArrayListCreate( |
15 CxList *list = cxArrayListCreate( |
| 15 cxDefaultAllocator, // use the default allocator |
16 cxDefaultAllocator, // use the default allocator |
| 16 cx_cmp_int32, // the compare function for the elements |
|
| 17 sizeof(int32_t), // the size of one element |
17 sizeof(int32_t), // the size of one element |
| 18 256 // reseve space for 256 elements |
18 256 // reseve space for 256 elements |
| 19 ); |
19 ); |
| |
20 cxSetCompareFunc(list, cx_cmp_int32); // the compare function for the elements |
| 20 ``` |
21 ``` |
| 21 |
22 |
| 22 In the next example we simply want to compare two `double` values with rounding tolerance. |
23 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 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 ```C |
| 25 double x = ... |
26 double x = ... |
| 26 |
27 |
| 27 if (0 == cx_vcmp(x, 47.11)) { |
28 if (0 == cx_vcmp(x, 47.11)) { |
| 28 // do something when equal (except tolerance) |
29 // do something when equal (except tolerance) |
| 29 } |
30 } |
| |
31 ``` |
| |
32 |
| |
33 If you only have a `cx_compare_func` at hand but need to call a function that expects a `cx_compare_func2` and a `context`, |
| |
34 you can use `cx_ccmp_wrap` and the `cx_compare_func_wrapper` struct to wrap your function. |
| |
35 |
| |
36 ```C |
| |
37 void some_fun(int x, int y, cx_compare_func2 f, void *context); |
| |
38 |
| |
39 cx_compare_func_wrapper wrapper = {my_cmp_fun}; |
| |
40 some_fun(x, y, cx_ccmp_wrap, &wrapper); |
| 30 ``` |
41 ``` |
| 31 |
42 |
| 32 ## List of Functions |
43 ## List of Functions |
| 33 |
44 |
| 34 ```C |
45 ```C |
| 71 int cx_cmp_uint64(const void *a, const void *b); |
82 int cx_cmp_uint64(const void *a, const void *b); |
| 72 int cx_cmp_size(const void *a, const void *b); |
83 int cx_cmp_size(const void *a, const void *b); |
| 73 int cx_cmp_uintptr(const void *a, const void *b); |
84 int cx_cmp_uintptr(const void *a, const void *b); |
| 74 int cx_cmp_ulongint(const void *a, const void *b); |
85 int cx_cmp_ulongint(const void *a, const void *b); |
| 75 int cx_cmp_ulonglong(const void *a, const void *b); |
86 int cx_cmp_ulonglong(const void *a, const void *b); |
| |
87 |
| |
88 // Three-arguments Flavour |
| |
89 int cx_ccmp_memcmp(const void *a, const void *b, void *size); |
| |
90 int cx_ccmp_wrap(const void *a, const void *b, void *cmp_fun_wrapper); |
| 76 ``` |
91 ``` |
| 77 |
92 |
| 78 <seealso> |
93 <seealso> |
| 79 <category ref="apidoc"> |
94 <category ref="apidoc"> |
| 80 <a href="https://ucx.sourceforge.io/api/compare_8h.html">compare.h</a> |
95 <a href="https://ucx.sourceforge.io/api/compare_8h.html">compare.h</a> |