add compare functions

Sat, 05 Nov 2022 17:50:04 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 05 Nov 2022 17:50:04 +0100
changeset 601
95ba6014041b
parent 600
19211b88cc0f
child 602
3b071ea0e9cf

add compare functions

src/CMakeLists.txt file | annotate | diff | comparison | revisions
src/compare.c file | annotate | diff | comparison | revisions
src/cx/compare.h file | annotate | diff | comparison | revisions
     1.1 --- a/src/CMakeLists.txt	Sat Nov 05 17:44:52 2022 +0100
     1.2 +++ b/src/CMakeLists.txt	Sat Nov 05 17:50:04 2022 +0100
     1.3 @@ -10,6 +10,7 @@
     1.4          hash_map.c
     1.5          basic_mempool.c
     1.6          printf.c
     1.7 +        compare.c
     1.8  )
     1.9  set(headers
    1.10          cx/common.h
    1.11 @@ -27,6 +28,7 @@
    1.12          cx/mempool.h
    1.13          cx/basic_mempool.h
    1.14          cx/printf.h
    1.15 +        cx/compare.h
    1.16          )
    1.17  
    1.18  add_library(ucx SHARED ${sources})
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/compare.c	Sat Nov 05 17:50:04 2022 +0100
     2.3 @@ -0,0 +1,182 @@
     2.4 +/*
     2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6 + *
     2.7 + * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
     2.8 + *
     2.9 + * Redistribution and use in source and binary forms, with or without
    2.10 + * modification, are permitted provided that the following conditions are met:
    2.11 + *
    2.12 + *   1. Redistributions of source code must retain the above copyright
    2.13 + *      notice, this list of conditions and the following disclaimer.
    2.14 + *
    2.15 + *   2. Redistributions in binary form must reproduce the above copyright
    2.16 + *      notice, this list of conditions and the following disclaimer in the
    2.17 + *      documentation and/or other materials provided with the distribution.
    2.18 + *
    2.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    2.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    2.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    2.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    2.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    2.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    2.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    2.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    2.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    2.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    2.29 + * POSSIBILITY OF SUCH DAMAGE.
    2.30 + */
    2.31 +
    2.32 +#include "cx/compare.h"
    2.33 +
    2.34 +#include <stdint.h>
    2.35 +#include <math.h>
    2.36 +
    2.37 +int cx_cmp_int(void const *i1, void const *i2) {
    2.38 +    int a = *((const int*) i1);
    2.39 +    int b = *((const int*) i2);
    2.40 +    if (a == b) {
    2.41 +        return 0;
    2.42 +    } else {
    2.43 +        return a < b ? -1 : 1;
    2.44 +    }
    2.45 +}
    2.46 +
    2.47 +int cx_cmp_longint(void const *i1, void const *i2) {
    2.48 +    long int a = *((const long int*) i1);
    2.49 +    long int b = *((const long int*) i2);
    2.50 +    if (a == b) {
    2.51 +        return 0;
    2.52 +    } else {
    2.53 +        return a < b ? -1 : 1;
    2.54 +    }
    2.55 +}
    2.56 +
    2.57 +int cx_cmp_longlong(void const *i1, void const *i2) {
    2.58 +    long long a = *((const long long*) i1);
    2.59 +    long long b = *((const long long*) i2);
    2.60 +    if (a == b) {
    2.61 +        return 0;
    2.62 +    } else {
    2.63 +        return a < b ? -1 : 1;
    2.64 +    }
    2.65 +}
    2.66 +
    2.67 +int cx_cmp_int16(void const *i1, void const *i2) {
    2.68 +    int16_t a = *((const int16_t*) i1);
    2.69 +    int16_t b = *((const int16_t*) i2);
    2.70 +    if (a == b) {
    2.71 +        return 0;
    2.72 +    } else {
    2.73 +        return a < b ? -1 : 1;
    2.74 +    }
    2.75 +}
    2.76 +
    2.77 +int cx_cmp_int32(void const *i1, void const *i2) {
    2.78 +    int32_t a = *((const int32_t*) i1);
    2.79 +    int32_t b = *((const int32_t*) i2);
    2.80 +    if (a == b) {
    2.81 +        return 0;
    2.82 +    } else {
    2.83 +        return a < b ? -1 : 1;
    2.84 +    }
    2.85 +}
    2.86 +
    2.87 +int cx_cmp_int64(void const *i1, void const *i2) {
    2.88 +    int64_t a = *((const int64_t*) i1);
    2.89 +    int64_t b = *((const int64_t*) i2);
    2.90 +    if (a == b) {
    2.91 +        return 0;
    2.92 +    } else {
    2.93 +        return a < b ? -1 : 1;
    2.94 +    }
    2.95 +}
    2.96 +
    2.97 +int cx_cmp_uint(void const *i1, void const *i2) {
    2.98 +    unsigned int a = *((const unsigned int*) i1);
    2.99 +    unsigned int b = *((const unsigned int*) i2);
   2.100 +    if (a == b) {
   2.101 +        return 0;
   2.102 +    } else {
   2.103 +        return a < b ? -1 : 1;
   2.104 +    }
   2.105 +}
   2.106 +
   2.107 +int cx_cmp_ulongint(void const *i1, void const *i2) {
   2.108 +    unsigned long int a = *((const unsigned long int*) i1);
   2.109 +    unsigned long int b = *((const unsigned long int*) i2);
   2.110 +    if (a == b) {
   2.111 +        return 0;
   2.112 +    } else {
   2.113 +        return a < b ? -1 : 1;
   2.114 +    }
   2.115 +}
   2.116 +
   2.117 +int cx_cmp_ulonglong(void const *i1, void const *i2) {
   2.118 +    unsigned long long a = *((const unsigned long long*) i1);
   2.119 +    unsigned long long b = *((const unsigned long long*) i2);
   2.120 +    if (a == b) {
   2.121 +        return 0;
   2.122 +    } else {
   2.123 +        return a < b ? -1 : 1;
   2.124 +    }
   2.125 +}
   2.126 +
   2.127 +int cx_cmp_uint16(void const *i1, void const *i2) {
   2.128 +    uint16_t a = *((const uint16_t*) i1);
   2.129 +    uint16_t b = *((const uint16_t*) i2);
   2.130 +    if (a == b) {
   2.131 +        return 0;
   2.132 +    } else {
   2.133 +        return a < b ? -1 : 1;
   2.134 +    }
   2.135 +}
   2.136 +
   2.137 +int cx_cmp_uint32(void const *i1, void const *i2) {
   2.138 +    uint32_t a = *((const uint32_t*) i1);
   2.139 +    uint32_t b = *((const uint32_t*) i2);
   2.140 +    if (a == b) {
   2.141 +        return 0;
   2.142 +    } else {
   2.143 +        return a < b ? -1 : 1;
   2.144 +    }
   2.145 +}
   2.146 +
   2.147 +int cx_cmp_uint64(void const *i1, void const *i2) {
   2.148 +    uint64_t a = *((const uint64_t*) i1);
   2.149 +    uint64_t b = *((const uint64_t*) i2);
   2.150 +    if (a == b) {
   2.151 +        return 0;
   2.152 +    } else {
   2.153 +        return a < b ? -1 : 1;
   2.154 +    }
   2.155 +}
   2.156 +
   2.157 +int cx_cmp_float(void const *f1, void const *f2) {
   2.158 +    float a = *((const float*) f1);
   2.159 +    float b = *((const float*) f2);
   2.160 +    if (fabsf(a - b) < 1e-6f) {
   2.161 +        return 0;
   2.162 +    } else {
   2.163 +        return a < b ? -1 : 1;
   2.164 +    }
   2.165 +}
   2.166 +
   2.167 +int cx_cmp_double(void const *d1, void const *d2) {
   2.168 +    double a = *((const double*) d1);
   2.169 +    double b = *((const double*) d2);
   2.170 +    if (fabs(a - b) < 1e-14) {
   2.171 +        return 0;
   2.172 +    } else {
   2.173 +        return a < b ? -1 : 1;
   2.174 +    }
   2.175 +}
   2.176 +
   2.177 +int cx_cmp_ptr(void const *ptr1, void const *ptr2) {
   2.178 +    const intptr_t p1 = (const intptr_t) ptr1;
   2.179 +    const intptr_t p2 = (const intptr_t) ptr2;
   2.180 +    if (p1 == p2) {
   2.181 +        return 0;
   2.182 +    } else {
   2.183 +        return p1  < p2 ? -1 : 1;
   2.184 +    }
   2.185 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/cx/compare.h	Sat Nov 05 17:50:04 2022 +0100
     3.3 @@ -0,0 +1,199 @@
     3.4 +/*
     3.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3.6 + *
     3.7 + * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
     3.8 + *
     3.9 + * Redistribution and use in source and binary forms, with or without
    3.10 + * modification, are permitted provided that the following conditions are met:
    3.11 + *
    3.12 + *   1. Redistributions of source code must retain the above copyright
    3.13 + *      notice, this list of conditions and the following disclaimer.
    3.14 + *
    3.15 + *   2. Redistributions in binary form must reproduce the above copyright
    3.16 + *      notice, this list of conditions and the following disclaimer in the
    3.17 + *      documentation and/or other materials provided with the distribution.
    3.18 + *
    3.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    3.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    3.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    3.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    3.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    3.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    3.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    3.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    3.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    3.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    3.29 + * POSSIBILITY OF SUCH DAMAGE.
    3.30 + */
    3.31 +/**
    3.32 + * \file compare.h
    3.33 + * \brief A collection of simple compare functions.
    3.34 + * \author Mike Becker
    3.35 + * \author Olaf Wintermann
    3.36 + * \version 3.0
    3.37 + * \copyright 2-Clause BSD License
    3.38 + */
    3.39 +
    3.40 +#ifndef UCX_COMPARE_H
    3.41 +#define UCX_COMPARE_H
    3.42 +
    3.43 +#ifdef __cplusplus
    3.44 +extern "C" {
    3.45 +#endif
    3.46 +
    3.47 +/**
    3.48 + * Compares two integers of type int.
    3.49 + *
    3.50 + * @param i1 pointer to integer one
    3.51 + * @param i2 pointer to integer two
    3.52 + * @return -1, if *i1 is less than *i2, 0 if both are equal,
    3.53 + * 1 if *i1 is greater than *i2
    3.54 + */
    3.55 +int cx_cmp_int(void const *i1, void const *i2);
    3.56 +
    3.57 +/**
    3.58 + * Compares two integers of type long int.
    3.59 + *
    3.60 + * @param i1 pointer to long integer one
    3.61 + * @param i2 pointer to long integer two
    3.62 + * @return -1, if *i1 is less than *i2, 0 if both are equal,
    3.63 + * 1 if *i1 is greater than *i2
    3.64 + */
    3.65 +int cx_cmp_longint(void const *i1, void const *i2);
    3.66 +
    3.67 +/**
    3.68 + * Compares two integers of type long long.
    3.69 + *
    3.70 + * @param i1 pointer to long long one
    3.71 + * @param i2 pointer to long long two
    3.72 + * @return -1, if *i1 is less than *i2, 0 if both are equal,
    3.73 + * 1 if *i1 is greater than *i2
    3.74 + */
    3.75 +int cx_cmp_longlong(void const *i1, void const *i2);
    3.76 +
    3.77 +/**
    3.78 + * Compares two integers of type int16_t.
    3.79 + *
    3.80 + * @param i1 pointer to int16_t one
    3.81 + * @param i2 pointer to int16_t two
    3.82 + * @return -1, if *i1 is less than *i2, 0 if both are equal,
    3.83 + * 1 if *i1 is greater than *i2
    3.84 + */
    3.85 +int cx_cmp_int16(void const *i1, void const *i2);
    3.86 +
    3.87 +/**
    3.88 + * Compares two integers of type int32_t.
    3.89 + *
    3.90 + * @param i1 pointer to int32_t one
    3.91 + * @param i2 pointer to int32_t two
    3.92 + * @return -1, if *i1 is less than *i2, 0 if both are equal,
    3.93 + * 1 if *i1 is greater than *i2
    3.94 + */
    3.95 +int cx_cmp_int32(void const *i1, void const *i2);
    3.96 +
    3.97 +/**
    3.98 + * Compares two integers of type int64_t.
    3.99 + *
   3.100 + * @param i1 pointer to int64_t one
   3.101 + * @param i2 pointer to int64_t two
   3.102 + * @return -1, if *i1 is less than *i2, 0 if both are equal,
   3.103 + * 1 if *i1 is greater than *i2
   3.104 + */
   3.105 +int cx_cmp_int64(void const *i1, void const *i2);
   3.106 +
   3.107 +/**
   3.108 + * Compares two integers of type unsigned int.
   3.109 + *
   3.110 + * @param i1 pointer to unsigned integer one
   3.111 + * @param i2 pointer to unsigned integer two
   3.112 + * @return -1, if *i1 is less than *i2, 0 if both are equal,
   3.113 + * 1 if *i1 is greater than *i2
   3.114 + */
   3.115 +int cx_cmp_uint(void const *i1, void const *i2);
   3.116 +
   3.117 +/**
   3.118 + * Compares two integers of type unsigned long int.
   3.119 + *
   3.120 + * @param i1 pointer to unsigned long integer one
   3.121 + * @param i2 pointer to unsigned long integer two
   3.122 + * @return -1, if *i1 is less than *i2, 0 if both are equal,
   3.123 + * 1 if *i1 is greater than *i2
   3.124 + */
   3.125 +int cx_cmp_ulongint(void const *i1, void const *i2);
   3.126 +
   3.127 +/**
   3.128 + * Compares two integers of type unsigned long long.
   3.129 + *
   3.130 + * @param i1 pointer to unsigned long long one
   3.131 + * @param i2 pointer to unsigned long long two
   3.132 + * @return -1, if *i1 is less than *i2, 0 if both are equal,
   3.133 + * 1 if *i1 is greater than *i2
   3.134 + */
   3.135 +int cx_cmp_ulonglong(void const *i1, void const *i2);
   3.136 +
   3.137 +/**
   3.138 + * Compares two integers of type uint16_t.
   3.139 + *
   3.140 + * @param i1 pointer to uint16_t one
   3.141 + * @param i2 pointer to uint16_t two
   3.142 + * @return -1, if *i1 is less than *i2, 0 if both are equal,
   3.143 + * 1 if *i1 is greater than *i2
   3.144 + */
   3.145 +int cx_cmp_uint16(void const *i1, void const *i2);
   3.146 +
   3.147 +/**
   3.148 + * Compares two integers of type uint32_t.
   3.149 + *
   3.150 + * @param i1 pointer to uint32_t one
   3.151 + * @param i2 pointer to uint32_t two
   3.152 + * @return -1, if *i1 is less than *i2, 0 if both are equal,
   3.153 + * 1 if *i1 is greater than *i2
   3.154 + */
   3.155 +int cx_cmp_uint32(void const *i1, void const *i2);
   3.156 +
   3.157 +/**
   3.158 + * Compares two integers of type uint64_t.
   3.159 + *
   3.160 + * @param i1 pointer to uint64_t one
   3.161 + * @param i2 pointer to uint64_t two
   3.162 + * @return -1, if *i1 is less than *i2, 0 if both are equal,
   3.163 + * 1 if *i1 is greater than *i2
   3.164 + */
   3.165 +int cx_cmp_uint64(void const *i1, void const *i2);
   3.166 +
   3.167 +/**
   3.168 + * Compares two real numbers of type float with precision 1e-6f.
   3.169 + *
   3.170 + * @param f1 pointer to float one
   3.171 + * @param f2 pointer to float two
   3.172 + * @return -1, if *f1 is less than *f2, 0 if both are equal,
   3.173 + * 1 if *f1 is greater than *f2
   3.174 + */
   3.175 +
   3.176 +int cx_cmp_float(void const *f1, void const *f2);
   3.177 +
   3.178 +/**
   3.179 + * Compares two real numbers of type double with precision 1e-14.
   3.180 + *
   3.181 + * @param d1 pointer to double one
   3.182 + * @param d2 pointer to double two
   3.183 + * @return -1, if *d1 is less than *d2, 0 if both are equal,
   3.184 + * 1 if *d1 is greater than *d2
   3.185 + */
   3.186 +int cx_cmp_double(void const *d1, void const *d2);
   3.187 +
   3.188 +/**
   3.189 + * Compares two pointers.
   3.190 + *
   3.191 + * @param ptr1 pointer one
   3.192 + * @param ptr2 pointer two
   3.193 + * @return -1 if ptr1 is less than ptr2, 0 if both are equal,
   3.194 + * 1 if ptr1 is greater than ptr2
   3.195 + */
   3.196 +int ucx_cmp_ptr(void const *ptr1, void const *ptr2);
   3.197 +
   3.198 +#ifdef __cplusplus
   3.199 +} // extern "C"
   3.200 +#endif
   3.201 +
   3.202 +#endif //UCX_COMPARE_H

mercurial