src/work/deba/iterator_test.cpp
changeset 1288 6cc7b573b7b5
equal deleted inserted replaced
-1:000000000000 0:53fa03777ec3
       
     1 #include <iostream>
       
     2 #include <algorithm>
       
     3 #include <iterator>
       
     4 #include <functional>
       
     5 #include <lemon/list_graph.h>
       
     6 #include <lemon/map_iterator.h>
       
     7 #include <lemon/graph_reader.h>
       
     8 #include <lemon/maps.h>
       
     9 
       
    10 using namespace std;
       
    11 using namespace lemon;
       
    12 
       
    13 template <typename F, typename G>
       
    14 struct unary_compose {
       
    15   typedef typename G::argument_type argument_type;
       
    16   typedef typename F::result_type result_type;
       
    17   
       
    18   unary_compose(const F& _f, const G& _g) : f(_f), g(_g) {}
       
    19 
       
    20   result_type operator()(const argument_type& x) {
       
    21     return f(g(x));
       
    22   }
       
    23 
       
    24 private:
       
    25   F f;
       
    26   G g;
       
    27 };
       
    28 
       
    29 template <typename F, typename G>
       
    30 unary_compose<F, G> compose1(const F& f, const G& g) {
       
    31   return unary_compose<F, G>(f, g);
       
    32 }
       
    33 
       
    34 
       
    35 
       
    36 
       
    37 template <typename T>
       
    38 struct Second {
       
    39   typedef T argument_type;
       
    40   typedef typename T::second_type result_type;
       
    41 
       
    42   typename T::second_type operator()(const T& t) const {
       
    43     return t.second;
       
    44   }
       
    45 };
       
    46 
       
    47 template <typename T>
       
    48 struct First {
       
    49   typedef T argument_type;
       
    50   typedef typename T::first_type result_type;
       
    51   typename T::first_type operator()(const T& t) const {
       
    52     return t.first;
       
    53   }
       
    54 };
       
    55 
       
    56 
       
    57 int main() {
       
    58 
       
    59   typedef ListGraph Graph;
       
    60 
       
    61   typedef Graph::Edge Edge;
       
    62   typedef Graph::Node Node;
       
    63   typedef Graph::EdgeIt EdgeIt;
       
    64   typedef Graph::NodeIt NodeIt;
       
    65   typedef Graph::EdgeMap<int> LengthMap;
       
    66 
       
    67   typedef IdMap<Graph, Edge> EdgeIdMap;
       
    68 
       
    69   Graph graph;
       
    70   LengthMap length(graph);
       
    71 
       
    72   readGraph(std::cin, graph, length);
       
    73 
       
    74   const LengthMap& constLength = length;
       
    75 
       
    76   copy(length.valueSet().begin(), length.valueSet().end(), 
       
    77        ostream_iterator<int>(cout, " "));
       
    78   cout << endl;
       
    79 
       
    80 
       
    81   copy(constLength.valueSet().begin(), constLength.valueSet().end(), 
       
    82        ostream_iterator<int>(cout, " "));
       
    83   cout << endl;
       
    84 
       
    85 
       
    86   transform(constLength.keySet().begin(), constLength.keySet().end(), 
       
    87 	    ostream_iterator<int>(cout, " "), 
       
    88 	    MapFunctor<EdgeIdMap>(EdgeIdMap(graph)));
       
    89   cout << endl;
       
    90 
       
    91 
       
    92   transform(constLength.mapSet().begin(), constLength.mapSet().end(), 
       
    93 	    ostream_iterator<int>(cout, " "), 
       
    94 	    Second<LengthMap::MapSet::Value>());
       
    95   cout << endl;
       
    96 
       
    97   transform(constLength.mapSet().begin(), constLength.mapSet().end(), 
       
    98 	    ostream_iterator<int>(cout, " "), 
       
    99 	    compose1(MapFunctor<EdgeIdMap>(EdgeIdMap(graph)), 
       
   100 		     First<LengthMap::MapSet::Value>() ));
       
   101   cout << endl;
       
   102 
       
   103   transform(length.mapSet().begin(), length.mapSet().end(), 
       
   104 	    ostream_iterator<int>(cout, " "), 
       
   105 	    Second<LengthMap::MapSet::Value>());
       
   106   cout << endl;
       
   107 
       
   108   transform(length.mapSet().begin(), length.mapSet().end(), 
       
   109 	    ostream_iterator<int>(cout, " "), 
       
   110 	    compose1(MapFunctor<EdgeIdMap>(EdgeIdMap(graph)), 
       
   111 		     First<LengthMap::MapSet::Value>() ));
       
   112   cout << endl;
       
   113 
       
   114   return 0;
       
   115 }