Specialized ConstMap for defining constant maps at compile time, by klao.
authormarci
Mon, 20 Sep 2004 17:53:33 +0000
changeset 8903a48bc350e0f
parent 889 47bb9b8f5705
child 891 74589d20dbc3
Specialized ConstMap for defining constant maps at compile time, by klao.
Time comparision of the generic and specialized maps.
src/hugo/maps.h
src/work/marci/const_map_time.cc
     1.1 --- a/src/hugo/maps.h	Mon Sep 20 16:20:11 2004 +0000
     1.2 +++ b/src/hugo/maps.h	Mon Sep 20 17:53:33 2004 +0000
     1.3 @@ -76,7 +76,21 @@
     1.4      ConstMap(const ConstMap<K,T1> &, const T &_v) : v(_v) {}
     1.5    };
     1.6  
     1.7 -
     1.8 +  //to document later
     1.9 +  template<typename T, T v>
    1.10 +  struct Const { };
    1.11 +  //to document later
    1.12 +  template<typename K, typename V, V v>
    1.13 +  class ConstMap<K, Const<V, v> > : public MapBase<K, V>
    1.14 +  {
    1.15 +  public:
    1.16 +    ConstMap() { }
    1.17 +    V operator[](const K&) const { return v; }
    1.18 +    void set(const K&, const V&) { }
    1.19 +  };
    1.20 +  //to document later
    1.21 +  typedef Const<bool, true> True;
    1.22 +  typedef Const<bool, false> False;
    1.23  
    1.24    /// \c std::map wrapper
    1.25  
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/work/marci/const_map_time.cc	Mon Sep 20 17:53:33 2004 +0000
     2.3 @@ -0,0 +1,46 @@
     2.4 +// Use a DIMACS max flow file as stdin.
     2.5 +// const_map_time < dimacs_max_flow_file
     2.6 +
     2.7 +#include <iostream>
     2.8 +
     2.9 +#include <hugo/maps.h>
    2.10 +#include <hugo/smart_graph.h>
    2.11 +#include <hugo/time_measure.h>
    2.12 +#include <hugo/dimacs.h>
    2.13 +#include <hugo/graph_wrapper.h>
    2.14 +
    2.15 +using namespace hugo;
    2.16 +
    2.17 +int main() {
    2.18 +
    2.19 +  typedef SmartGraph Graph;
    2.20 +  typedef Graph::Node Node;
    2.21 +  typedef Graph::Edge Edge;
    2.22 +  typedef Graph::EdgeIt EdgeIt;
    2.23 +
    2.24 +  Graph g;
    2.25 +
    2.26 +  Node s, t;
    2.27 +  NullMap<Edge, int> cap;
    2.28 +  readDimacs(std::cin, g, cap, s, t);
    2.29 +  //typedef ConstMap<Node, Bool<true> > CN1; CN1 cn1;
    2.30 +  typedef ConstMap<Node, True> CN1; CN1 cn1;
    2.31 +  typedef ConstMap<Node, bool> CN2; CN2 cn2(true);
    2.32 +  // typedef ConstMap<Edge, Bool<true> > CE1; CE1 ce1;
    2.33 +  typedef ConstMap<Edge, True> CE1; CE1 ce1;
    2.34 +  typedef ConstMap<Edge, bool> CE2; CE2 ce2(true);
    2.35 +  typedef SubGraphWrapper<Graph, CN1, CE1> SB1; SB1 sb1(g, cn1, ce1);
    2.36 +  typedef SubGraphWrapper<Graph, CN2, CE2> SB2; SB2 sb2(g, cn2, ce2);
    2.37 +  Timer ts;
    2.38 +  cout << "specialized (compile-time) const map time:" << endl;
    2.39 +  ts.reset();
    2.40 +  for (SB1::NodeIt n(sb1); n!=INVALID; ++n)
    2.41 +    for (SB1::EdgeIt e(sb1); e!=INVALID; ++e) { }
    2.42 +  cout << ts << endl;
    2.43 +  ts.reset();
    2.44 +  cout << "generic const map time:" << endl;
    2.45 +  for (SB2::NodeIt n(sb2); n!=INVALID; ++n) 
    2.46 +    for (SB2::EdgeIt e(sb2); e!=INVALID; ++e) { }
    2.47 +  cout << ts << endl;
    2.48 +  return 0;
    2.49 +}