00001 #ifndef ANIMAL_ALGO_UTILS
00002 #define ANIMAL_ALGO_UTILS
00003
00004 #include <assert.h>
00005 #include <iostream>
00006 #include <vector>
00007
00008 namespace nr {
00009
00011 template <class T>
00012 inline T
00013 MAX( const T& a, const T& b ) { return a > b ? a : b; }
00014
00015 template <class T1,class T2>
00016 inline T2
00017 MAX( const T1& a, const T2& b ) { return a > b ? static_cast<T2>(a) : b; }
00018
00019
00021 template <class T>
00022 inline T
00023 SQR( const T& a ) { return a * a; }
00024
00026 template <class T>
00027 inline T
00028 SIGN(const T& a, const T& b) { return ((b) >= 0.0 ? fabs(a) : -fabs(a)); }
00029
00031 inline void nrerror( const char* s ){
00032 std::cerr << s << std::endl;
00033 assert(false);
00034 }
00035
00036
00037
00038
00042
00043 template
00044 <
00045 class RealT = double,
00046 class VectorT = RealT*,
00047 class MatrixT = RealT**
00048 >
00049 struct Old_Traits
00050 {
00051
00053 typedef RealT Real;
00055 typedef VectorT Vector;
00057 typedef MatrixT Matrix;
00058
00061
00066 static inline void alloc_vector( Vector& v, unsigned int size )
00067 {
00068 v = new Real[ size ];
00069 }
00070
00072 static inline void free_vector( Vector& vec )
00073 {
00074 delete vec;
00075 }
00076
00082 static inline void alloc_matrix( Matrix& mat, unsigned int rows, unsigned int columns )
00083 {
00084 mat = new Real*[rows];
00085 for( unsigned int i = 0; i<rows; ++i ){
00086 mat[i] = new Real[columns];
00087 }
00088 }
00089
00095 static inline void free_matrix( Matrix& mat, unsigned int rows, unsigned int )
00096 {
00097 for( unsigned int i = 0; i<rows; ++i ){
00098 delete mat[i];
00099 }
00100 delete mat;
00101 }
00102
00104
00105 };
00106
00107
00108
00109
00110
00114
00115 template
00116 <
00117 class RealT = double,
00118 class VectorT = std::vector< RealT >,
00119 class MatrixT = std::vector< VectorT >
00120 >
00121 struct STL_Traits
00122 {
00123
00125 typedef RealT Real;
00127 typedef VectorT Vector;
00129 typedef MatrixT Matrix;
00130
00135 static void alloc_vector( Vector& v, unsigned int size )
00136 {
00137 v = Vector(size);
00138 }
00139
00141 static inline void free_vector( Vector& )
00142 {
00143 }
00144
00150 static inline void alloc_matrix( Matrix& mat, unsigned int rows, unsigned int columns )
00151 {
00152 mat = Matrix(rows);
00153 for( unsigned int i = 0; i<rows; ++i ){
00154 mat[i] = Vector(columns);
00155 }
00156 }
00157
00159 static inline void free_matrix( Matrix&, unsigned int, unsigned int )
00160 {
00161 }
00162
00163 };
00164
00165 }
00166 #endif