-
Notifications
You must be signed in to change notification settings - Fork 3
/
general.hpp
82 lines (66 loc) · 1.94 KB
/
general.hpp
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
#ifndef general_hpp
#define general_hpp
/*! \file
\brief Miscellaneous functions and classes
*/
#include <functional>
#include <iostream>
#include <sstream>
#include <string>
#include <math.h>
using std::unary_function;
using std::cerr;
using std::ostream;
using std::string;
using std::endl;
//! returns ceiling of a/b
int inline divCeil(int a, int b)
{
return a / b + (a % b == 0 ? 0 : 1);
};
//! returns true if |a - b| < 1e-6
bool inline equals(double a, double b)
{
double c = a - b;
return -1e-6 < c && c < 1e-6;
};
#if (defined(_MSC_VER) || (defined(WIN32) && defined(__GNUC__))) && !defined(rint)
//! microsoft's c library doesn't have any rounding functions
#define rint(x) floor((x) + 0.5)
#endif
//! Return a * b / c. Use when there is a chance a * b might silently overflow
int inline mdiv(int a, int b, int c)
{
return (int)((double)a * (double)b / (double)c);
}
//! Functor that deletes a pointer
template<class T> struct DeletePointer : public unary_function<T, void>
{
void operator()(T x) { if (!x) delete x; }
};
//! General purpose exception class
class Exception
{
public:
const char * file;
int line;
string message;
Exception(const char * file_, int line_);
Exception(string message_, const char * file_, int line_);
//! Print a description of the exception to a character stream
virtual void write(ostream & o) const;
//! Return class name
virtual char const * className() const;
};
//! More natural way to print an exception
ostream & operator<<(ostream & o, Exception const & e);
#define THROW_STREAM(x) \
do { \
std::ostringstream str; \
str << x; \
throw Exception(str.str(), __FILE__, __LINE__); \
} while (false)
#define WARN_STREAM(x) \
std::cerr << "Warning from " << __FILE__ << ":" \
<< __LINE__ << ".\n" << x;
#endif