]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | //======================================================================= |
2 | // Copyright 1997, 1998, 1999, 2000 University of Notre Dame. | |
3 | // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek | |
4 | // | |
5 | // Distributed under the Boost Software License, Version 1.0. (See | |
6 | // accompanying file LICENSE_1_0.txt or copy at | |
7 | // http://www.boost.org/LICENSE_1_0.txt) | |
8 | //======================================================================= | |
9 | ||
10 | /* | |
11 | Sample data (edge_iterator_constructor.dat): | |
12 | 5 | |
13 | 10 | |
14 | 0 1 | |
15 | 1 2 | |
16 | 1 3 | |
17 | 2 4 | |
18 | 3 4 | |
19 | 1 0 | |
20 | 2 1 | |
21 | 3 1 | |
22 | 4 2 | |
23 | 4 3 | |
24 | ||
25 | Sample output: | |
26 | ||
f67539c2 TL |
27 | 0 --> 1 |
28 | 1 --> 2 3 0 | |
29 | 2 --> 4 1 | |
30 | 3 --> 4 1 | |
31 | 4 --> 2 3 | |
7c673cae FG |
32 | |
33 | */ | |
34 | ||
35 | #include <boost/config.hpp> | |
36 | #include <utility> | |
37 | #include <iostream> | |
38 | #include <fstream> | |
39 | ||
40 | #include <iterator> | |
41 | #include <boost/graph/graph_utility.hpp> | |
42 | #include <boost/graph/adjacency_list.hpp> | |
43 | ||
f67539c2 TL |
44 | class edge_stream_iterator |
45 | { | |
7c673cae | 46 | public: |
f67539c2 TL |
47 | typedef std::input_iterator_tag iterator_category; |
48 | typedef std::pair< int, int > value_type; | |
49 | typedef std::ptrdiff_t difference_type; | |
50 | typedef const value_type* pointer; | |
51 | typedef const value_type& reference; | |
7c673cae | 52 | |
f67539c2 TL |
53 | edge_stream_iterator() : m_stream(0), m_end_marker(false) {} |
54 | edge_stream_iterator(std::istream& s) : m_stream(&s) { m_read(); } | |
55 | reference operator*() const { return m_edge; } | |
56 | edge_stream_iterator& operator++() | |
57 | { | |
58 | m_read(); | |
59 | return *this; | |
60 | } | |
61 | edge_stream_iterator operator++(int) | |
62 | { | |
63 | edge_stream_iterator tmp = *this; | |
64 | m_read(); | |
65 | return tmp; | |
7c673cae | 66 | } |
7c673cae | 67 | |
f67539c2 TL |
68 | protected: |
69 | std::istream* m_stream; | |
70 | value_type m_edge; | |
71 | bool m_end_marker; | |
72 | void m_read() | |
73 | { | |
74 | m_end_marker = (*m_stream) ? true : false; | |
75 | if (m_end_marker) | |
76 | { | |
77 | *m_stream >> m_edge.first >> m_edge.second; | |
78 | } | |
79 | m_end_marker = (*m_stream) ? true : false; | |
80 | } | |
81 | friend bool operator==( | |
82 | const edge_stream_iterator& x, const edge_stream_iterator& y); | |
7c673cae | 83 | }; |
f67539c2 | 84 | bool operator==(const edge_stream_iterator& x, const edge_stream_iterator& y) |
7c673cae | 85 | { |
f67539c2 TL |
86 | return (x.m_stream == y.m_stream && x.m_end_marker == y.m_end_marker) |
87 | || (x.m_end_marker == false && y.m_end_marker == false); | |
7c673cae | 88 | } |
f67539c2 | 89 | bool operator!=(const edge_stream_iterator& x, const edge_stream_iterator& y) |
7c673cae | 90 | { |
f67539c2 | 91 | return !(x == y); |
7c673cae FG |
92 | } |
93 | ||
f67539c2 | 94 | int main(int argc, const char** argv) |
7c673cae | 95 | { |
f67539c2 TL |
96 | typedef boost::adjacency_list<> IteratorConstructibleGraph; |
97 | typedef boost::graph_traits< IteratorConstructibleGraph > Traits; | |
98 | Traits::vertices_size_type size_V; | |
99 | Traits::edges_size_type size_E; | |
7c673cae | 100 | |
f67539c2 TL |
101 | std::ifstream f(argc >= 2 ? argv[1] : "edge_iterator_constructor.dat"); |
102 | f >> size_V >> size_E; | |
7c673cae | 103 | |
f67539c2 | 104 | edge_stream_iterator edge_iter(f), end; |
7c673cae | 105 | #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 |
f67539c2 TL |
106 | // VC++ can't handle the iterator constructor |
107 | IteratorConstructibleGraph G(size_V); | |
108 | while (edge_iter != end) | |
109 | { | |
110 | int i, j; | |
111 | boost::tie(i, j) = *edge_iter++; | |
112 | boost::add_edge(i, j, G); | |
113 | } | |
7c673cae | 114 | #else |
f67539c2 | 115 | IteratorConstructibleGraph G(edge_iter, end, size_V); |
7c673cae | 116 | #endif |
f67539c2 | 117 | boost::print_graph(G); |
7c673cae | 118 | |
f67539c2 | 119 | return 0; |
7c673cae | 120 | } |