types.h

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2001-2006 Rational Discovery LLC 
00003 //
00004 //  @@ All Rights Reserved @@
00005 //  This file is part of the RDKit.
00006 //  The contents are covered by the terms of the BSD license
00007 //  which is included in the file license.txt, found at the root
00008 //  of the RDKit source tree.
00009 //
00010 
00011 #ifndef __RD_TYPES_H__
00012 #define __RD_TYPES_H__
00013 
00014 #ifdef WIN32
00015 #define _USE_MATH_DEFINES
00016 #endif
00017 
00018 #include <cmath>
00019 
00020 #include <RDGeneral/Invariant.h>
00021 #include "Dict.h"
00022 
00023 #include <vector>
00024 #include <deque>
00025 #include <map>
00026 #include <set>
00027 #include <string>
00028 #include <algorithm>
00029 #include <numeric>
00030 #include <list>
00031 #include<limits>
00032 
00033 #include <cstring>
00034 
00035 #include <boost/any.hpp>
00036 #include <boost/lexical_cast.hpp>
00037 
00038 
00039 namespace RDKit {
00040 
00041 #ifndef WIN32
00042   typedef long long int LONGINT;
00043 #else
00044   typedef __int64 LONGINT;
00045 #endif
00046 #ifdef max
00047 #undef max // FUCK I hate this nonsense
00048 #endif  
00049 #ifdef min
00050 #undef min // FUCK I hate this nonsense
00051 #endif  
00052   
00053   const double MAX_DOUBLE = std::numeric_limits<double>::max();
00054   const double EPS_DOUBLE = std::numeric_limits<double>::epsilon();
00055   const double SMALL_DOUBLE = 1.0e-8;
00056   const double MAX_INT = static_cast<double>(std::numeric_limits<int>::max());
00057   const double MAX_LONGINT = static_cast<double>(std::numeric_limits<LONGINT>::max());
00058   const double PI=3.1415926535897931;
00059 
00060   typedef unsigned int                       UINT;
00061   typedef unsigned short                     USHORT;
00062   typedef unsigned char                      UCHAR;
00063 
00064   typedef std::vector<int>                   INT_VECT;
00065   typedef INT_VECT::iterator                 INT_VECT_I;
00066   typedef INT_VECT::const_iterator           INT_VECT_CI;
00067   typedef INT_VECT::reverse_iterator        INT_VECT_RI;
00068   typedef INT_VECT::const_reverse_iterator  INT_VECT_CRI;
00069 
00070   typedef std::list<int>                     INT_LIST;
00071   typedef INT_LIST::iterator                 INT_LIST_I;
00072   typedef INT_LIST::const_iterator           INT_LIST_CI;
00073 
00074   typedef std::list<INT_VECT>                LIST_INT_VECT;
00075   typedef LIST_INT_VECT::iterator            LIST_INT_VECT_I;
00076   typedef LIST_INT_VECT::const_iterator      LIST_INT_VECT_CI;
00077 
00078   typedef std::vector<INT_VECT>              VECT_INT_VECT;
00079   typedef VECT_INT_VECT::iterator            VECT_INT_VECT_I;
00080   typedef VECT_INT_VECT::const_iterator      VECT_INT_VECT_CI;
00081 
00082   typedef std::vector<UINT>::const_iterator  UINT_VECT_CI;
00083   typedef std::vector<UINT>                  UINT_VECT;
00084 
00085   typedef std::vector<std::string>::const_iterator  STR_VECT_CI;
00086   typedef std::vector<std::string>::iterator        STR_VECT_I;
00087   typedef std::vector<std::string>                  STR_VECT;
00088 
00089 
00090   typedef std::vector<double>                 DOUBLE_VECT;
00091   typedef DOUBLE_VECT::iterator DOUBLE_VECT_I;
00092   typedef DOUBLE_VECT::const_iterator DOUBLE_VECT_CI;
00093   typedef std::vector<DOUBLE_VECT>            VECT_DOUBLE_VECT;
00094   typedef VECT_DOUBLE_VECT::iterator VECT_DOUBLE_VECT_I;
00095   typedef VECT_DOUBLE_VECT::const_iterator VECT_DOUBLE_VECT_CI;
00096         
00097   typedef std::map<std::string, UINT>                 STR_UINT_MAP;
00098   typedef std::map<std::string, UINT>::const_iterator STR_UINT_MAP_CI;
00099 
00100   typedef std::map<int, INT_VECT> INT_INT_VECT_MAP;
00101   typedef INT_INT_VECT_MAP::const_iterator INT_INT_VECT_MAP_CI;
00102 
00103   typedef std::map<int, int> INT_MAP_INT;
00104   typedef INT_MAP_INT::iterator INT_MAP_INT_I;
00105   typedef INT_MAP_INT::const_iterator INT_MAP_INT_CI;
00106 
00107   typedef std::deque<int> INT_DEQUE;
00108   typedef INT_DEQUE::iterator INT_DEQUE_I;
00109   typedef INT_DEQUE::const_iterator INT_DEQUE_CI;
00110 
00111   typedef std::map<int, INT_DEQUE> INT_INT_DEQ_MAP;
00112   typedef INT_INT_DEQ_MAP::const_iterator INT_INT_DEQ_MAP_CI;
00113 
00114   typedef std::set<int>                     INT_SET;
00115   typedef INT_SET::iterator                 INT_SET_I;
00116   typedef INT_SET::const_iterator           INT_SET_CI;
00117 
00118   //! functor to compare two doubles with a tolerance
00119   struct ltDouble {
00120   public:
00121     ltDouble() : _tol(1.0e-8) {};
00122     bool operator() (double d1, double d2) const {
00123       if (fabs(d1 - d2) < _tol) {
00124         return false;
00125       }
00126       else {
00127         return (d1 < d2);
00128       }
00129     }
00130   private:
00131     double _tol;
00132   };
00133 
00134   //! std::map from double to integer.
00135   typedef std::map<double, int, ltDouble> DOUBLE_INT_MAP;
00136 
00137   //! functor for returning the larger of two values
00138   template <typename T>
00139   struct larger_of {
00140     T operator()(T arg1,T arg2) { return arg1>arg2 ? arg1 : arg2; };
00141   };
00142 
00143 
00144   //! functor for comparing two strings
00145   struct charptr_functor {
00146     bool operator()(const char* s1, const char* s2) const
00147     {
00148       //std::cout << s1 << " " << s2 << " " << strcmp(s1, s2) << "\n";
00149 
00150       return strcmp(s1, s2) < 0;
00151     };
00152   };
00153   
00154   //! \brief calculate the union of two INT_VECTs and put the results in a
00155   //! third vector
00156   void Union(const INT_VECT &r1, const INT_VECT &r2, INT_VECT &res);
00157 
00158   //! \brief calculate the intersection of two INT_VECTs and put the results in a
00159   //! third vector
00160   void Intersect(const INT_VECT &r1, const INT_VECT &r2, INT_VECT &res);
00161 
00162   //! calculating the union of the INT_VECT's in a VECT_INT_VECT
00163   /*!
00164       \param rings   the INT_VECT's to consider
00165       \param res     used to return results
00166       \param exclude any values in this optional INT_VECT will be excluded
00167              from the union.
00168   */
00169   void Union(const VECT_INT_VECT &rings, INT_VECT &res, const INT_VECT *exclude=NULL);
00170 
00171   //! given a current combination of numbers change it to the next possible combination
00172   /*!
00173     \param comb the <b>sorted</b> vector to consider
00174     \param tot the maximum number possible in the vector
00175 
00176     \return -1 on failure, the index of the last number changed on success.
00177     Example:
00178       for all combinations 3 of numbers between 0 and tot=5 
00179       given (0,1,2) the function wil return (0,1,3) etc.
00180 
00181       
00182   */
00183   int nextCombination(INT_VECT &comb, int tot);
00184 
00185 
00186   //! rounds a value to the closest int
00187   double round(double v);  
00188 
00189 }; // end of namespace
00190 
00191 
00192 
00193 #endif