1 // Copyright 2005 The Trustees of Indiana University.
3 // Use, modification and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 // Authors: Douglas Gregor
9 #ifndef BOOST_GRAPH_METIS_HPP
10 #define BOOST_GRAPH_METIS_HPP
12 #ifdef BOOST_GRAPH_METIS_NO_INLINE
13 # define BOOST_GRAPH_METIS_INLINE_KEYWORD
15 # define BOOST_GRAPH_METIS_INLINE_KEYWORD inline
27 namespace boost { namespace graph {
29 class metis_exception : public std::exception {};
30 class metis_input_exception : public metis_exception {};
35 typedef std::size_t vertices_size_type;
36 typedef std::size_t edges_size_type;
37 typedef double vertex_weight_type;
38 typedef double edge_weight_type;
43 typedef std::input_iterator_tag iterator_category;
44 typedef std::pair<vertices_size_type, vertices_size_type> value_type;
45 typedef const value_type& reference;
46 typedef const value_type* pointer;
47 typedef std::ptrdiff_t difference_type;
50 class postincrement_proxy
52 postincrement_proxy(const value_type& value) : value(value) { }
55 reference operator*() const { return value; }
59 friend class edge_iterator;
63 edge_iterator& operator++();
64 postincrement_proxy operator++(int);
66 reference operator*() const { return self->edge; }
67 pointer operator->() const { return &self->edge; }
69 // Default copy constructor and assignment operator are okay
72 edge_iterator(metis_reader* self);
73 void advance(bool skip_initial_read);
77 friend class metis_reader;
78 friend bool operator==(edge_iterator, edge_iterator);
79 friend bool operator!=(edge_iterator, edge_iterator);
81 friend class edge_iterator;
83 class edge_weight_iterator
86 typedef std::input_iterator_tag iterator_category;
87 typedef edge_weight_type value_type;
88 typedef const value_type& reference;
89 typedef const value_type* pointer;
91 // Default copy constructor and assignment operator are okay
93 reference operator*() const { return self->edge_weight; }
94 pointer operator->() const { return &self->edge_weight; }
96 edge_weight_iterator& operator++() { return *this; }
97 edge_weight_iterator operator++(int) { return *this; }
100 edge_weight_iterator(metis_reader* self) : self(self) { }
104 friend class metis_reader;
107 metis_reader(std::istream& in) : in(in), edge_weight(1.0) { start(); }
109 edge_iterator begin();
111 edge_weight_iterator weight_begin();
113 vertices_size_type num_vertices() const { return n_vertices; }
114 edges_size_type num_edges() const { return n_edges; }
116 std::size_t num_vertex_weights() const { return n_vertex_weights; }
118 vertex_weight_type vertex_weight(vertices_size_type v, std::size_t n)
119 { return vertex_weights[v*num_vertex_weights() + n]; }
121 bool has_edge_weights() const { return edge_weights; }
126 // Configuration information
129 // Information about the current METIS file
130 vertices_size_type n_vertices;
131 edges_size_type n_edges;
132 std::size_t n_vertex_weights;
135 // Information about the current edge/vertex
136 std::istringstream line_in;
137 std::pair<vertices_size_type, vertices_size_type> edge;
138 std::vector<vertex_weight_type> vertex_weights;
139 edge_weight_type edge_weight;
141 friend bool operator==(edge_iterator, edge_iterator);
142 friend bool operator!=(edge_iterator, edge_iterator);
145 class metis_distribution
148 typedef int process_id_type;
149 typedef std::size_t size_type;
150 typedef std::vector<process_id_type>::iterator iterator;
152 metis_distribution(std::istream& in, process_id_type my_id);
154 size_type block_size(process_id_type id, size_type) const;
155 process_id_type operator()(size_type n) const { return vertices[n]; }
156 size_type local(size_type n) const;
157 size_type global(size_type n) const { return global(my_id, n); }
158 size_type global(process_id_type id, size_type n) const;
160 iterator begin() { return vertices.begin(); }
161 iterator end() { return vertices.end(); }
164 process_id_type my_id;
165 std::vector<process_id_type> vertices;
168 #if !defined(BOOST_GRAPH_METIS_NO_INLINE) || defined(BOOST_GRAPH_METIS_SOURCE)
169 BOOST_GRAPH_METIS_INLINE_KEYWORD
170 bool operator==(metis_reader::edge_iterator x, metis_reader::edge_iterator y)
172 return (x.self == y.self
173 || (x.self && x.self->edge.first == x.self->num_vertices())
174 || (y.self && y.self->edge.first == y.self->num_vertices()));
177 BOOST_GRAPH_METIS_INLINE_KEYWORD
178 bool operator!=(metis_reader::edge_iterator x, metis_reader::edge_iterator y)
183 BOOST_GRAPH_METIS_INLINE_KEYWORD
184 metis_reader::edge_iterator::edge_iterator(metis_reader* self)
187 if (self) advance(true);
190 BOOST_GRAPH_METIS_INLINE_KEYWORD
191 metis_reader::edge_iterator& metis_reader::edge_iterator::operator++()
197 BOOST_GRAPH_METIS_INLINE_KEYWORD
198 void metis_reader::edge_iterator::advance(bool skip_initial_read)
202 if (!skip_initial_read) {
203 // Try to read the next edge
204 if (self->line_in >> std::ws >> self->edge.second) {
206 if (self->has_edge_weights()) {
207 if (!(self->line_in >> self->edge_weight))
208 boost::throw_exception(metis_input_exception());
213 // Check if we're done
215 if (self->edge.first == self->num_vertices())
219 // Find the next line
221 while (getline(self->in, line) && !line.empty() && line[0] == '%') {
222 /* Keep reading lines in the loop header... */
224 if (!self->in) boost::throw_exception(metis_input_exception());
225 self->line_in.str(line);
226 self->line_in.clear();
228 // Read the next line
229 std::size_t weights_left = self->n_vertex_weights;
230 vertex_weight_type weight;
231 while (weights_left > 0) {
232 if (self->line_in >> weight) self->vertex_weights.push_back(weight);
233 else boost::throw_exception(metis_input_exception());
237 // Successive iterations will pick up edges for this vertex.
238 skip_initial_read = false;
242 BOOST_GRAPH_METIS_INLINE_KEYWORD
243 metis_reader::edge_iterator::postincrement_proxy
244 metis_reader::edge_iterator::operator++(int)
246 postincrement_proxy result(**this);
251 BOOST_GRAPH_METIS_INLINE_KEYWORD
252 metis_reader::edge_iterator metis_reader::begin()
254 if (edge.first != 0) start();
255 return edge_iterator(this);
258 BOOST_GRAPH_METIS_INLINE_KEYWORD
259 metis_reader::edge_iterator metis_reader::end()
261 return edge_iterator(0);
264 BOOST_GRAPH_METIS_INLINE_KEYWORD
265 metis_reader::edge_weight_iterator metis_reader::weight_begin()
267 return edge_weight_iterator(this);
270 BOOST_GRAPH_METIS_INLINE_KEYWORD
271 void metis_reader::start()
273 in.seekg(0, std::ios::beg);
275 while (getline(in, line) && !line.empty() && line[0] == '%') {
276 /* Keep getting lines in loop header. */
279 if (!in || line.empty()) boost::throw_exception(metis_input_exception());
281 // Determine number of vertices and edges in the graph
283 if (!(line_in >> n_vertices >> n_edges)) boost::throw_exception(metis_input_exception());
285 // Determine whether vertex or edge weights are included in the graph
288 n_vertex_weights = fmt / 10;
289 edge_weights = (fmt % 10 == 1);
291 // Determine how many (if any!) vertex weights are included
292 if (n_vertex_weights) line_in >> n_vertex_weights;
294 // Setup the iteration data structures
298 vertex_weights.reserve(n_vertex_weights * num_vertices());
301 metis_distribution::metis_distribution(std::istream& in, process_id_type my_id)
303 vertices(std::istream_iterator<process_id_type>(in),
304 std::istream_iterator<process_id_type>())
309 metis_distribution::size_type
310 metis_distribution::block_size(process_id_type id, size_type) const
312 return std::count(vertices.begin(), vertices.end(), id);
315 metis_distribution::size_type metis_distribution::local(size_type n) const
317 return std::count(vertices.begin(), vertices.begin() + n, vertices[n]);
320 metis_distribution::size_type
321 metis_distribution::global(process_id_type id, size_type n) const
323 std::vector<process_id_type>::const_iterator i = vertices.begin();
324 while (*i != id) ++i;
327 do { ++i; } while (*i != id);
331 return i - vertices.begin();
336 } } // end namespace boost::graph
338 #endif // BOOST_GRAPH_METIS_HPP