[Lemon-commits] Balazs Dezso: Changes in BpGraph lgf reader and ...

Lemon HG hg at lemon.cs.elte.hu
Fri Mar 1 17:49:48 CET 2013


details:   http://lemon.cs.elte.hu/hg/lemon/rev/4936be66d2f5
changeset: 1198:4936be66d2f5
user:      Balazs Dezso <deba [at] google.com>
date:      Tue Feb 12 07:15:52 2013 +0100
description:
	Changes in BpGraph lgf reader and writer (#69)
	 - Add typesade RedNode and BlueNode reading and writing
	 - RedNodes and BlueNodes don't need to have distinct label set
	 - Add tests

diffstat:

 lemon/lgf_reader.h             |  252 ++++++++++------
 lemon/lgf_writer.h             |  235 ++++++++++------
 test/CMakeLists.txt            |    1 +
 test/lgf_reader_writer_test.cc |  578 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 880 insertions(+), 186 deletions(-)

diffs (truncated from 1518 to 300 lines):

diff --git a/lemon/lgf_reader.h b/lemon/lgf_reader.h
--- a/lemon/lgf_reader.h
+++ b/lemon/lgf_reader.h
@@ -154,16 +154,16 @@
       }
     };
 
-    template <typename Value>
+    template <typename Value,
+              typename Map = std::map<std::string, Value> >
     struct MapLookUpConverter {
-      const std::map<std::string, Value>& _map;
-
-      MapLookUpConverter(const std::map<std::string, Value>& map)
+      const Map& _map;
+
+      MapLookUpConverter(const Map& map)
         : _map(map) {}
 
       Value operator()(const std::string& str) {
-        typename std::map<std::string, Value>::const_iterator it =
-          _map.find(str);
+        typename Map::const_iterator it = _map.find(str);
         if (it == _map.end()) {
           std::ostringstream msg;
           msg << "Item not found: " << str;
@@ -173,6 +173,39 @@
       }
     };
 
+    template <typename Value,
+              typename Map1 = std::map<std::string, Value>,
+              typename Map2 = std::map<std::string, Value> >
+    struct DoubleMapLookUpConverter {
+      const Map1& _map1;
+      const Map2& _map2;
+
+      DoubleMapLookUpConverter(const Map1& map1, const Map2& map2)
+        : _map1(map1), _map2(map2) {}
+
+      Value operator()(const std::string& str) {
+        typename Map1::const_iterator it1 = _map1.find(str);
+        typename Map2::const_iterator it2 = _map2.find(str);
+        if (it1 == _map1.end()) {
+          if (it2 == _map2.end()) {
+            std::ostringstream msg;
+            msg << "Item not found: " << str;
+            throw FormatError(msg.str());
+          } else {
+            return it2->second;
+          }
+        } else {
+          if (it2 == _map2.end()) {
+            return it1->second;
+          } else {
+            std::ostringstream msg;
+            msg << "Item is ambigous: " << str;
+            throw FormatError(msg.str());
+          }
+        }
+      }
+    };
+
     template <typename GR>
     struct GraphArcLookUpConverter {
       const GR& _graph;
@@ -2171,15 +2204,19 @@
     std::string _edges_caption;
     std::string _attributes_caption;
 
-    typedef std::map<std::string, Node> NodeIndex;
-    NodeIndex _node_index;
+    typedef std::map<std::string, RedNode> RedNodeIndex;
+    RedNodeIndex _red_node_index;
+    typedef std::map<std::string, BlueNode> BlueNodeIndex;
+    BlueNodeIndex _blue_node_index;
     typedef std::map<std::string, Edge> EdgeIndex;
     EdgeIndex _edge_index;
 
     typedef std::vector<std::pair<std::string,
-      _reader_bits::MapStorageBase<Node>*> > NodeMaps;
-    NodeMaps _red_maps;
-    NodeMaps _blue_maps;
+      _reader_bits::MapStorageBase<RedNode>*> > RedNodeMaps;
+    RedNodeMaps _red_node_maps;
+    typedef std::vector<std::pair<std::string,
+      _reader_bits::MapStorageBase<BlueNode>*> > BlueNodeMaps;
+    BlueNodeMaps _blue_node_maps;
 
     typedef std::vector<std::pair<std::string,
       _reader_bits::MapStorageBase<Edge>*> > EdgeMaps;
@@ -2241,13 +2278,13 @@
 
     /// \brief Destructor
     ~BpGraphReader() {
-      for (typename NodeMaps::iterator it = _red_maps.begin();
-           it != _red_maps.end(); ++it) {
+      for (typename RedNodeMaps::iterator it = _red_node_maps.begin();
+           it != _red_node_maps.end(); ++it) {
         delete it->second;
       }
 
-      for (typename NodeMaps::iterator it = _blue_maps.begin();
-           it != _blue_maps.end(); ++it) {
+      for (typename BlueNodeMaps::iterator it = _blue_node_maps.begin();
+           it != _blue_node_maps.end(); ++it) {
         delete it->second;
       }
 
@@ -2284,11 +2321,12 @@
       other._is = 0;
       other.local_is = false;
 
-      _node_index.swap(other._node_index);
+      _red_node_index.swap(other._red_node_index);
+      _blue_node_index.swap(other._blue_node_index);
       _edge_index.swap(other._edge_index);
 
-      _red_maps.swap(other._red_maps);
-      _blue_maps.swap(other._blue_maps);
+      _red_node_maps.swap(other._red_node_maps);
+      _blue_node_maps.swap(other._blue_node_maps);
       _edge_maps.swap(other._edge_maps);
       _attributes.swap(other._attributes);
 
@@ -2311,12 +2349,12 @@
     template <typename Map>
     BpGraphReader& nodeMap(const std::string& caption, Map& map) {
       checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
-      _reader_bits::MapStorageBase<Node>* red_storage =
-        new _reader_bits::MapStorage<Node, Map>(map);
-      _red_maps.push_back(std::make_pair(caption, red_storage));
-      _reader_bits::MapStorageBase<Node>* blue_storage =
-        new _reader_bits::MapStorage<Node, Map>(map);
-      _blue_maps.push_back(std::make_pair(caption, blue_storage));
+      _reader_bits::MapStorageBase<RedNode>* red_storage =
+        new _reader_bits::MapStorage<RedNode, Map>(map);
+      _red_node_maps.push_back(std::make_pair(caption, red_storage));
+      _reader_bits::MapStorageBase<BlueNode>* blue_storage =
+        new _reader_bits::MapStorage<BlueNode, Map>(map);
+      _blue_node_maps.push_back(std::make_pair(caption, blue_storage));
       return *this;
     }
 
@@ -2328,22 +2366,22 @@
     BpGraphReader& nodeMap(const std::string& caption, Map& map,
                            const Converter& converter = Converter()) {
       checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
-      _reader_bits::MapStorageBase<Node>* red_storage =
-        new _reader_bits::MapStorage<Node, Map, Converter>(map, converter);
-      _red_maps.push_back(std::make_pair(caption, red_storage));
-      _reader_bits::MapStorageBase<Node>* blue_storage =
-        new _reader_bits::MapStorage<Node, Map, Converter>(map, converter);
-      _blue_maps.push_back(std::make_pair(caption, blue_storage));
+      _reader_bits::MapStorageBase<RedNode>* red_storage =
+        new _reader_bits::MapStorage<RedNode, Map, Converter>(map, converter);
+      _red_node_maps.push_back(std::make_pair(caption, red_storage));
+      _reader_bits::MapStorageBase<BlueNode>* blue_storage =
+        new _reader_bits::MapStorage<BlueNode, Map, Converter>(map, converter);
+      _blue_node_maps.push_back(std::make_pair(caption, blue_storage));
       return *this;
     }
 
     /// Add a red node map reading rule to the reader.
     template <typename Map>
     BpGraphReader& redNodeMap(const std::string& caption, Map& map) {
-      checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
-      _reader_bits::MapStorageBase<Node>* storage =
-        new _reader_bits::MapStorage<Node, Map>(map);
-      _red_maps.push_back(std::make_pair(caption, storage));
+      checkConcept<concepts::WriteMap<RedNode, typename Map::Value>, Map>();
+      _reader_bits::MapStorageBase<RedNode>* storage =
+        new _reader_bits::MapStorage<RedNode, Map>(map);
+      _red_node_maps.push_back(std::make_pair(caption, storage));
       return *this;
     }
 
@@ -2354,20 +2392,20 @@
     template <typename Map, typename Converter>
     BpGraphReader& redNodeMap(const std::string& caption, Map& map,
                               const Converter& converter = Converter()) {
-      checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
-      _reader_bits::MapStorageBase<Node>* storage =
-        new _reader_bits::MapStorage<Node, Map, Converter>(map, converter);
-      _red_maps.push_back(std::make_pair(caption, storage));
+      checkConcept<concepts::WriteMap<RedNode, typename Map::Value>, Map>();
+      _reader_bits::MapStorageBase<RedNode>* storage =
+        new _reader_bits::MapStorage<RedNode, Map, Converter>(map, converter);
+      _red_node_maps.push_back(std::make_pair(caption, storage));
       return *this;
     }
 
     /// Add a blue node map reading rule to the reader.
     template <typename Map>
     BpGraphReader& blueNodeMap(const std::string& caption, Map& map) {
-      checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
-      _reader_bits::MapStorageBase<Node>* storage =
-        new _reader_bits::MapStorage<Node, Map>(map);
-      _blue_maps.push_back(std::make_pair(caption, storage));
+      checkConcept<concepts::WriteMap<BlueNode, typename Map::Value>, Map>();
+      _reader_bits::MapStorageBase<BlueNode>* storage =
+        new _reader_bits::MapStorage<BlueNode, Map>(map);
+      _blue_node_maps.push_back(std::make_pair(caption, storage));
       return *this;
     }
 
@@ -2378,10 +2416,10 @@
     template <typename Map, typename Converter>
     BpGraphReader& blueNodeMap(const std::string& caption, Map& map,
                                const Converter& converter = Converter()) {
-      checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
-      _reader_bits::MapStorageBase<Node>* storage =
-        new _reader_bits::MapStorage<Node, Map, Converter>(map, converter);
-      _blue_maps.push_back(std::make_pair(caption, storage));
+      checkConcept<concepts::WriteMap<BlueNode, typename Map::Value>, Map>();
+      _reader_bits::MapStorageBase<BlueNode>* storage =
+        new _reader_bits::MapStorage<BlueNode, Map, Converter>(map, converter);
+      _blue_node_maps.push_back(std::make_pair(caption, storage));
       return *this;
     }
 
@@ -2473,14 +2511,39 @@
     ///
     /// Add a node reading rule to reader.
     BpGraphReader& node(const std::string& caption, Node& node) {
-      typedef _reader_bits::MapLookUpConverter<Node> Converter;
-      Converter converter(_node_index);
+      typedef _reader_bits::DoubleMapLookUpConverter<
+        Node, RedNodeIndex, BlueNodeIndex> Converter;
+      Converter converter(_red_node_index, _blue_node_index);
       _reader_bits::ValueStorageBase* storage =
         new _reader_bits::ValueStorage<Node, Converter>(node, converter);
       _attributes.insert(std::make_pair(caption, storage));
       return *this;
     }
 
+    /// \brief Red node reading rule
+    ///
+    /// Add a red node reading rule to reader.
+    BpGraphReader& redNode(const std::string& caption, RedNode& node) {
+      typedef _reader_bits::MapLookUpConverter<RedNode> Converter;
+      Converter converter(_red_node_index);
+      _reader_bits::ValueStorageBase* storage =
+        new _reader_bits::ValueStorage<RedNode, Converter>(node, converter);
+      _attributes.insert(std::make_pair(caption, storage));
+      return *this;
+    }
+
+    /// \brief Blue node reading rule
+    ///
+    /// Add a blue node reading rule to reader.
+    BpGraphReader& blueNode(const std::string& caption, BlueNode& node) {
+      typedef _reader_bits::MapLookUpConverter<BlueNode> Converter;
+      Converter converter(_blue_node_index);
+      _reader_bits::ValueStorageBase* storage =
+        new _reader_bits::ValueStorage<BlueNode, Converter>(node, converter);
+      _attributes.insert(std::make_pair(caption, storage));
+      return *this;
+    }
+
     /// \brief Edge reading rule
     ///
     /// Add an edge reading rule to reader.
@@ -2549,8 +2612,11 @@
       LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
       _use_nodes = true;
       _writer_bits::DefaultConverter<typename Map::Value> converter;
-      for (NodeIt n(_graph); n != INVALID; ++n) {
-        _node_index.insert(std::make_pair(converter(map[n]), n));
+      for (RedNodeIt n(_graph); n != INVALID; ++n) {
+        _red_node_index.insert(std::make_pair(converter(map[n]), n));
+      }
+      for (BlueNodeIt n(_graph); n != INVALID; ++n) {
+        _blue_node_index.insert(std::make_pair(converter(map[n]), n));
       }
       return *this;
     }
@@ -2566,8 +2632,11 @@
       checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
       LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
       _use_nodes = true;
-      for (NodeIt n(_graph); n != INVALID; ++n) {
-        _node_index.insert(std::make_pair(converter(map[n]), n));
+      for (RedNodeIt n(_graph); n != INVALID; ++n) {
+        _red_node_index.insert(std::make_pair(converter(map[n]), n));
+      }
+      for (BlueNodeIt n(_graph); n != INVALID; ++n) {      
+        _blue_node_index.insert(std::make_pair(converter(map[n]), n));
       }
       return *this;
     }
@@ -2664,13 +2733,13 @@
 
     void readRedNodes() {
 
-      std::vector<int> map_index(_red_maps.size());
+      std::vector<int> map_index(_red_node_maps.size());
       int map_num, label_index;
 
       char c;



More information about the Lemon-commits mailing list