utils.h

Go to the documentation of this file.
00001 //
00002 //  Copyright (C) 2002-2008 Greg Landrum and 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_UTILS_H__
00012 #define __RD_UTILS_H__
00013 
00014 #include "types.h"
00015 #include <RDGeneral/Invariant.h>
00016 
00017 #include <boost/random.hpp>
00018 
00019 namespace RDKit{
00020   const int NUM_PRIMES_AVAIL = 1000; //!< the number of primes available and stored
00021   extern int firstThousandPrimes[NUM_PRIMES_AVAIL];
00022   
00023   const int FILE_MAXLINE=256;  //!< an assumed maximum length for lines read from files
00024   
00025   //! \brief compute the product of the set of primes corresponding to the
00026   //!        values in an INT_VECT 
00027   double computeIntVectPrimesProduct(const INT_VECT &ring);
00028 
00029   //! floating point comparison with a tolerance
00030   bool feq(double v1,double v2,double tol=1e-4);
00031   
00032   typedef boost::minstd_rand rng_type;
00033   typedef boost::uniform_int<> uniform_int;
00034   typedef boost::uniform_real<> uniform_double;
00035   typedef boost::variate_generator<rng_type &,uniform_int> int_source_type;
00036   typedef boost::variate_generator<rng_type &,uniform_double> double_source_type;
00037 
00038   //! Optionally seed and return a reference to the global (Boost) random generator
00039   rng_type &getRandomGenerator(int seed=-1);
00040 
00041   //! Return a random double value between 0.0 and 1.0
00042   //! Optionally seed the random number generator 
00043   double getRandomVal(int seed = -1);
00044 
00045 
00046   template <class T>
00047   unsigned int countSwapsToInterconvert(const T &ref,T probe){
00048     PRECONDITION(ref.size()==probe.size(),"size mismatch");
00049     typename T::const_iterator refIt=ref.begin();
00050     typename T::iterator probeIt=probe.begin();
00051     typename T::iterator probeIt2;
00052 
00053     unsigned int nSwaps = 0;
00054     while(refIt!=ref.end()){
00055       if((*probeIt)!=(*refIt)){
00056         bool foundIt=false;
00057         probeIt2=probeIt;
00058         while((*probeIt2)!=(*refIt) && probeIt2!=probe.end()){
00059           ++probeIt2;
00060         }
00061         if(probeIt2 != probe.end()){
00062           foundIt=true;
00063         }
00064         CHECK_INVARIANT(foundIt,"could not find probe element");
00065 
00066         std::swap(*probeIt,*probeIt2);
00067         nSwaps++;
00068       }
00069       ++probeIt;
00070       ++refIt;
00071     }
00072     return nSwaps;
00073   }
00074   
00075 
00076   
00077 }
00078 
00079 
00080 
00081 #endif