COIN-OR::LEMON - Graph Library

Ticket #69: b79821620180.patch

File b79821620180.patch, 56.7 KB (added by Balazs Dezso, 6 years ago)
  • lemon/lgf_reader.h

    # HG changeset patch
    # User Balazs Dezso <deba@google.com>
    # Date 1360649752 -3600
    # Node ID b798216201803095a4bdeb3badb0e16cb69008ba
    # Parent  07fd7ea22c78d27a3e731abf2b5e0586feaa6e79
    Changes in BpGraph lgf reader and writer
     - Add typesade RedNode and BlueNode reading and writing
     - RedNodes and BlueNodes don't need to have distinct label set
     - Add tests
    
    diff -r 07fd7ea22c78 -r b79821620180 lemon/lgf_reader.h
    a b  
    154154      }
    155155    };
    156156
    157     template <typename Value>
     157    template <typename Value,
     158              typename Map = std::map<std::string, Value> >
    158159    struct MapLookUpConverter {
    159       const std::map<std::string, Value>& _map;
    160 
    161       MapLookUpConverter(const std::map<std::string, Value>& map)
     160      const Map& _map;
     161
     162      MapLookUpConverter(const Map& map)
    162163        : _map(map) {}
    163164
    164165      Value operator()(const std::string& str) {
    165         typename std::map<std::string, Value>::const_iterator it =
    166           _map.find(str);
     166        typename Map::const_iterator it = _map.find(str);
    167167        if (it == _map.end()) {
    168168          std::ostringstream msg;
    169169          msg << "Item not found: " << str;
     
    173173      }
    174174    };
    175175
     176    template <typename Value,
     177              typename Map1 = std::map<std::string, Value>,
     178              typename Map2 = std::map<std::string, Value> >
     179    struct DoubleMapLookUpConverter {
     180      const Map1& _map1;
     181      const Map2& _map2;
     182
     183      DoubleMapLookUpConverter(const Map1& map1, const Map2& map2)
     184        : _map1(map1), _map2(map2) {}
     185
     186      Value operator()(const std::string& str) {
     187        typename Map1::const_iterator it1 = _map1.find(str);
     188        typename Map2::const_iterator it2 = _map2.find(str);
     189        if (it1 == _map1.end()) {
     190          if (it2 == _map2.end()) {
     191            std::ostringstream msg;
     192            msg << "Item not found: " << str;
     193            throw FormatError(msg.str());
     194          } else {
     195            return it2->second;
     196          }
     197        } else {
     198          if (it2 == _map2.end()) {
     199            return it1->second;
     200          } else {
     201            std::ostringstream msg;
     202            msg << "Item is ambigous: " << str;
     203            throw FormatError(msg.str());
     204          }
     205        }
     206      }
     207    };
     208
    176209    template <typename GR>
    177210    struct GraphArcLookUpConverter {
    178211      const GR& _graph;
     
    21572190    std::string _edges_caption;
    21582191    std::string _attributes_caption;
    21592192
    2160     typedef std::map<std::string, Node> NodeIndex;
    2161     NodeIndex _node_index;
     2193    typedef std::map<std::string, RedNode> RedNodeIndex;
     2194    RedNodeIndex _red_node_index;
     2195    typedef std::map<std::string, BlueNode> BlueNodeIndex;
     2196    BlueNodeIndex _blue_node_index;
    21622197    typedef std::map<std::string, Edge> EdgeIndex;
    21632198    EdgeIndex _edge_index;
    21642199
    21652200    typedef std::vector<std::pair<std::string,
    2166       _reader_bits::MapStorageBase<Node>*> > NodeMaps;
    2167     NodeMaps _red_maps;
    2168     NodeMaps _blue_maps;
     2201      _reader_bits::MapStorageBase<RedNode>*> > RedNodeMaps;
     2202    RedNodeMaps _red_node_maps;
     2203    typedef std::vector<std::pair<std::string,
     2204      _reader_bits::MapStorageBase<BlueNode>*> > BlueNodeMaps;
     2205    BlueNodeMaps _blue_node_maps;
    21692206
    21702207    typedef std::vector<std::pair<std::string,
    21712208      _reader_bits::MapStorageBase<Edge>*> > EdgeMaps;
     
    22272264
    22282265    /// \brief Destructor
    22292266    ~BpGraphReader() {
    2230       for (typename NodeMaps::iterator it = _red_maps.begin();
    2231            it != _red_maps.end(); ++it) {
     2267      for (typename RedNodeMaps::iterator it = _red_node_maps.begin();
     2268           it != _red_node_maps.end(); ++it) {
    22322269        delete it->second;
    22332270      }
    22342271
    2235       for (typename NodeMaps::iterator it = _blue_maps.begin();
    2236            it != _blue_maps.end(); ++it) {
     2272      for (typename BlueNodeMaps::iterator it = _blue_node_maps.begin();
     2273           it != _blue_node_maps.end(); ++it) {
    22372274        delete it->second;
    22382275      }
    22392276
     
    22702307      other._is = 0;
    22712308      other.local_is = false;
    22722309
    2273       _node_index.swap(other._node_index);
     2310      _red_node_index.swap(other._red_node_index);
     2311      _blue_node_index.swap(other._blue_node_index);
    22742312      _edge_index.swap(other._edge_index);
    22752313
    2276       _red_maps.swap(other._red_maps);
    2277       _blue_maps.swap(other._blue_maps);
     2314      _red_node_maps.swap(other._red_node_maps);
     2315      _blue_node_maps.swap(other._blue_node_maps);
    22782316      _edge_maps.swap(other._edge_maps);
    22792317      _attributes.swap(other._attributes);
    22802318
     
    22972335    template <typename Map>
    22982336    BpGraphReader& nodeMap(const std::string& caption, Map& map) {
    22992337      checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
    2300       _reader_bits::MapStorageBase<Node>* red_storage =
    2301         new _reader_bits::MapStorage<Node, Map>(map);
    2302       _red_maps.push_back(std::make_pair(caption, red_storage));
    2303       _reader_bits::MapStorageBase<Node>* blue_storage =
    2304         new _reader_bits::MapStorage<Node, Map>(map);
    2305       _blue_maps.push_back(std::make_pair(caption, blue_storage));
     2338      _reader_bits::MapStorageBase<RedNode>* red_storage =
     2339        new _reader_bits::MapStorage<RedNode, Map>(map);
     2340      _red_node_maps.push_back(std::make_pair(caption, red_storage));
     2341      _reader_bits::MapStorageBase<BlueNode>* blue_storage =
     2342        new _reader_bits::MapStorage<BlueNode, Map>(map);
     2343      _blue_node_maps.push_back(std::make_pair(caption, blue_storage));
    23062344      return *this;
    23072345    }
    23082346
     
    23142352    BpGraphReader& nodeMap(const std::string& caption, Map& map,
    23152353                           const Converter& converter = Converter()) {
    23162354      checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
    2317       _reader_bits::MapStorageBase<Node>* red_storage =
    2318         new _reader_bits::MapStorage<Node, Map, Converter>(map, converter);
    2319       _red_maps.push_back(std::make_pair(caption, red_storage));
    2320       _reader_bits::MapStorageBase<Node>* blue_storage =
    2321         new _reader_bits::MapStorage<Node, Map, Converter>(map, converter);
    2322       _blue_maps.push_back(std::make_pair(caption, blue_storage));
     2355      _reader_bits::MapStorageBase<RedNode>* red_storage =
     2356        new _reader_bits::MapStorage<RedNode, Map, Converter>(map, converter);
     2357      _red_node_maps.push_back(std::make_pair(caption, red_storage));
     2358      _reader_bits::MapStorageBase<BlueNode>* blue_storage =
     2359        new _reader_bits::MapStorage<BlueNode, Map, Converter>(map, converter);
     2360      _blue_node_maps.push_back(std::make_pair(caption, blue_storage));
    23232361      return *this;
    23242362    }
    23252363
    23262364    /// Add a red node map reading rule to the reader.
    23272365    template <typename Map>
    23282366    BpGraphReader& redNodeMap(const std::string& caption, Map& map) {
    2329       checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
    2330       _reader_bits::MapStorageBase<Node>* storage =
    2331         new _reader_bits::MapStorage<Node, Map>(map);
    2332       _red_maps.push_back(std::make_pair(caption, storage));
     2367      checkConcept<concepts::WriteMap<RedNode, typename Map::Value>, Map>();
     2368      _reader_bits::MapStorageBase<RedNode>* storage =
     2369        new _reader_bits::MapStorage<RedNode, Map>(map);
     2370      _red_node_maps.push_back(std::make_pair(caption, storage));
    23332371      return *this;
    23342372    }
    23352373
     
    23402378    template <typename Map, typename Converter>
    23412379    BpGraphReader& redNodeMap(const std::string& caption, Map& map,
    23422380                              const Converter& converter = Converter()) {
    2343       checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
    2344       _reader_bits::MapStorageBase<Node>* storage =
    2345         new _reader_bits::MapStorage<Node, Map, Converter>(map, converter);
    2346       _red_maps.push_back(std::make_pair(caption, storage));
     2381      checkConcept<concepts::WriteMap<RedNode, typename Map::Value>, Map>();
     2382      _reader_bits::MapStorageBase<RedNode>* storage =
     2383        new _reader_bits::MapStorage<RedNode, Map, Converter>(map, converter);
     2384      _red_node_maps.push_back(std::make_pair(caption, storage));
    23472385      return *this;
    23482386    }
    23492387
    23502388    /// Add a blue node map reading rule to the reader.
    23512389    template <typename Map>
    23522390    BpGraphReader& blueNodeMap(const std::string& caption, Map& map) {
    2353       checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
    2354       _reader_bits::MapStorageBase<Node>* storage =
    2355         new _reader_bits::MapStorage<Node, Map>(map);
    2356       _blue_maps.push_back(std::make_pair(caption, storage));
     2391      checkConcept<concepts::WriteMap<BlueNode, typename Map::Value>, Map>();
     2392      _reader_bits::MapStorageBase<BlueNode>* storage =
     2393        new _reader_bits::MapStorage<BlueNode, Map>(map);
     2394      _blue_node_maps.push_back(std::make_pair(caption, storage));
    23572395      return *this;
    23582396    }
    23592397
     
    23642402    template <typename Map, typename Converter>
    23652403    BpGraphReader& blueNodeMap(const std::string& caption, Map& map,
    23662404                               const Converter& converter = Converter()) {
    2367       checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
    2368       _reader_bits::MapStorageBase<Node>* storage =
    2369         new _reader_bits::MapStorage<Node, Map, Converter>(map, converter);
    2370       _blue_maps.push_back(std::make_pair(caption, storage));
     2405      checkConcept<concepts::WriteMap<BlueNode, typename Map::Value>, Map>();
     2406      _reader_bits::MapStorageBase<BlueNode>* storage =
     2407        new _reader_bits::MapStorage<BlueNode, Map, Converter>(map, converter);
     2408      _blue_node_maps.push_back(std::make_pair(caption, storage));
    23712409      return *this;
    23722410    }
    23732411
     
    24592497    ///
    24602498    /// Add a node reading rule to reader.
    24612499    BpGraphReader& node(const std::string& caption, Node& node) {
    2462       typedef _reader_bits::MapLookUpConverter<Node> Converter;
    2463       Converter converter(_node_index);
     2500      typedef _reader_bits::DoubleMapLookUpConverter<
     2501        Node, RedNodeIndex, BlueNodeIndex> Converter;
     2502      Converter converter(_red_node_index, _blue_node_index);
    24642503      _reader_bits::ValueStorageBase* storage =
    24652504        new _reader_bits::ValueStorage<Node, Converter>(node, converter);
    24662505      _attributes.insert(std::make_pair(caption, storage));
    24672506      return *this;
    24682507    }
    24692508
     2509    /// \brief Red node reading rule
     2510    ///
     2511    /// Add a red node reading rule to reader.
     2512    BpGraphReader& redNode(const std::string& caption, RedNode& node) {
     2513      typedef _reader_bits::MapLookUpConverter<RedNode> Converter;
     2514      Converter converter(_red_node_index);
     2515      _reader_bits::ValueStorageBase* storage =
     2516        new _reader_bits::ValueStorage<RedNode, Converter>(node, converter);
     2517      _attributes.insert(std::make_pair(caption, storage));
     2518      return *this;
     2519    }
     2520
     2521    /// \brief Blue node reading rule
     2522    ///
     2523    /// Add a blue node reading rule to reader.
     2524    BpGraphReader& blueNode(const std::string& caption, BlueNode& node) {
     2525      typedef _reader_bits::MapLookUpConverter<BlueNode> Converter;
     2526      Converter converter(_blue_node_index);
     2527      _reader_bits::ValueStorageBase* storage =
     2528        new _reader_bits::ValueStorage<BlueNode, Converter>(node, converter);
     2529      _attributes.insert(std::make_pair(caption, storage));
     2530      return *this;
     2531    }
     2532
    24702533    /// \brief Edge reading rule
    24712534    ///
    24722535    /// Add an edge reading rule to reader.
     
    25352598      LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
    25362599      _use_nodes = true;
    25372600      _writer_bits::DefaultConverter<typename Map::Value> converter;
    2538       for (NodeIt n(_graph); n != INVALID; ++n) {
    2539         _node_index.insert(std::make_pair(converter(map[n]), n));
     2601      for (RedNodeIt n(_graph); n != INVALID; ++n) {
     2602        _red_node_index.insert(std::make_pair(converter(map[n]), n));
     2603      }
     2604      for (BlueNodeIt n(_graph); n != INVALID; ++n) {
     2605        _blue_node_index.insert(std::make_pair(converter(map[n]), n));
    25402606      }
    25412607      return *this;
    25422608    }
     
    25522618      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
    25532619      LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
    25542620      _use_nodes = true;
    2555       for (NodeIt n(_graph); n != INVALID; ++n) {
    2556         _node_index.insert(std::make_pair(converter(map[n]), n));
     2621      for (RedNodeIt n(_graph); n != INVALID; ++n) {
     2622        _red_node_index.insert(std::make_pair(converter(map[n]), n));
     2623      }
     2624      for (BlueNodeIt n(_graph); n != INVALID; ++n) {     
     2625        _blue_node_index.insert(std::make_pair(converter(map[n]), n));
    25572626      }
    25582627      return *this;
    25592628    }
     
    26502719
    26512720    void readRedNodes() {
    26522721
    2653       std::vector<int> map_index(_red_maps.size());
     2722      std::vector<int> map_index(_red_node_maps.size());
    26542723      int map_num, label_index;
    26552724
    26562725      char c;
    26572726      if (!readLine() || !(line >> c) || c == '@') {
    26582727        if (readSuccess() && line) line.putback(c);
    2659         if (!_red_maps.empty())
     2728        if (!_red_node_maps.empty())
    26602729          throw FormatError("Cannot find map names");
    26612730        return;
    26622731      }
     
    26772746          ++index;
    26782747        }
    26792748
    2680         for (int i = 0; i < static_cast<int>(_red_maps.size()); ++i) {
     2749        for (int i = 0; i < static_cast<int>(_red_node_maps.size()); ++i) {
    26812750          std::map<std::string, int>::iterator jt =
    2682             maps.find(_red_maps[i].first);
     2751            maps.find(_red_node_maps[i].first);
    26832752          if (jt == maps.end()) {
    26842753            std::ostringstream msg;
    2685             msg << "Map not found: " << _red_maps[i].first;
     2754            msg << "Map not found: " << _red_node_maps[i].first;
    26862755            throw FormatError(msg.str());
    26872756          }
    26882757          map_index[i] = jt->second;
     
    27132782        if (line >> std::ws >> c)
    27142783          throw FormatError("Extra character at the end of line");
    27152784
    2716         Node n;
     2785        RedNode n;
    27172786        if (!_use_nodes) {
    27182787          n = _graph.addRedNode();
    27192788          if (label_index != -1)
    2720             _node_index.insert(std::make_pair(tokens[label_index], n));
     2789            _red_node_index.insert(std::make_pair(tokens[label_index], n));
    27212790        } else {
    27222791          if (label_index == -1)
    27232792            throw FormatError("Label map not found");
    2724           typename std::map<std::string, Node>::iterator it =
    2725             _node_index.find(tokens[label_index]);
    2726           if (it == _node_index.end()) {
     2793          typename std::map<std::string, RedNode>::iterator it =
     2794            _red_node_index.find(tokens[label_index]);
     2795          if (it == _red_node_index.end()) {
    27272796            std::ostringstream msg;
    27282797            msg << "Node with label not found: " << tokens[label_index];
    27292798            throw FormatError(msg.str());
     
    27312800          n = it->second;
    27322801        }
    27332802
    2734         for (int i = 0; i < static_cast<int>(_red_maps.size()); ++i) {
    2735           _red_maps[i].second->set(n, tokens[map_index[i]]);
     2803        for (int i = 0; i < static_cast<int>(_red_node_maps.size()); ++i) {
     2804          _red_node_maps[i].second->set(n, tokens[map_index[i]]);
    27362805        }
    27372806
    27382807      }
     
    27432812
    27442813    void readBlueNodes() {
    27452814
    2746       std::vector<int> map_index(_blue_maps.size());
     2815      std::vector<int> map_index(_blue_node_maps.size());
    27472816      int map_num, label_index;
    27482817
    27492818      char c;
    27502819      if (!readLine() || !(line >> c) || c == '@') {
    27512820        if (readSuccess() && line) line.putback(c);
    2752         if (!_blue_maps.empty())
     2821        if (!_blue_node_maps.empty())
    27532822          throw FormatError("Cannot find map names");
    27542823        return;
    27552824      }
     
    27702839          ++index;
    27712840        }
    27722841
    2773         for (int i = 0; i < static_cast<int>(_blue_maps.size()); ++i) {
     2842        for (int i = 0; i < static_cast<int>(_blue_node_maps.size()); ++i) {
    27742843          std::map<std::string, int>::iterator jt =
    2775             maps.find(_blue_maps[i].first);
     2844            maps.find(_blue_node_maps[i].first);
    27762845          if (jt == maps.end()) {
    27772846            std::ostringstream msg;
    2778             msg << "Map not found: " << _blue_maps[i].first;
     2847            msg << "Map not found: " << _blue_node_maps[i].first;
    27792848            throw FormatError(msg.str());
    27802849          }
    27812850          map_index[i] = jt->second;
     
    28062875        if (line >> std::ws >> c)
    28072876          throw FormatError("Extra character at the end of line");
    28082877
    2809         Node n;
     2878        BlueNode n;
    28102879        if (!_use_nodes) {
    28112880          n = _graph.addBlueNode();
    28122881          if (label_index != -1)
    2813             _node_index.insert(std::make_pair(tokens[label_index], n));
     2882            _blue_node_index.insert(std::make_pair(tokens[label_index], n));
    28142883        } else {
    28152884          if (label_index == -1)
    28162885            throw FormatError("Label map not found");
    2817           typename std::map<std::string, Node>::iterator it =
    2818             _node_index.find(tokens[label_index]);
    2819           if (it == _node_index.end()) {
     2886          typename std::map<std::string, BlueNode>::iterator it =
     2887            _blue_node_index.find(tokens[label_index]);
     2888          if (it == _blue_node_index.end()) {
    28202889            std::ostringstream msg;
    28212890            msg << "Node with label not found: " << tokens[label_index];
    28222891            throw FormatError(msg.str());
     
    28242893          n = it->second;
    28252894        }
    28262895
    2827         for (int i = 0; i < static_cast<int>(_blue_maps.size()); ++i) {
    2828           _blue_maps[i].second->set(n, tokens[map_index[i]]);
     2896        for (int i = 0; i < static_cast<int>(_blue_node_maps.size()); ++i) {
     2897          _blue_node_maps[i].second->set(n, tokens[map_index[i]]);
    28292898        }
    28302899
    28312900      }
     
    29102979
    29112980        Edge e;
    29122981        if (!_use_edges) {
    2913 
    2914           typename NodeIndex::iterator it;
    2915 
    2916           it = _node_index.find(source_token);
    2917           if (it == _node_index.end()) {
     2982          typename RedNodeIndex::iterator rit =
     2983            _red_node_index.find(source_token);
     2984          if (rit == _red_node_index.end()) {
    29182985            std::ostringstream msg;
    29192986            msg << "Item not found: " << source_token;
    29202987            throw FormatError(msg.str());
    29212988          }
    2922           Node source = it->second;
    2923           if (!_graph.red(source)) {
    2924             std::ostringstream msg;
    2925             msg << "Item is not red node: " << source_token;
    2926             throw FormatError(msg.str());
    2927           }
    2928 
    2929           it = _node_index.find(target_token);
    2930           if (it == _node_index.end()) {
     2989          RedNode source = rit->second;
     2990          typename BlueNodeIndex::iterator it =
     2991            _blue_node_index.find(target_token);
     2992          if (it == _blue_node_index.end()) {
    29312993            std::ostringstream msg;
    29322994            msg << "Item not found: " << target_token;
    29332995            throw FormatError(msg.str());
    29342996          }
    2935           Node target = it->second;
    2936           if (!_graph.blue(target)) {
    2937             std::ostringstream msg;
    2938             msg << "Item is not red node: " << source_token;
    2939             throw FormatError(msg.str());
    2940           }
     2997          BlueNode target = it->second;
    29412998
    29422999          // It is checked that source is red and
    29433000          // target is blue, so this should be safe:
    2944           e = _graph.addEdge(_graph.asRedNodeUnsafe(source),
    2945                              _graph.asBlueNodeUnsafe(target));
     3001          e = _graph.addEdge(source, target);
    29463002          if (label_index != -1)
    29473003            _edge_index.insert(std::make_pair(tokens[label_index], e));
    29483004        } else {
  • lemon/lgf_writer.h

    diff -r 07fd7ea22c78 -r b79821620180 lemon/lgf_writer.h
    a b  
    185185    public:
    186186      ValueStorage(const Value& value, const Converter& converter = Converter())
    187187        : _value(value), _converter(converter) {}
    188 
     188     
    189189      virtual std::string get() {
    190190        return _converter(_value);
    191191      }
    192192    };
    193193
    194     template <typename Value>
     194    template <typename Value,
     195              typename Map = std::map<Value, std::string> >
    195196    struct MapLookUpConverter {
    196       const std::map<Value, std::string>& _map;
     197      const Map& _map;
    197198
    198       MapLookUpConverter(const std::map<Value, std::string>& map)
     199      MapLookUpConverter(const Map& map)
    199200        : _map(map) {}
    200201
    201       std::string operator()(const Value& str) {
    202         typename std::map<Value, std::string>::const_iterator it =
    203           _map.find(str);
     202      std::string operator()(const Value& value) {
     203        typename Map::const_iterator it = _map.find(value);
    204204        if (it == _map.end()) {
    205205          throw FormatError("Item not found");
    206206        }
     
    208208      }
    209209    };
    210210
     211    template <typename Value,
     212              typename Map1 = std::map<Value, std::string>,
     213              typename Map2 = std::map<Value, std::string> >
     214    struct DoubleMapLookUpConverter {
     215      const Map1& _map1;
     216      const Map2& _map2;
     217
     218      DoubleMapLookUpConverter(const Map1& map1, const Map2& map2)
     219        : _map1(map1), _map2(map2) {}
     220
     221      std::string operator()(const Value& value) {
     222        typename Map1::const_iterator it1 = _map1.find(value);
     223        typename Map1::const_iterator it2 = _map2.find(value);
     224        if (it1 == _map1.end()) {
     225          if (it2 == _map2.end()) {
     226            throw FormatError("Item not found");
     227          } else {
     228            return it2->second;
     229          }
     230        } else {
     231          if (it2 == _map2.end()) {
     232            return it1->second;
     233          } else {
     234            throw FormatError("Item is ambigous");
     235          }
     236        }
     237      }
     238    };
     239
    211240    template <typename Graph>
    212241    struct GraphArcLookUpConverter {
    213242      const Graph& _graph;
     
    16511680    std::string _edges_caption;
    16521681    std::string _attributes_caption;
    16531682
    1654     typedef std::map<Node, std::string> NodeIndex;
    1655     NodeIndex _node_index;
     1683    typedef std::map<Node, std::string> RedNodeIndex;
     1684    RedNodeIndex _red_node_index;
     1685    typedef std::map<Node, std::string> BlueNodeIndex;
     1686    BlueNodeIndex _blue_node_index;
    16561687    typedef std::map<Edge, std::string> EdgeIndex;
    16571688    EdgeIndex _edge_index;
    16581689
    16591690    typedef std::vector<std::pair<std::string,
    1660       _writer_bits::MapStorageBase<Node>* > > NodeMaps;
    1661     NodeMaps _red_maps;
    1662     NodeMaps _blue_maps;
     1691      _writer_bits::MapStorageBase<RedNode>* > > RedNodeMaps;
     1692    RedNodeMaps _red_node_maps;
     1693    typedef std::vector<std::pair<std::string,
     1694      _writer_bits::MapStorageBase<BlueNode>* > > BlueNodeMaps;
     1695    BlueNodeMaps _blue_node_maps;
    16631696
    16641697    typedef std::vector<std::pair<std::string,
    16651698      _writer_bits::MapStorageBase<Edge>* > >EdgeMaps;
     
    17101743
    17111744    /// \brief Destructor
    17121745    ~BpGraphWriter() {
    1713       for (typename NodeMaps::iterator it = _red_maps.begin();
    1714            it != _red_maps.end(); ++it) {
     1746      for (typename RedNodeMaps::iterator it = _red_node_maps.begin();
     1747           it != _red_node_maps.end(); ++it) {
    17151748        delete it->second;
    17161749      }
    17171750
    1718       for (typename NodeMaps::iterator it = _blue_maps.begin();
    1719            it != _blue_maps.end(); ++it) {
     1751      for (typename BlueNodeMaps::iterator it = _blue_node_maps.begin();
     1752           it != _blue_node_maps.end(); ++it) {
    17201753        delete it->second;
    17211754      }
    17221755
     
    17531786      other._os = 0;
    17541787      other.local_os = false;
    17551788
    1756       _node_index.swap(other._node_index);
     1789      _red_node_index.swap(other._red_node_index);
     1790      _blue_node_index.swap(other._blue_node_index);
    17571791      _edge_index.swap(other._edge_index);
    17581792
    1759       _red_maps.swap(other._red_maps);
    1760       _blue_maps.swap(other._blue_maps);
     1793      _red_node_maps.swap(other._red_node_maps);
     1794      _blue_node_maps.swap(other._blue_node_maps);
    17611795      _edge_maps.swap(other._edge_maps);
    17621796      _attributes.swap(other._attributes);
    17631797
     
    17791813    template <typename Map>
    17801814    BpGraphWriter& nodeMap(const std::string& caption, const Map& map) {
    17811815      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
    1782       _writer_bits::MapStorageBase<Node>* red_storage =
    1783         new _writer_bits::MapStorage<Node, Map>(map);
    1784       _red_maps.push_back(std::make_pair(caption, red_storage));
    1785       _writer_bits::MapStorageBase<Node>* blue_storage =
    1786         new _writer_bits::MapStorage<Node, Map>(map);
    1787       _blue_maps.push_back(std::make_pair(caption, blue_storage));
     1816      _writer_bits::MapStorageBase<RedNode>* red_storage =
     1817        new _writer_bits::MapStorage<RedNode, Map>(map);
     1818      _red_node_maps.push_back(std::make_pair(caption, red_storage));
     1819      _writer_bits::MapStorageBase<BlueNode>* blue_storage =
     1820        new _writer_bits::MapStorage<BlueNode, Map>(map);
     1821      _blue_node_maps.push_back(std::make_pair(caption, blue_storage));
    17881822      return *this;
    17891823    }
    17901824
     
    17961830    BpGraphWriter& nodeMap(const std::string& caption, const Map& map,
    17971831                           const Converter& converter = Converter()) {
    17981832      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
    1799       _writer_bits::MapStorageBase<Node>* red_storage =
    1800         new _writer_bits::MapStorage<Node, Map, Converter>(map, converter);
    1801       _red_maps.push_back(std::make_pair(caption, red_storage));
    1802       _writer_bits::MapStorageBase<Node>* blue_storage =
    1803         new _writer_bits::MapStorage<Node, Map, Converter>(map, converter);
    1804       _blue_maps.push_back(std::make_pair(caption, blue_storage));
     1833      _writer_bits::MapStorageBase<RedNode>* red_storage =
     1834        new _writer_bits::MapStorage<RedNode, Map, Converter>(map, converter);
     1835      _red_node_maps.push_back(std::make_pair(caption, red_storage));
     1836      _writer_bits::MapStorageBase<BlueNode>* blue_storage =
     1837        new _writer_bits::MapStorage<BlueNode, Map, Converter>(map, converter);
     1838      _blue_node_maps.push_back(std::make_pair(caption, blue_storage));
    18051839      return *this;
    18061840    }
    18071841
     
    18101844    /// Add a red node map writing rule to the writer.
    18111845    template <typename Map>
    18121846    BpGraphWriter& redNodeMap(const std::string& caption, const Map& map) {
    1813       checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
    1814       _writer_bits::MapStorageBase<Node>* storage =
    1815         new _writer_bits::MapStorage<Node, Map>(map);
    1816       _red_maps.push_back(std::make_pair(caption, storage));
     1847      checkConcept<concepts::ReadMap<RedNode, typename Map::Value>, Map>();
     1848      _writer_bits::MapStorageBase<RedNode>* storage =
     1849        new _writer_bits::MapStorage<RedNode, Map>(map);
     1850      _red_node_maps.push_back(std::make_pair(caption, storage));
    18171851      return *this;
    18181852    }
    18191853
     
    18241858    template <typename Map, typename Converter>
    18251859    BpGraphWriter& redNodeMap(const std::string& caption, const Map& map,
    18261860                              const Converter& converter = Converter()) {
    1827       checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
    1828       _writer_bits::MapStorageBase<Node>* storage =
    1829         new _writer_bits::MapStorage<Node, Map, Converter>(map, converter);
    1830       _red_maps.push_back(std::make_pair(caption, storage));
     1861      checkConcept<concepts::ReadMap<RedNode, typename Map::Value>, Map>();
     1862      _writer_bits::MapStorageBase<RedNode>* storage =
     1863        new _writer_bits::MapStorage<RedNode, Map, Converter>(map, converter);
     1864      _red_node_maps.push_back(std::make_pair(caption, storage));
    18311865      return *this;
    18321866    }
    18331867
     
    18361870    /// Add a blue node map writing rule to the writer.
    18371871    template <typename Map>
    18381872    BpGraphWriter& blueNodeMap(const std::string& caption, const Map& map) {
    1839       checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
    1840       _writer_bits::MapStorageBase<Node>* storage =
    1841         new _writer_bits::MapStorage<Node, Map>(map);
    1842       _blue_maps.push_back(std::make_pair(caption, storage));
     1873      checkConcept<concepts::ReadMap<BlueNode, typename Map::Value>, Map>();
     1874      _writer_bits::MapStorageBase<BlueNode>* storage =
     1875        new _writer_bits::MapStorage<BlueNode, Map>(map);
     1876      _blue_node_maps.push_back(std::make_pair(caption, storage));
    18431877      return *this;
    18441878    }
    18451879
     
    18501884    template <typename Map, typename Converter>
    18511885    BpGraphWriter& blueNodeMap(const std::string& caption, const Map& map,
    18521886                               const Converter& converter = Converter()) {
    1853       checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
    1854       _writer_bits::MapStorageBase<Node>* storage =
    1855         new _writer_bits::MapStorage<Node, Map, Converter>(map, converter);
    1856       _blue_maps.push_back(std::make_pair(caption, storage));
     1887      checkConcept<concepts::ReadMap<BlueNode, typename Map::Value>, Map>();
     1888      _writer_bits::MapStorageBase<BlueNode>* storage =
     1889        new _writer_bits::MapStorage<BlueNode, Map, Converter>(map, converter);
     1890      _blue_node_maps.push_back(std::make_pair(caption, storage));
    18571891      return *this;
    18581892    }
    18591893
     
    19451979    ///
    19461980    /// Add a node writing rule to the writer.
    19471981    BpGraphWriter& node(const std::string& caption, const Node& node) {
     1982      typedef _writer_bits::DoubleMapLookUpConverter<
     1983        Node, RedNodeIndex, BlueNodeIndex> Converter;
     1984      Converter converter(_red_node_index, _blue_node_index);
     1985      _writer_bits::ValueStorageBase* storage =
     1986        new _writer_bits::ValueStorage<Node, Converter>(node, converter);
     1987      _attributes.push_back(std::make_pair(caption, storage));
     1988      return *this;
     1989    }
     1990
     1991    /// \brief Red node writing rule
     1992    ///
     1993    /// Add a red node writing rule to the writer.
     1994    BpGraphWriter& redNode(const std::string& caption, const RedNode& node) {
    19481995      typedef _writer_bits::MapLookUpConverter<Node> Converter;
    1949       Converter converter(_node_index);
     1996      Converter converter(_red_node_index);
     1997      _writer_bits::ValueStorageBase* storage =
     1998        new _writer_bits::ValueStorage<Node, Converter>(node, converter);
     1999      _attributes.push_back(std::make_pair(caption, storage));
     2000      return *this;
     2001    }
     2002
     2003    /// \brief Blue node writing rule
     2004    ///
     2005    /// Add a blue node writing rule to the writer.
     2006    BpGraphWriter& blueNode(const std::string& caption, const BlueNode& node) {
     2007      typedef _writer_bits::MapLookUpConverter<Node> Converter;
     2008      Converter converter(_blue_node_index);
    19502009      _writer_bits::ValueStorageBase* storage =
    19512010        new _writer_bits::ValueStorage<Node, Converter>(node, converter);
    19522011      _attributes.push_back(std::make_pair(caption, storage));
     
    20332092  private:
    20342093
    20352094    void writeRedNodes() {
    2036       _writer_bits::MapStorageBase<Node>* label = 0;
    2037       for (typename NodeMaps::iterator it = _red_maps.begin();
    2038            it != _red_maps.end(); ++it) {
     2095      _writer_bits::MapStorageBase<RedNode>* label = 0;
     2096      for (typename RedNodeMaps::iterator it = _red_node_maps.begin();
     2097           it != _red_node_maps.end(); ++it) {
    20392098        if (it->first == "label") {
    20402099          label = it->second;
    20412100          break;
     
    20512110      if (label == 0) {
    20522111        *_os << "label" << '\t';
    20532112      }
    2054       for (typename NodeMaps::iterator it = _red_maps.begin();
    2055            it != _red_maps.end(); ++it) {
     2113      for (typename RedNodeMaps::iterator it = _red_node_maps.begin();
     2114           it != _red_node_maps.end(); ++it) {
    20562115        _writer_bits::writeToken(*_os, it->first) << '\t';
    20572116      }
    20582117      *_os << std::endl;
    20592118
    2060       std::vector<Node> nodes;
     2119      std::vector<RedNode> nodes;
    20612120      for (RedNodeIt n(_graph); n != INVALID; ++n) {
    20622121        nodes.push_back(n);
    20632122      }
     
    20712130      }
    20722131
    20732132      for (int i = 0; i < static_cast<int>(nodes.size()); ++i) {
    2074         Node n = nodes[i];
     2133        RedNode n = nodes[i];
    20752134        if (label == 0) {
    20762135          std::ostringstream os;
    2077           os << _graph.id(n);
     2136          os << _graph.id(static_cast<Node>(n));
    20782137          _writer_bits::writeToken(*_os, os.str());
    20792138          *_os << '\t';
    2080           _node_index.insert(std::make_pair(n, os.str()));
     2139          _red_node_index.insert(std::make_pair(n, os.str()));
    20812140        }
    2082         for (typename NodeMaps::iterator it = _red_maps.begin();
    2083              it != _red_maps.end(); ++it) {
     2141        for (typename RedNodeMaps::iterator it = _red_node_maps.begin();
     2142             it != _red_node_maps.end(); ++it) {
    20842143          std::string value = it->second->get(n);
    20852144          _writer_bits::writeToken(*_os, value);
    20862145          if (it->first == "label") {
    2087             _node_index.insert(std::make_pair(n, value));
     2146            _red_node_index.insert(std::make_pair(n, value));
    20882147          }
    20892148          *_os << '\t';
    20902149        }
     
    20932152    }
    20942153
    20952154    void writeBlueNodes() {
    2096       _writer_bits::MapStorageBase<Node>* label = 0;
    2097       for (typename NodeMaps::iterator it = _blue_maps.begin();
    2098            it != _blue_maps.end(); ++it) {
     2155      _writer_bits::MapStorageBase<BlueNode>* label = 0;
     2156      for (typename BlueNodeMaps::iterator it = _blue_node_maps.begin();
     2157           it != _blue_node_maps.end(); ++it) {
    20992158        if (it->first == "label") {
    21002159          label = it->second;
    21012160          break;
     
    21112170      if (label == 0) {
    21122171        *_os << "label" << '\t';
    21132172      }
    2114       for (typename NodeMaps::iterator it = _blue_maps.begin();
    2115            it != _blue_maps.end(); ++it) {
     2173      for (typename BlueNodeMaps::iterator it = _blue_node_maps.begin();
     2174           it != _blue_node_maps.end(); ++it) {
    21162175        _writer_bits::writeToken(*_os, it->first) << '\t';
    21172176      }
    21182177      *_os << std::endl;
    21192178
    2120       std::vector<Node> nodes;
     2179      std::vector<BlueNode> nodes;
    21212180      for (BlueNodeIt n(_graph); n != INVALID; ++n) {
    21222181        nodes.push_back(n);
    21232182      }
     
    21312190      }
    21322191
    21332192      for (int i = 0; i < static_cast<int>(nodes.size()); ++i) {
    2134         Node n = nodes[i];
     2193        BlueNode n = nodes[i];
    21352194        if (label == 0) {
    21362195          std::ostringstream os;
    2137           os << _graph.id(n);
     2196          os << _graph.id(static_cast<Node>(n));
    21382197          _writer_bits::writeToken(*_os, os.str());
    21392198          *_os << '\t';
    2140           _node_index.insert(std::make_pair(n, os.str()));
     2199          _blue_node_index.insert(std::make_pair(n, os.str()));
    21412200        }
    2142         for (typename NodeMaps::iterator it = _blue_maps.begin();
    2143              it != _blue_maps.end(); ++it) {
     2201        for (typename BlueNodeMaps::iterator it = _blue_node_maps.begin();
     2202             it != _blue_node_maps.end(); ++it) {
    21442203          std::string value = it->second->get(n);
    21452204          _writer_bits::writeToken(*_os, value);
    21462205          if (it->first == "label") {
    2147             _node_index.insert(std::make_pair(n, value));
     2206            _blue_node_index.insert(std::make_pair(n, value));
    21482207          }
    21492208          *_os << '\t';
    21502209        }
     
    21532212    }
    21542213
    21552214    void createRedNodeIndex() {
    2156       _writer_bits::MapStorageBase<Node>* label = 0;
    2157       for (typename NodeMaps::iterator it = _red_maps.begin();
    2158            it != _red_maps.end(); ++it) {
     2215      _writer_bits::MapStorageBase<RedNode>* label = 0;
     2216      for (typename RedNodeMaps::iterator it = _red_node_maps.begin();
     2217           it != _red_node_maps.end(); ++it) {
    21592218        if (it->first == "label") {
    21602219          label = it->second;
    21612220          break;
     
    21632222      }
    21642223
    21652224      if (label == 0) {
    2166         for (NodeIt n(_graph); n != INVALID; ++n) {
     2225        for (RedNodeIt n(_graph); n != INVALID; ++n) {
    21672226          std::ostringstream os;
    21682227          os << _graph.id(n);
    2169           _node_index.insert(std::make_pair(n, os.str()));
     2228          _red_node_index.insert(std::make_pair(n, os.str()));
    21702229        }
    21712230      } else {
    2172         for (NodeIt n(_graph); n != INVALID; ++n) {
     2231        for (RedNodeIt n(_graph); n != INVALID; ++n) {
    21732232          std::string value = label->get(n);
    2174           _node_index.insert(std::make_pair(n, value));
     2233          _red_node_index.insert(std::make_pair(n, value));
    21752234        }
    21762235      }
    21772236    }
    21782237
    21792238    void createBlueNodeIndex() {
    2180       _writer_bits::MapStorageBase<Node>* label = 0;
    2181       for (typename NodeMaps::iterator it = _blue_maps.begin();
    2182            it != _blue_maps.end(); ++it) {
     2239      _writer_bits::MapStorageBase<BlueNode>* label = 0;
     2240      for (typename BlueNodeMaps::iterator it = _blue_node_maps.begin();
     2241           it != _blue_node_maps.end(); ++it) {
    21832242        if (it->first == "label") {
    21842243          label = it->second;
    21852244          break;
     
    21872246      }
    21882247
    21892248      if (label == 0) {
    2190         for (NodeIt n(_graph); n != INVALID; ++n) {
     2249        for (BlueNodeIt n(_graph); n != INVALID; ++n) {
    21912250          std::ostringstream os;
    21922251          os << _graph.id(n);
    2193           _node_index.insert(std::make_pair(n, os.str()));
     2252          _blue_node_index.insert(std::make_pair(n, os.str()));
    21942253        }
    21952254      } else {
    2196         for (NodeIt n(_graph); n != INVALID; ++n) {
     2255        for (BlueNodeIt n(_graph); n != INVALID; ++n) {
    21972256          std::string value = label->get(n);
    2198           _node_index.insert(std::make_pair(n, value));
     2257          _blue_node_index.insert(std::make_pair(n, value));
    21992258        }
    22002259      }
    22012260    }
     
    22412300
    22422301      for (int i = 0; i < static_cast<int>(edges.size()); ++i) {
    22432302        Edge e = edges[i];
    2244         _writer_bits::writeToken(*_os, _node_index.
     2303        _writer_bits::writeToken(*_os, _red_node_index.
    22452304                                 find(_graph.redNode(e))->second);
    22462305        *_os << '\t';
    2247         _writer_bits::writeToken(*_os, _node_index.
     2306        _writer_bits::writeToken(*_os, _blue_node_index.
    22482307                                 find(_graph.blueNode(e))->second);
    22492308        *_os << '\t';
    22502309        if (label == 0) {
  • test/CMakeLists.txt

    diff -r 07fd7ea22c78 -r b79821620180 test/CMakeLists.txt
    a b  
    3030  hao_orlin_test
    3131  heap_test
    3232  kruskal_test
     33  lgf_reader_writer_test
    3334  maps_test
    3435  matching_test
    3536  max_clique_test
  • test/Makefile.am

    diff -r 07fd7ea22c78 -r b79821620180 test/Makefile.am
    a b  
    3232        test/hao_orlin_test \
    3333        test/heap_test \
    3434        test/kruskal_test \
     35        test/lgf_reader_writer_test \
    3536        test/maps_test \
    3637        test/matching_test \
    3738        test/max_clique_test \
     
    8485test_kruskal_test_SOURCES = test/kruskal_test.cc
    8586test_hao_orlin_test_SOURCES = test/hao_orlin_test.cc
    8687test_lp_test_SOURCES = test/lp_test.cc
     88test_lgf_reader_writer_test_SOURCES = test/lgf_reader_writer_test.cc
    8789test_maps_test_SOURCES = test/maps_test.cc
    8890test_mip_test_SOURCES = test/mip_test.cc
    8991test_matching_test_SOURCES = test/matching_test.cc
  • new file test/lgf_reader_writer_test.cc

    diff -r 07fd7ea22c78 -r b79821620180 test/lgf_reader_writer_test.cc
    - +  
     1/* -*- mode: C++; indent-tabs-mode: nil; -*-
     2 *
     3 * This file is a part of LEMON, a generic C++ optimization library.
     4 *
     5 * Copyright (C) 2003-2010
     6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8 *
     9 * Permission to use, modify and distribute this software is granted
     10 * provided that this copyright notice appears in all copies. For
     11 * precise terms see the accompanying LICENSE file.
     12 *
     13 * This software is provided "AS IS" with no warranty of any kind,
     14 * express or implied, and with no claim as to its suitability for any
     15 * purpose.
     16 *
     17 */
     18
     19#include <string>
     20
     21#include <lemon/concepts/digraph.h>
     22#include <lemon/concepts/graph.h>
     23#include <lemon/concepts/bpgraph.h>
     24
     25#include <lemon/list_graph.h>
     26#include <lemon/smart_graph.h>
     27#include <lemon/lgf_reader.h>
     28
     29#include "test_tools.h"
     30
     31struct ReaderConverter {
     32  int operator()(const std::string& str) const {
     33    return str.length();
     34  }
     35};
     36
     37struct WriterConverter {
     38  std::string operator()(int value) const {
     39    return std::string(value, '*');
     40  }
     41};
     42
     43void checkDigraphReaderCompile() {
     44  typedef lemon::concepts::ExtendableDigraphComponent<
     45    lemon::concepts::Digraph> Digraph;
     46  Digraph digraph;
     47  Digraph::NodeMap<int> node_map(digraph);
     48  Digraph::ArcMap<int> arc_map(digraph);
     49  Digraph::Node node;
     50  Digraph::Arc arc;
     51  int attr;
     52
     53  lemon::DigraphReader<Digraph> reader(digraph, "filename");
     54  reader.nodeMap("node_map", node_map);
     55  reader.nodeMap("node_map", node_map, ReaderConverter());
     56  reader.arcMap("arc_map", arc_map);
     57  reader.arcMap("arc_map", arc_map, ReaderConverter());
     58  reader.attribute("attr", attr);
     59  reader.attribute("attr", attr, ReaderConverter());
     60  reader.node("node", node);
     61  reader.arc("arc", arc);
     62
     63  reader.nodes("alt_nodes_caption");
     64  reader.arcs("alt_arcs_caption");
     65  reader.attributes("alt_attrs_caption");
     66
     67  reader.useNodes(node_map);
     68  reader.useNodes(node_map, WriterConverter());
     69  reader.useArcs(arc_map);
     70  reader.useArcs(arc_map, WriterConverter());
     71
     72  reader.skipNodes();
     73  reader.skipArcs();
     74
     75  reader.run();
     76
     77  lemon::DigraphReader<Digraph> reader2(digraph, std::cin);
     78}
     79
     80void checkDigraphWriterCompile() {
     81  typedef lemon::concepts::Digraph Digraph;
     82  Digraph digraph;
     83  Digraph::NodeMap<int> node_map(digraph);
     84  Digraph::ArcMap<int> arc_map(digraph);
     85  Digraph::Node node;
     86  Digraph::Arc arc;
     87  int attr;
     88
     89  lemon::DigraphWriter<Digraph> writer(digraph, "filename");
     90  writer.nodeMap("node_map", node_map);
     91  writer.nodeMap("node_map", node_map, WriterConverter());
     92  writer.arcMap("arc_map", arc_map);
     93  writer.arcMap("arc_map", arc_map, WriterConverter());
     94  writer.attribute("attr", attr);
     95  writer.attribute("attr", attr, WriterConverter());
     96  writer.node("node", node);
     97  writer.arc("arc", arc);
     98
     99  writer.nodes("alt_nodes_caption");
     100  writer.arcs("alt_arcs_caption");
     101  writer.attributes("alt_attrs_caption");
     102
     103  writer.skipNodes();
     104  writer.skipArcs();
     105
     106  writer.run();
     107}
     108
     109void checkGraphReaderCompile() {
     110  typedef lemon::concepts::ExtendableGraphComponent<
     111    lemon::concepts::Graph> Graph;
     112  Graph graph;
     113  Graph::NodeMap<int> node_map(graph);
     114  Graph::ArcMap<int> arc_map(graph);
     115  Graph::EdgeMap<int> edge_map(graph);
     116  Graph::Node node;
     117  Graph::Arc arc;
     118  Graph::Edge edge;
     119  int attr;
     120
     121  lemon::GraphReader<Graph> reader(graph, "filename");
     122  reader.nodeMap("node_map", node_map);
     123  reader.nodeMap("node_map", node_map, ReaderConverter());
     124  reader.arcMap("arc_map", arc_map);
     125  reader.arcMap("arc_map", arc_map, ReaderConverter());
     126  reader.edgeMap("edge_map", edge_map);
     127  reader.edgeMap("edge_map", edge_map, ReaderConverter());
     128  reader.attribute("attr", attr);
     129  reader.attribute("attr", attr, ReaderConverter());
     130  reader.node("node", node);
     131  reader.arc("arc", arc);
     132
     133  reader.nodes("alt_nodes_caption");
     134  reader.edges("alt_edges_caption");
     135  reader.attributes("alt_attrs_caption");
     136
     137  reader.useNodes(node_map);
     138  reader.useNodes(node_map, WriterConverter());
     139  reader.useEdges(edge_map);
     140  reader.useEdges(edge_map, WriterConverter());
     141
     142  reader.skipNodes();
     143  reader.skipEdges();
     144
     145  reader.run();
     146
     147  lemon::GraphReader<Graph> reader2(graph, std::cin);
     148}
     149
     150void checkGraphWriterCompile() {
     151  typedef lemon::concepts::Graph Graph;
     152  Graph graph;
     153  Graph::NodeMap<int> node_map(graph);
     154  Graph::ArcMap<int> arc_map(graph);
     155  Graph::EdgeMap<int> edge_map(graph);
     156  Graph::Node node;
     157  Graph::Arc arc;
     158  Graph::Edge edge;
     159  int attr;
     160
     161  lemon::GraphWriter<Graph> writer(graph, "filename");
     162  writer.nodeMap("node_map", node_map);
     163  writer.nodeMap("node_map", node_map, WriterConverter());
     164  writer.arcMap("arc_map", arc_map);
     165  writer.arcMap("arc_map", arc_map, WriterConverter());
     166  writer.edgeMap("edge_map", edge_map);
     167  writer.edgeMap("edge_map", edge_map, WriterConverter());
     168  writer.attribute("attr", attr);
     169  writer.attribute("attr", attr, WriterConverter());
     170  writer.node("node", node);
     171  writer.arc("arc", arc);
     172  writer.edge("edge", edge);
     173
     174  writer.nodes("alt_nodes_caption");
     175  writer.edges("alt_edges_caption");
     176  writer.attributes("alt_attrs_caption");
     177
     178  writer.skipNodes();
     179  writer.skipEdges();
     180
     181  writer.run();
     182
     183  lemon::GraphWriter<Graph> writer2(graph, std::cout);
     184}
     185
     186void checkBpGraphReaderCompile() {
     187  typedef lemon::concepts::ExtendableBpGraphComponent<
     188    lemon::concepts::BpGraph> BpGraph;
     189  BpGraph graph;
     190  BpGraph::NodeMap<int> node_map(graph);
     191  BpGraph::RedNodeMap<int> red_node_map(graph);
     192  BpGraph::BlueNodeMap<int> blue_node_map(graph);
     193  BpGraph::ArcMap<int> arc_map(graph);
     194  BpGraph::EdgeMap<int> edge_map(graph);
     195  BpGraph::Node node;
     196  BpGraph::RedNode red_node;
     197  BpGraph::BlueNode blue_node;
     198  BpGraph::Arc arc;
     199  BpGraph::Edge edge;
     200  int attr;
     201
     202  lemon::BpGraphReader<BpGraph> reader(graph, "filename");
     203  reader.nodeMap("node_map", node_map);
     204  reader.nodeMap("node_map", node_map, ReaderConverter());
     205  reader.redNodeMap("red_node_map", red_node_map);
     206  reader.redNodeMap("red_node_map", red_node_map, ReaderConverter());
     207  reader.blueNodeMap("blue_node_map", blue_node_map);
     208  reader.blueNodeMap("blue_node_map", blue_node_map, ReaderConverter());
     209  reader.arcMap("arc_map", arc_map);
     210  reader.arcMap("arc_map", arc_map, ReaderConverter());
     211  reader.edgeMap("edge_map", edge_map);
     212  reader.edgeMap("edge_map", edge_map, ReaderConverter());
     213  reader.attribute("attr", attr);
     214  reader.attribute("attr", attr, ReaderConverter());
     215  reader.node("node", node);
     216  reader.redNode("red_node", red_node);
     217  reader.blueNode("blue_node", blue_node);
     218  reader.arc("arc", arc);
     219
     220  reader.nodes("alt_nodes_caption");
     221  reader.edges("alt_edges_caption");
     222  reader.attributes("alt_attrs_caption");
     223
     224  reader.useNodes(node_map);
     225  reader.useNodes(node_map, WriterConverter());
     226  reader.useEdges(edge_map);
     227  reader.useEdges(edge_map, WriterConverter());
     228
     229  reader.skipNodes();
     230  reader.skipEdges();
     231
     232  reader.run();
     233
     234  lemon::BpGraphReader<BpGraph> reader2(graph, std::cin);
     235}
     236
     237void checkBpGraphWriterCompile() {
     238  typedef lemon::concepts::BpGraph BpGraph;
     239  BpGraph graph;
     240  BpGraph::NodeMap<int> node_map(graph);
     241  BpGraph::RedNodeMap<int> red_node_map(graph);
     242  BpGraph::BlueNodeMap<int> blue_node_map(graph);
     243  BpGraph::ArcMap<int> arc_map(graph);
     244  BpGraph::EdgeMap<int> edge_map(graph);
     245  BpGraph::Node node;
     246  BpGraph::RedNode red_node;
     247  BpGraph::BlueNode blue_node;
     248  BpGraph::Arc arc;
     249  BpGraph::Edge edge;
     250  int attr;
     251
     252  lemon::BpGraphWriter<BpGraph> writer(graph, "filename");
     253  writer.nodeMap("node_map", node_map);
     254  writer.nodeMap("node_map", node_map, WriterConverter());
     255  writer.redNodeMap("red_node_map", red_node_map);
     256  writer.redNodeMap("red_node_map", red_node_map, WriterConverter());
     257  writer.blueNodeMap("blue_node_map", blue_node_map);
     258  writer.blueNodeMap("blue_node_map", blue_node_map, WriterConverter());
     259  writer.arcMap("arc_map", arc_map);
     260  writer.arcMap("arc_map", arc_map, WriterConverter());
     261  writer.edgeMap("edge_map", edge_map);
     262  writer.edgeMap("edge_map", edge_map, WriterConverter());
     263  writer.attribute("attr", attr);
     264  writer.attribute("attr", attr, WriterConverter());
     265  writer.node("node", node);
     266  writer.redNode("red_node", red_node);
     267  writer.blueNode("blue_node", blue_node);
     268  writer.arc("arc", arc);
     269
     270  writer.nodes("alt_nodes_caption");
     271  writer.edges("alt_edges_caption");
     272  writer.attributes("alt_attrs_caption");
     273
     274  writer.skipNodes();
     275  writer.skipEdges();
     276
     277  writer.run();
     278
     279  lemon::BpGraphWriter<BpGraph> writer2(graph, std::cout);
     280}
     281
     282void checkDigraphReaderWriter() {
     283  typedef lemon::SmartDigraph Digraph;
     284  Digraph digraph;
     285  Digraph::Node n1 = digraph.addNode();
     286  Digraph::Node n2 = digraph.addNode();
     287  Digraph::Node n3 = digraph.addNode();
     288
     289  Digraph::Arc a1 = digraph.addArc(n1, n2);
     290  Digraph::Arc a2 = digraph.addArc(n2, n3);
     291
     292  Digraph::NodeMap<int> node_map(digraph);
     293  node_map[n1] = 11;
     294  node_map[n2] = 12;
     295  node_map[n3] = 13;
     296
     297  Digraph::ArcMap<int> arc_map(digraph);
     298  arc_map[a1] = 21;
     299  arc_map[a2] = 22;
     300
     301  int attr = 100;
     302
     303  std::ostringstream os;
     304  lemon::DigraphWriter<Digraph> writer(digraph, os);
     305
     306  writer.nodeMap("node_map1", node_map);
     307  writer.nodeMap("node_map2", node_map, WriterConverter());
     308  writer.arcMap("arc_map1", arc_map);
     309  writer.arcMap("arc_map2", arc_map, WriterConverter());
     310  writer.node("node", n2);
     311  writer.arc("arc", a1);
     312  writer.attribute("attr1", attr);
     313  writer.attribute("attr2", attr, WriterConverter());
     314
     315  writer.run();
     316
     317  typedef lemon::ListDigraph ExpDigraph;
     318  ExpDigraph exp_digraph;
     319  ExpDigraph::NodeMap<int> exp_node_map1(exp_digraph);
     320  ExpDigraph::NodeMap<int> exp_node_map2(exp_digraph);
     321  ExpDigraph::ArcMap<int> exp_arc_map1(exp_digraph);
     322  ExpDigraph::ArcMap<int> exp_arc_map2(exp_digraph);
     323  ExpDigraph::Node exp_n2;
     324  ExpDigraph::Arc exp_a1;
     325  int exp_attr1;
     326  int exp_attr2;
     327
     328  std::istringstream is(os.str());
     329  lemon::DigraphReader<ExpDigraph> reader(exp_digraph, is);
     330
     331  reader.nodeMap("node_map1", exp_node_map1);
     332  reader.nodeMap("node_map2", exp_node_map2, ReaderConverter());
     333  reader.arcMap("arc_map1", exp_arc_map1);
     334  reader.arcMap("arc_map2", exp_arc_map2, ReaderConverter());
     335  reader.node("node", exp_n2);
     336  reader.arc("arc", exp_a1);
     337  reader.attribute("attr1", exp_attr1);
     338  reader.attribute("attr2", exp_attr2, ReaderConverter());
     339
     340  reader.run();
     341
     342  check(lemon::countNodes(exp_digraph) == 3, "Wrong number of nodes");
     343  check(lemon::countArcs(exp_digraph) == 2, "Wrong number of arcs");
     344  check(exp_node_map1[exp_n2] == 12, "Wrong map value");
     345  check(exp_node_map2[exp_n2] == 12, "Wrong map value");
     346  check(exp_arc_map1[exp_a1] == 21, "Wrong map value");
     347  check(exp_arc_map2[exp_a1] == 21, "Wrong map value");
     348  check(exp_attr1 == 100, "Wrong attr value");
     349  check(exp_attr2 == 100, "Wrong attr value");
     350}
     351
     352void checkGraphReaderWriter() {
     353  typedef lemon::SmartGraph Graph;
     354  Graph graph;
     355  Graph::Node n1 = graph.addNode();
     356  Graph::Node n2 = graph.addNode();
     357  Graph::Node n3 = graph.addNode();
     358
     359  Graph::Edge e1 = graph.addEdge(n1, n2);
     360  Graph::Edge e2 = graph.addEdge(n2, n3);
     361
     362  Graph::NodeMap<int> node_map(graph);
     363  node_map[n1] = 11;
     364  node_map[n2] = 12;
     365  node_map[n3] = 13;
     366
     367  Graph::EdgeMap<int> edge_map(graph);
     368  edge_map[e1] = 21;
     369  edge_map[e2] = 22;
     370
     371  Graph::ArcMap<int> arc_map(graph);
     372  arc_map[graph.direct(e1, true)] = 211;
     373  arc_map[graph.direct(e1, false)] = 212;
     374  arc_map[graph.direct(e2, true)] = 221;
     375  arc_map[graph.direct(e2, false)] = 222;
     376
     377  int attr = 100;
     378
     379  std::ostringstream os;
     380  lemon::GraphWriter<Graph> writer(graph, os);
     381
     382  writer.nodeMap("node_map1", node_map);
     383  writer.nodeMap("node_map2", node_map, WriterConverter());
     384  writer.edgeMap("edge_map1", edge_map);
     385  writer.edgeMap("edge_map2", edge_map, WriterConverter());
     386  writer.arcMap("arc_map1", arc_map);
     387  writer.arcMap("arc_map2", arc_map, WriterConverter());
     388  writer.node("node", n2);
     389  writer.edge("edge", e1);
     390  writer.arc("arc", graph.direct(e1, false));
     391  writer.attribute("attr1", attr);
     392  writer.attribute("attr2", attr, WriterConverter());
     393
     394  writer.run();
     395
     396  typedef lemon::ListGraph ExpGraph;
     397  ExpGraph exp_graph;
     398  ExpGraph::NodeMap<int> exp_node_map1(exp_graph);
     399  ExpGraph::NodeMap<int> exp_node_map2(exp_graph);
     400  ExpGraph::EdgeMap<int> exp_edge_map1(exp_graph);
     401  ExpGraph::EdgeMap<int> exp_edge_map2(exp_graph);
     402  ExpGraph::ArcMap<int> exp_arc_map1(exp_graph);
     403  ExpGraph::ArcMap<int> exp_arc_map2(exp_graph);
     404  ExpGraph::Node exp_n2;
     405  ExpGraph::Edge exp_e1;
     406  ExpGraph::Arc exp_a1;
     407  int exp_attr1;
     408  int exp_attr2;
     409
     410  std::istringstream is(os.str());
     411  lemon::GraphReader<ExpGraph> reader(exp_graph, is);
     412
     413  reader.nodeMap("node_map1", exp_node_map1);
     414  reader.nodeMap("node_map2", exp_node_map2, ReaderConverter());
     415  reader.edgeMap("edge_map1", exp_edge_map1);
     416  reader.edgeMap("edge_map2", exp_edge_map2, ReaderConverter());
     417  reader.arcMap("arc_map1", exp_arc_map1);
     418  reader.arcMap("arc_map2", exp_arc_map2, ReaderConverter());
     419  reader.node("node", exp_n2);
     420  reader.edge("edge", exp_e1);
     421  reader.arc("arc", exp_a1);
     422  reader.attribute("attr1", exp_attr1);
     423  reader.attribute("attr2", exp_attr2, ReaderConverter());
     424
     425  reader.run();
     426
     427  check(lemon::countNodes(exp_graph) == 3, "Wrong number of nodes");
     428  check(lemon::countEdges(exp_graph) == 2, "Wrong number of edges");
     429  check(lemon::countArcs(exp_graph) == 4, "Wrong number of arcs");
     430  check(exp_node_map1[exp_n2] == 12, "Wrong map value");
     431  check(exp_node_map2[exp_n2] == 12, "Wrong map value");
     432  check(exp_edge_map1[exp_e1] == 21, "Wrong map value");
     433  check(exp_edge_map2[exp_e1] == 21, "Wrong map value");
     434  check(exp_arc_map1[exp_a1] == 212, "Wrong map value");
     435  check(exp_arc_map2[exp_a1] == 212, "Wrong map value");
     436  check(exp_attr1 == 100, "Wrong attr value");
     437  check(exp_attr2 == 100, "Wrong attr value");
     438}
     439
     440void checkBpGraphReaderWriter() {
     441  typedef lemon::SmartBpGraph Graph;
     442  Graph graph;
     443  Graph::RedNode rn1 = graph.addRedNode();
     444  Graph::RedNode rn2 = graph.addRedNode();
     445  Graph::RedNode rn3 = graph.addRedNode();
     446  Graph::BlueNode bn1 = graph.addBlueNode();
     447  Graph::BlueNode bn2 = graph.addBlueNode();
     448  Graph::Node n = bn1;
     449
     450  Graph::Edge e1 = graph.addEdge(rn1, bn1);
     451  Graph::Edge e2 = graph.addEdge(rn2, bn1);
     452
     453  Graph::NodeMap<int> node_map(graph);
     454  node_map[rn1] = 11;
     455  node_map[rn2] = 12;
     456  node_map[rn3] = 13;
     457  node_map[bn1] = 14;
     458  node_map[bn2] = 15;
     459
     460  Graph::NodeMap<int> red_node_map(graph);
     461  red_node_map[rn1] = 411;
     462  red_node_map[rn2] = 412;
     463  red_node_map[rn3] = 413;
     464
     465  Graph::NodeMap<int> blue_node_map(graph);
     466  blue_node_map[bn1] = 414;
     467  blue_node_map[bn2] = 415;
     468
     469  Graph::EdgeMap<int> edge_map(graph);
     470  edge_map[e1] = 21;
     471  edge_map[e2] = 22;
     472
     473  Graph::ArcMap<int> arc_map(graph);
     474  arc_map[graph.direct(e1, true)] = 211;
     475  arc_map[graph.direct(e1, false)] = 212;
     476  arc_map[graph.direct(e2, true)] = 221;
     477  arc_map[graph.direct(e2, false)] = 222;
     478
     479  int attr = 100;
     480
     481  std::ostringstream os;
     482  lemon::BpGraphWriter<Graph> writer(graph, os);
     483
     484  writer.nodeMap("node_map1", node_map);
     485  writer.nodeMap("node_map2", node_map, WriterConverter());
     486  writer.nodeMap("red_node_map1", red_node_map);
     487  writer.nodeMap("red_node_map2", red_node_map, WriterConverter());
     488  writer.nodeMap("blue_node_map1", blue_node_map);
     489  writer.nodeMap("blue_node_map2", blue_node_map, WriterConverter());
     490  writer.edgeMap("edge_map1", edge_map);
     491  writer.edgeMap("edge_map2", edge_map, WriterConverter());
     492  writer.arcMap("arc_map1", arc_map);
     493  writer.arcMap("arc_map2", arc_map, WriterConverter());
     494  writer.node("node", n);
     495  writer.redNode("red_node", rn1);
     496  writer.blueNode("blue_node", bn2);
     497  writer.edge("edge", e1);
     498  writer.arc("arc", graph.direct(e1, false));
     499  writer.attribute("attr1", attr);
     500  writer.attribute("attr2", attr, WriterConverter());
     501
     502  writer.run();
     503
     504  typedef lemon::ListBpGraph ExpGraph;
     505  ExpGraph exp_graph;
     506  ExpGraph::NodeMap<int> exp_node_map1(exp_graph);
     507  ExpGraph::NodeMap<int> exp_node_map2(exp_graph);
     508  ExpGraph::RedNodeMap<int> exp_red_node_map1(exp_graph);
     509  ExpGraph::RedNodeMap<int> exp_red_node_map2(exp_graph);
     510  ExpGraph::BlueNodeMap<int> exp_blue_node_map1(exp_graph);
     511  ExpGraph::BlueNodeMap<int> exp_blue_node_map2(exp_graph);
     512  ExpGraph::EdgeMap<int> exp_edge_map1(exp_graph);
     513  ExpGraph::EdgeMap<int> exp_edge_map2(exp_graph);
     514  ExpGraph::ArcMap<int> exp_arc_map1(exp_graph);
     515  ExpGraph::ArcMap<int> exp_arc_map2(exp_graph);
     516  ExpGraph::Node exp_n;
     517  ExpGraph::RedNode exp_rn1;
     518  ExpGraph::BlueNode exp_bn2;
     519  ExpGraph::Edge exp_e1;
     520  ExpGraph::Arc exp_a1;
     521  int exp_attr1;
     522  int exp_attr2;
     523
     524  std::istringstream is(os.str());
     525  lemon::BpGraphReader<ExpGraph> reader(exp_graph, is);
     526
     527  reader.nodeMap("node_map1", exp_node_map1);
     528  reader.nodeMap("node_map2", exp_node_map2, ReaderConverter());
     529  reader.redNodeMap("red_node_map1", exp_red_node_map1);
     530  reader.redNodeMap("red_node_map2", exp_red_node_map2, ReaderConverter());
     531  reader.blueNodeMap("blue_node_map1", exp_blue_node_map1);
     532  reader.blueNodeMap("blue_node_map2", exp_blue_node_map2, ReaderConverter());
     533  reader.edgeMap("edge_map1", exp_edge_map1);
     534  reader.edgeMap("edge_map2", exp_edge_map2, ReaderConverter());
     535  reader.arcMap("arc_map1", exp_arc_map1);
     536  reader.arcMap("arc_map2", exp_arc_map2, ReaderConverter());
     537  reader.node("node", exp_n);
     538  reader.redNode("red_node", exp_rn1);
     539  reader.blueNode("blue_node", exp_bn2);
     540  reader.edge("edge", exp_e1);
     541  reader.arc("arc", exp_a1);
     542  reader.attribute("attr1", exp_attr1);
     543  reader.attribute("attr2", exp_attr2, ReaderConverter());
     544
     545  reader.run();
     546
     547  check(lemon::countNodes(exp_graph) == 5, "Wrong number of nodes");
     548  check(lemon::countRedNodes(exp_graph) == 3, "Wrong number of red nodes");
     549  check(lemon::countBlueNodes(exp_graph) == 2, "Wrong number of blue nodes");
     550  check(lemon::countEdges(exp_graph) == 2, "Wrong number of edges");
     551  check(lemon::countArcs(exp_graph) == 4, "Wrong number of arcs");
     552  check(exp_node_map1[exp_n] == 14, "Wrong map value");
     553  check(exp_node_map2[exp_n] == 14, "Wrong map value");
     554  check(exp_red_node_map1[exp_rn1] == 411, "Wrong map value");
     555  check(exp_red_node_map2[exp_rn1] == 411, "Wrong map value");
     556  check(exp_blue_node_map1[exp_bn2] == 415, "Wrong map value");
     557  check(exp_blue_node_map2[exp_bn2] == 415, "Wrong map value");
     558  check(exp_edge_map1[exp_e1] == 21, "Wrong map value");
     559  check(exp_edge_map2[exp_e1] == 21, "Wrong map value");
     560  check(exp_arc_map1[exp_a1] == 212, "Wrong map value");
     561  check(exp_arc_map2[exp_a1] == 212, "Wrong map value");
     562  check(exp_attr1 == 100, "Wrong attr value");
     563  check(exp_attr2 == 100, "Wrong attr value");
     564}
     565
     566
     567int main() {
     568  { // Check digrpah
     569    checkDigraphReaderWriter();
     570  }
     571  { // Check graph
     572    checkGraphReaderWriter();
     573  }
     574  { // Check bipartite graph
     575    checkBpGraphReaderWriter();
     576  }
     577  return 0;
     578}