dimacs.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. // Copyright 2005 The Trustees of Indiana University.
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Authors: Alex Breuer
  6. // Andrew Lumsdaine
  7. #ifndef BOOST_GRAPH_DIMACS_HPP
  8. #define BOOST_GRAPH_DIMACS_HPP
  9. #include <string>
  10. #include <sstream>
  11. #include <iostream>
  12. #include <fstream>
  13. #include <iterator>
  14. #include <exception>
  15. #include <vector>
  16. #include <queue>
  17. #include <boost/assert.hpp>
  18. namespace boost
  19. {
  20. namespace graph
  21. {
  22. class BOOST_SYMBOL_VISIBLE dimacs_exception : public std::exception
  23. {
  24. };
  25. class dimacs_basic_reader
  26. {
  27. public:
  28. typedef std::size_t vertices_size_type;
  29. typedef std::size_t edges_size_type;
  30. typedef double vertex_weight_type;
  31. typedef double edge_weight_type;
  32. typedef std::pair< vertices_size_type, vertices_size_type > edge_type;
  33. enum incr_mode
  34. {
  35. edge,
  36. edge_weight
  37. };
  38. dimacs_basic_reader(std::istream& in, bool want_weights = true)
  39. : inpt(in), seen_edges(0), want_weights(want_weights)
  40. {
  41. while (getline(inpt, buf) && !buf.empty() && buf[0] == 'c')
  42. ;
  43. if (buf[0] != 'p')
  44. {
  45. boost::throw_exception(dimacs_exception());
  46. }
  47. std::stringstream instr(buf);
  48. std::string junk;
  49. instr >> junk >> junk >> num_vertices >> num_edges;
  50. read_edge_weights.push(-1);
  51. incr(edge_weight);
  52. }
  53. // for a past the end iterator
  54. dimacs_basic_reader()
  55. : inpt(std::cin)
  56. , num_vertices(0)
  57. , num_edges(0)
  58. , seen_edges(0)
  59. , want_weights(false)
  60. {
  61. }
  62. edge_type edge_deref()
  63. {
  64. BOOST_ASSERT(!read_edges.empty());
  65. return read_edges.front();
  66. }
  67. inline edge_type* edge_ref()
  68. {
  69. BOOST_ASSERT(!read_edges.empty());
  70. return &read_edges.front();
  71. }
  72. inline edge_weight_type edge_weight_deref()
  73. {
  74. BOOST_ASSERT(!read_edge_weights.empty());
  75. return read_edge_weights.front();
  76. }
  77. inline dimacs_basic_reader incr(incr_mode mode)
  78. {
  79. if (mode == edge)
  80. {
  81. BOOST_ASSERT(!read_edges.empty());
  82. read_edges.pop();
  83. }
  84. else if (mode == edge_weight)
  85. {
  86. BOOST_ASSERT(!read_edge_weights.empty());
  87. read_edge_weights.pop();
  88. }
  89. if ((mode == edge && read_edges.empty())
  90. || (mode == edge_weight && read_edge_weights.empty()))
  91. {
  92. if (seen_edges > num_edges)
  93. {
  94. boost::throw_exception(dimacs_exception());
  95. }
  96. while (getline(inpt, buf) && !buf.empty() && buf[0] == 'c')
  97. ;
  98. if (!inpt.eof())
  99. {
  100. int source, dest, weight;
  101. read_edge_line((char*)buf.c_str(), source, dest, weight);
  102. seen_edges++;
  103. source--;
  104. dest--;
  105. read_edges.push(edge_type(source, dest));
  106. if (want_weights)
  107. {
  108. read_edge_weights.push(weight);
  109. }
  110. }
  111. BOOST_ASSERT(read_edges.size() < 100);
  112. BOOST_ASSERT(read_edge_weights.size() < 100);
  113. }
  114. // the 1000000 just happens to be about how many edges can be read
  115. // in 10s
  116. // if( !(seen_edges % 1000000) && !process_id( pg ) && mode ==
  117. // edge ) {
  118. // std::cout << "read " << seen_edges << " edges" <<
  119. // std::endl;
  120. // }
  121. return *this;
  122. }
  123. inline bool done_edges()
  124. {
  125. return inpt.eof() && read_edges.size() == 0;
  126. }
  127. inline bool done_edge_weights()
  128. {
  129. return inpt.eof() && read_edge_weights.size() == 0;
  130. }
  131. inline vertices_size_type n_vertices() { return num_vertices; }
  132. inline vertices_size_type processed_edges()
  133. {
  134. return seen_edges - read_edges.size();
  135. }
  136. inline vertices_size_type processed_edge_weights()
  137. {
  138. return seen_edges - read_edge_weights.size();
  139. }
  140. inline vertices_size_type n_edges() { return num_edges; }
  141. protected:
  142. bool read_edge_line(char* linebuf, int& from, int& to, int& weight)
  143. {
  144. char *fs = NULL, *ts = NULL, *ws = NULL;
  145. char* tmp = linebuf + 2;
  146. fs = tmp;
  147. if ('e' == linebuf[0])
  148. {
  149. while (*tmp != '\n' && *tmp != '\0')
  150. {
  151. if (*tmp == ' ')
  152. {
  153. *tmp = '\0';
  154. ts = ++tmp;
  155. break;
  156. }
  157. tmp++;
  158. }
  159. *tmp = '\0';
  160. if (NULL == fs || NULL == ts)
  161. return false;
  162. from = atoi(fs);
  163. to = atoi(ts);
  164. weight = 0;
  165. }
  166. else if ('a' == linebuf[0])
  167. {
  168. while (*tmp != '\n' && *tmp != '\0')
  169. {
  170. if (*tmp == ' ')
  171. {
  172. *tmp = '\0';
  173. ts = ++tmp;
  174. break;
  175. }
  176. tmp++;
  177. }
  178. while (*tmp != '\n' && *tmp != '\0')
  179. {
  180. if (*tmp == ' ')
  181. {
  182. *tmp = '\0';
  183. ws = ++tmp;
  184. break;
  185. }
  186. tmp++;
  187. }
  188. while (*tmp != '\n' && *tmp != '\0')
  189. tmp++;
  190. *tmp = '\0';
  191. if (fs == NULL || ts == NULL || ws == NULL)
  192. return false;
  193. from = atoi(fs);
  194. to = atoi(ts);
  195. if (want_weights)
  196. weight = atoi(ws);
  197. else
  198. weight = 0;
  199. }
  200. else
  201. {
  202. return false;
  203. }
  204. return true;
  205. }
  206. std::queue< edge_type > read_edges;
  207. std::queue< edge_weight_type > read_edge_weights;
  208. std::istream& inpt;
  209. std::string buf;
  210. vertices_size_type num_vertices, num_edges, seen_edges;
  211. bool want_weights;
  212. };
  213. template < typename T > class dimacs_edge_iterator
  214. {
  215. public:
  216. typedef dimacs_basic_reader::edge_type edge_type;
  217. typedef dimacs_basic_reader::incr_mode incr_mode;
  218. typedef std::input_iterator_tag iterator_category;
  219. typedef edge_type value_type;
  220. typedef value_type reference;
  221. typedef edge_type* pointer;
  222. typedef std::ptrdiff_t difference_type;
  223. dimacs_edge_iterator(T& reader) : reader(reader) {}
  224. inline dimacs_edge_iterator& operator++()
  225. {
  226. reader.incr(dimacs_basic_reader::edge);
  227. return *this;
  228. }
  229. inline edge_type operator*() { return reader.edge_deref(); }
  230. inline edge_type* operator->() { return reader.edge_ref(); }
  231. // don't expect this to do the right thing if you're not comparing
  232. // against a general past-the-end-iterator made with the default
  233. // constructor for dimacs_basic_reader
  234. inline bool operator==(dimacs_edge_iterator arg)
  235. {
  236. if (reader.n_vertices() == 0)
  237. {
  238. return arg.reader.done_edges();
  239. }
  240. else if (arg.reader.n_vertices() == 0)
  241. {
  242. return reader.done_edges();
  243. }
  244. else
  245. {
  246. return false;
  247. }
  248. return false;
  249. }
  250. inline bool operator!=(dimacs_edge_iterator arg)
  251. {
  252. if (reader.n_vertices() == 0)
  253. {
  254. return !arg.reader.done_edges();
  255. }
  256. else if (arg.reader.n_vertices() == 0)
  257. {
  258. return !reader.done_edges();
  259. }
  260. else
  261. {
  262. return true;
  263. }
  264. return true;
  265. }
  266. private:
  267. T& reader;
  268. };
  269. template < typename T > class dimacs_edge_weight_iterator
  270. {
  271. public:
  272. typedef dimacs_basic_reader::edge_weight_type edge_weight_type;
  273. typedef dimacs_basic_reader::incr_mode incr_mode;
  274. dimacs_edge_weight_iterator(T& reader) : reader(reader) {}
  275. inline dimacs_edge_weight_iterator& operator++()
  276. {
  277. reader.incr(dimacs_basic_reader::edge_weight);
  278. return *this;
  279. }
  280. inline edge_weight_type operator*()
  281. {
  282. return reader.edge_weight_deref();
  283. }
  284. // don't expect this to do the right thing if you're not comparing
  285. // against a general past-the-end-iterator made with the default
  286. // constructor for dimacs_basic_reader
  287. inline bool operator==(dimacs_edge_weight_iterator arg)
  288. {
  289. if (reader.n_vertices() == 0)
  290. {
  291. return arg.reader.done_edge_weights();
  292. }
  293. else if (arg.reader.n_vertices() == 0)
  294. {
  295. return reader.done_edge_weights();
  296. }
  297. else
  298. {
  299. return false;
  300. }
  301. return false;
  302. }
  303. inline bool operator!=(dimacs_edge_weight_iterator arg)
  304. {
  305. if (reader.n_vertices() == 0)
  306. {
  307. return !arg.reader.done_edge_weights();
  308. }
  309. else if (arg.reader.n_vertices() == 0)
  310. {
  311. return !reader.done_edge_weights();
  312. }
  313. else
  314. {
  315. return true;
  316. }
  317. return true;
  318. }
  319. private:
  320. T& reader;
  321. };
  322. }
  323. } // end namespace boost::graph
  324. #endif