src/benchmark/bench_tools.h
changeset 716 e7f13f60fcfd
child 718 75d36edc6bc4
equal deleted inserted replaced
-1:000000000000 0:a6b674874084
       
     1 // -*- mode:C++ -*-
       
     2 #ifndef HUGO_BENCH_TEST_H
       
     3 #define HUGO_BENCH_TEST_H
       
     4 
       
     5 #include<vector>
       
     6 
       
     7 ///An experimental typedef factory
       
     8 #define GRAPH_TYPEDEF_FACTORY(Graph) \
       
     9    typedef typename Graph::   Node      Node;\
       
    10    typedef typename Graph::   NodeIt    NodeIn;\
       
    11    typedef typename Graph::   Edge      Edge;\
       
    12    typedef typename Graph::   EdgeIt    EdgeIt;\
       
    13    typedef typename Graph:: InEdgeIt  InEdgeIt;\
       
    14    typedef typename Graph::OutEdgeIt OutEdgeIt;
       
    15 
       
    16 #define GRAPH_TYPEDEF_FACTORY_NOTYPENAME(Graph) \
       
    17    typedef Graph::   Node      Node;\
       
    18    typedef Graph::   NodeIt    NodeIn;\
       
    19    typedef Graph::   Edge      Edge;\
       
    20    typedef Graph::   EdgeIt    EdgeIt;\
       
    21    typedef Graph:: InEdgeIt  InEdgeIt;\
       
    22    typedef Graph::OutEdgeIt OutEdgeIt;
       
    23  
       
    24 
       
    25 ///A primitive primtest
       
    26 bool isPrim(int n)
       
    27 {
       
    28   if(n%2) {
       
    29     for(int k=3;n/k>=k;k+=2)
       
    30       if(!(n%k)) return false;
       
    31     return true;
       
    32   }
       
    33   return false;
       
    34 }
       
    35 
       
    36 ///Finds the smallest prime not less then \c n.
       
    37 int nextPrim(int n)
       
    38 {
       
    39   for(n+=!(n%2);!isPrim(n);n+=2) ;
       
    40   return n;
       
    41 }
       
    42 
       
    43 
       
    44 /// Class to generate consecutive primes
       
    45 class Primes 
       
    46 {
       
    47   std::vector<int> primes;
       
    48   int n;
       
    49   
       
    50   bool isPrime(int m) 
       
    51   {
       
    52     for(int i=0;m<primes[i]*primes[i];i++) if(!(m%primes[i])) return false;
       
    53     return true;
       
    54   }
       
    55 public:
       
    56   Primes() : n(1) {}
       
    57   
       
    58   int operator() ()
       
    59     {
       
    60       if(primes.size()==0) {
       
    61 	primes.push_back(2);
       
    62 	return 2;
       
    63       }
       
    64       else {
       
    65 	do n+=2; while(!isPrime(n));
       
    66 	primes.push_back(n);
       
    67 	return n;
       
    68       }
       
    69     }
       
    70 };
       
    71 
       
    72 
       
    73 #endif