Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:51:46

0001 //----------------------------------------------------------------------------
0002 /// @file file_vector.hpp
0003 /// @brief This file contains functions for to work with random data and files
0004 ///        Have functions for to create a vector with random data, and
0005 ///        functions for lo load a vector of numbers or strings from the file
0006 ///
0007 /// @author Copyright (c) 2015 Francisco José Tapia (fjtapia@gmail.com )\n
0008 ///         Distributed under the Boost Software License, Version 1.0.\n
0009 ///         ( See accompanyingfile LICENSE_1_0.txt or copy at
0010 ///           http://www.boost.org/LICENSE_1_0.txt  )
0011 /// @version 0.1
0012 ///
0013 /// @remarks
0014 //-----------------------------------------------------------------------------
0015 #ifndef __BOOST_SORT_COMMON_FILE_VECTOR_HPP
0016 #define __BOOST_SORT_COMMON_FILE_VECTOR_HPP
0017 
0018 #include <ios>
0019 #include <cstdio>
0020 #include <cstdlib>
0021 #include <ciso646>
0022 #include <vector>
0023 #include <string>
0024 #include <fstream>
0025 #include <sstream>
0026 #include <iostream>
0027 #include <random>
0028 #include <cstdint>
0029 
0030 namespace boost
0031 {
0032 namespace sort
0033 {
0034 namespace common
0035 {
0036 //
0037 //-----------------------------------------------------------------------------
0038 //  function : generate_file
0039 /// @brief Generate a binary file filed with random numbers of 64 bits
0040 /// @param [in] filename : name of the file
0041 /// @param [in] NElem : number of 64 bits numbers to insert in the file
0042 /// @exception
0043 /// @return
0044 /// @remarks
0045 //-----------------------------------------------------------------------------
0046 inline int generate_file(const std::string & filename, size_t NElem)
0047 {   //------------------------------- begin ----------------------------------
0048     std::ofstream ofile;
0049     ofile.open(filename, std::ios_base::out | std::ios_base::binary |
0050                          std::ios_base::trunc);
0051     if (ofile.bad())
0052     {
0053         throw std::ios_base::failure("could not open file \n");
0054     };
0055     std::mt19937_64 my_rand(0);
0056 
0057     for (size_t i = 0; i < NElem; ++i)
0058     {
0059         uint64_t Aux = my_rand();
0060         ofile.write((char *) &Aux, 8);
0061     }
0062     ofile.close();
0063     return 0;
0064 };
0065 //
0066 //-----------------------------------------------------------------------------
0067 //  function : fill_vector_uint64
0068 /// @brief : fill a vector of uint64_t elements from a file
0069 /// @param [in] filename : name of the file
0070 /// @param [in] V : vector to fill
0071 /// @param [in] NElem : number of elements for to read from the file
0072 /// @exception
0073 /// @return
0074 /// @remarks
0075 //-----------------------------------------------------------------------------
0076 inline int fill_vector_uint64(const std::string & filename,
0077                               std::vector<uint64_t> & V, size_t NElem)
0078 {   //----------------------- begin ------------------------------------------
0079     std::ifstream input(filename, std::ios_base::in | std::ios_base::binary);
0080     if (input.fail())
0081     {
0082         throw std::ios_base::failure("could not open file \n");
0083     };
0084     //------------------------------------------------------------------------
0085     // Calculate the lenght of the file and the number of elements inside
0086     //------------------------------------------------------------------------
0087     input.seekg(0, std::ios_base::end);
0088     size_t length = input.tellg();
0089     size_t uCount = length / 8;
0090     if (uCount < NElem)
0091     {
0092         throw std::ios_base::failure("incorrect length of the file\n");
0093     };
0094     V.clear();
0095     V.reserve(NElem);
0096 
0097     uint64_t Aux = 0;
0098     input.seekg(0, std::ios_base::beg);
0099     for (size_t i = 0; i < NElem; ++i)
0100     {
0101         input.read(reinterpret_cast<char *>(&Aux), 8);
0102         V.push_back(Aux);
0103     };
0104     input.close();
0105     return 0;
0106 };
0107 
0108 //
0109 //-----------------------------------------------------------------------------
0110 //  function :write_file_uint64
0111 /// @brief Write a file with the contnt of a vector of Uint64_t elements
0112 /// @param [in] V : vector from read the numbersl
0113 /// @param [in] filename : name of the file
0114 /// @exception
0115 /// @return
0116 /// @remarks
0117 //-----------------------------------------------------------------------------
0118 inline int write_file_uint64 (const std::vector<uint64_t> & V,
0119                               const std::string & filename)
0120 {   //--------------------------------- begin --------------------------------
0121     std::ofstream ofile;
0122     ofile.open(filename,
0123                     std::ios_base::out | std::ios_base::binary
0124                                     | std::ios_base::trunc);
0125     if (ofile.bad())
0126     {
0127         throw std::ios_base::failure("could not open file \n");
0128     };
0129     for (size_t i = 0; i < V.size(); ++i)
0130     {
0131         ofile.write((char *) &(V[i]), 8);
0132     }
0133     ofile.close();
0134     return 0;
0135 };
0136 //
0137 //-----------------------------------------------------------------------------
0138 //  function : fill_vector_string
0139 /// @brief fill a vector of strings from a file
0140 /// @param [in] filename : name of the file from read the strings
0141 /// @param [in] V : vector where store the strings
0142 /// @param [in] NElem : Number of strings for to read from the file
0143 /// @exception
0144 /// @return
0145 /// @remarks
0146 //-----------------------------------------------------------------------------
0147 inline int fill_vector_string (const std::string & filename,
0148                                std::vector<std::string> & V, size_t NElem)
0149 {   //----------------------- begin ------------------------------------------
0150     std::ifstream input(filename, std::ios_base::in | std::ios_base::binary);
0151     if (input.fail())
0152     {
0153         throw std::ios_base::failure("could not open file \n");
0154     };
0155     //------------------------------------------------------------------------
0156     // Calculate the lenght of the file and the number of elements inside
0157     //------------------------------------------------------------------------
0158     input.seekg(0, std::ios_base::end);
0159     V.clear();
0160     V.reserve(NElem);
0161 
0162     std::string inval;
0163     input.seekg(0, std::ios_base::beg);
0164 
0165     for (size_t i = 0; i < NElem; ++i)
0166     {
0167         if (!input.eof())
0168         {
0169             input >> inval;
0170             V.push_back(inval);
0171             inval.clear();
0172         }
0173         else
0174         {
0175             throw std::ios_base::failure("Insuficient lenght of the file\n");
0176         };
0177     };
0178     input.close();
0179     return 0;
0180 };
0181 
0182 //
0183 //-----------------------------------------------------------------------------
0184 //  function :write_file_string
0185 /// @brief : write a file with the strings of a vector
0186 /// @param [in] V : vector from read the sttrings
0187 /// @param [in] filename : file where store the strings
0188 /// @exception
0189 /// @return
0190 /// @remarks
0191 //-----------------------------------------------------------------------------
0192 inline int write_file_string (const std::vector<std::string> & V,
0193                              const std::string & filename)
0194 {   //--------------------------------- begin --------------------------------
0195     std::ofstream ofile;
0196     ofile.open(filename,
0197                     std::ios_base::out | std::ios_base::binary
0198                                     | std::ios_base::trunc);
0199     if (ofile.bad())
0200     {
0201         throw std::ios_base::failure("could not open file \n");
0202     };
0203     for (size_t i = 0; i < V.size(); ++i)
0204     {
0205         ofile.write((char *) &(V[i][0]), V[i].size());
0206         ofile.put(0x0);
0207     }
0208     ofile.close();
0209     return 0;
0210 };
0211 //---------------------------------------------------------------------------
0212 /// @struct uint64_file_generator
0213 /// @brief This struct is a number generator from a file, with several options
0214 ///        for to limit the numbers between 0 and Max_Val
0215 /// @remarks
0216 //---------------------------------------------------------------------------
0217 struct uint64_file_generator
0218 {   //----------------------------------------------------------------------
0219     //                  VARIABLES
0220     //----------------------------------------------------------------------
0221     std::ifstream input;
0222     size_t NMax, Pos;
0223     size_t Max_Val;
0224     std::string s;
0225 
0226     //----------------------------------------------------------------------
0227     //                    FUNCTIONS
0228     //----------------------------------------------------------------------
0229     uint64_file_generator(const std::string & filename)
0230     {   //---------------------------- begin ---------------------------------
0231         s = filename;
0232         input.open(filename, std::ios_base::in | std::ios_base::binary);
0233         if (input.fail() or input.bad())
0234         {
0235             throw std::ios_base::failure("could not open file \n");
0236         };
0237         //--------------------------------------------------------------------
0238         // Calculate the lenght of the file and the number of elements inside
0239         //--------------------------------------------------------------------
0240         input.seekg(0, std::ios_base::end);
0241         size_t length = input.tellg();
0242         NMax = length / 8;
0243         Pos = 0;
0244         Max_Val = ~((size_t) 0);
0245         input.seekg(0);
0246     };
0247 
0248     void set_max_val(size_t MV){ Max_Val = MV; };
0249 
0250     size_t size() const { return NMax; };
0251 
0252     uint64_t get(void)
0253     {
0254         uint64_t Aux;
0255         input.read(reinterpret_cast<char *>(&Aux), 8);
0256         return (Aux % Max_Val);
0257     };
0258 
0259     uint64_t operator ( )(){ return get(); };
0260 
0261     void reset(void) { input.seekg(0, std::ios_base::beg); };
0262 
0263     ~uint64_file_generator() { if (input.is_open()) input.close(); };
0264 };
0265 //
0266 //****************************************************************************
0267 };// end namespace benchmark
0268 };// end namespace sort
0269 };// end namespace boost
0270 //****************************************************************************
0271 //
0272 #endif