nochnoikoshmar1

Untitled

Jan 8th, 2024
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <numeric>
  4. #include <random>
  5. #include <vector>
  6. #include <future>
  7. #include <execution>
  8. #include <chrono>
  9. using namespace std;
  10.  
  11. /*#define PROFILE_CONCAT_INTERNAL(X, Y) X##Y
  12. #define PROFILE_CONCAT(X, Y) PROFILE_CONCAT_INTERNAL(X, Y)
  13. #define UNIQUE_VAR_NAME_PROFILE PROFILE_CONCAT(profileGuard, __LINE__)
  14. #define LOG_DURATION(x) LogDuration UNIQUE_VAR_NAME_PROFILE(x)
  15. #define LOG_DURATION_STREAM(x, y) LogDuration UNIQUE_VAR_NAME_PROFILE(x, y)*/
  16.  
  17. /*class LogDuration {
  18. public:
  19.     // заменим имя типа std::chrono::steady_clock
  20.     // с помощью using для удобства
  21.     using Clock = std::chrono::steady_clock;
  22.  
  23.     LogDuration(const std::string& id, std::ostream& dst_stream = std::cerr)
  24.         : id_(id)
  25.         , dst_stream_(dst_stream) {
  26.     }
  27.  
  28.     ~LogDuration() {
  29.         using namespace std::chrono;
  30.         using namespace std::literals;
  31.  
  32.         const auto end_time = Clock::now();
  33.         const auto dur = end_time - start_time_;
  34.         dst_stream_ << id_ << ": "s << duration_cast<milliseconds>(dur).count() << " ms"s << std::endl;
  35.     }
  36.  
  37. private:
  38.     const std::string id_;
  39.     const Clock::time_point start_time_ = Clock::now();
  40.     std::ostream& dst_stream_;
  41. };*/
  42.  
  43. template <typename It>
  44. void PrintRange(It range_begin, It range_end) {
  45.     for (auto it = range_begin; it != range_end; ++it) {
  46.         cout << *it << " "s;
  47.     }
  48.     cout << endl;
  49. }
  50.  
  51. template <typename RandomIt>
  52. void MergeSort(const RandomIt &range_begin,const RandomIt &range_end) {
  53.     if (range_end - range_begin < 2) return;      
  54.    
  55.     RandomIt mid = range_begin + (range_end - range_begin) / 2;
  56.    
  57.     MergeSort(range_begin, mid);
  58.    
  59.     auto future1 = std::async( [&]{
  60.         MergeSort( mid, range_end);
  61.     });
  62.     future1.get();
  63.     std::inplace_merge(range_begin, mid, range_end);
  64. }
  65. int main() {
  66.     //LOG_DURATION("vector w/o reserve");
  67.     mt19937 generator;
  68.     vector<int> test_vector(1000);
  69.     iota(test_vector.begin(), test_vector.end(), 1);
  70.     shuffle(test_vector.begin(), test_vector.end(), generator);
  71.     PrintRange(test_vector.begin(), test_vector.end());
  72.     MergeSort(test_vector.begin(), test_vector.end());
  73.     PrintRange(test_vector.begin(), test_vector.end());
  74.     MergeSort(test_vector.data(), test_vector.data() + test_vector.size());
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment