Advertisement
QwarkDev

сияй

Sep 9th, 2020
983
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <ctime>
  5.  
  6. constexpr std::size_t MAX_INPUT_INTEGER = INT_MAX;
  7.  
  8. struct BitsContainer
  9. {
  10.     unsigned int *arr;
  11.     std::size_t length;
  12. };
  13.  
  14. BitsContainer createContainer()
  15. {
  16.     BitsContainer buffer;
  17.  
  18.     buffer.arr = new unsigned int[MAX_INPUT_INTEGER / 32 + 1]();
  19.     buffer.length = MAX_INPUT_INTEGER / 32 + 1;
  20.  
  21.     return buffer;
  22. }
  23.  
  24. void printContainer(BitsContainer& bctrDist)
  25. {
  26.     for (size_t i = 0; i < bctrDist.length; i++)
  27.     {
  28.         for (size_t j = 0; j < 32; j++)
  29.         {
  30.             if (bctrDist.arr[i] & (1 << j)) std::cout << "1";
  31.             else std::cout << "0";
  32.         }
  33.     }
  34. }
  35.  
  36. void setBit(BitsContainer& bctrDist, std::size_t n)
  37. {
  38.     std::size_t t = n / 32;
  39.     int tnn = n % 32;
  40.  
  41.     bctrDist.arr[t] |= (1 << tnn);
  42. }
  43.  
  44. void readFile(std::string path, BitsContainer& bctrDist)
  45. {
  46.     std::ifstream file;
  47.     file.open(path);
  48.  
  49.     if (!file.is_open()) { std::cout << "FILE NOT FOUND" << std::endl; }
  50.  
  51.     while (!file.eof())
  52.     {
  53.         unsigned int buffer;
  54.         file >> buffer;
  55.  
  56.         setBit(bctrDist, buffer);
  57.     }
  58. }
  59.  
  60. void writeFile(std::string path, BitsContainer& bctrDist)
  61. {
  62.     std::ofstream file;
  63.     file.open(path);
  64.  
  65.     if (!file.is_open()) { std::cout << "FILE NOT CREATED!" << std::endl; }
  66.  
  67.     for (size_t i = 0; i < bctrDist.length; i++)
  68.     {
  69.         for (size_t j = 0; j < 32; j++)
  70.         {
  71.             if (bctrDist.arr[i] & (1 << j)) { file << 32 * i + j << " "; }
  72.         }
  73.     }
  74. }
  75.  
  76. int main(int argc, char **argv)
  77. {
  78.     std::clock_t t0 = clock();
  79.  
  80.     auto t = createContainer();
  81.     readFile("file.txt", t);
  82.     //printContainer(t);
  83.     writeFile("sorted.txt", t);
  84.  
  85.     t0 = clock() - t0;
  86.  
  87.     std::cout << t0 << " ms";
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement