1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright (C) 2004, 2009 International Business Machines and others.
// All Rights Reserved.
// This code is published under the Eclipse Public License.
//
// Authors: Carl Laird, Andreas Waechter IBM 2004-08-13
#ifndef __IPUTILS_HPP__
#define __IPUTILS_HPP__
// Standard Ip Include Files
#include "IpTypes.hpp"
#include "IpDebug.hpp"
#include <algorithm>
#include <limits>
#include <stdexcept>
#include <sstream>
namespace Ipopt
{
template<typename T>
inline T Max(
T a,
T b
)
{
return std::max(a, b);
}
template<typename T>
inline T Max(
T a,
T b,
T c
)
{
return std::max(std::max(a, b), c);
}
template<typename T>
inline T Max(
T a,
T b,
T c,
T d
)
{
return std::max(std::max(a, b), std::max(c, d));
}
template<typename T>
inline T Min(
T a,
T b
)
{
return std::min(a, b);
}
template<typename T>
inline T Min(
T a,
T b,
T c
)
{
return std::min(std::min(a, b), c);
}
template<typename T>
inline T Min(
T a,
T b,
T c,
T d
)
{
return std::min(std::min(a, b), std::min(c, d));
}
/** Function returning true iff the argument is a valid double number
* (not NaN or Inf). */
IPOPTLIB_EXPORT bool IsFiniteNumber(
Number val
);
/** Function returning a random number between 0 and 1 */
IPOPTLIB_EXPORT Number IpRandom01();
/** Function resetting the random number generator */
IPOPTLIB_EXPORT void IpResetRandom01();
/** method determining CPU time */
IPOPTLIB_EXPORT Number CpuTime();
/** method determining system time */
IPOPTLIB_EXPORT Number SysTime();
/** method determining wallclock time since first call */
IPOPTLIB_EXPORT Number WallclockTime();
/** Method for comparing two numbers within machine precision.
*
* @return true, if lhs is less or equal the rhs, relaxing
* this inequality by something a little larger than machine
* precision relative to the absolute value of BasVal
*/
IPOPTLIB_EXPORT bool Compare_le(
Number lhs,
Number rhs,
Number BasVal
);
/** Method for printing a formatted output to a string with given size. */
#ifdef __GNUC__
__attribute__((format(printf, 3, 4)))
#endif
IPOPTLIB_EXPORT int Snprintf(
char* str,
long size,
const char* format,
...
);
/** Method to calculate new length for a memory increase based on a recommendation and limits in integer type.
*
* Checks whether recommended can be represented by T.
* If so, sets len to min of recommendation and min.
* If not, sets len to maximal value available for T, if this is larger than current value for len.
* If not, throws a std::overflow_error exception.
*
* @since 3.14.0
*/
template<typename T>
inline void ComputeMemIncrease(
T& len, ///< current length on input, new length on output
double recommended, ///< recommended size
T min, ///< minimal size that should ensured
const char* context ///< context from where this function is called - used to setup message for exception
)
{
if( recommended >= std::numeric_limits<T>::max() )
{
// increase len to the maximum possible, if that is still an increase
if( len < std::numeric_limits<T>::max() )
{
len = std::numeric_limits<T>::max();
}
else
{
DBG_ASSERT(context != NULL);
std::stringstream what;
what << "Cannot allocate more than " << std::numeric_limits<T>::max()*sizeof(T) << " bytes for " << context << " due to limitation on integer type";
throw std::overflow_error(what.str());
}
}
else
{
len = Max(min, (T) recommended);
}
}
} //namespace Ipopt
#endif