Wed, 17 Dec 2025 19:05:50 +0100
huge refactoring of collections to add support for 3-arg compare functions
|
1143
0559812df10c
assign proper names to the documentation topics
Mike Becker <universe@uap-core.de>
parents:
1142
diff
changeset
|
1 | # Compare Functions |
| 1141 | 2 | |
|
1167
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
3 | The `compare.h` header file contains a collection of compare functions for various primitive types. |
|
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
4 | |
|
1618
ef7cab6eb131
huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents:
1424
diff
changeset
|
5 | They come in three flavors: |
|
1167
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
6 | - prefixed with `cx_vcmp` they are taking the values directly as arguments |
|
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
7 | - prefixed with `cx_cmp` the signature is designed to be compatible with the `cx_compare_func` function pointer type. |
|
1618
ef7cab6eb131
huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents:
1424
diff
changeset
|
8 | - prefixed with `cx_ccmp` the signature is designed to be compatible with the `cx_compare_func2` function pointer type. |
|
1167
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
9 | |
|
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
10 | ## Examples |
|
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
11 | |
|
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
12 | In the following example we use `cx_cmp_int32` as compare function for a `CxList` of `int32_t` values. |
|
1146
151c057faf7c
add marker to every incomplete page
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
13 | |
|
1167
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
14 | ```C |
|
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
15 | CxList *list = cxArrayListCreate( |
|
1318
12fa1d37fe48
allow changing the cxDefaultAllocator - resolves #669
Mike Becker <universe@uap-core.de>
parents:
1174
diff
changeset
|
16 | cxDefaultAllocator, // use the default allocator |
|
1167
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
17 | sizeof(int32_t), // the size of one element |
|
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
18 | 256 // reseve space for 256 elements |
|
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
19 | ); |
|
1618
ef7cab6eb131
huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents:
1424
diff
changeset
|
20 | cxSetCompareFunc(list, cx_cmp_int32); // the compare function for the elements |
|
1167
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
21 | ``` |
|
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
22 | |
|
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
23 | In the next example we simply want to compare two `double` values with rounding tolerance. |
|
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
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. |
|
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
25 | ```C |
|
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
26 | double x = ... |
|
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
27 | |
|
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
28 | if (0 == cx_vcmp(x, 47.11)) { |
|
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
29 | // do something when equal (except tolerance) |
|
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
30 | } |
|
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
31 | ``` |
|
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
32 | |
|
1618
ef7cab6eb131
huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents:
1424
diff
changeset
|
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`, |
|
ef7cab6eb131
huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents:
1424
diff
changeset
|
34 | you can use `cx_ccmp_wrap` and the `cx_compare_func_wrapper` struct to wrap your function. |
|
ef7cab6eb131
huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents:
1424
diff
changeset
|
35 | |
|
ef7cab6eb131
huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents:
1424
diff
changeset
|
36 | ```C |
|
ef7cab6eb131
huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents:
1424
diff
changeset
|
37 | void some_fun(int x, int y, cx_compare_func2 f, void *context); |
|
ef7cab6eb131
huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents:
1424
diff
changeset
|
38 | |
|
ef7cab6eb131
huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents:
1424
diff
changeset
|
39 | cx_compare_func_wrapper wrapper = {my_cmp_fun}; |
|
ef7cab6eb131
huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents:
1424
diff
changeset
|
40 | some_fun(x, y, cx_ccmp_wrap, &wrapper); |
|
ef7cab6eb131
huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents:
1424
diff
changeset
|
41 | ``` |
|
ef7cab6eb131
huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents:
1424
diff
changeset
|
42 | |
|
1167
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
43 | ## List of Functions |
|
1142
9437530176bc
add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents:
1141
diff
changeset
|
44 | |
|
1167
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
45 | ```C |
|
1174
ee473780cc0d
add missing documentation about what header to include
Mike Becker <universe@uap-core.de>
parents:
1167
diff
changeset
|
46 | #include <cx/compare.h> |
|
ee473780cc0d
add missing documentation about what header to include
Mike Becker <universe@uap-core.de>
parents:
1167
diff
changeset
|
47 | |
|
1167
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
48 | // Value Flavour |
|
1424
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
49 | int cx_vcmp_double(double a, double b); |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
50 | int cx_vcmp_float(float a, float b); |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
51 | int cx_vcmp_int(int a, int b); |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
52 | int cx_vcmp_int16(int16_t a, int16_t b); |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
53 | int cx_vcmp_int32(int32_t a, int32_t b)); |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
54 | int cx_vcmp_int64(int64_t a, int64_t b); |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
55 | int cx_vcmp_intptr(intptr_t a, intptr_t b); |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
56 | int cx_vcmp_longint(long int a, long int b); |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
57 | int cx_vcmp_longlong(long long a, long long b); |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
58 | int cx_vcmp_uint(unsigned int a, unsigned int b); |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
59 | int cx_vcmp_uint16(uint16_t a, uint16_t b); |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
60 | int cx_vcmp_uint32(uint32_t a, uint32_t b); |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
61 | int cx_vcmp_uint64(uint64_t a, uint64_t b); |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
62 | int cx_vcmp_size(size_t a, size_t b); |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
63 | int cx_vcmp_uintptr(uintptr_t a, uintptr_t b); |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
64 | int cx_vcmp_ulongint(unsigned long int a, unsigned long int b); |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
65 | int cx_vcmp_ulonglong(unsigned long long a, unsigned long long b); |
|
1167
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
66 | |
|
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
67 | // Pointer Flavour |
|
1424
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
68 | // (unspecified types make them compatible with cx_compare_func) |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
69 | int cx_cmp_ptr(const void *a, const void *b); |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
70 | int cx_cmp_double(const void *a, const void *b); |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
71 | int cx_cmp_float(const void *a, const void *b); |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
72 | int cx_cmp_int(const void *a, const void *b); |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
73 | int cx_cmp_int16(const void *a, const void *b); |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
74 | int cx_cmp_int32(const void *a, const void *b); |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
75 | int cx_cmp_int64(const void *a, const void *b); |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
76 | int cx_cmp_intptr(const void *a, const void *b); |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
77 | int cx_cmp_longint(const void *a, const void *b); |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
78 | int cx_cmp_longlong(const void *a, const void *b); |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
79 | int cx_cmp_uint(const void *a, const void *b); |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
80 | int cx_cmp_uint16(const void *a, const void *b); |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
81 | int cx_cmp_uint32(const void *a, const void *b); |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
82 | int cx_cmp_uint64(const void *a, const void *b); |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
83 | int cx_cmp_size(const void *a, const void *b); |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
84 | int cx_cmp_uintptr(const void *a, const void *b); |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
85 | int cx_cmp_ulongint(const void *a, const void *b); |
|
563033aa998c
fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents:
1399
diff
changeset
|
86 | int cx_cmp_ulonglong(const void *a, const void *b); |
|
1618
ef7cab6eb131
huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents:
1424
diff
changeset
|
87 | |
|
ef7cab6eb131
huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents:
1424
diff
changeset
|
88 | // Three-arguments Flavour |
|
ef7cab6eb131
huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents:
1424
diff
changeset
|
89 | int cx_ccmp_memcmp(const void *a, const void *b, void *size); |
|
ef7cab6eb131
huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents:
1424
diff
changeset
|
90 | int cx_ccmp_wrap(const void *a, const void *b, void *cmp_fun_wrapper); |
|
1167
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
91 | ``` |
|
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
92 | |
|
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
93 | <seealso> |
|
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
94 | <category ref="apidoc"> |
|
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
95 | <a href="https://ucx.sourceforge.io/api/compare_8h.html">compare.h</a> |
|
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
96 | </category> |
|
feab7c1e80d4
add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
97 | </seealso> |