1 /**************************************************************************/
2 /*  Copyright 2009 Tim Day                                                */
3 /*                                                                        */
4 /*  This file is part of Fracplanet                                       */
5 /*                                                                        */
6 /*  Fracplanet is free software: you can redistribute it and/or modify    */
7 /*  it under the terms of the GNU General Public License as published by  */
8 /*  the Free Software Foundation, either version 3 of the License, or     */
9 /*  (at your option) any later version.                                   */
10 /*                                                                        */
11 /*  Fracplanet is distributed in the hope that it will be useful,         */
12 /*  but WITHOUT ANY WARRANTY; without even the implied warranty of        */
13 /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         */
14 /*  GNU General Public License for more details.                          */
15 /*                                                                        */
16 /*  You should have received a copy of the GNU General Public License     */
17 /*  along with Fracplanet.  If not, see <http://www.gnu.org/licenses/>.   */
18 /**************************************************************************/
19 
20 #ifndef _common_h_
21 #define _common_h_
22 
23 #include <cassert>
24 #include <cmath>
25 #include <cstdint>
26 #include <cstdlib>
27 #include <ctime>
28 #include <fstream>
29 #include <iomanip>
30 #include <iostream>
31 #include <memory>
32 #include <numeric>
33 #include <sstream>
34 #include <string>
35 #include <vector>
36 
37 #include <boost/array.hpp>
38 #include <boost/bind.hpp>
39 #include <boost/function.hpp>
40 #include <boost/noncopyable.hpp>
41 #include <boost/optional.hpp>
42 #include <boost/random.hpp>
43 #include <boost/range.hpp>
44 #include <boost/scoped_array.hpp>
45 #include <boost/scoped_ptr.hpp>
46 
47 #define stringify(S) __STRING(S)
48 
49 typedef unsigned int uint;
50 typedef unsigned short ushort;
51 typedef unsigned char uchar;
52 
maximum(T a,T b)53 template <class T> inline const T maximum(T a,T b) {return (a>b ? a : b);}
minimum(T a,T b)54 template <class T> inline const T minimum(T a,T b) {return (a<b ? a : b);}
55 
maximum(T a,T b,T c)56 template <class T> inline const T maximum(T a,T b,T c) {return maximum(a,maximum(b,c));}
minimum(T a,T b,T c)57 template <class T> inline const T minimum(T a,T b,T c) {return minimum(a,minimum(b,c));}
58 
maximum(T a,T b,T c,T d)59 template <class T> inline const T maximum(T a,T b,T c,T d) {return maximum(maximum(a,b),maximum(c,d));}
minimum(T a,T b,T c,T d)60 template <class T> inline const T minimum(T a,T b,T c,T d) {return minimum(minimum(a,b),minimum(c,d));}
61 
sqr(T a)62 template <class T> inline const T sqr(T a) {return a*a;}
63 
clamped(T v,T lo,T hi)64 template <class T> inline const T clamped(T v,T lo,T hi) {return (v<lo ? lo : (v>hi ? hi : v));}
65 
clamp(T & v,T lo,T hi)66 template <class T> inline void clamp(T& v,T lo,T hi) {v=(v<lo ? lo : (v>hi ? hi : v));}
67 
exchange(T & a,T & b)68 template <class T> inline void exchange(T& a,T& b) {const T x(a);a=b;b=x;}
69 
70 extern void fatal_error(const char*);
71 
fatal_error(const std::string & s)72 inline void fatal_error(const std::string& s)
73 {
74   fatal_error(s.c_str());
75 }
76 
77 extern void fatal_internal_error(const char* src_file,uint src_line);
78 
79 extern void constraint_violation(const char* test,const char* src_file,uint src_line);
80 #define constraint(TEST) {if (!TEST) {constraint_violation(#TEST,__FILE__,__LINE__);}}
81 
82 #endif
83