OMSimulatorLib
The OMSimulator project is a FMI-based co-simulation environment.
Loading...
Searching...
No Matches
Util.h
Go to the documentation of this file.
1/*
2 * This file is part of OpenModelica.
3 *
4 * Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
5 * c/o Linköpings universitet, Department of Computer and Information Science,
6 * SE-58183 Linköping, Sweden.
7 *
8 * All rights reserved.
9 *
10 * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
11 * THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
12 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
13 * RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
14 * ACCORDING TO RECIPIENTS CHOICE.
15 *
16 * The OpenModelica software and the Open Source Modelica
17 * Consortium (OSMC) Public License (OSMC-PL) are obtained
18 * from OSMC, either from the above address,
19 * from the URLs: http://www.ida.liu.se/projects/OpenModelica or
20 * http://www.openmodelica.org, and in the OpenModelica distribution.
21 * GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
22 *
23 * This program is distributed WITHOUT ANY WARRANTY; without
24 * even the implied warranty of MERCHANTABILITY or FITNESS
25 * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
26 * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
27 *
28 * See the full OSMC Public License conditions for more details.
29 *
30 */
31
32#ifndef _OMS_UTIL_H_
33#define _OMS_UTIL_H_
34
35#include <algorithm>
36#include <cctype>
37#include <locale>
38#include <math.h>
39#include <sstream>
40#include <string>
41
42// trim from start (in place)
43// https://stackoverflow.com/a/217605/7534030
44static inline void ltrim(std::string &s)
45{
46 s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) { return !std::isspace(ch); }));
47}
48
49// trim from end (in place)
50// https://stackoverflow.com/a/217605/7534030
51static inline void rtrim(std::string &s)
52{
53 s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { return !std::isspace(ch); }).base(), s.end());
54}
55
56// trim from both ends (in place)
57// https://stackoverflow.com/a/217605/7534030
58static inline void trim(std::string &s)
59{
60 ltrim(s);
61 rtrim(s);
62}
63
64const double DOUBLEEQUAL_ABSTOL = 1e-10;
65const double DOUBLEEQUAL_RELTOL = 1e-5;
66
67// http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
68static inline bool almostEqualRelativeAndAbs(double a, double b, double reltol=DOUBLEEQUAL_RELTOL, double abstol=DOUBLEEQUAL_ABSTOL)
69{
70 double diff = fabs(a - b);
71 if (diff <= abstol)
72 return true;
73
74 if (diff <= fmax(fabs(a), fabs(b)) * reltol)
75 return true;
76
77 return false;
78}
79
80template <class T>
81void reverseArray(T* array, unsigned int length)
82{
83 T tmp;
84 for (unsigned int start = 0, end = length-1; start < end; start++, end--)
85 {
86 tmp = array[start];
87 array[start] = array[end];
88 array[end] = tmp;
89 }
90}
91
92#endif
const double DOUBLEEQUAL_RELTOL
Definition Util.h:65
static void ltrim(std::string &s)
Definition Util.h:44
void reverseArray(T *array, unsigned int length)
Definition Util.h:81
static void trim(std::string &s)
Definition Util.h:58
const double DOUBLEEQUAL_ABSTOL
Definition Util.h:64
static bool almostEqualRelativeAndAbs(double a, double b, double reltol=DOUBLEEQUAL_RELTOL, double abstol=DOUBLEEQUAL_ABSTOL)
Definition Util.h:68
static void rtrim(std::string &s)
Definition Util.h:51