Wed, 25 Dec 2024 19:33:04 +0100
add implementation for creating new json values
relates to #527
601 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
26 | * POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | |
28 | ||
29 | #include "cx/compare.h" | |
30 | ||
31 | #include <math.h> | |
32 | ||
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
33 | int cx_cmp_int(const void *i1, const void *i2) { |
678 | 34 | int a = *((const int *) i1); |
35 | int b = *((const int *) i2); | |
601 | 36 | if (a == b) { |
37 | return 0; | |
38 | } else { | |
39 | return a < b ? -1 : 1; | |
40 | } | |
41 | } | |
42 | ||
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
43 | int cx_cmp_longint(const void *i1, const void *i2) { |
678 | 44 | long int a = *((const long int *) i1); |
45 | long int b = *((const long int *) i2); | |
601 | 46 | if (a == b) { |
47 | return 0; | |
48 | } else { | |
49 | return a < b ? -1 : 1; | |
50 | } | |
51 | } | |
52 | ||
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
53 | int cx_cmp_longlong(const void *i1, const void *i2) { |
678 | 54 | long long a = *((const long long *) i1); |
55 | long long b = *((const long long *) i2); | |
601 | 56 | if (a == b) { |
57 | return 0; | |
58 | } else { | |
59 | return a < b ? -1 : 1; | |
60 | } | |
61 | } | |
62 | ||
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
63 | int cx_cmp_int16(const void *i1, const void *i2) { |
678 | 64 | int16_t a = *((const int16_t *) i1); |
65 | int16_t b = *((const int16_t *) i2); | |
601 | 66 | if (a == b) { |
67 | return 0; | |
68 | } else { | |
69 | return a < b ? -1 : 1; | |
70 | } | |
71 | } | |
72 | ||
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
73 | int cx_cmp_int32(const void *i1, const void *i2) { |
678 | 74 | int32_t a = *((const int32_t *) i1); |
75 | int32_t b = *((const int32_t *) i2); | |
601 | 76 | if (a == b) { |
77 | return 0; | |
78 | } else { | |
79 | return a < b ? -1 : 1; | |
80 | } | |
81 | } | |
82 | ||
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
83 | int cx_cmp_int64(const void *i1, const void *i2) { |
678 | 84 | int64_t a = *((const int64_t *) i1); |
85 | int64_t b = *((const int64_t *) i2); | |
601 | 86 | if (a == b) { |
87 | return 0; | |
88 | } else { | |
89 | return a < b ? -1 : 1; | |
90 | } | |
91 | } | |
92 | ||
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
93 | int cx_cmp_uint(const void *i1, const void *i2) { |
678 | 94 | unsigned int a = *((const unsigned int *) i1); |
95 | unsigned int b = *((const unsigned int *) i2); | |
601 | 96 | if (a == b) { |
97 | return 0; | |
98 | } else { | |
99 | return a < b ? -1 : 1; | |
100 | } | |
101 | } | |
102 | ||
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
103 | int cx_cmp_ulongint(const void *i1, const void *i2) { |
678 | 104 | unsigned long int a = *((const unsigned long int *) i1); |
105 | unsigned long int b = *((const unsigned long int *) i2); | |
601 | 106 | if (a == b) { |
107 | return 0; | |
108 | } else { | |
109 | return a < b ? -1 : 1; | |
110 | } | |
111 | } | |
112 | ||
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
113 | int cx_cmp_ulonglong(const void *i1, const void *i2) { |
678 | 114 | unsigned long long a = *((const unsigned long long *) i1); |
115 | unsigned long long b = *((const unsigned long long *) i2); | |
601 | 116 | if (a == b) { |
117 | return 0; | |
118 | } else { | |
119 | return a < b ? -1 : 1; | |
120 | } | |
121 | } | |
122 | ||
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
123 | int cx_cmp_uint16(const void *i1, const void *i2) { |
678 | 124 | uint16_t a = *((const uint16_t *) i1); |
125 | uint16_t b = *((const uint16_t *) i2); | |
601 | 126 | if (a == b) { |
127 | return 0; | |
128 | } else { | |
129 | return a < b ? -1 : 1; | |
130 | } | |
131 | } | |
132 | ||
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
133 | int cx_cmp_uint32(const void *i1, const void *i2) { |
678 | 134 | uint32_t a = *((const uint32_t *) i1); |
135 | uint32_t b = *((const uint32_t *) i2); | |
601 | 136 | if (a == b) { |
137 | return 0; | |
138 | } else { | |
139 | return a < b ? -1 : 1; | |
140 | } | |
141 | } | |
142 | ||
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
143 | int cx_cmp_uint64(const void *i1, const void *i2) { |
678 | 144 | uint64_t a = *((const uint64_t *) i1); |
145 | uint64_t b = *((const uint64_t *) i2); | |
601 | 146 | if (a == b) { |
147 | return 0; | |
148 | } else { | |
149 | return a < b ? -1 : 1; | |
150 | } | |
151 | } | |
152 | ||
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
153 | int cx_cmp_float(const void *f1, const void *f2) { |
678 | 154 | float a = *((const float *) f1); |
155 | float b = *((const float *) f2); | |
601 | 156 | if (fabsf(a - b) < 1e-6f) { |
157 | return 0; | |
158 | } else { | |
159 | return a < b ? -1 : 1; | |
160 | } | |
161 | } | |
162 | ||
631
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
163 | int cx_cmp_double( |
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
164 | const void *d1, |
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
165 | const void *d2 |
631
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
166 | ) { |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
167 | double a = *((const double *) d1); |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
168 | double b = *((const double *) d2); |
601 | 169 | if (fabs(a - b) < 1e-14) { |
170 | return 0; | |
171 | } else { | |
172 | return a < b ? -1 : 1; | |
173 | } | |
174 | } | |
175 | ||
631
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
176 | int cx_cmp_intptr( |
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
177 | const void *ptr1, |
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
178 | const void *ptr2 |
631
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
179 | ) { |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
180 | intptr_t p1 = *(const intptr_t *) ptr1; |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
181 | intptr_t p2 = *(const intptr_t *) ptr2; |
601 | 182 | if (p1 == p2) { |
183 | return 0; | |
184 | } else { | |
631
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
185 | return p1 < p2 ? -1 : 1; |
601 | 186 | } |
187 | } | |
631
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
188 | |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
189 | int cx_cmp_uintptr( |
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
190 | const void *ptr1, |
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
191 | const void *ptr2 |
631
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
192 | ) { |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
193 | uintptr_t p1 = *(const uintptr_t *) ptr1; |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
194 | uintptr_t p2 = *(const uintptr_t *) ptr2; |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
195 | if (p1 == p2) { |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
196 | return 0; |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
197 | } else { |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
198 | return p1 < p2 ? -1 : 1; |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
199 | } |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
200 | } |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
201 | |
762
4523f6d42512
add cx_cmp_ptr() - fix #340
Mike Becker <universe@uap-core.de>
parents:
678
diff
changeset
|
202 | int cx_cmp_ptr( |
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
203 | const void *ptr1, |
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
204 | const void *ptr2 |
762
4523f6d42512
add cx_cmp_ptr() - fix #340
Mike Becker <universe@uap-core.de>
parents:
678
diff
changeset
|
205 | ) { |
4523f6d42512
add cx_cmp_ptr() - fix #340
Mike Becker <universe@uap-core.de>
parents:
678
diff
changeset
|
206 | uintptr_t p1 = (uintptr_t) ptr1; |
4523f6d42512
add cx_cmp_ptr() - fix #340
Mike Becker <universe@uap-core.de>
parents:
678
diff
changeset
|
207 | uintptr_t p2 = (uintptr_t) ptr2; |
4523f6d42512
add cx_cmp_ptr() - fix #340
Mike Becker <universe@uap-core.de>
parents:
678
diff
changeset
|
208 | if (p1 == p2) { |
4523f6d42512
add cx_cmp_ptr() - fix #340
Mike Becker <universe@uap-core.de>
parents:
678
diff
changeset
|
209 | return 0; |
4523f6d42512
add cx_cmp_ptr() - fix #340
Mike Becker <universe@uap-core.de>
parents:
678
diff
changeset
|
210 | } else { |
4523f6d42512
add cx_cmp_ptr() - fix #340
Mike Becker <universe@uap-core.de>
parents:
678
diff
changeset
|
211 | return p1 < p2 ? -1 : 1; |
4523f6d42512
add cx_cmp_ptr() - fix #340
Mike Becker <universe@uap-core.de>
parents:
678
diff
changeset
|
212 | } |
4523f6d42512
add cx_cmp_ptr() - fix #340
Mike Becker <universe@uap-core.de>
parents:
678
diff
changeset
|
213 | } |