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