src/compare.c

Mon, 11 Nov 2024 21:42:14 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 11 Nov 2024 21:42:14 +0100
changeset 986
38fa7e41194c
parent 890
54565fd74e74
permissions
-rw-r--r--

simplify cx_array_copy() - fixes #474

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "cx/compare.h"

#include <math.h>

int cx_cmp_int(const void *i1, const void *i2) {
    int a = *((const int *) i1);
    int b = *((const int *) i2);
    if (a == b) {
        return 0;
    } else {
        return a < b ? -1 : 1;
    }
}

int cx_cmp_longint(const void *i1, const void *i2) {
    long int a = *((const long int *) i1);
    long int b = *((const long int *) i2);
    if (a == b) {
        return 0;
    } else {
        return a < b ? -1 : 1;
    }
}

int cx_cmp_longlong(const void *i1, const void *i2) {
    long long a = *((const long long *) i1);
    long long b = *((const long long *) i2);
    if (a == b) {
        return 0;
    } else {
        return a < b ? -1 : 1;
    }
}

int cx_cmp_int16(const void *i1, const void *i2) {
    int16_t a = *((const int16_t *) i1);
    int16_t b = *((const int16_t *) i2);
    if (a == b) {
        return 0;
    } else {
        return a < b ? -1 : 1;
    }
}

int cx_cmp_int32(const void *i1, const void *i2) {
    int32_t a = *((const int32_t *) i1);
    int32_t b = *((const int32_t *) i2);
    if (a == b) {
        return 0;
    } else {
        return a < b ? -1 : 1;
    }
}

int cx_cmp_int64(const void *i1, const void *i2) {
    int64_t a = *((const int64_t *) i1);
    int64_t b = *((const int64_t *) i2);
    if (a == b) {
        return 0;
    } else {
        return a < b ? -1 : 1;
    }
}

int cx_cmp_uint(const void *i1, const void *i2) {
    unsigned int a = *((const unsigned int *) i1);
    unsigned int b = *((const unsigned int *) i2);
    if (a == b) {
        return 0;
    } else {
        return a < b ? -1 : 1;
    }
}

int cx_cmp_ulongint(const void *i1, const void *i2) {
    unsigned long int a = *((const unsigned long int *) i1);
    unsigned long int b = *((const unsigned long int *) i2);
    if (a == b) {
        return 0;
    } else {
        return a < b ? -1 : 1;
    }
}

int cx_cmp_ulonglong(const void *i1, const void *i2) {
    unsigned long long a = *((const unsigned long long *) i1);
    unsigned long long b = *((const unsigned long long *) i2);
    if (a == b) {
        return 0;
    } else {
        return a < b ? -1 : 1;
    }
}

int cx_cmp_uint16(const void *i1, const void *i2) {
    uint16_t a = *((const uint16_t *) i1);
    uint16_t b = *((const uint16_t *) i2);
    if (a == b) {
        return 0;
    } else {
        return a < b ? -1 : 1;
    }
}

int cx_cmp_uint32(const void *i1, const void *i2) {
    uint32_t a = *((const uint32_t *) i1);
    uint32_t b = *((const uint32_t *) i2);
    if (a == b) {
        return 0;
    } else {
        return a < b ? -1 : 1;
    }
}

int cx_cmp_uint64(const void *i1, const void *i2) {
    uint64_t a = *((const uint64_t *) i1);
    uint64_t b = *((const uint64_t *) i2);
    if (a == b) {
        return 0;
    } else {
        return a < b ? -1 : 1;
    }
}

int cx_cmp_float(const void *f1, const void *f2) {
    float a = *((const float *) f1);
    float b = *((const float *) f2);
    if (fabsf(a - b) < 1e-6f) {
        return 0;
    } else {
        return a < b ? -1 : 1;
    }
}

int cx_cmp_double(
        const void *d1,
        const void *d2
) {
    double a = *((const double *) d1);
    double b = *((const double *) d2);
    if (fabs(a - b) < 1e-14) {
        return 0;
    } else {
        return a < b ? -1 : 1;
    }
}

int cx_cmp_intptr(
        const void *ptr1,
        const void *ptr2
) {
    intptr_t p1 = *(const intptr_t *) ptr1;
    intptr_t p2 = *(const intptr_t *) ptr2;
    if (p1 == p2) {
        return 0;
    } else {
        return p1 < p2 ? -1 : 1;
    }
}

int cx_cmp_uintptr(
        const void *ptr1,
        const void *ptr2
) {
    uintptr_t p1 = *(const uintptr_t *) ptr1;
    uintptr_t p2 = *(const uintptr_t *) ptr2;
    if (p1 == p2) {
        return 0;
    } else {
        return p1 < p2 ? -1 : 1;
    }
}

int cx_cmp_ptr(
        const void *ptr1,
        const void *ptr2
) {
    uintptr_t p1 = (uintptr_t) ptr1;
    uintptr_t p2 = (uintptr_t) ptr2;
    if (p1 == p2) {
        return 0;
    } else {
        return p1 < p2 ? -1 : 1;
    }
}

mercurial