Biorithm  1.1
 All Classes Functions Variables Typedefs Friends
generic.h
00001 /*
00002  * generic.h
00003  *
00004  *  Created on: Jun 16, 2011
00005  *
00006  *  Author: ahsanur<rahman.md.ahsanur@gmail.com>
00007  *  Purpose: frequently used headers + functions related to random numbers
00008  *
00009  */
00010 
00011 #ifndef GENERIC_H_
00012 #define GENERIC_H_
00013 
00014 #include <iostream>
00015 #include <fstream>
00016 #include <vector>
00017 #include <string>
00018 #include <map>
00019 #include <set>
00020 #include <ctime>
00021 #include <sys/time.h>
00022 #include <assert.h>
00023 #include <cstdlib>
00024 #include <climits>
00025 #include <sstream>
00026 using namespace std;
00027 
00028 typedef unsigned long NaturalNumber;
00029 typedef double MyNT;
00030 
00031 namespace myNamespace
00032 {
00046         void getIndexedOutFile(ofstream &file, string filenamePrefix, NaturalNumber index);
00047 
00049         void getIndexedInFile(ifstream &file, string filenamePrefix, NaturalNumber index);
00050 
00051 
00053         template <typename T>
00054         inline NaturalNumber round(T x) {       x = x+0.5; return (-(NaturalNumber)(x < 0)) + (NaturalNumber)x;}
00055 
00057         template <class X>
00058         string getString(X val)
00059         {
00060                 std::stringstream out;
00061                 out << val;
00062                 return out.str();
00063         }
00064 
00065 
00066         double getCPUTime();
00067 
00068         //
00069         // Reset the random number generator with the system clock.
00070 
00071         void seed();
00072 
00077 
00078         void seed(int p);
00079 
00080         //
00081         // Generate a random number between 0 and 1
00082         // return a uniform random number in [0,1].
00083 
00084         double unifRand();
00085 
00086         //
00087         // Generate a random number in a real interval.
00088         // param a one end point of the interval
00089         // param b the other end of the interval
00090         // return a inform rand numberin [a,b].
00091 
00092         double unifRand(double a, double b);
00093 
00094         //
00095         // Generate a random integer between 1 and a given value.
00096         // param n the largest value
00097         // return a uniform random value in [1,...,n]
00098 
00099         long unifRand(long n);
00100 
00108         template < typename setX>
00109         void getRandomItems(setX &random, setX &items, NaturalNumber size)
00110         {
00111                 assert(size <= items.size());
00112 
00113                 vector< typename setX::iterator > index;
00114                 for(typename setX::iterator it = items.begin(); it != items.end(); it ++)
00115                         index.push_back(it);
00116 
00117                 random_shuffle(index.begin(), index.end());
00118 
00119                 for(NaturalNumber i = 0; i < size; i ++)
00120                         random.insert(*index[i]);
00121 /*
00122  * ALT implementation
00123  *
00124                 //set of randomly selected indices for items
00125                 set<NaturalNumber> index;
00126                 NaturalNumber r, i;
00127 
00128                 while(index.size() < size)
00129                 {
00130                         r = unifRand(0,items.size()-1);
00131                         index.insert(r);
00132                 }
00133 
00134                 set<NaturalNumber>::iterator it;
00135                 typename setX::iterator sit = items.begin();
00136                 for(i = 0, it = index.begin(); it != index.end(); it ++)
00137                 {
00138                         //find the r-th elt in index
00139                         r = *it;
00140                         for(; i < r; i ++)
00141                                 sit++;
00142 
00143                         random.insert(*sit);
00144                 }
00145 */
00146 
00147         }
00148 
00149 
00150 //      template void getRandomItems< set<string> >(set<string> &random, set<string> &items, NaturalNumber size);
00151 
00152 //      template <typename setX>
00153 //      void getRandomItems(NaturalNumber size, setX &random, setX &items);
00154 
00157         template <typename setX>
00158         void eraseSubset(setX &originalSet, setX &subset)
00159         {
00160                 typename setX::iterator it, sit = originalSet.begin();
00161                 for(it = subset.begin(); it != subset.end(); it ++)
00162                 {
00163                         sit = originalSet.find(*it);
00164 
00165                         assert(sit != originalSet.end());
00166 
00167                         originalSet.erase(sit);
00168                 }
00169         }
00170 
00171 //      class MultiRandSeqGenerator;
00172 
00173 }       //end of namespace
00174 
00175 /*
00176 class MultiRandSeqGenerator
00177 {
00178 private:
00179 
00180 #ifndef MULTIRAND
00181 #define MULTIRAND
00182 //#include <tr1/random>
00183 //using namespace __gnu_cxx;
00184 
00185         vector< std::tr1::mt19937 > _seq;
00186         std::tr1::uniform_int<NaturalNumber> *_distr;
00187         NaturalNumber _maxRandNum, _numSeq;
00188 
00189 #endif // MULTIRAND
00190 
00191 public:
00192 
00194         MultiRandSeqGenerator(NaturalNumber numSeq, NaturalNumber maxRandNum = ULONG_MAX);
00195 
00196         MyNT rand(NaturalNumber index);
00197 
00198 };      //class
00199 */
00200 
00201 #endif /* GENERIC_H_ */
00202 
 All Classes Functions Variables Typedefs Friends