RDCode: utils.h Source File

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