src/compare.c

Fri, 12 Apr 2024 21:48:12 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 12 Apr 2024 21:48:12 +0200
changeset 849
edb9f875b7f9
parent 762
4523f6d42512
permissions
-rw-r--r--

improves interface of cx_sprintf() variants

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

mercurial