File indexing completed on 2025-01-18 09:51:46
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
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
0039
0040
0041
0042
0043
0044
0045
0046 inline int generate_file(const std::string & filename, size_t NElem)
0047 {
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
0068
0069
0070
0071
0072
0073
0074
0075
0076 inline int fill_vector_uint64(const std::string & filename,
0077 std::vector<uint64_t> & V, size_t NElem)
0078 {
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
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
0111
0112
0113
0114
0115
0116
0117
0118 inline int write_file_uint64 (const std::vector<uint64_t> & V,
0119 const std::string & filename)
0120 {
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
0139
0140
0141
0142
0143
0144
0145
0146
0147 inline int fill_vector_string (const std::string & filename,
0148 std::vector<std::string> & V, size_t NElem)
0149 {
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
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
0185
0186
0187
0188
0189
0190
0191
0192 inline int write_file_string (const std::vector<std::string> & V,
0193 const std::string & filename)
0194 {
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
0213
0214
0215
0216
0217 struct uint64_file_generator
0218 {
0219
0220
0221 std::ifstream input;
0222 size_t NMax, Pos;
0223 size_t Max_Val;
0224 std::string s;
0225
0226
0227
0228
0229 uint64_file_generator(const std::string & filename)
0230 {
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
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 };
0268 };
0269 };
0270
0271
0272 #endif