benchmark/bench_tools.h
changeset 1435 8e85e6bbefdf
parent 979 b5fb023cdb7b
child 1689 f1795dafe42c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/benchmark/bench_tools.h	Mon May 23 04:48:14 2005 +0000
     1.3 @@ -0,0 +1,128 @@
     1.4 +// -*- mode:C++ -*-
     1.5 +#ifndef LEMON_BENCH_TEST_H
     1.6 +#define LEMON_BENCH_TEST_H
     1.7 +
     1.8 +#include<vector>
     1.9 +#include<iostream>
    1.10 +
    1.11 +#include<lemon/time_measure.h>
    1.12 +
    1.13 +///An experimental typedef factory
    1.14 +#define GRAPH_TYPEDEF_FACTORY(Graph) \
    1.15 +   typedef typename Graph::   Node      Node;\
    1.16 +   typedef typename Graph::   NodeIt    NodeIt;\
    1.17 +   typedef typename Graph::   Edge      Edge;\
    1.18 +   typedef typename Graph::   EdgeIt    EdgeIt;\
    1.19 +   typedef typename Graph:: InEdgeIt  InEdgeIt;\
    1.20 +   typedef typename Graph::OutEdgeIt OutEdgeIt;
    1.21 +
    1.22 +#define GRAPH_TYPEDEF_FACTORY_NOTYPENAME(Graph) \
    1.23 +   typedef Graph::   Node      Node;\
    1.24 +   typedef Graph::   NodeIt    NodeIt;\
    1.25 +   typedef Graph::   Edge      Edge;\
    1.26 +   typedef Graph::   EdgeIt    EdgeIt;\
    1.27 +   typedef Graph:: InEdgeIt  InEdgeIt;\
    1.28 +   typedef Graph::OutEdgeIt OutEdgeIt;
    1.29 + 
    1.30 +
    1.31 +///A primitive primtest
    1.32 +
    1.33 +///\bug 2 is not a prime according to this function!
    1.34 +///
    1.35 +///\bug This function should go out of header file. I'm making it
    1.36 +/// inline for now.
    1.37 +inline bool isPrim(int n)
    1.38 +{
    1.39 +  if(n%2) {
    1.40 +    for(int k=3;n/k>=k;k+=2)
    1.41 +      if(!(n%k)) return false;
    1.42 +    return true;
    1.43 +  }
    1.44 +  return false;
    1.45 +}
    1.46 +
    1.47 +///Finds the smallest prime not less then \c n.
    1.48 +
    1.49 +///\bug This function should go out of header file. I'm making it
    1.50 +/// inline for now.
    1.51 +inline int nextPrim(int n)
    1.52 +{
    1.53 +  for(n+=!(n%2);!isPrim(n);n+=2) ;
    1.54 +  return n;
    1.55 +}
    1.56 +
    1.57 +
    1.58 +/// Class to generate consecutive primes
    1.59 +class Primes 
    1.60 +{
    1.61 +  std::vector<int> primes;
    1.62 +  int n;
    1.63 +  
    1.64 +  bool isPrime(int m) 
    1.65 +  {
    1.66 +    for(int i=0;m<primes[i]*primes[i];i++) if(!(m%primes[i])) return false;
    1.67 +    return true;
    1.68 +  }
    1.69 +public:
    1.70 +  Primes() : n(1) {}
    1.71 +  
    1.72 +  int operator() ()
    1.73 +    {
    1.74 +      if(primes.size()==0) {
    1.75 +	primes.push_back(2);
    1.76 +	return 2;
    1.77 +      }
    1.78 +      else {
    1.79 +	do n+=2; while(!isPrime(n));
    1.80 +	primes.push_back(n);
    1.81 +	return n;
    1.82 +      }
    1.83 +    }
    1.84 +};
    1.85 +
    1.86 +inline void PrintTime(char *ID,lemon::Timer &T) 
    1.87 +{
    1.88 +  lemon::TimeStamp S(T);
    1.89 +  std::cout << ID << ' ' << S.getUserTime() << ' '
    1.90 +	    << S.getSystemTime() << ' ' << S.getRealTime() << std::endl;
    1.91 +}
    1.92 +
    1.93 +
    1.94 +
    1.95 +///
    1.96 +template<class Graph>
    1.97 +void addHiperCube(Graph &G,int dim,std::vector<typename Graph::Node> &nodes)
    1.98 +{
    1.99 +  GRAPH_TYPEDEF_FACTORY(Graph);
   1.100 +  
   1.101 +  std::vector<int> bits(dim+1);
   1.102 +  bits[0]=1;
   1.103 +  for(int i=1;i<=dim;i++) bits[i]=2*bits[i-1];
   1.104 +  
   1.105 +  for(int i=0;i<bits[dim];i++) {
   1.106 +    nodes.push_back(G.addNode());
   1.107 +    for(int j=0;j<dim;j++) if(i&bits[j]) G.addEdge(nodes[i-bits[j]],nodes[i]);
   1.108 +  }
   1.109 +}
   1.110 +
   1.111 +///
   1.112 +template<class Graph>
   1.113 +void addBiDirHiperCube(Graph &G,int dim,std::vector<typename Graph::Node>&nodes)
   1.114 +{
   1.115 +  GRAPH_TYPEDEF_FACTORY(Graph);
   1.116 +  
   1.117 +  std::vector<int> bits(dim+1);
   1.118 +  bits[0]=1;
   1.119 +  for(int i=1;i<=dim;i++) bits[i]=2*bits[i-1];
   1.120 +  
   1.121 +  for(int i=0;i<bits[dim];i++) {
   1.122 +    nodes.push_back(G.addNode());
   1.123 +    for(int j=0;j<dim;j++) if(i&bits[j]) {
   1.124 +      G.addEdge(nodes[i-bits[j]],nodes[i]);
   1.125 +      G.addEdge(nodes[i],nodes[i-bits[j]]);
   1.126 +    }
   1.127 +    
   1.128 +  }
   1.129 +}  
   1.130 +
   1.131 +#endif