dimacs.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 { namespace graph {
  19. class BOOST_SYMBOL_VISIBLE dimacs_exception : public std::exception {};
  20. class dimacs_basic_reader {
  21. public:
  22. typedef std::size_t vertices_size_type;
  23. typedef std::size_t edges_size_type;
  24. typedef double vertex_weight_type;
  25. typedef double edge_weight_type;
  26. typedef std::pair<vertices_size_type,
  27. vertices_size_type> edge_type;
  28. enum incr_mode {edge, edge_weight};
  29. dimacs_basic_reader( std::istream& in, bool want_weights = true ) :
  30. inpt( in ), seen_edges( 0 ), want_weights(want_weights)
  31. {
  32. while( getline( inpt, buf ) && !buf.empty() && buf[0] == 'c' );
  33. if( buf[0] != 'p' ) {
  34. boost::throw_exception(dimacs_exception());
  35. }
  36. std::stringstream instr( buf );
  37. std::string junk;
  38. instr >> junk >> junk >> num_vertices >> num_edges;
  39. read_edge_weights.push( -1 );
  40. incr( edge_weight );
  41. }
  42. //for a past the end iterator
  43. dimacs_basic_reader() : inpt( std::cin ), num_vertices( 0 ),
  44. num_edges( 0 ), seen_edges( 0 ), want_weights(false) {}
  45. edge_type edge_deref() {
  46. BOOST_ASSERT( !read_edges.empty() );
  47. return read_edges.front();
  48. }
  49. inline edge_type* edge_ref() {
  50. BOOST_ASSERT( !read_edges.empty() );
  51. return &read_edges.front();
  52. }
  53. inline edge_weight_type edge_weight_deref() {
  54. BOOST_ASSERT( !read_edge_weights.empty() );
  55. return read_edge_weights.front();
  56. }
  57. inline dimacs_basic_reader incr( incr_mode mode ) {
  58. if( mode == edge ) {
  59. BOOST_ASSERT( !read_edges.empty() );
  60. read_edges.pop();
  61. }
  62. else if( mode == edge_weight ) {
  63. BOOST_ASSERT( !read_edge_weights.empty() );
  64. read_edge_weights.pop();
  65. }
  66. if( (mode == edge && read_edges.empty()) ||
  67. (mode == edge_weight && read_edge_weights.empty() )) {
  68. if( seen_edges > num_edges ) {
  69. boost::throw_exception(dimacs_exception());
  70. }
  71. while( getline( inpt, buf ) && !buf.empty() && buf[0] == 'c' );
  72. if( !inpt.eof() ) {
  73. int source, dest, weight;
  74. read_edge_line((char*) buf.c_str(), source, dest, weight);
  75. seen_edges++;
  76. source--;
  77. dest--;
  78. read_edges.push( edge_type( source, dest ) );
  79. if (want_weights) {
  80. read_edge_weights.push( weight );
  81. }
  82. }
  83. BOOST_ASSERT( read_edges.size() < 100 );
  84. BOOST_ASSERT( read_edge_weights.size() < 100 );
  85. }
  86. // the 1000000 just happens to be about how many edges can be read in
  87. // 10s
  88. // if( !(seen_edges % 1000000) && !process_id( pg ) && mode == edge ) {
  89. // std::cout << "read " << seen_edges << " edges" << std::endl;
  90. // }
  91. return *this;
  92. }
  93. inline bool done_edges() {
  94. return inpt.eof() && read_edges.size() == 0;
  95. }
  96. inline bool done_edge_weights() {
  97. return inpt.eof() && read_edge_weights.size() == 0;
  98. }
  99. inline vertices_size_type n_vertices() {
  100. return num_vertices;
  101. }
  102. inline vertices_size_type processed_edges() {
  103. return seen_edges - read_edges.size();
  104. }
  105. inline vertices_size_type processed_edge_weights() {
  106. return seen_edges - read_edge_weights.size();
  107. }
  108. inline vertices_size_type n_edges() {
  109. return num_edges;
  110. }
  111. protected:
  112. bool read_edge_line(char *linebuf, int &from, int &to, int &weight)
  113. {
  114. char *fs = NULL, *ts = NULL, *ws = NULL;
  115. char *tmp = linebuf + 2;
  116. fs = tmp;
  117. if ('e' == linebuf[0]) {
  118. while (*tmp != '\n' && *tmp != '\0') {
  119. if (*tmp == ' ') {
  120. *tmp = '\0';
  121. ts = ++tmp;
  122. break;
  123. }
  124. tmp++;
  125. }
  126. *tmp = '\0';
  127. if (NULL == fs || NULL == ts) return false;
  128. from = atoi(fs); to = atoi(ts); weight = 0;
  129. } else if ('a' == linebuf[0]) {
  130. while (*tmp != '\n' && *tmp != '\0') {
  131. if (*tmp == ' ') {
  132. *tmp = '\0';
  133. ts = ++tmp;
  134. break;
  135. }
  136. tmp++;
  137. }
  138. while (*tmp != '\n' && *tmp != '\0') {
  139. if (*tmp == ' ') {
  140. *tmp = '\0';
  141. ws = ++tmp;
  142. break;
  143. }
  144. tmp++;
  145. }
  146. while (*tmp != '\n' && *tmp != '\0') tmp++;
  147. *tmp = '\0';
  148. if (fs == NULL || ts == NULL || ws == NULL) return false;
  149. from = atoi(fs); to = atoi(ts) ;
  150. if (want_weights) weight = atoi(ws); else weight = 0;
  151. } else {
  152. return false;
  153. }
  154. return true;
  155. }
  156. std::queue<edge_type> read_edges;
  157. std::queue<edge_weight_type> read_edge_weights;
  158. std::istream& inpt;
  159. std::string buf;
  160. vertices_size_type num_vertices, num_edges, seen_edges;
  161. bool want_weights;
  162. };
  163. template<typename T>
  164. class dimacs_edge_iterator {
  165. public:
  166. typedef dimacs_basic_reader::edge_type edge_type;
  167. typedef dimacs_basic_reader::incr_mode incr_mode;
  168. typedef std::input_iterator_tag iterator_category;
  169. typedef edge_type value_type;
  170. typedef value_type reference;
  171. typedef edge_type* pointer;
  172. typedef std::ptrdiff_t difference_type;
  173. dimacs_edge_iterator( T& reader ) :
  174. reader( reader ) {}
  175. inline dimacs_edge_iterator& operator++() {
  176. reader.incr( dimacs_basic_reader::edge );
  177. return *this;
  178. }
  179. inline edge_type operator*() {
  180. return reader.edge_deref();
  181. }
  182. inline edge_type* operator->() {
  183. return reader.edge_ref();
  184. }
  185. // don't expect this to do the right thing if you're not comparing against a
  186. // general past-the-end-iterator made with the default constructor for
  187. // dimacs_basic_reader
  188. inline bool operator==( dimacs_edge_iterator arg ) {
  189. if( reader.n_vertices() == 0 ) {
  190. return arg.reader.done_edges();
  191. }
  192. else if( arg.reader.n_vertices() == 0 ) {
  193. return reader.done_edges();
  194. }
  195. else {
  196. return false;
  197. }
  198. return false;
  199. }
  200. inline bool operator!=( dimacs_edge_iterator arg ) {
  201. if( reader.n_vertices() == 0 ) {
  202. return !arg.reader.done_edges();
  203. }
  204. else if( arg.reader.n_vertices() == 0 ) {
  205. return !reader.done_edges();
  206. }
  207. else {
  208. return true;
  209. }
  210. return true;
  211. }
  212. private:
  213. T& reader;
  214. };
  215. template<typename T>
  216. class dimacs_edge_weight_iterator {
  217. public:
  218. typedef dimacs_basic_reader::edge_weight_type edge_weight_type;
  219. typedef dimacs_basic_reader::incr_mode incr_mode;
  220. dimacs_edge_weight_iterator( T& reader ) : reader( reader ) {}
  221. inline dimacs_edge_weight_iterator& operator++() {
  222. reader.incr( dimacs_basic_reader::edge_weight );
  223. return *this;
  224. }
  225. inline edge_weight_type operator*() {
  226. return reader.edge_weight_deref();
  227. }
  228. // don't expect this to do the right thing if you're not comparing against a
  229. // general past-the-end-iterator made with the default constructor for
  230. // dimacs_basic_reader
  231. inline bool operator==( dimacs_edge_weight_iterator arg ) {
  232. if( reader.n_vertices() == 0 ) {
  233. return arg.reader.done_edge_weights();
  234. }
  235. else if( arg.reader.n_vertices() == 0 ) {
  236. return reader.done_edge_weights();
  237. }
  238. else {
  239. return false;
  240. }
  241. return false;
  242. }
  243. inline bool operator!=( dimacs_edge_weight_iterator arg ) {
  244. if( reader.n_vertices() == 0 ) {
  245. return !arg.reader.done_edge_weights();
  246. }
  247. else if( arg.reader.n_vertices() == 0 ) {
  248. return !reader.done_edge_weights();
  249. }
  250. else {
  251. return true;
  252. }
  253. return true;
  254. }
  255. private:
  256. T& reader;
  257. };
  258. } } // end namespace boost::graph
  259. #endif