Ipopt Documentation  
IpUtils.hpp
Go to the documentation of this file.
1 // Copyright (C) 2004, 2009 International Business Machines and others.
2 // All Rights Reserved.
3 // This code is published under the Eclipse Public License.
4 //
5 // Authors: Carl Laird, Andreas Waechter IBM 2004-08-13
6 
7 #ifndef __IPUTILS_HPP__
8 #define __IPUTILS_HPP__
9 
10 // Standard Ip Include Files
11 #include "IpTypes.hpp"
12 #include "IpDebug.hpp"
13 
14 #include <algorithm>
15 #include <limits>
16 #include <stdexcept>
17 #include <sstream>
18 
19 namespace Ipopt
20 {
21 
22 template<typename T>
23 inline T Max(
24  T a,
25  T b
26 )
27 {
28  return std::max(a, b);
29 }
30 
31 template<typename T>
32 inline T Max(
33  T a,
34  T b,
35  T c
36 )
37 {
38  return std::max(std::max(a, b), c);
39 }
40 
41 template<typename T>
42 inline T Max(
43  T a,
44  T b,
45  T c,
46  T d
47 )
48 {
49  return std::max(std::max(a, b), std::max(c, d));
50 }
51 
52 template<typename T>
53 inline T Min(
54  T a,
55  T b
56 )
57 {
58  return std::min(a, b);
59 }
60 
61 template<typename T>
62 inline T Min(
63  T a,
64  T b,
65  T c
66 )
67 {
68  return std::min(std::min(a, b), c);
69 }
70 
71 template<typename T>
72 inline T Min(
73  T a,
74  T b,
75  T c,
76  T d
77 )
78 {
79  return std::min(std::min(a, b), std::min(c, d));
80 }
81 
85  Number val
86 );
87 
90 
93 
96 
99 
102 
112  void (*handle_interrupt)(void),
113  bool* interrupt_flag,
114  unsigned int abortlimit = std::numeric_limits<unsigned int>::max()
115 );
116 
123 
131  Number lhs,
132  Number rhs,
133  Number BasVal
134 );
135 
137 #ifdef __GNUC__
138 __attribute__((format(printf, 3, 4)))
139 #endif
141  char* str,
142  long size,
143  const char* format,
144  ...
145 );
146 
156 template<typename T>
157 inline void ComputeMemIncrease(
158  T& len,
159  double recommended,
160  T min,
161  const char* context
162 )
163 {
164  if( recommended >= std::numeric_limits<T>::max() )
165  {
166  // increase len to the maximum possible, if that is still an increase
167  if( len < std::numeric_limits<T>::max() )
168  {
169  len = std::numeric_limits<T>::max();
170  }
171  else
172  {
173  DBG_ASSERT(context != NULL);
174  std::stringstream what;
175  what << "Cannot allocate more than " << std::numeric_limits<T>::max()*sizeof(T) << " bytes for " << context << " due to limitation on integer type";
176  throw std::overflow_error(what.str());
177  }
178  }
179  else
180  {
181  len = Max(min, (T) recommended);
182  }
183 }
184 
185 } //namespace Ipopt
186 
187 #endif
IPOPTLIB_EXPORT Number WallclockTime()
method determining wallclock time since first call
#define IPOPTLIB_EXPORT
Definition: config.h:88
IPOPTLIB_EXPORT bool IsFiniteNumber(Number val)
Function returning true iff the argument is a valid double number (not NaN or Inf).
IPOPTLIB_EXPORT bool RegisterInterruptHandler(void(*handle_interrupt)(void), bool *interrupt_flag, unsigned int abortlimit=std::numeric_limits< unsigned int >::max())
register handler for interrupt signals
void ComputeMemIncrease(T &len, double recommended, T min, const char *context)
Method to calculate new length for a memory increase based on a recommendation and limits in integer ...
Definition: IpUtils.hpp:157
IPOPTLIB_EXPORT Number CpuTime()
method determining CPU time
IPOPTLIB_EXPORT void IpResetRandom01()
Function resetting the random number generator.
This file contains a base class for all exceptions and a set of macros to help with exceptions...
IPOPTLIB_EXPORT int Snprintf(char *str, long size, const char *format,...)
Method for printing a formatted output to a string with given size.
IPOPTLIB_EXPORT bool Compare_le(Number lhs, Number rhs, Number BasVal)
Method for comparing two numbers within machine precision.
IPOPTLIB_EXPORT Number IpRandom01()
Function returning a random number between 0 and 1.
IPOPTLIB_EXPORT bool UnregisterInterruptHandler(void)
unregister previously registered handler for interrupt signals
T Max(T a, T b)
Definition: IpUtils.hpp:23
ipnumber Number
Type of all numbers.
Definition: IpTypes.hpp:17
#define DBG_ASSERT(test)
Definition: IpDebug.hpp:27
T Min(T a, T b)
Definition: IpUtils.hpp:53
IPOPTLIB_EXPORT Number SysTime()
method determining system time