COIN-OR::LEMON - Graph Library

Changeset 1198:4936be66d2f5 in lemon for lemon/lgf_reader.h


Ignore:
Timestamp:
02/12/13 07:15:52 (11 years ago)
Author:
Balazs Dezso <deba@…>
Branch:
default
Phase:
public
Message:

Changes in BpGraph? lgf reader and writer (#69)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • lemon/lgf_reader.h

    r1197 r1198  
    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;
     
    171171        }
    172172        return it->second;
     173      }
     174    };
     175
     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        }
    173206      }
    174207    };
     
    21722205    std::string _attributes_caption;
    21732206
    2174     typedef std::map<std::string, Node> NodeIndex;
    2175     NodeIndex _node_index;
     2207    typedef std::map<std::string, RedNode> RedNodeIndex;
     2208    RedNodeIndex _red_node_index;
     2209    typedef std::map<std::string, BlueNode> BlueNodeIndex;
     2210    BlueNodeIndex _blue_node_index;
    21762211    typedef std::map<std::string, Edge> EdgeIndex;
    21772212    EdgeIndex _edge_index;
    21782213
    21792214    typedef std::vector<std::pair<std::string,
    2180       _reader_bits::MapStorageBase<Node>*> > NodeMaps;
    2181     NodeMaps _red_maps;
    2182     NodeMaps _blue_maps;
     2215      _reader_bits::MapStorageBase<RedNode>*> > RedNodeMaps;
     2216    RedNodeMaps _red_node_maps;
     2217    typedef std::vector<std::pair<std::string,
     2218      _reader_bits::MapStorageBase<BlueNode>*> > BlueNodeMaps;
     2219    BlueNodeMaps _blue_node_maps;
    21832220
    21842221    typedef std::vector<std::pair<std::string,
     
    22422279    /// \brief Destructor
    22432280    ~BpGraphReader() {
    2244       for (typename NodeMaps::iterator it = _red_maps.begin();
    2245            it != _red_maps.end(); ++it) {
     2281      for (typename RedNodeMaps::iterator it = _red_node_maps.begin();
     2282           it != _red_node_maps.end(); ++it) {
    22462283        delete it->second;
    22472284      }
    22482285
    2249       for (typename NodeMaps::iterator it = _blue_maps.begin();
    2250            it != _blue_maps.end(); ++it) {
     2286      for (typename BlueNodeMaps::iterator it = _blue_node_maps.begin();
     2287           it != _blue_node_maps.end(); ++it) {
    22512288        delete it->second;
    22522289      }
     
    22852322      other.local_is = false;
    22862323
    2287       _node_index.swap(other._node_index);
     2324      _red_node_index.swap(other._red_node_index);
     2325      _blue_node_index.swap(other._blue_node_index);
    22882326      _edge_index.swap(other._edge_index);
    22892327
    2290       _red_maps.swap(other._red_maps);
    2291       _blue_maps.swap(other._blue_maps);
     2328      _red_node_maps.swap(other._red_node_maps);
     2329      _blue_node_maps.swap(other._blue_node_maps);
    22922330      _edge_maps.swap(other._edge_maps);
    22932331      _attributes.swap(other._attributes);
     
    23122350    BpGraphReader& nodeMap(const std::string& caption, Map& map) {
    23132351      checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
    2314       _reader_bits::MapStorageBase<Node>* red_storage =
    2315         new _reader_bits::MapStorage<Node, Map>(map);
    2316       _red_maps.push_back(std::make_pair(caption, red_storage));
    2317       _reader_bits::MapStorageBase<Node>* blue_storage =
    2318         new _reader_bits::MapStorage<Node, Map>(map);
    2319       _blue_maps.push_back(std::make_pair(caption, blue_storage));
     2352      _reader_bits::MapStorageBase<RedNode>* red_storage =
     2353        new _reader_bits::MapStorage<RedNode, Map>(map);
     2354      _red_node_maps.push_back(std::make_pair(caption, red_storage));
     2355      _reader_bits::MapStorageBase<BlueNode>* blue_storage =
     2356        new _reader_bits::MapStorage<BlueNode, Map>(map);
     2357      _blue_node_maps.push_back(std::make_pair(caption, blue_storage));
    23202358      return *this;
    23212359    }
     
    23292367                           const Converter& converter = Converter()) {
    23302368      checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
    2331       _reader_bits::MapStorageBase<Node>* red_storage =
    2332         new _reader_bits::MapStorage<Node, Map, Converter>(map, converter);
    2333       _red_maps.push_back(std::make_pair(caption, red_storage));
    2334       _reader_bits::MapStorageBase<Node>* blue_storage =
    2335         new _reader_bits::MapStorage<Node, Map, Converter>(map, converter);
    2336       _blue_maps.push_back(std::make_pair(caption, blue_storage));
     2369      _reader_bits::MapStorageBase<RedNode>* red_storage =
     2370        new _reader_bits::MapStorage<RedNode, Map, Converter>(map, converter);
     2371      _red_node_maps.push_back(std::make_pair(caption, red_storage));
     2372      _reader_bits::MapStorageBase<BlueNode>* blue_storage =
     2373        new _reader_bits::MapStorage<BlueNode, Map, Converter>(map, converter);
     2374      _blue_node_maps.push_back(std::make_pair(caption, blue_storage));
    23372375      return *this;
    23382376    }
     
    23412379    template <typename Map>
    23422380    BpGraphReader& redNodeMap(const std::string& caption, Map& map) {
    2343       checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
    2344       _reader_bits::MapStorageBase<Node>* storage =
    2345         new _reader_bits::MapStorage<Node, Map>(map);
    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>(map);
     2384      _red_node_maps.push_back(std::make_pair(caption, storage));
    23472385      return *this;
    23482386    }
     
    23552393    BpGraphReader& redNodeMap(const std::string& caption, Map& map,
    23562394                              const Converter& converter = Converter()) {
    2357       checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
    2358       _reader_bits::MapStorageBase<Node>* storage =
    2359         new _reader_bits::MapStorage<Node, Map, Converter>(map, converter);
    2360       _red_maps.push_back(std::make_pair(caption, storage));
     2395      checkConcept<concepts::WriteMap<RedNode, typename Map::Value>, Map>();
     2396      _reader_bits::MapStorageBase<RedNode>* storage =
     2397        new _reader_bits::MapStorage<RedNode, Map, Converter>(map, converter);
     2398      _red_node_maps.push_back(std::make_pair(caption, storage));
    23612399      return *this;
    23622400    }
     
    23652403    template <typename Map>
    23662404    BpGraphReader& blueNodeMap(const std::string& caption, Map& map) {
    2367       checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
    2368       _reader_bits::MapStorageBase<Node>* storage =
    2369         new _reader_bits::MapStorage<Node, Map>(map);
    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>(map);
     2408      _blue_node_maps.push_back(std::make_pair(caption, storage));
    23712409      return *this;
    23722410    }
     
    23792417    BpGraphReader& blueNodeMap(const std::string& caption, Map& map,
    23802418                               const Converter& converter = Converter()) {
    2381       checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
    2382       _reader_bits::MapStorageBase<Node>* storage =
    2383         new _reader_bits::MapStorage<Node, Map, Converter>(map, converter);
    2384       _blue_maps.push_back(std::make_pair(caption, storage));
     2419      checkConcept<concepts::WriteMap<BlueNode, typename Map::Value>, Map>();
     2420      _reader_bits::MapStorageBase<BlueNode>* storage =
     2421        new _reader_bits::MapStorage<BlueNode, Map, Converter>(map, converter);
     2422      _blue_node_maps.push_back(std::make_pair(caption, storage));
    23852423      return *this;
    23862424    }
     
    24742512    /// Add a node reading rule to reader.
    24752513    BpGraphReader& node(const std::string& caption, Node& node) {
    2476       typedef _reader_bits::MapLookUpConverter<Node> Converter;
    2477       Converter converter(_node_index);
     2514      typedef _reader_bits::DoubleMapLookUpConverter<
     2515        Node, RedNodeIndex, BlueNodeIndex> Converter;
     2516      Converter converter(_red_node_index, _blue_node_index);
    24782517      _reader_bits::ValueStorageBase* storage =
    24792518        new _reader_bits::ValueStorage<Node, Converter>(node, converter);
     2519      _attributes.insert(std::make_pair(caption, storage));
     2520      return *this;
     2521    }
     2522
     2523    /// \brief Red node reading rule
     2524    ///
     2525    /// Add a red node reading rule to reader.
     2526    BpGraphReader& redNode(const std::string& caption, RedNode& node) {
     2527      typedef _reader_bits::MapLookUpConverter<RedNode> Converter;
     2528      Converter converter(_red_node_index);
     2529      _reader_bits::ValueStorageBase* storage =
     2530        new _reader_bits::ValueStorage<RedNode, Converter>(node, converter);
     2531      _attributes.insert(std::make_pair(caption, storage));
     2532      return *this;
     2533    }
     2534
     2535    /// \brief Blue node reading rule
     2536    ///
     2537    /// Add a blue node reading rule to reader.
     2538    BpGraphReader& blueNode(const std::string& caption, BlueNode& node) {
     2539      typedef _reader_bits::MapLookUpConverter<BlueNode> Converter;
     2540      Converter converter(_blue_node_index);
     2541      _reader_bits::ValueStorageBase* storage =
     2542        new _reader_bits::ValueStorage<BlueNode, Converter>(node, converter);
    24802543      _attributes.insert(std::make_pair(caption, storage));
    24812544      return *this;
     
    25502613      _use_nodes = true;
    25512614      _writer_bits::DefaultConverter<typename Map::Value> converter;
    2552       for (NodeIt n(_graph); n != INVALID; ++n) {
    2553         _node_index.insert(std::make_pair(converter(map[n]), n));
     2615      for (RedNodeIt n(_graph); n != INVALID; ++n) {
     2616        _red_node_index.insert(std::make_pair(converter(map[n]), n));
     2617      }
     2618      for (BlueNodeIt n(_graph); n != INVALID; ++n) {
     2619        _blue_node_index.insert(std::make_pair(converter(map[n]), n));
    25542620      }
    25552621      return *this;
     
    25672633      LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
    25682634      _use_nodes = true;
    2569       for (NodeIt n(_graph); n != INVALID; ++n) {
    2570         _node_index.insert(std::make_pair(converter(map[n]), n));
     2635      for (RedNodeIt n(_graph); n != INVALID; ++n) {
     2636        _red_node_index.insert(std::make_pair(converter(map[n]), n));
     2637      }
     2638      for (BlueNodeIt n(_graph); n != INVALID; ++n) {     
     2639        _blue_node_index.insert(std::make_pair(converter(map[n]), n));
    25712640      }
    25722641      return *this;
     
    26652734    void readRedNodes() {
    26662735
    2667       std::vector<int> map_index(_red_maps.size());
     2736      std::vector<int> map_index(_red_node_maps.size());
    26682737      int map_num, label_index;
    26692738
     
    26712740      if (!readLine() || !(line >> c) || c == '@') {
    26722741        if (readSuccess() && line) line.putback(c);
    2673         if (!_red_maps.empty())
     2742        if (!_red_node_maps.empty())
    26742743          throw FormatError("Cannot find map names");
    26752744        return;
     
    26922761        }
    26932762
    2694         for (int i = 0; i < static_cast<int>(_red_maps.size()); ++i) {
     2763        for (int i = 0; i < static_cast<int>(_red_node_maps.size()); ++i) {
    26952764          std::map<std::string, int>::iterator jt =
    2696             maps.find(_red_maps[i].first);
     2765            maps.find(_red_node_maps[i].first);
    26972766          if (jt == maps.end()) {
    26982767            std::ostringstream msg;
    2699             msg << "Map not found: " << _red_maps[i].first;
     2768            msg << "Map not found: " << _red_node_maps[i].first;
    27002769            throw FormatError(msg.str());
    27012770          }
     
    27282797          throw FormatError("Extra character at the end of line");
    27292798
    2730         Node n;
     2799        RedNode n;
    27312800        if (!_use_nodes) {
    27322801          n = _graph.addRedNode();
    27332802          if (label_index != -1)
    2734             _node_index.insert(std::make_pair(tokens[label_index], n));
     2803            _red_node_index.insert(std::make_pair(tokens[label_index], n));
    27352804        } else {
    27362805          if (label_index == -1)
    27372806            throw FormatError("Label map not found");
    2738           typename std::map<std::string, Node>::iterator it =
    2739             _node_index.find(tokens[label_index]);
    2740           if (it == _node_index.end()) {
     2807          typename std::map<std::string, RedNode>::iterator it =
     2808            _red_node_index.find(tokens[label_index]);
     2809          if (it == _red_node_index.end()) {
    27412810            std::ostringstream msg;
    27422811            msg << "Node with label not found: " << tokens[label_index];
     
    27462815        }
    27472816
    2748         for (int i = 0; i < static_cast<int>(_red_maps.size()); ++i) {
    2749           _red_maps[i].second->set(n, tokens[map_index[i]]);
     2817        for (int i = 0; i < static_cast<int>(_red_node_maps.size()); ++i) {
     2818          _red_node_maps[i].second->set(n, tokens[map_index[i]]);
    27502819        }
    27512820
     
    27582827    void readBlueNodes() {
    27592828
    2760       std::vector<int> map_index(_blue_maps.size());
     2829      std::vector<int> map_index(_blue_node_maps.size());
    27612830      int map_num, label_index;
    27622831
     
    27642833      if (!readLine() || !(line >> c) || c == '@') {
    27652834        if (readSuccess() && line) line.putback(c);
    2766         if (!_blue_maps.empty())
     2835        if (!_blue_node_maps.empty())
    27672836          throw FormatError("Cannot find map names");
    27682837        return;
     
    27852854        }
    27862855
    2787         for (int i = 0; i < static_cast<int>(_blue_maps.size()); ++i) {
     2856        for (int i = 0; i < static_cast<int>(_blue_node_maps.size()); ++i) {
    27882857          std::map<std::string, int>::iterator jt =
    2789             maps.find(_blue_maps[i].first);
     2858            maps.find(_blue_node_maps[i].first);
    27902859          if (jt == maps.end()) {
    27912860            std::ostringstream msg;
    2792             msg << "Map not found: " << _blue_maps[i].first;
     2861            msg << "Map not found: " << _blue_node_maps[i].first;
    27932862            throw FormatError(msg.str());
    27942863          }
     
    28212890          throw FormatError("Extra character at the end of line");
    28222891
    2823         Node n;
     2892        BlueNode n;
    28242893        if (!_use_nodes) {
    28252894          n = _graph.addBlueNode();
    28262895          if (label_index != -1)
    2827             _node_index.insert(std::make_pair(tokens[label_index], n));
     2896            _blue_node_index.insert(std::make_pair(tokens[label_index], n));
    28282897        } else {
    28292898          if (label_index == -1)
    28302899            throw FormatError("Label map not found");
    2831           typename std::map<std::string, Node>::iterator it =
    2832             _node_index.find(tokens[label_index]);
    2833           if (it == _node_index.end()) {
     2900          typename std::map<std::string, BlueNode>::iterator it =
     2901            _blue_node_index.find(tokens[label_index]);
     2902          if (it == _blue_node_index.end()) {
    28342903            std::ostringstream msg;
    28352904            msg << "Node with label not found: " << tokens[label_index];
     
    28392908        }
    28402909
    2841         for (int i = 0; i < static_cast<int>(_blue_maps.size()); ++i) {
    2842           _blue_maps[i].second->set(n, tokens[map_index[i]]);
     2910        for (int i = 0; i < static_cast<int>(_blue_node_maps.size()); ++i) {
     2911          _blue_node_maps[i].second->set(n, tokens[map_index[i]]);
    28432912        }
    28442913
     
    29252994        Edge e;
    29262995        if (!_use_edges) {
    2927 
    2928           typename NodeIndex::iterator it;
    2929 
    2930           it = _node_index.find(source_token);
    2931           if (it == _node_index.end()) {
     2996          typename RedNodeIndex::iterator rit =
     2997            _red_node_index.find(source_token);
     2998          if (rit == _red_node_index.end()) {
    29322999            std::ostringstream msg;
    29333000            msg << "Item not found: " << source_token;
    29343001            throw FormatError(msg.str());
    29353002          }
    2936           Node source = it->second;
    2937           if (!_graph.red(source)) {
    2938             std::ostringstream msg;
    2939             msg << "Item is not red node: " << source_token;
    2940             throw FormatError(msg.str());
    2941           }
    2942 
    2943           it = _node_index.find(target_token);
    2944           if (it == _node_index.end()) {
     3003          RedNode source = rit->second;
     3004          typename BlueNodeIndex::iterator it =
     3005            _blue_node_index.find(target_token);
     3006          if (it == _blue_node_index.end()) {
    29453007            std::ostringstream msg;
    29463008            msg << "Item not found: " << target_token;
    29473009            throw FormatError(msg.str());
    29483010          }
    2949           Node target = it->second;
    2950           if (!_graph.blue(target)) {
    2951             std::ostringstream msg;
    2952             msg << "Item is not red node: " << source_token;
    2953             throw FormatError(msg.str());
    2954           }
     3011          BlueNode target = it->second;
    29553012
    29563013          // It is checked that source is red and
    29573014          // target is blue, so this should be safe:
    2958           e = _graph.addEdge(_graph.asRedNodeUnsafe(source),
    2959                              _graph.asBlueNodeUnsafe(target));
     3015          e = _graph.addEdge(source, target);
    29603016          if (label_index != -1)
    29613017            _edge_index.insert(std::make_pair(tokens[label_index], e));
Note: See TracChangeset for help on using the changeset viewer.