fork download
  1. #include <boost/fusion/adapted.hpp>
  2. #include <boost/spirit/include/qi.hpp>
  3. #include <boost/iostreams/device/mapped_file.hpp>
  4.  
  5. namespace qi = boost::spirit::qi;
  6.  
  7. #if 1
  8. struct float3
  9. {
  10. float x,y,z;
  11. };
  12.  
  13. BOOST_FUSION_ADAPT_STRUCT(float3, (float, x)(float, y)(float, z))
  14.  
  15. typedef std::vector<float3> data_t;
  16. #else
  17. // there is no speed difference when parsing into a straight container
  18. // (only works with the spirit implementation)
  19. typedef std::vector<float> data_t;
  20. #endif
  21.  
  22. double scan_float(const char* str, size_t& off, size_t len);
  23.  
  24. int main()
  25. {
  26. #if 0
  27. std::cin.unsetf(std::ios::skipws);
  28. std::istreambuf_iterator<char> f_(std::cin), l_;
  29.  
  30. const std::vector<char> v(f_, l_);
  31. auto f = v.data(), l = f+v.size();
  32. #elif 1
  33. boost::iostreams::mapped_file mmap(
  34. "input.txt",
  35. boost::iostreams::mapped_file::readonly);
  36. auto f = mmap.const_data();
  37. auto l = f + mmap.size();
  38. #endif
  39.  
  40. data_t data;
  41. data.reserve(11000000);
  42.  
  43. #if 0
  44. using namespace qi;
  45. bool ok = phrase_parse(f,l,(double_ > double_ > double_) % eol, blank, data);
  46. if (ok)
  47. std::cout << "parse success\n";
  48. else
  49. std::cerr << "parse failed: '" << std::string(f,l) << "'\n";
  50.  
  51. if (f!=l) std::cerr << "trailing unparsed: '" << std::string(f,l) << "'\n";
  52. #elif 0
  53. errno = 0;
  54. char* next = nullptr;
  55. float3 tmp;
  56. while (errno == 0 && f && f<(l-12) ) {
  57. tmp.x = strtod(f, &next); f = next;
  58. tmp.y = strtod(f, &next); f = next;
  59. tmp.z = strtod(f, &next); f = next;
  60. data.push_back(tmp);
  61. }
  62. #elif 0
  63. FILE* file = fopen("input.txt", "r");
  64. if (NULL == file) {
  65. printf("Failed to open 'input.txt'");
  66. return 255;
  67. }
  68. float3 tmp;
  69. do {
  70. int nItemsRead = fscanf(file,"%f %f %f\n", &tmp.x, &tmp.y, &tmp.z);
  71. if (3 != nItemsRead)
  72. break;
  73. data.push_back(tmp);
  74. } while (1);
  75. #else
  76. size_t len = std::distance(f,l);
  77. const char* str = f;
  78. float3 tmp;
  79. for (size_t i=0; i<len;)
  80. {
  81. while (i<len && isspace(str[i])) ++i;
  82. tmp.x = scan_float(str, i, len);
  83. while (i<len && isspace(str[i])) ++i;
  84. tmp.y = scan_float(str, i, len);
  85. while (i<len && isspace(str[i])) ++i;
  86. tmp.z = scan_float(str, i, len);
  87.  
  88. if (i<len) // requires at least trailing newline for now
  89. data.push_back(tmp);
  90. //if (0 == (data.size()%1000)) std::cout << "now at " << data.size() << "/11000000\n";
  91. }
  92. #endif
  93.  
  94. std::cout << "data.size(): " << data.size() << "\n";
  95. }
  96.  
  97. double scan_float(const char* str, size_t& off, size_t len)
  98. {
  99. static const double bases[13] = {
  100. 0.0, 10.0, 100.0, 1000.0, 10000.0,
  101. 100000.0, 1000000.0, 10000000.0, 100000000.0,
  102. 1000000000.0, 10000000000.0, 100000000000.0, 1000000000000.0,
  103. };
  104.  
  105. bool begin = false;
  106. bool fail = false;
  107. bool minus = false;
  108. int pfrac = 0;
  109.  
  110. double dec = 0.0;
  111. double frac = 0.0;
  112.  
  113. for (; !fail && off<len; ++off)
  114. {
  115. char c = str[off];
  116. if (c == '+')
  117. {
  118. if (!begin)
  119. begin = true;
  120. else
  121. fail = true;
  122. }
  123. else if (c == '-')
  124. {
  125. if (!begin)
  126. begin = true;
  127. else
  128. fail = true;
  129. minus = true;
  130. }
  131. else if (c == '.')
  132. {
  133. if (!begin)
  134. begin = true;
  135. else if (pfrac)
  136. fail = true;
  137. pfrac = 1;
  138. }
  139. else if (c >= '0' && c <= '9')
  140. {
  141. if (!begin)
  142. begin = true;
  143. if (pfrac == 0)
  144. {
  145. dec *= 10;
  146. dec += c - '0';
  147. }
  148. else if (pfrac < 13)
  149. {
  150. frac += (c - '0') / bases[pfrac];
  151. ++pfrac;
  152. }
  153. }
  154. else
  155. {
  156. break;
  157. }
  158. }
  159.  
  160. if (!fail)
  161. {
  162. double f = dec + frac;
  163. if (minus)
  164. f = -f;
  165. return f;
  166. }
  167.  
  168. return 0.0;
  169. }
  170.  
  171.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:36: fatal error: boost/fusion/adapted.hpp: No such file or directory
compilation terminated.
stdout
Standard output is empty