Changeset 1198:4936be66d2f5 in lemon for lemon/lgf_reader.h
- Timestamp:
- 02/12/13 07:15:52 (11 years ago)
- Branch:
- default
- Phase:
- public
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lemon/lgf_reader.h
r1197 r1198 155 155 }; 156 156 157 template <typename Value> 157 template <typename Value, 158 typename Map = std::map<std::string, Value> > 158 159 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) 162 163 : _map(map) {} 163 164 164 165 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); 167 167 if (it == _map.end()) { 168 168 std::ostringstream msg; … … 171 171 } 172 172 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 } 173 206 } 174 207 }; … … 2172 2205 std::string _attributes_caption; 2173 2206 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; 2176 2211 typedef std::map<std::string, Edge> EdgeIndex; 2177 2212 EdgeIndex _edge_index; 2178 2213 2179 2214 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; 2183 2220 2184 2221 typedef std::vector<std::pair<std::string, … … 2242 2279 /// \brief Destructor 2243 2280 ~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) { 2246 2283 delete it->second; 2247 2284 } 2248 2285 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) { 2251 2288 delete it->second; 2252 2289 } … … 2285 2322 other.local_is = false; 2286 2323 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); 2288 2326 _edge_index.swap(other._edge_index); 2289 2327 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); 2292 2330 _edge_maps.swap(other._edge_maps); 2293 2331 _attributes.swap(other._attributes); … … 2312 2350 BpGraphReader& nodeMap(const std::string& caption, Map& map) { 2313 2351 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)); 2320 2358 return *this; 2321 2359 } … … 2329 2367 const Converter& converter = Converter()) { 2330 2368 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)); 2337 2375 return *this; 2338 2376 } … … 2341 2379 template <typename Map> 2342 2380 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)); 2347 2385 return *this; 2348 2386 } … … 2355 2393 BpGraphReader& redNodeMap(const std::string& caption, Map& map, 2356 2394 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)); 2361 2399 return *this; 2362 2400 } … … 2365 2403 template <typename Map> 2366 2404 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)); 2371 2409 return *this; 2372 2410 } … … 2379 2417 BpGraphReader& blueNodeMap(const std::string& caption, Map& map, 2380 2418 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)); 2385 2423 return *this; 2386 2424 } … … 2474 2512 /// Add a node reading rule to reader. 2475 2513 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); 2478 2517 _reader_bits::ValueStorageBase* storage = 2479 2518 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); 2480 2543 _attributes.insert(std::make_pair(caption, storage)); 2481 2544 return *this; … … 2550 2613 _use_nodes = true; 2551 2614 _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)); 2554 2620 } 2555 2621 return *this; … … 2567 2633 LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); 2568 2634 _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)); 2571 2640 } 2572 2641 return *this; … … 2665 2734 void readRedNodes() { 2666 2735 2667 std::vector<int> map_index(_red_ maps.size());2736 std::vector<int> map_index(_red_node_maps.size()); 2668 2737 int map_num, label_index; 2669 2738 … … 2671 2740 if (!readLine() || !(line >> c) || c == '@') { 2672 2741 if (readSuccess() && line) line.putback(c); 2673 if (!_red_ maps.empty())2742 if (!_red_node_maps.empty()) 2674 2743 throw FormatError("Cannot find map names"); 2675 2744 return; … … 2692 2761 } 2693 2762 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) { 2695 2764 std::map<std::string, int>::iterator jt = 2696 maps.find(_red_ maps[i].first);2765 maps.find(_red_node_maps[i].first); 2697 2766 if (jt == maps.end()) { 2698 2767 std::ostringstream msg; 2699 msg << "Map not found: " << _red_ maps[i].first;2768 msg << "Map not found: " << _red_node_maps[i].first; 2700 2769 throw FormatError(msg.str()); 2701 2770 } … … 2728 2797 throw FormatError("Extra character at the end of line"); 2729 2798 2730 Node n;2799 RedNode n; 2731 2800 if (!_use_nodes) { 2732 2801 n = _graph.addRedNode(); 2733 2802 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)); 2735 2804 } else { 2736 2805 if (label_index == -1) 2737 2806 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()) { 2741 2810 std::ostringstream msg; 2742 2811 msg << "Node with label not found: " << tokens[label_index]; … … 2746 2815 } 2747 2816 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]]); 2750 2819 } 2751 2820 … … 2758 2827 void readBlueNodes() { 2759 2828 2760 std::vector<int> map_index(_blue_ maps.size());2829 std::vector<int> map_index(_blue_node_maps.size()); 2761 2830 int map_num, label_index; 2762 2831 … … 2764 2833 if (!readLine() || !(line >> c) || c == '@') { 2765 2834 if (readSuccess() && line) line.putback(c); 2766 if (!_blue_ maps.empty())2835 if (!_blue_node_maps.empty()) 2767 2836 throw FormatError("Cannot find map names"); 2768 2837 return; … … 2785 2854 } 2786 2855 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) { 2788 2857 std::map<std::string, int>::iterator jt = 2789 maps.find(_blue_ maps[i].first);2858 maps.find(_blue_node_maps[i].first); 2790 2859 if (jt == maps.end()) { 2791 2860 std::ostringstream msg; 2792 msg << "Map not found: " << _blue_ maps[i].first;2861 msg << "Map not found: " << _blue_node_maps[i].first; 2793 2862 throw FormatError(msg.str()); 2794 2863 } … … 2821 2890 throw FormatError("Extra character at the end of line"); 2822 2891 2823 Node n;2892 BlueNode n; 2824 2893 if (!_use_nodes) { 2825 2894 n = _graph.addBlueNode(); 2826 2895 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)); 2828 2897 } else { 2829 2898 if (label_index == -1) 2830 2899 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()) { 2834 2903 std::ostringstream msg; 2835 2904 msg << "Node with label not found: " << tokens[label_index]; … … 2839 2908 } 2840 2909 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]]); 2843 2912 } 2844 2913 … … 2925 2994 Edge e; 2926 2995 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()) { 2932 2999 std::ostringstream msg; 2933 3000 msg << "Item not found: " << source_token; 2934 3001 throw FormatError(msg.str()); 2935 3002 } 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()) { 2945 3007 std::ostringstream msg; 2946 3008 msg << "Item not found: " << target_token; 2947 3009 throw FormatError(msg.str()); 2948 3010 } 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; 2955 3012 2956 3013 // It is checked that source is red and 2957 3014 // 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); 2960 3016 if (label_index != -1) 2961 3017 _edge_index.insert(std::make_pair(tokens[label_index], e));
Note: See TracChangeset
for help on using the changeset viewer.