Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <ctime>
- constexpr std::size_t MAX_INPUT_INTEGER = INT_MAX;
- struct BitsContainer
- {
- unsigned int *arr;
- std::size_t length;
- };
- BitsContainer createContainer()
- {
- BitsContainer buffer;
- buffer.arr = new unsigned int[MAX_INPUT_INTEGER / 32 + 1]();
- buffer.length = MAX_INPUT_INTEGER / 32 + 1;
- return buffer;
- }
- void printContainer(BitsContainer& bctrDist)
- {
- for (size_t i = 0; i < bctrDist.length; i++)
- {
- for (size_t j = 0; j < 32; j++)
- {
- if (bctrDist.arr[i] & (1 << j)) std::cout << "1";
- else std::cout << "0";
- }
- }
- }
- void setBit(BitsContainer& bctrDist, std::size_t n)
- {
- std::size_t t = n / 32;
- int tnn = n % 32;
- bctrDist.arr[t] |= (1 << tnn);
- }
- void readFile(std::string path, BitsContainer& bctrDist)
- {
- std::ifstream file;
- file.open(path);
- if (!file.is_open()) { std::cout << "FILE NOT FOUND" << std::endl; }
- while (!file.eof())
- {
- unsigned int buffer;
- file >> buffer;
- setBit(bctrDist, buffer);
- }
- }
- void writeFile(std::string path, BitsContainer& bctrDist)
- {
- std::ofstream file;
- file.open(path);
- if (!file.is_open()) { std::cout << "FILE NOT CREATED!" << std::endl; }
- for (size_t i = 0; i < bctrDist.length; i++)
- {
- for (size_t j = 0; j < 32; j++)
- {
- if (bctrDist.arr[i] & (1 << j)) { file << 32 * i + j << " "; }
- }
- }
- }
- int main(int argc, char **argv)
- {
- std::clock_t t0 = clock();
- auto t = createContainer();
- readFile("file.txt", t);
- //printContainer(t);
- writeFile("sorted.txt", t);
- t0 = clock() - t0;
- std::cout << t0 << " ms";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement