compare.h 2.13 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
2
3
4
/*
  This file is part of CDO. CDO is a collection of Operators to
  manipulate and analyse Climate model Data.

Uwe Schulzweida's avatar
Uwe Schulzweida committed
5
  Copyright (C) 2003-2020 Uwe Schulzweida, <uwe.schulzweida AT mpimet.mpg.de>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
6
7
8
9
10
11
12
13
14
15
16
17
  See COPYING file for copying and redistribution conditions.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; version 2 of the License.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
*/

18
19
#ifndef COMPARE_H
#define COMPARE_H
Uwe Schulzweida's avatar
Uwe Schulzweida committed
20

21
#include <cmath>
22
#include <cstring>
23
#include <string>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
24

25
//#define DBL_IS_EQUAL(x,y) (!(x < y || y < x))
26
27
28
29
//#define DBL_IS_EQUAL(x, y) (std::isnan(x) || std::isnan(y) ? (std::isnan(x) && std::isnan(y) ? 1 : 0) : !(x < y || y < x))
static inline bool
DBL_IS_EQUAL(double x, double y)
{
30
  return (std::isnan(x) || std::isnan(y) ? (std::isnan(x) && std::isnan(y)) : !(x < y || y < x));
31
32
33
34
35
}

//#define IS_NOT_EQUAL(x, y) (x < y || y < x)
//#define IS_EQUAL(x, y) (!IS_NOT_EQUAL(x, y))
constexpr bool
36
IS_NOT_EQUAL(double x, double y) noexcept
37
38
39
{
  return (x < y || y < x);
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
40

41
constexpr bool
42
IS_EQUAL(double x, double y) noexcept
43
44
45
{
  return !(x < y || y < x);
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
46

47
static inline bool
48
cdo_cmpstr(const char *x, const char *y)
49
{
50
  return strcmp(x, y) == 0;
51
52
53
}

static inline bool
54
cdo_cmpstrLenRhs(const char *lhs, const char *rhs)
55
{
56
  return strncmp(lhs, rhs, strlen(rhs)) == 0;
57
}
58

59
static inline bool
60
cdo_cmpstrLenRhs(const char *lhs, const char *rhs, size_t &len)
61
{
62
63
64
65
  return strncmp(lhs, rhs, len = strlen(rhs)) == 0;
}

static inline bool
Uwe Schulzweida's avatar
Uwe Schulzweida committed
66
cdo_cmpstr(const std::string &lhs, const std::string &rhs)
67
68
69
70
71
72
73
74
75
76
77
78
79
80
{
  return (lhs.compare(rhs) == 0);
}

static inline bool
cdo_cmpstrLenRhs(const std::string &lhs, const std::string &rhs)
{
  return (lhs.compare(0, rhs.length(), rhs) == 0);
}

static inline bool
cdo_cmpstrLenRhs(const std::string &lhs, const std::string &rhs, size_t &len)
{
  return (lhs.compare(0, len = rhs.length(), rhs) == 0);
81
82
}

83
#endif /* COMPARE_H */