lemon/lemon_writer.h
changeset 1720 578d8b2b76c6
parent 1690 7db44a7ab939
child 1845 f8bbfed86036
equal deleted inserted replaced
4:b891f1f314b9 5:808c7adc60aa
    33 #include <lemon/invalid.h>
    33 #include <lemon/invalid.h>
    34 #include <lemon/graph_utils.h>
    34 #include <lemon/graph_utils.h>
    35 #include <lemon/bits/item_writer.h>
    35 #include <lemon/bits/item_writer.h>
    36 #include <lemon/utility.h>
    36 #include <lemon/utility.h>
    37 #include <lemon/maps.h>
    37 #include <lemon/maps.h>
       
    38 #include <lemon/xy.h>
    38 
    39 
    39 #include <lemon/concept_check.h>
    40 #include <lemon/concept_check.h>
    40 #include <lemon/concept/maps.h>
    41 #include <lemon/concept/maps.h>
    41 
    42 
    42 
    43 
    82 	const Item& item;
    83 	const Item& item;
    83       };
    84       };
    84 
    85 
    85     };
    86     };
    86 
    87 
       
    88     template <typename Map>
       
    89     struct Ref { typedef const Map& Type; };
       
    90 
       
    91     template <typename Graph, typename Map>
       
    92     class ForwardComposeMap {
       
    93     public:
       
    94       typedef typename Graph::UndirEdge Key;
       
    95       typedef typename Map::Value Value;
       
    96 
       
    97       ForwardComposeMap(const Graph& _graph, const Map& _map) 
       
    98 	: graph(_graph), map(_map) {}
       
    99       
       
   100       Value operator[](const Key& key) {
       
   101 	return map[graph.direct(key, false)];
       
   102       }
       
   103 
       
   104     private:
       
   105       typename Ref<Map>::Type map;
       
   106       const Graph& graph;
       
   107     };
       
   108 
       
   109     template <typename Graph, typename Map>
       
   110     ForwardComposeMap<Graph, Map>
       
   111     forwardComposeMap(const Graph& graph, const Map& map) {
       
   112       return ForwardComposeMap<Graph, Map>(graph, map);
       
   113     }
       
   114 
       
   115     template <typename Graph, typename Map>
       
   116     class BackwardComposeMap {
       
   117     public:
       
   118       typedef typename Graph::UndirEdge Key;
       
   119       typedef typename Map::Value Value;
       
   120 
       
   121       BackwardComposeMap(const Graph& _graph, const Map& _map) 
       
   122 	: graph(_graph), map(_map) {}
       
   123       
       
   124       Value operator[](const Key& key) {
       
   125 	return map[graph.direct(key, false)];
       
   126       }
       
   127 
       
   128     private:
       
   129       typename Ref<Map>::Type map;
       
   130       const Graph& graph;
       
   131     };
       
   132 
       
   133     template <typename Graph, typename Map>
       
   134     BackwardComposeMap<Graph, Map>
       
   135     backwardComposeMap(const Graph& graph, const Map& map) {
       
   136       return BackwardComposeMap<Graph, Map>(graph, map);
       
   137     }
       
   138 
       
   139     template <typename Graph, typename Map>
       
   140     struct Ref<ForwardComposeMap<Graph, Map> > { 
       
   141       typedef ForwardComposeMap<Graph, Map> Type;
       
   142     };
       
   143 
       
   144     template <typename Graph, typename Map>
       
   145     struct Ref<BackwardComposeMap<Graph, Map> > { 
       
   146       typedef BackwardComposeMap<Graph, Map> Type; 
       
   147     };
       
   148 
       
   149     template <typename Map>
       
   150     struct Ref<XMap<Map> > { 
       
   151       typedef XMap<Map> Type;
       
   152     };
       
   153     template <typename Map>
       
   154     struct Ref<ConstXMap<Map> > { 
       
   155       typedef ConstXMap<Map> Type;
       
   156     };
       
   157 
       
   158     template <typename Map>
       
   159     struct Ref<YMap<Map> > { 
       
   160       typedef YMap<Map> Type;
       
   161     };
       
   162     template <typename Map>
       
   163     struct Ref<ConstYMap<Map> > { 
       
   164       typedef ConstYMap<Map> Type;
       
   165     };
       
   166 
    87   }
   167   }
    88 
   168 
    89   /// \ingroup io_group
   169   /// \ingroup io_group
    90   /// \brief Lemon Format writer class.
   170   /// \brief Lemon Format writer class.
    91   /// 
   171   /// 
   232       typedef _Map Map;
   312       typedef _Map Map;
   233       typedef _Writer Writer;
   313       typedef _Writer Writer;
   234       typedef typename Writer::Value Value;
   314       typedef typename Writer::Value Value;
   235       typedef _Item Item;
   315       typedef _Item Item;
   236       
   316       
   237       typename SmartConstReference<Map>::Type map;
   317       typename _writer_bits::Ref<Map>::Type map;
   238       Writer writer;
   318       Writer writer;
   239 
   319 
   240       MapWriter(const Map& _map, const Writer& _writer) 
   320       MapWriter(const Map& _map, const Writer& _writer) 
   241 	: map(_map), writer(_writer) {}
   321 	: map(_map), writer(_writer) {}
   242 
   322 
   451     MapWriters writers;
   531     MapWriters writers;
   452 
   532 
   453     WriterBase<Node>* idMap;
   533     WriterBase<Node>* idMap;
   454     bool forceIdMap;
   534     bool forceIdMap;
   455    
   535    
   456     typename SmartConstReference<Graph>::Type graph;   
   536     const Graph& graph;   
   457     std::string id;
   537     std::string id;
   458 
   538 
   459   };
   539   };
   460 
   540 
   461   /// \ingroup io_group
   541   /// \ingroup io_group
   625     MapWriters writers;
   705     MapWriters writers;
   626 
   706 
   627     WriterBase<Edge>* idMap;
   707     WriterBase<Edge>* idMap;
   628     bool forceIdMap;
   708     bool forceIdMap;
   629    
   709    
   630     typename SmartConstReference<Graph>::Type graph;   
   710     const Graph& graph;   
   631     std::string id;
   711     std::string id;
   632 
   712 
   633     std::auto_ptr<IdWriterBase<Node> > nodeIdWriter;
   713     std::auto_ptr<IdWriterBase<Node> > nodeIdWriter;
   634   };
   714   };
   635 
   715 
   744     template <typename Writer, typename Map>
   824     template <typename Writer, typename Map>
   745     UndirEdgeSetWriter& writeEdgeMap(std::string name, const Map& map, 
   825     UndirEdgeSetWriter& writeEdgeMap(std::string name, const Map& map, 
   746 				     const Writer& writer = Writer()) {
   826 				     const Writer& writer = Writer()) {
   747       checkConcept<concept::ReadMap<Edge, typename Map::Value>, Map>();
   827       checkConcept<concept::ReadMap<Edge, typename Map::Value>, Map>();
   748       checkConcept<_writer_bits::ItemWriter<typename Map::Value>, Writer>();
   828       checkConcept<_writer_bits::ItemWriter<typename Map::Value>, Writer>();
   749       writeUndirEdge("+" + name, composeMap(forwardMap(graph), map), writer);
   829       writeUndirEdge("+" + name, 
   750       writeUndirEdge("-" + name, composeMap(backwardMap(graph), map), writer);
   830 		     _writer_bits::forwardComposeMap(graph, map), writer);
       
   831       writeUndirEdge("-" + name, 
       
   832 		     _writer_bits::backwardComposeMap(graph, map), writer);
   751       return *this;
   833       return *this;
   752     }
   834     }
   753 
   835 
   754   protected:
   836   protected:
   755 
   837 
   851     MapWriters writers;
   933     MapWriters writers;
   852 
   934 
   853     WriterBase<UndirEdge>* idMap;
   935     WriterBase<UndirEdge>* idMap;
   854     bool forceIdMap;
   936     bool forceIdMap;
   855    
   937    
   856     typename SmartConstReference<Graph>::Type graph;   
   938     const Graph& graph;   
   857     std::string id;
   939     std::string id;
   858 
   940 
   859     std::auto_ptr<IdWriterBase<Node> > nodeIdWriter;
   941     std::auto_ptr<IdWriterBase<Node> > nodeIdWriter;
   860   };
   942   };
   861 
   943