file_vector.hpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. //----------------------------------------------------------------------------
  2. /// @file file_vector.hpp
  3. /// @brief This file contains functions for to work with random data and files
  4. /// Have functions for to create a vector with random data, and
  5. /// functions for lo load a vector of numbers or strings from the file
  6. ///
  7. /// @author Copyright (c) 2015 Francisco José Tapia ([email protected] )\n
  8. /// Distributed under the Boost Software License, Version 1.0.\n
  9. /// ( See accompanyingfile LICENSE_1_0.txt or copy at
  10. /// http://www.boost.org/LICENSE_1_0.txt )
  11. /// @version 0.1
  12. ///
  13. /// @remarks
  14. //-----------------------------------------------------------------------------
  15. #ifndef __BOOST_SORT_COMMON_FILE_VECTOR_HPP
  16. #define __BOOST_SORT_COMMON_FILE_VECTOR_HPP
  17. #include <ios>
  18. #include <cstdio>
  19. #include <cstdlib>
  20. #include <ciso646>
  21. #include <vector>
  22. #include <string>
  23. #include <fstream>
  24. #include <sstream>
  25. #include <iostream>
  26. #include <random>
  27. #include <cstdint>
  28. namespace boost
  29. {
  30. namespace sort
  31. {
  32. namespace common
  33. {
  34. //
  35. //-----------------------------------------------------------------------------
  36. // function : generate_file
  37. /// @brief Generate a binary file filed with random numbers of 64 bits
  38. /// @param [in] filename : name of the file
  39. /// @param [in] NElem : number of 64 bits numbers to insert in the file
  40. /// @exception
  41. /// @return
  42. /// @remarks
  43. //-----------------------------------------------------------------------------
  44. inline int generate_file(const std::string & filename, size_t NElem)
  45. { //------------------------------- begin ----------------------------------
  46. std::ofstream ofile;
  47. ofile.open(filename, std::ios_base::out | std::ios_base::binary |
  48. std::ios_base::trunc);
  49. if (ofile.bad())
  50. {
  51. throw std::ios_base::failure("could not open file \n");
  52. };
  53. std::mt19937_64 my_rand(0);
  54. for (size_t i = 0; i < NElem; ++i)
  55. {
  56. uint64_t Aux = my_rand();
  57. ofile.write((char *) &Aux, 8);
  58. }
  59. ofile.close();
  60. return 0;
  61. };
  62. //
  63. //-----------------------------------------------------------------------------
  64. // function : fill_vector_uint64
  65. /// @brief : fill a vector of uint64_t elements from a file
  66. /// @param [in] filename : name of the file
  67. /// @param [in] V : vector to fill
  68. /// @param [in] NElem : number of elements for to read from the file
  69. /// @exception
  70. /// @return
  71. /// @remarks
  72. //-----------------------------------------------------------------------------
  73. inline int fill_vector_uint64(const std::string & filename,
  74. std::vector<uint64_t> & V, size_t NElem)
  75. { //----------------------- begin ------------------------------------------
  76. std::ifstream input(filename, std::ios_base::in | std::ios_base::binary);
  77. if (input.fail())
  78. {
  79. throw std::ios_base::failure("could not open file \n");
  80. };
  81. //------------------------------------------------------------------------
  82. // Calculate the lenght of the file and the number of elements inside
  83. //------------------------------------------------------------------------
  84. input.seekg(0, std::ios_base::end);
  85. size_t length = input.tellg();
  86. size_t uCount = length / 8;
  87. if (uCount < NElem)
  88. {
  89. throw std::ios_base::failure("incorrect length of the file\n");
  90. };
  91. V.clear();
  92. V.reserve(NElem);
  93. uint64_t Aux = 0;
  94. input.seekg(0, std::ios_base::beg);
  95. for (size_t i = 0; i < NElem; ++i)
  96. {
  97. input.read(reinterpret_cast<char *>(&Aux), 8);
  98. V.push_back(Aux);
  99. };
  100. input.close();
  101. return 0;
  102. };
  103. //
  104. //-----------------------------------------------------------------------------
  105. // function :write_file_uint64
  106. /// @brief Write a file with the contnt of a vector of Uint64_t elements
  107. /// @param [in] V : vector from read the numbersl
  108. /// @param [in] filename : name of the file
  109. /// @exception
  110. /// @return
  111. /// @remarks
  112. //-----------------------------------------------------------------------------
  113. inline int write_file_uint64 (const std::vector<uint64_t> & V,
  114. const std::string & filename)
  115. { //--------------------------------- begin --------------------------------
  116. std::ofstream ofile;
  117. ofile.open(filename,
  118. std::ios_base::out | std::ios_base::binary
  119. | std::ios_base::trunc);
  120. if (ofile.bad())
  121. {
  122. throw std::ios_base::failure("could not open file \n");
  123. };
  124. for (size_t i = 0; i < V.size(); ++i)
  125. {
  126. ofile.write((char *) &(V[i]), 8);
  127. }
  128. ofile.close();
  129. return 0;
  130. };
  131. //
  132. //-----------------------------------------------------------------------------
  133. // function : fill_vector_string
  134. /// @brief fill a vector of strings from a file
  135. /// @param [in] filename : name of the file from read the strings
  136. /// @param [in] V : vector where store the strings
  137. /// @param [in] NElem : Number of strings for to read from the file
  138. /// @exception
  139. /// @return
  140. /// @remarks
  141. //-----------------------------------------------------------------------------
  142. inline int fill_vector_string (const std::string & filename,
  143. std::vector<std::string> & V, size_t NElem)
  144. { //----------------------- begin ------------------------------------------
  145. std::ifstream input(filename, std::ios_base::in | std::ios_base::binary);
  146. if (input.fail())
  147. {
  148. throw std::ios_base::failure("could not open file \n");
  149. };
  150. //------------------------------------------------------------------------
  151. // Calculate the lenght of the file and the number of elements inside
  152. //------------------------------------------------------------------------
  153. input.seekg(0, std::ios_base::end);
  154. V.clear();
  155. V.reserve(NElem);
  156. std::string inval;
  157. input.seekg(0, std::ios_base::beg);
  158. for (size_t i = 0; i < NElem; ++i)
  159. {
  160. if (!input.eof())
  161. {
  162. input >> inval;
  163. V.push_back(inval);
  164. inval.clear();
  165. }
  166. else
  167. {
  168. throw std::ios_base::failure("Insuficient lenght of the file\n");
  169. };
  170. };
  171. input.close();
  172. return 0;
  173. };
  174. //
  175. //-----------------------------------------------------------------------------
  176. // function :write_file_string
  177. /// @brief : write a file with the strings of a vector
  178. /// @param [in] V : vector from read the sttrings
  179. /// @param [in] filename : file where store the strings
  180. /// @exception
  181. /// @return
  182. /// @remarks
  183. //-----------------------------------------------------------------------------
  184. inline int write_file_string (const std::vector<std::string> & V,
  185. const std::string & filename)
  186. { //--------------------------------- begin --------------------------------
  187. std::ofstream ofile;
  188. ofile.open(filename,
  189. std::ios_base::out | std::ios_base::binary
  190. | std::ios_base::trunc);
  191. if (ofile.bad())
  192. {
  193. throw std::ios_base::failure("could not open file \n");
  194. };
  195. for (size_t i = 0; i < V.size(); ++i)
  196. {
  197. ofile.write((char *) &(V[i][0]), V[i].size());
  198. ofile.put(0x0);
  199. }
  200. ofile.close();
  201. return 0;
  202. };
  203. //---------------------------------------------------------------------------
  204. /// @struct uint64_file_generator
  205. /// @brief This struct is a number generator from a file, with several options
  206. /// for to limit the numbers between 0 and Max_Val
  207. /// @remarks
  208. //---------------------------------------------------------------------------
  209. struct uint64_file_generator
  210. { //----------------------------------------------------------------------
  211. // VARIABLES
  212. //----------------------------------------------------------------------
  213. std::ifstream input;
  214. size_t NMax, Pos;
  215. size_t Max_Val;
  216. std::string s;
  217. //----------------------------------------------------------------------
  218. // FUNCTIONS
  219. //----------------------------------------------------------------------
  220. uint64_file_generator(const std::string & filename)
  221. { //---------------------------- begin ---------------------------------
  222. s = filename;
  223. input.open(filename, std::ios_base::in | std::ios_base::binary);
  224. if (input.fail() or input.bad())
  225. {
  226. throw std::ios_base::failure("could not open file \n");
  227. };
  228. //--------------------------------------------------------------------
  229. // Calculate the lenght of the file and the number of elements inside
  230. //--------------------------------------------------------------------
  231. input.seekg(0, std::ios_base::end);
  232. size_t length = input.tellg();
  233. NMax = length / 8;
  234. Pos = 0;
  235. Max_Val = ~((size_t) 0);
  236. input.seekg(0);
  237. };
  238. void set_max_val(size_t MV){ Max_Val = MV; };
  239. size_t size() const { return NMax; };
  240. uint64_t get(void)
  241. {
  242. uint64_t Aux;
  243. input.read(reinterpret_cast<char *>(&Aux), 8);
  244. return (Aux % Max_Val);
  245. };
  246. uint64_t operator ( )(){ return get(); };
  247. void reset(void) { input.seekg(0, std::ios_base::beg); };
  248. ~uint64_file_generator() { if (input.is_open()) input.close(); };
  249. };
  250. //
  251. //****************************************************************************
  252. };// end namespace benchmark
  253. };// end namespace sort
  254. };// end namespace boost
  255. //****************************************************************************
  256. //
  257. #endif