docs/Writerside/topics/compare.h.md

Sun, 23 Nov 2025 13:15:19 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 23 Nov 2025 13:15:19 +0100
changeset 1508
dfc0ddd9571e
parent 1424
563033aa998c
permissions
-rw-r--r--

optimize sorted insertion by using the infimum instead of the supremum

The reason is that the supremum returns the equal element with the smallest index, and we want the largest.
Therefore, we use the infimum, which already gives us the largest index when there are equal elements, and increase the index by one. The infimum is also guaranteed to exist in that case.

1143
0559812df10c assign proper names to the documentation topics
Mike Becker <universe@uap-core.de>
parents: 1142
diff changeset
1 # Compare Functions
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
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
feab7c1e80d4 add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
5 They come in two flavors:
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.
feab7c1e80d4 add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
8
feab7c1e80d4 add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
9 ## Examples
feab7c1e80d4 add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
10
feab7c1e80d4 add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
11 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
12
1167
feab7c1e80d4 add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
13 ```C
feab7c1e80d4 add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
14 CxList *list = cxArrayListCreate(
1318
12fa1d37fe48 allow changing the cxDefaultAllocator - resolves #669
Mike Becker <universe@uap-core.de>
parents: 1174
diff changeset
15 cxDefaultAllocator, // use the default allocator
1167
feab7c1e80d4 add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
16 cx_cmp_int32, // the compare function for the elements
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 );
feab7c1e80d4 add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
20 ```
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 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
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.
feab7c1e80d4 add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
24 ```C
feab7c1e80d4 add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
25 double x = ...
feab7c1e80d4 add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
26
feab7c1e80d4 add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
27 if (0 == cx_vcmp(x, 47.11)) {
feab7c1e80d4 add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
28 // do something when equal (except tolerance)
feab7c1e80d4 add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
29 }
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 ## List of Functions
1142
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
33
1167
feab7c1e80d4 add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
34 ```C
1174
ee473780cc0d add missing documentation about what header to include
Mike Becker <universe@uap-core.de>
parents: 1167
diff changeset
35 #include <cx/compare.h>
ee473780cc0d add missing documentation about what header to include
Mike Becker <universe@uap-core.de>
parents: 1167
diff changeset
36
1167
feab7c1e80d4 add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
37 // 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
38 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
39 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
40 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
41 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
42 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
43 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
44 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
45 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
46 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
47 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
48 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
49 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
50 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
51 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
52 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
53 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
54 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
55
feab7c1e80d4 add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
56 // 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
57 // (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
58 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
59 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
60 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
61 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
62 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
63 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
64 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
65 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
66 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
67 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
68 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
69 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
70 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
71 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
72 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
73 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
74 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
75 int cx_cmp_ulonglong(const void *a, const void *b);
1167
feab7c1e80d4 add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
76 ```
feab7c1e80d4 add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
77
feab7c1e80d4 add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
78 <seealso>
feab7c1e80d4 add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
79 <category ref="apidoc">
feab7c1e80d4 add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
80 <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
81 </category>
feab7c1e80d4 add documentation for compare.h
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
82 </seealso>

mercurial