| 2117 | template <typename BGR> |
| 2118 | class BpGraphReader; |
| 2119 | |
| 2120 | template <typename TBGR> |
| 2121 | BpGraphReader<TBGR> bpGraphReader(TBGR& graph, std::istream& is = std::cin); |
| 2122 | template <typename TBGR> |
| 2123 | BpGraphReader<TBGR> bpGraphReader(TBGR& graph, const std::string& fn); |
| 2124 | template <typename TBGR> |
| 2125 | BpGraphReader<TBGR> bpGraphReader(TBGR& graph, const char *fn); |
| 2126 | |
| 2127 | /// \ingroup lemon_io |
| 2128 | /// |
| 2129 | /// \brief \ref lgf-format "LGF" reader for bipartite graphs |
| 2130 | /// |
| 2131 | /// This utility reads an \ref lgf-format "LGF" file. |
| 2132 | /// |
| 2133 | /// It can be used almost the same way as \c GraphReader, but it |
| 2134 | /// reads the red and blue nodes from separate sections, and these |
| 2135 | /// sections can contain different set of maps. |
| 2136 | /// |
| 2137 | /// The red and blue maps are read from the corresponding |
| 2138 | /// sections. If a map is defined with the same name in both of |
| 2139 | /// these sections, then it can be read as a node map. |
| 2140 | template <typename BGR> |
| 2141 | class BpGraphReader { |
| 2142 | public: |
| 2143 | |
| 2144 | typedef BGR Graph; |
| 2145 | |
| 2146 | private: |
| 2147 | |
| 2148 | TEMPLATE_BPGRAPH_TYPEDEFS(BGR); |
| 2149 | |
| 2150 | std::istream* _is; |
| 2151 | bool local_is; |
| 2152 | std::string _filename; |
| 2153 | |
| 2154 | BGR& _graph; |
| 2155 | |
| 2156 | std::string _nodes_caption; |
| 2157 | std::string _edges_caption; |
| 2158 | std::string _attributes_caption; |
| 2159 | |
| 2160 | typedef std::map<std::string, Node> NodeIndex; |
| 2161 | NodeIndex _node_index; |
| 2162 | typedef std::map<std::string, Edge> EdgeIndex; |
| 2163 | EdgeIndex _edge_index; |
| 2164 | |
| 2165 | typedef std::vector<std::pair<std::string, |
| 2166 | _reader_bits::MapStorageBase<Node>*> > NodeMaps; |
| 2167 | NodeMaps _red_maps; |
| 2168 | NodeMaps _blue_maps; |
| 2169 | |
| 2170 | typedef std::vector<std::pair<std::string, |
| 2171 | _reader_bits::MapStorageBase<Edge>*> > EdgeMaps; |
| 2172 | EdgeMaps _edge_maps; |
| 2173 | |
| 2174 | typedef std::multimap<std::string, _reader_bits::ValueStorageBase*> |
| 2175 | Attributes; |
| 2176 | Attributes _attributes; |
| 2177 | |
| 2178 | bool _use_nodes; |
| 2179 | bool _use_edges; |
| 2180 | |
| 2181 | bool _skip_nodes; |
| 2182 | bool _skip_edges; |
| 2183 | |
| 2184 | int line_num; |
| 2185 | std::istringstream line; |
| 2186 | |
| 2187 | public: |
| 2188 | |
| 2189 | /// \brief Constructor |
| 2190 | /// |
| 2191 | /// Construct an undirected graph reader, which reads from the given |
| 2192 | /// input stream. |
| 2193 | BpGraphReader(BGR& graph, std::istream& is = std::cin) |
| 2194 | : _is(&is), local_is(false), _graph(graph), |
| 2195 | _use_nodes(false), _use_edges(false), |
| 2196 | _skip_nodes(false), _skip_edges(false) {} |
| 2197 | |
| 2198 | /// \brief Constructor |
| 2199 | /// |
| 2200 | /// Construct an undirected graph reader, which reads from the given |
| 2201 | /// file. |
| 2202 | BpGraphReader(BGR& graph, const std::string& fn) |
| 2203 | : _is(new std::ifstream(fn.c_str())), local_is(true), |
| 2204 | _filename(fn), _graph(graph), |
| 2205 | _use_nodes(false), _use_edges(false), |
| 2206 | _skip_nodes(false), _skip_edges(false) { |
| 2207 | if (!(*_is)) { |
| 2208 | delete _is; |
| 2209 | throw IoError("Cannot open file", fn); |
| 2210 | } |
| 2211 | } |
| 2212 | |
| 2213 | /// \brief Constructor |
| 2214 | /// |
| 2215 | /// Construct an undirected graph reader, which reads from the given |
| 2216 | /// file. |
| 2217 | BpGraphReader(BGR& graph, const char* fn) |
| 2218 | : _is(new std::ifstream(fn)), local_is(true), |
| 2219 | _filename(fn), _graph(graph), |
| 2220 | _use_nodes(false), _use_edges(false), |
| 2221 | _skip_nodes(false), _skip_edges(false) { |
| 2222 | if (!(*_is)) { |
| 2223 | delete _is; |
| 2224 | throw IoError("Cannot open file", fn); |
| 2225 | } |
| 2226 | } |
| 2227 | |
| 2228 | /// \brief Destructor |
| 2229 | ~BpGraphReader() { |
| 2230 | for (typename NodeMaps::iterator it = _red_maps.begin(); |
| 2231 | it != _red_maps.end(); ++it) { |
| 2232 | delete it->second; |
| 2233 | } |
| 2234 | |
| 2235 | for (typename NodeMaps::iterator it = _blue_maps.begin(); |
| 2236 | it != _blue_maps.end(); ++it) { |
| 2237 | delete it->second; |
| 2238 | } |
| 2239 | |
| 2240 | for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
| 2241 | it != _edge_maps.end(); ++it) { |
| 2242 | delete it->second; |
| 2243 | } |
| 2244 | |
| 2245 | for (typename Attributes::iterator it = _attributes.begin(); |
| 2246 | it != _attributes.end(); ++it) { |
| 2247 | delete it->second; |
| 2248 | } |
| 2249 | |
| 2250 | if (local_is) { |
| 2251 | delete _is; |
| 2252 | } |
| 2253 | |
| 2254 | } |
| 2255 | |
| 2256 | private: |
| 2257 | template <typename TBGR> |
| 2258 | friend BpGraphReader<TBGR> bpGraphReader(TBGR& graph, std::istream& is); |
| 2259 | template <typename TBGR> |
| 2260 | friend BpGraphReader<TBGR> bpGraphReader(TBGR& graph, |
| 2261 | const std::string& fn); |
| 2262 | template <typename TBGR> |
| 2263 | friend BpGraphReader<TBGR> bpGraphReader(TBGR& graph, const char *fn); |
| 2264 | |
| 2265 | BpGraphReader(BpGraphReader& other) |
| 2266 | : _is(other._is), local_is(other.local_is), _graph(other._graph), |
| 2267 | _use_nodes(other._use_nodes), _use_edges(other._use_edges), |
| 2268 | _skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) { |
| 2269 | |
| 2270 | other._is = 0; |
| 2271 | other.local_is = false; |
| 2272 | |
| 2273 | _node_index.swap(other._node_index); |
| 2274 | _edge_index.swap(other._edge_index); |
| 2275 | |
| 2276 | _red_maps.swap(other._red_maps); |
| 2277 | _blue_maps.swap(other._blue_maps); |
| 2278 | _edge_maps.swap(other._edge_maps); |
| 2279 | _attributes.swap(other._attributes); |
| 2280 | |
| 2281 | _nodes_caption = other._nodes_caption; |
| 2282 | _edges_caption = other._edges_caption; |
| 2283 | _attributes_caption = other._attributes_caption; |
| 2284 | |
| 2285 | } |
| 2286 | |
| 2287 | BpGraphReader& operator=(const BpGraphReader&); |
| 2288 | |
| 2289 | public: |
| 2290 | |
| 2291 | /// \name Reading Rules |
| 2292 | /// @{ |
| 2293 | |
| 2294 | /// \brief Node map reading rule |
| 2295 | /// |
| 2296 | /// Add a node map reading rule to the reader. |
| 2297 | template <typename Map> |
| 2298 | BpGraphReader& nodeMap(const std::string& caption, Map& map) { |
| 2299 | 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)); |
| 2306 | return *this; |
| 2307 | } |
| 2308 | |
| 2309 | /// \brief Node map reading rule |
| 2310 | /// |
| 2311 | /// Add a node map reading rule with specialized converter to the |
| 2312 | /// reader. |
| 2313 | template <typename Map, typename Converter> |
| 2314 | BpGraphReader& nodeMap(const std::string& caption, Map& map, |
| 2315 | const Converter& converter = Converter()) { |
| 2316 | 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)); |
| 2323 | return *this; |
| 2324 | } |
| 2325 | |
| 2326 | /// Add a red map reading rule to the reader. |
| 2327 | template <typename Map> |
| 2328 | BpGraphReader& redMap(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)); |
| 2333 | return *this; |
| 2334 | } |
| 2335 | |
| 2336 | /// \brief Red map reading rule |
| 2337 | /// |
| 2338 | /// Add a red map reading rule with specialized converter to the |
| 2339 | /// reader. |
| 2340 | template <typename Map, typename Converter> |
| 2341 | BpGraphReader& redMap(const std::string& caption, Map& map, |
| 2342 | 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)); |
| 2347 | return *this; |
| 2348 | } |
| 2349 | |
| 2350 | /// Add a blue map reading rule to the reader. |
| 2351 | template <typename Map> |
| 2352 | BpGraphReader& blueMap(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)); |
| 2357 | return *this; |
| 2358 | } |
| 2359 | |
| 2360 | /// \brief Blue map reading rule |
| 2361 | /// |
| 2362 | /// Add a blue map reading rule with specialized converter to the |
| 2363 | /// reader. |
| 2364 | template <typename Map, typename Converter> |
| 2365 | BpGraphReader& blueMap(const std::string& caption, Map& map, |
| 2366 | 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)); |
| 2371 | return *this; |
| 2372 | } |
| 2373 | |
| 2374 | /// \brief Edge map reading rule |
| 2375 | /// |
| 2376 | /// Add an edge map reading rule to the reader. |
| 2377 | template <typename Map> |
| 2378 | BpGraphReader& edgeMap(const std::string& caption, Map& map) { |
| 2379 | checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>(); |
| 2380 | _reader_bits::MapStorageBase<Edge>* storage = |
| 2381 | new _reader_bits::MapStorage<Edge, Map>(map); |
| 2382 | _edge_maps.push_back(std::make_pair(caption, storage)); |
| 2383 | return *this; |
| 2384 | } |
| 2385 | |
| 2386 | /// \brief Edge map reading rule |
| 2387 | /// |
| 2388 | /// Add an edge map reading rule with specialized converter to the |
| 2389 | /// reader. |
| 2390 | template <typename Map, typename Converter> |
| 2391 | BpGraphReader& edgeMap(const std::string& caption, Map& map, |
| 2392 | const Converter& converter = Converter()) { |
| 2393 | checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>(); |
| 2394 | _reader_bits::MapStorageBase<Edge>* storage = |
| 2395 | new _reader_bits::MapStorage<Edge, Map, Converter>(map, converter); |
| 2396 | _edge_maps.push_back(std::make_pair(caption, storage)); |
| 2397 | return *this; |
| 2398 | } |
| 2399 | |
| 2400 | /// \brief Arc map reading rule |
| 2401 | /// |
| 2402 | /// Add an arc map reading rule to the reader. |
| 2403 | template <typename Map> |
| 2404 | BpGraphReader& arcMap(const std::string& caption, Map& map) { |
| 2405 | checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>(); |
| 2406 | _reader_bits::MapStorageBase<Edge>* forward_storage = |
| 2407 | new _reader_bits::GraphArcMapStorage<Graph, true, Map>(_graph, map); |
| 2408 | _edge_maps.push_back(std::make_pair('+' + caption, forward_storage)); |
| 2409 | _reader_bits::MapStorageBase<Edge>* backward_storage = |
| 2410 | new _reader_bits::GraphArcMapStorage<BGR, false, Map>(_graph, map); |
| 2411 | _edge_maps.push_back(std::make_pair('-' + caption, backward_storage)); |
| 2412 | return *this; |
| 2413 | } |
| 2414 | |
| 2415 | /// \brief Arc map reading rule |
| 2416 | /// |
| 2417 | /// Add an arc map reading rule with specialized converter to the |
| 2418 | /// reader. |
| 2419 | template <typename Map, typename Converter> |
| 2420 | BpGraphReader& arcMap(const std::string& caption, Map& map, |
| 2421 | const Converter& converter = Converter()) { |
| 2422 | checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>(); |
| 2423 | _reader_bits::MapStorageBase<Edge>* forward_storage = |
| 2424 | new _reader_bits::GraphArcMapStorage<BGR, true, Map, Converter> |
| 2425 | (_graph, map, converter); |
| 2426 | _edge_maps.push_back(std::make_pair('+' + caption, forward_storage)); |
| 2427 | _reader_bits::MapStorageBase<Edge>* backward_storage = |
| 2428 | new _reader_bits::GraphArcMapStorage<BGR, false, Map, Converter> |
| 2429 | (_graph, map, converter); |
| 2430 | _edge_maps.push_back(std::make_pair('-' + caption, backward_storage)); |
| 2431 | return *this; |
| 2432 | } |
| 2433 | |
| 2434 | /// \brief Attribute reading rule |
| 2435 | /// |
| 2436 | /// Add an attribute reading rule to the reader. |
| 2437 | template <typename Value> |
| 2438 | BpGraphReader& attribute(const std::string& caption, Value& value) { |
| 2439 | _reader_bits::ValueStorageBase* storage = |
| 2440 | new _reader_bits::ValueStorage<Value>(value); |
| 2441 | _attributes.insert(std::make_pair(caption, storage)); |
| 2442 | return *this; |
| 2443 | } |
| 2444 | |
| 2445 | /// \brief Attribute reading rule |
| 2446 | /// |
| 2447 | /// Add an attribute reading rule with specialized converter to the |
| 2448 | /// reader. |
| 2449 | template <typename Value, typename Converter> |
| 2450 | BpGraphReader& attribute(const std::string& caption, Value& value, |
| 2451 | const Converter& converter = Converter()) { |
| 2452 | _reader_bits::ValueStorageBase* storage = |
| 2453 | new _reader_bits::ValueStorage<Value, Converter>(value, converter); |
| 2454 | _attributes.insert(std::make_pair(caption, storage)); |
| 2455 | return *this; |
| 2456 | } |
| 2457 | |
| 2458 | /// \brief Node reading rule |
| 2459 | /// |
| 2460 | /// Add a node reading rule to reader. |
| 2461 | BpGraphReader& node(const std::string& caption, Node& node) { |
| 2462 | typedef _reader_bits::MapLookUpConverter<Node> Converter; |
| 2463 | Converter converter(_node_index); |
| 2464 | _reader_bits::ValueStorageBase* storage = |
| 2465 | new _reader_bits::ValueStorage<Node, Converter>(node, converter); |
| 2466 | _attributes.insert(std::make_pair(caption, storage)); |
| 2467 | return *this; |
| 2468 | } |
| 2469 | |
| 2470 | /// \brief Edge reading rule |
| 2471 | /// |
| 2472 | /// Add an edge reading rule to reader. |
| 2473 | BpGraphReader& edge(const std::string& caption, Edge& edge) { |
| 2474 | typedef _reader_bits::MapLookUpConverter<Edge> Converter; |
| 2475 | Converter converter(_edge_index); |
| 2476 | _reader_bits::ValueStorageBase* storage = |
| 2477 | new _reader_bits::ValueStorage<Edge, Converter>(edge, converter); |
| 2478 | _attributes.insert(std::make_pair(caption, storage)); |
| 2479 | return *this; |
| 2480 | } |
| 2481 | |
| 2482 | /// \brief Arc reading rule |
| 2483 | /// |
| 2484 | /// Add an arc reading rule to reader. |
| 2485 | BpGraphReader& arc(const std::string& caption, Arc& arc) { |
| 2486 | typedef _reader_bits::GraphArcLookUpConverter<BGR> Converter; |
| 2487 | Converter converter(_graph, _edge_index); |
| 2488 | _reader_bits::ValueStorageBase* storage = |
| 2489 | new _reader_bits::ValueStorage<Arc, Converter>(arc, converter); |
| 2490 | _attributes.insert(std::make_pair(caption, storage)); |
| 2491 | return *this; |
| 2492 | } |
| 2493 | |
| 2494 | /// @} |
| 2495 | |
| 2496 | /// \name Select Section by Name |
| 2497 | /// @{ |
| 2498 | |
| 2499 | /// \brief Set \c \@nodes section to be read |
| 2500 | /// |
| 2501 | /// Set \c \@nodes section to be read. |
| 2502 | BpGraphReader& nodes(const std::string& caption) { |
| 2503 | _nodes_caption = caption; |
| 2504 | return *this; |
| 2505 | } |
| 2506 | |
| 2507 | /// \brief Set \c \@edges section to be read |
| 2508 | /// |
| 2509 | /// Set \c \@edges section to be read. |
| 2510 | BpGraphReader& edges(const std::string& caption) { |
| 2511 | _edges_caption = caption; |
| 2512 | return *this; |
| 2513 | } |
| 2514 | |
| 2515 | /// \brief Set \c \@attributes section to be read |
| 2516 | /// |
| 2517 | /// Set \c \@attributes section to be read. |
| 2518 | BpGraphReader& attributes(const std::string& caption) { |
| 2519 | _attributes_caption = caption; |
| 2520 | return *this; |
| 2521 | } |
| 2522 | |
| 2523 | /// @} |
| 2524 | |
| 2525 | /// \name Using Previously Constructed Node or Edge Set |
| 2526 | /// @{ |
| 2527 | |
| 2528 | /// \brief Use previously constructed node set |
| 2529 | /// |
| 2530 | /// Use previously constructed node set, and specify the node |
| 2531 | /// label map. |
| 2532 | template <typename Map> |
| 2533 | BpGraphReader& useNodes(const Map& map) { |
| 2534 | checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
| 2535 | LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); |
| 2536 | _use_nodes = true; |
| 2537 | _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)); |
| 2540 | } |
| 2541 | return *this; |
| 2542 | } |
| 2543 | |
| 2544 | /// \brief Use previously constructed node set |
| 2545 | /// |
| 2546 | /// Use previously constructed node set, and specify the node |
| 2547 | /// label map and a functor which converts the label map values to |
| 2548 | /// \c std::string. |
| 2549 | template <typename Map, typename Converter> |
| 2550 | BpGraphReader& useNodes(const Map& map, |
| 2551 | const Converter& converter = Converter()) { |
| 2552 | checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
| 2553 | LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); |
| 2554 | _use_nodes = true; |
| 2555 | for (NodeIt n(_graph); n != INVALID; ++n) { |
| 2556 | _node_index.insert(std::make_pair(converter(map[n]), n)); |
| 2557 | } |
| 2558 | return *this; |
| 2559 | } |
| 2560 | |
| 2561 | /// \brief Use previously constructed edge set |
| 2562 | /// |
| 2563 | /// Use previously constructed edge set, and specify the edge |
| 2564 | /// label map. |
| 2565 | template <typename Map> |
| 2566 | BpGraphReader& useEdges(const Map& map) { |
| 2567 | checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>(); |
| 2568 | LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member"); |
| 2569 | _use_edges = true; |
| 2570 | _writer_bits::DefaultConverter<typename Map::Value> converter; |
| 2571 | for (EdgeIt a(_graph); a != INVALID; ++a) { |
| 2572 | _edge_index.insert(std::make_pair(converter(map[a]), a)); |
| 2573 | } |
| 2574 | return *this; |
| 2575 | } |
| 2576 | |
| 2577 | /// \brief Use previously constructed edge set |
| 2578 | /// |
| 2579 | /// Use previously constructed edge set, and specify the edge |
| 2580 | /// label map and a functor which converts the label map values to |
| 2581 | /// \c std::string. |
| 2582 | template <typename Map, typename Converter> |
| 2583 | BpGraphReader& useEdges(const Map& map, |
| 2584 | const Converter& converter = Converter()) { |
| 2585 | checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>(); |
| 2586 | LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member"); |
| 2587 | _use_edges = true; |
| 2588 | for (EdgeIt a(_graph); a != INVALID; ++a) { |
| 2589 | _edge_index.insert(std::make_pair(converter(map[a]), a)); |
| 2590 | } |
| 2591 | return *this; |
| 2592 | } |
| 2593 | |
| 2594 | /// \brief Skip the reading of node section |
| 2595 | /// |
| 2596 | /// Omit the reading of the node section. This implies that each node |
| 2597 | /// map reading rule will be abandoned, and the nodes of the graph |
| 2598 | /// will not be constructed, which usually cause that the edge set |
| 2599 | /// could not be read due to lack of node name |
| 2600 | /// could not be read due to lack of node name resolving. |
| 2601 | /// Therefore \c skipEdges() function should also be used, or |
| 2602 | /// \c useNodes() should be used to specify the label of the nodes. |
| 2603 | BpGraphReader& skipNodes() { |
| 2604 | LEMON_ASSERT(!_skip_nodes, "Skip nodes already set"); |
| 2605 | _skip_nodes = true; |
| 2606 | return *this; |
| 2607 | } |
| 2608 | |
| 2609 | /// \brief Skip the reading of edge section |
| 2610 | /// |
| 2611 | /// Omit the reading of the edge section. This implies that each edge |
| 2612 | /// map reading rule will be abandoned, and the edges of the graph |
| 2613 | /// will not be constructed. |
| 2614 | BpGraphReader& skipEdges() { |
| 2615 | LEMON_ASSERT(!_skip_edges, "Skip edges already set"); |
| 2616 | _skip_edges = true; |
| 2617 | return *this; |
| 2618 | } |
| 2619 | |
| 2620 | /// @} |
| 2621 | |
| 2622 | private: |
| 2623 | |
| 2624 | bool readLine() { |
| 2625 | std::string str; |
| 2626 | while(++line_num, std::getline(*_is, str)) { |
| 2627 | line.clear(); line.str(str); |
| 2628 | char c; |
| 2629 | if (line >> std::ws >> c && c != '#') { |
| 2630 | line.putback(c); |
| 2631 | return true; |
| 2632 | } |
| 2633 | } |
| 2634 | return false; |
| 2635 | } |
| 2636 | |
| 2637 | bool readSuccess() { |
| 2638 | return static_cast<bool>(*_is); |
| 2639 | } |
| 2640 | |
| 2641 | void skipSection() { |
| 2642 | char c; |
| 2643 | while (readSuccess() && line >> c && c != '@') { |
| 2644 | readLine(); |
| 2645 | } |
| 2646 | if (readSuccess()) { |
| 2647 | line.putback(c); |
| 2648 | } |
| 2649 | } |
| 2650 | |
| 2651 | void readRedNodes() { |
| 2652 | |
| 2653 | std::vector<int> map_index(_red_maps.size()); |
| 2654 | int map_num, label_index; |
| 2655 | |
| 2656 | char c; |
| 2657 | if (!readLine() || !(line >> c) || c == '@') { |
| 2658 | if (readSuccess() && line) line.putback(c); |
| 2659 | if (!_red_maps.empty()) |
| 2660 | throw FormatError("Cannot find map names"); |
| 2661 | return; |
| 2662 | } |
| 2663 | line.putback(c); |
| 2664 | |
| 2665 | { |
| 2666 | std::map<std::string, int> maps; |
| 2667 | |
| 2668 | std::string map; |
| 2669 | int index = 0; |
| 2670 | while (_reader_bits::readToken(line, map)) { |
| 2671 | if (maps.find(map) != maps.end()) { |
| 2672 | std::ostringstream msg; |
| 2673 | msg << "Multiple occurence of red map: " << map; |
| 2674 | throw FormatError(msg.str()); |
| 2675 | } |
| 2676 | maps.insert(std::make_pair(map, index)); |
| 2677 | ++index; |
| 2678 | } |
| 2679 | |
| 2680 | for (int i = 0; i < static_cast<int>(_red_maps.size()); ++i) { |
| 2681 | std::map<std::string, int>::iterator jt = |
| 2682 | maps.find(_red_maps[i].first); |
| 2683 | if (jt == maps.end()) { |
| 2684 | std::ostringstream msg; |
| 2685 | msg << "Map not found: " << _red_maps[i].first; |
| 2686 | throw FormatError(msg.str()); |
| 2687 | } |
| 2688 | map_index[i] = jt->second; |
| 2689 | } |
| 2690 | |
| 2691 | { |
| 2692 | std::map<std::string, int>::iterator jt = maps.find("label"); |
| 2693 | if (jt != maps.end()) { |
| 2694 | label_index = jt->second; |
| 2695 | } else { |
| 2696 | label_index = -1; |
| 2697 | } |
| 2698 | } |
| 2699 | map_num = maps.size(); |
| 2700 | } |
| 2701 | |
| 2702 | while (readLine() && line >> c && c != '@') { |
| 2703 | line.putback(c); |
| 2704 | |
| 2705 | std::vector<std::string> tokens(map_num); |
| 2706 | for (int i = 0; i < map_num; ++i) { |
| 2707 | if (!_reader_bits::readToken(line, tokens[i])) { |
| 2708 | std::ostringstream msg; |
| 2709 | msg << "Column not found (" << i + 1 << ")"; |
| 2710 | throw FormatError(msg.str()); |
| 2711 | } |
| 2712 | } |
| 2713 | if (line >> std::ws >> c) |
| 2714 | throw FormatError("Extra character at the end of line"); |
| 2715 | |
| 2716 | Node n; |
| 2717 | if (!_use_nodes) { |
| 2718 | n = _graph.addRedNode(); |
| 2719 | if (label_index != -1) |
| 2720 | _node_index.insert(std::make_pair(tokens[label_index], n)); |
| 2721 | } else { |
| 2722 | if (label_index == -1) |
| 2723 | 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()) { |
| 2727 | std::ostringstream msg; |
| 2728 | msg << "Node with label not found: " << tokens[label_index]; |
| 2729 | throw FormatError(msg.str()); |
| 2730 | } |
| 2731 | n = it->second; |
| 2732 | } |
| 2733 | |
| 2734 | for (int i = 0; i < static_cast<int>(_red_maps.size()); ++i) { |
| 2735 | _red_maps[i].second->set(n, tokens[map_index[i]]); |
| 2736 | } |
| 2737 | |
| 2738 | } |
| 2739 | if (readSuccess()) { |
| 2740 | line.putback(c); |
| 2741 | } |
| 2742 | } |
| 2743 | |
| 2744 | void readBlueNodes() { |
| 2745 | |
| 2746 | std::vector<int> map_index(_blue_maps.size()); |
| 2747 | int map_num, label_index; |
| 2748 | |
| 2749 | char c; |
| 2750 | if (!readLine() || !(line >> c) || c == '@') { |
| 2751 | if (readSuccess() && line) line.putback(c); |
| 2752 | if (!_blue_maps.empty()) |
| 2753 | throw FormatError("Cannot find map names"); |
| 2754 | return; |
| 2755 | } |
| 2756 | line.putback(c); |
| 2757 | |
| 2758 | { |
| 2759 | std::map<std::string, int> maps; |
| 2760 | |
| 2761 | std::string map; |
| 2762 | int index = 0; |
| 2763 | while (_reader_bits::readToken(line, map)) { |
| 2764 | if (maps.find(map) != maps.end()) { |
| 2765 | std::ostringstream msg; |
| 2766 | msg << "Multiple occurence of blue map: " << map; |
| 2767 | throw FormatError(msg.str()); |
| 2768 | } |
| 2769 | maps.insert(std::make_pair(map, index)); |
| 2770 | ++index; |
| 2771 | } |
| 2772 | |
| 2773 | for (int i = 0; i < static_cast<int>(_blue_maps.size()); ++i) { |
| 2774 | std::map<std::string, int>::iterator jt = |
| 2775 | maps.find(_blue_maps[i].first); |
| 2776 | if (jt == maps.end()) { |
| 2777 | std::ostringstream msg; |
| 2778 | msg << "Map not found: " << _blue_maps[i].first; |
| 2779 | throw FormatError(msg.str()); |
| 2780 | } |
| 2781 | map_index[i] = jt->second; |
| 2782 | } |
| 2783 | |
| 2784 | { |
| 2785 | std::map<std::string, int>::iterator jt = maps.find("label"); |
| 2786 | if (jt != maps.end()) { |
| 2787 | label_index = jt->second; |
| 2788 | } else { |
| 2789 | label_index = -1; |
| 2790 | } |
| 2791 | } |
| 2792 | map_num = maps.size(); |
| 2793 | } |
| 2794 | |
| 2795 | while (readLine() && line >> c && c != '@') { |
| 2796 | line.putback(c); |
| 2797 | |
| 2798 | std::vector<std::string> tokens(map_num); |
| 2799 | for (int i = 0; i < map_num; ++i) { |
| 2800 | if (!_reader_bits::readToken(line, tokens[i])) { |
| 2801 | std::ostringstream msg; |
| 2802 | msg << "Column not found (" << i + 1 << ")"; |
| 2803 | throw FormatError(msg.str()); |
| 2804 | } |
| 2805 | } |
| 2806 | if (line >> std::ws >> c) |
| 2807 | throw FormatError("Extra character at the end of line"); |
| 2808 | |
| 2809 | Node n; |
| 2810 | if (!_use_nodes) { |
| 2811 | n = _graph.addBlueNode(); |
| 2812 | if (label_index != -1) |
| 2813 | _node_index.insert(std::make_pair(tokens[label_index], n)); |
| 2814 | } else { |
| 2815 | if (label_index == -1) |
| 2816 | 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()) { |
| 2820 | std::ostringstream msg; |
| 2821 | msg << "Node with label not found: " << tokens[label_index]; |
| 2822 | throw FormatError(msg.str()); |
| 2823 | } |
| 2824 | n = it->second; |
| 2825 | } |
| 2826 | |
| 2827 | for (int i = 0; i < static_cast<int>(_blue_maps.size()); ++i) { |
| 2828 | _blue_maps[i].second->set(n, tokens[map_index[i]]); |
| 2829 | } |
| 2830 | |
| 2831 | } |
| 2832 | if (readSuccess()) { |
| 2833 | line.putback(c); |
| 2834 | } |
| 2835 | } |
| 2836 | |
| 2837 | void readEdges() { |
| 2838 | |
| 2839 | std::vector<int> map_index(_edge_maps.size()); |
| 2840 | int map_num, label_index; |
| 2841 | |
| 2842 | char c; |
| 2843 | if (!readLine() || !(line >> c) || c == '@') { |
| 2844 | if (readSuccess() && line) line.putback(c); |
| 2845 | if (!_edge_maps.empty()) |
| 2846 | throw FormatError("Cannot find map names"); |
| 2847 | return; |
| 2848 | } |
| 2849 | line.putback(c); |
| 2850 | |
| 2851 | { |
| 2852 | std::map<std::string, int> maps; |
| 2853 | |
| 2854 | std::string map; |
| 2855 | int index = 0; |
| 2856 | while (_reader_bits::readToken(line, map)) { |
| 2857 | if (maps.find(map) != maps.end()) { |
| 2858 | std::ostringstream msg; |
| 2859 | msg << "Multiple occurence of edge map: " << map; |
| 2860 | throw FormatError(msg.str()); |
| 2861 | } |
| 2862 | maps.insert(std::make_pair(map, index)); |
| 2863 | ++index; |
| 2864 | } |
| 2865 | |
| 2866 | for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) { |
| 2867 | std::map<std::string, int>::iterator jt = |
| 2868 | maps.find(_edge_maps[i].first); |
| 2869 | if (jt == maps.end()) { |
| 2870 | std::ostringstream msg; |
| 2871 | msg << "Map not found: " << _edge_maps[i].first; |
| 2872 | throw FormatError(msg.str()); |
| 2873 | } |
| 2874 | map_index[i] = jt->second; |
| 2875 | } |
| 2876 | |
| 2877 | { |
| 2878 | std::map<std::string, int>::iterator jt = maps.find("label"); |
| 2879 | if (jt != maps.end()) { |
| 2880 | label_index = jt->second; |
| 2881 | } else { |
| 2882 | label_index = -1; |
| 2883 | } |
| 2884 | } |
| 2885 | map_num = maps.size(); |
| 2886 | } |
| 2887 | |
| 2888 | while (readLine() && line >> c && c != '@') { |
| 2889 | line.putback(c); |
| 2890 | |
| 2891 | std::string source_token; |
| 2892 | std::string target_token; |
| 2893 | |
| 2894 | if (!_reader_bits::readToken(line, source_token)) |
| 2895 | throw FormatError("Red node not found"); |
| 2896 | |
| 2897 | if (!_reader_bits::readToken(line, target_token)) |
| 2898 | throw FormatError("Blue node not found"); |
| 2899 | |
| 2900 | std::vector<std::string> tokens(map_num); |
| 2901 | for (int i = 0; i < map_num; ++i) { |
| 2902 | if (!_reader_bits::readToken(line, tokens[i])) { |
| 2903 | std::ostringstream msg; |
| 2904 | msg << "Column not found (" << i + 1 << ")"; |
| 2905 | throw FormatError(msg.str()); |
| 2906 | } |
| 2907 | } |
| 2908 | if (line >> std::ws >> c) |
| 2909 | throw FormatError("Extra character at the end of line"); |
| 2910 | |
| 2911 | Edge e; |
| 2912 | if (!_use_edges) { |
| 2913 | |
| 2914 | typename NodeIndex::iterator it; |
| 2915 | |
| 2916 | it = _node_index.find(source_token); |
| 2917 | if (it == _node_index.end()) { |
| 2918 | std::ostringstream msg; |
| 2919 | msg << "Item not found: " << source_token; |
| 2920 | throw FormatError(msg.str()); |
| 2921 | } |
| 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()) { |
| 2931 | std::ostringstream msg; |
| 2932 | msg << "Item not found: " << target_token; |
| 2933 | throw FormatError(msg.str()); |
| 2934 | } |
| 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 | } |
| 2941 | |
| 2942 | e = _graph.addEdge(source, target); |
| 2943 | if (label_index != -1) |
| 2944 | _edge_index.insert(std::make_pair(tokens[label_index], e)); |
| 2945 | } else { |
| 2946 | if (label_index == -1) |
| 2947 | throw FormatError("Label map not found"); |
| 2948 | typename std::map<std::string, Edge>::iterator it = |
| 2949 | _edge_index.find(tokens[label_index]); |
| 2950 | if (it == _edge_index.end()) { |
| 2951 | std::ostringstream msg; |
| 2952 | msg << "Edge with label not found: " << tokens[label_index]; |
| 2953 | throw FormatError(msg.str()); |
| 2954 | } |
| 2955 | e = it->second; |
| 2956 | } |
| 2957 | |
| 2958 | for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) { |
| 2959 | _edge_maps[i].second->set(e, tokens[map_index[i]]); |
| 2960 | } |
| 2961 | |
| 2962 | } |
| 2963 | if (readSuccess()) { |
| 2964 | line.putback(c); |
| 2965 | } |
| 2966 | } |
| 2967 | |
| 2968 | void readAttributes() { |
| 2969 | |
| 2970 | std::set<std::string> read_attr; |
| 2971 | |
| 2972 | char c; |
| 2973 | while (readLine() && line >> c && c != '@') { |
| 2974 | line.putback(c); |
| 2975 | |
| 2976 | std::string attr, token; |
| 2977 | if (!_reader_bits::readToken(line, attr)) |
| 2978 | throw FormatError("Attribute name not found"); |
| 2979 | if (!_reader_bits::readToken(line, token)) |
| 2980 | throw FormatError("Attribute value not found"); |
| 2981 | if (line >> c) |
| 2982 | throw FormatError("Extra character at the end of line"); |
| 2983 | |
| 2984 | { |
| 2985 | std::set<std::string>::iterator it = read_attr.find(attr); |
| 2986 | if (it != read_attr.end()) { |
| 2987 | std::ostringstream msg; |
| 2988 | msg << "Multiple occurence of attribute: " << attr; |
| 2989 | throw FormatError(msg.str()); |
| 2990 | } |
| 2991 | read_attr.insert(attr); |
| 2992 | } |
| 2993 | |
| 2994 | { |
| 2995 | typename Attributes::iterator it = _attributes.lower_bound(attr); |
| 2996 | while (it != _attributes.end() && it->first == attr) { |
| 2997 | it->second->set(token); |
| 2998 | ++it; |
| 2999 | } |
| 3000 | } |
| 3001 | |
| 3002 | } |
| 3003 | if (readSuccess()) { |
| 3004 | line.putback(c); |
| 3005 | } |
| 3006 | for (typename Attributes::iterator it = _attributes.begin(); |
| 3007 | it != _attributes.end(); ++it) { |
| 3008 | if (read_attr.find(it->first) == read_attr.end()) { |
| 3009 | std::ostringstream msg; |
| 3010 | msg << "Attribute not found: " << it->first; |
| 3011 | throw FormatError(msg.str()); |
| 3012 | } |
| 3013 | } |
| 3014 | } |
| 3015 | |
| 3016 | public: |
| 3017 | |
| 3018 | /// \name Execution of the Reader |
| 3019 | /// @{ |
| 3020 | |
| 3021 | /// \brief Start the batch processing |
| 3022 | /// |
| 3023 | /// This function starts the batch processing |
| 3024 | void run() { |
| 3025 | |
| 3026 | LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); |
| 3027 | |
| 3028 | bool red_nodes_done = _skip_nodes; |
| 3029 | bool blue_nodes_done = _skip_nodes; |
| 3030 | bool edges_done = _skip_edges; |
| 3031 | bool attributes_done = false; |
| 3032 | |
| 3033 | line_num = 0; |
| 3034 | readLine(); |
| 3035 | skipSection(); |
| 3036 | |
| 3037 | while (readSuccess()) { |
| 3038 | try { |
| 3039 | char c; |
| 3040 | std::string section, caption; |
| 3041 | line >> c; |
| 3042 | _reader_bits::readToken(line, section); |
| 3043 | _reader_bits::readToken(line, caption); |
| 3044 | |
| 3045 | if (line >> c) |
| 3046 | throw FormatError("Extra character at the end of line"); |
| 3047 | |
| 3048 | if (section == "red_nodes" && !red_nodes_done) { |
| 3049 | if (_nodes_caption.empty() || _nodes_caption == caption) { |
| 3050 | readRedNodes(); |
| 3051 | red_nodes_done = true; |
| 3052 | } |
| 3053 | } else if (section == "blue_nodes" && !blue_nodes_done) { |
| 3054 | if (_nodes_caption.empty() || _nodes_caption == caption) { |
| 3055 | readBlueNodes(); |
| 3056 | blue_nodes_done = true; |
| 3057 | } |
| 3058 | } else if ((section == "edges" || section == "arcs") && |
| 3059 | !edges_done) { |
| 3060 | if (_edges_caption.empty() || _edges_caption == caption) { |
| 3061 | readEdges(); |
| 3062 | edges_done = true; |
| 3063 | } |
| 3064 | } else if (section == "attributes" && !attributes_done) { |
| 3065 | if (_attributes_caption.empty() || _attributes_caption == caption) { |
| 3066 | readAttributes(); |
| 3067 | attributes_done = true; |
| 3068 | } |
| 3069 | } else { |
| 3070 | readLine(); |
| 3071 | skipSection(); |
| 3072 | } |
| 3073 | } catch (FormatError& error) { |
| 3074 | error.line(line_num); |
| 3075 | error.file(_filename); |
| 3076 | throw; |
| 3077 | } |
| 3078 | } |
| 3079 | |
| 3080 | if (!red_nodes_done) { |
| 3081 | throw FormatError("Section @red_nodes not found"); |
| 3082 | } |
| 3083 | |
| 3084 | if (!blue_nodes_done) { |
| 3085 | throw FormatError("Section @blue_nodes not found"); |
| 3086 | } |
| 3087 | |
| 3088 | if (!edges_done) { |
| 3089 | throw FormatError("Section @edges not found"); |
| 3090 | } |
| 3091 | |
| 3092 | if (!attributes_done && !_attributes.empty()) { |
| 3093 | throw FormatError("Section @attributes not found"); |
| 3094 | } |
| 3095 | |
| 3096 | } |
| 3097 | |
| 3098 | /// @} |
| 3099 | |
| 3100 | }; |
| 3101 | |
| 3102 | /// \ingroup lemon_io |
| 3103 | /// |
| 3104 | /// \brief Return a \ref BpGraphReader class |
| 3105 | /// |
| 3106 | /// This function just returns a \ref BpGraphReader class. |
| 3107 | /// |
| 3108 | /// With this function a graph can be read from an |
| 3109 | /// \ref lgf-format "LGF" file or input stream with several maps and |
| 3110 | /// attributes. For example, there is bipartite weighted matching problem |
| 3111 | /// on a graph, i.e. a graph with a \e weight map on the edges. This |
| 3112 | /// graph can be read with the following code: |
| 3113 | /// |
| 3114 | ///\code |
| 3115 | ///ListBpGraph graph; |
| 3116 | ///ListBpGraph::EdgeMap<int> weight(graph); |
| 3117 | ///bpGraphReader(graph, std::cin). |
| 3118 | /// edgeMap("weight", weight). |
| 3119 | /// run(); |
| 3120 | ///\endcode |
| 3121 | /// |
| 3122 | /// For a complete documentation, please see the \ref BpGraphReader |
| 3123 | /// class documentation. |
| 3124 | /// \warning Don't forget to put the \ref BpGraphReader::run() "run()" |
| 3125 | /// to the end of the parameter list. |
| 3126 | /// \relates BpGraphReader |
| 3127 | /// \sa bpGraphReader(TBGR& graph, const std::string& fn) |
| 3128 | /// \sa bpGraphReader(TBGR& graph, const char* fn) |
| 3129 | template <typename TBGR> |
| 3130 | BpGraphReader<TBGR> bpGraphReader(TBGR& graph, std::istream& is) { |
| 3131 | BpGraphReader<TBGR> tmp(graph, is); |
| 3132 | return tmp; |
| 3133 | } |
| 3134 | |
| 3135 | /// \brief Return a \ref BpGraphReader class |
| 3136 | /// |
| 3137 | /// This function just returns a \ref BpGraphReader class. |
| 3138 | /// \relates BpGraphReader |
| 3139 | /// \sa bpGraphReader(TBGR& graph, std::istream& is) |
| 3140 | template <typename TBGR> |
| 3141 | BpGraphReader<TBGR> bpGraphReader(TBGR& graph, const std::string& fn) { |
| 3142 | BpGraphReader<TBGR> tmp(graph, fn); |
| 3143 | return tmp; |
| 3144 | } |
| 3145 | |
| 3146 | /// \brief Return a \ref BpGraphReader class |
| 3147 | /// |
| 3148 | /// This function just returns a \ref BpGraphReader class. |
| 3149 | /// \relates BpGraphReader |
| 3150 | /// \sa bpGraphReader(TBGR& graph, std::istream& is) |
| 3151 | template <typename TBGR> |
| 3152 | BpGraphReader<TBGR> bpGraphReader(TBGR& graph, const char* fn) { |
| 3153 | BpGraphReader<TBGR> tmp(graph, fn); |
| 3154 | return tmp; |
| 3155 | } |
| 3156 | |