123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- #ifndef __BOOST_SORT_COMMON_FILE_VECTOR_HPP
- #define __BOOST_SORT_COMMON_FILE_VECTOR_HPP
- #include <ios>
- #include <cstdio>
- #include <cstdlib>
- #include <ciso646>
- #include <vector>
- #include <string>
- #include <fstream>
- #include <sstream>
- #include <iostream>
- #include <random>
- #include <cstdint>
- namespace boost
- {
- namespace sort
- {
- namespace common
- {
- inline int generate_file(const std::string & filename, size_t NElem)
- {
- std::ofstream ofile;
- ofile.open(filename, std::ios_base::out | std::ios_base::binary |
- std::ios_base::trunc);
- if (ofile.bad())
- {
- throw std::ios_base::failure("could not open file \n");
- };
- std::mt19937_64 my_rand(0);
- for (size_t i = 0; i < NElem; ++i)
- {
- uint64_t Aux = my_rand();
- ofile.write((char *) &Aux, 8);
- }
- ofile.close();
- return 0;
- };
- inline int fill_vector_uint64(const std::string & filename,
- std::vector<uint64_t> & V, size_t NElem)
- {
- std::ifstream input(filename, std::ios_base::in | std::ios_base::binary);
- if (input.fail())
- {
- throw std::ios_base::failure("could not open file \n");
- };
-
-
-
- input.seekg(0, std::ios_base::end);
- size_t length = input.tellg();
- size_t uCount = length / 8;
- if (uCount < NElem)
- {
- throw std::ios_base::failure("incorrect length of the file\n");
- };
- V.clear();
- V.reserve(NElem);
- uint64_t Aux = 0;
- input.seekg(0, std::ios_base::beg);
- for (size_t i = 0; i < NElem; ++i)
- {
- input.read(reinterpret_cast<char *>(&Aux), 8);
- V.push_back(Aux);
- };
- input.close();
- return 0;
- };
- inline int write_file_uint64 (const std::vector<uint64_t> & V,
- const std::string & filename)
- {
- std::ofstream ofile;
- ofile.open(filename,
- std::ios_base::out | std::ios_base::binary
- | std::ios_base::trunc);
- if (ofile.bad())
- {
- throw std::ios_base::failure("could not open file \n");
- };
- for (size_t i = 0; i < V.size(); ++i)
- {
- ofile.write((char *) &(V[i]), 8);
- }
- ofile.close();
- return 0;
- };
- inline int fill_vector_string (const std::string & filename,
- std::vector<std::string> & V, size_t NElem)
- {
- std::ifstream input(filename, std::ios_base::in | std::ios_base::binary);
- if (input.fail())
- {
- throw std::ios_base::failure("could not open file \n");
- };
-
-
-
- input.seekg(0, std::ios_base::end);
- V.clear();
- V.reserve(NElem);
- std::string inval;
- input.seekg(0, std::ios_base::beg);
- for (size_t i = 0; i < NElem; ++i)
- {
- if (!input.eof())
- {
- input >> inval;
- V.push_back(inval);
- inval.clear();
- }
- else
- {
- throw std::ios_base::failure("Insuficient lenght of the file\n");
- };
- };
- input.close();
- return 0;
- };
- inline int write_file_string (const std::vector<std::string> & V,
- const std::string & filename)
- {
- std::ofstream ofile;
- ofile.open(filename,
- std::ios_base::out | std::ios_base::binary
- | std::ios_base::trunc);
- if (ofile.bad())
- {
- throw std::ios_base::failure("could not open file \n");
- };
- for (size_t i = 0; i < V.size(); ++i)
- {
- ofile.write((char *) &(V[i][0]), V[i].size());
- ofile.put(0x0);
- }
- ofile.close();
- return 0;
- };
- struct uint64_file_generator
- {
-
-
- std::ifstream input;
- size_t NMax, Pos;
- size_t Max_Val;
- std::string s;
-
-
-
- uint64_file_generator(const std::string & filename)
- {
- s = filename;
- input.open(filename, std::ios_base::in | std::ios_base::binary);
- if (input.fail() or input.bad())
- {
- throw std::ios_base::failure("could not open file \n");
- };
-
-
-
- input.seekg(0, std::ios_base::end);
- size_t length = input.tellg();
- NMax = length / 8;
- Pos = 0;
- Max_Val = ~((size_t) 0);
- input.seekg(0);
- };
- void set_max_val(size_t MV){ Max_Val = MV; };
- size_t size() const { return NMax; };
- uint64_t get(void)
- {
- uint64_t Aux;
- input.read(reinterpret_cast<char *>(&Aux), 8);
- return (Aux % Max_Val);
- };
- uint64_t operator ( )(){ return get(); };
- void reset(void) { input.seekg(0, std::ios_base::beg); };
- ~uint64_file_generator() { if (input.is_open()) input.close(); };
- };
- };
- };
- };
- #endif
|