Advertisement
oster_man

for_each

Aug 19th, 2023
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 KB | None | 0 0
  1. #include <algorithm>
  2. #include <execution>
  3. #include <iostream>
  4. #include <list>
  5. #include <random>
  6. #include <string>
  7. #include <string_view>
  8. #include <type_traits>
  9. #include <vector>
  10. #include <future>
  11.  
  12. #include "log_duration.h"
  13.  
  14. using namespace std;
  15.  
  16. string GenerateWord(mt19937& generator, int max_length) {
  17.     const int length = uniform_int_distribution(1, max_length)(generator);
  18.     string word;
  19.     word.reserve(length);
  20.     for (int i = 0; i < length; ++i) {
  21.         word.push_back(uniform_int_distribution('a', 'z')(generator));
  22.     }
  23.     return word;
  24. }
  25.  
  26. template <template <typename> typename Container>
  27. Container<string> GenerateDictionary(mt19937& generator, int word_count, int max_length) {
  28.     vector<string> words;
  29.     words.reserve(word_count);
  30.     for (int i = 0; i < word_count; ++i) {
  31.         words.push_back(GenerateWord(generator, max_length));
  32.     }
  33.     return Container(words.begin(), words.end());
  34. }
  35.  
  36. struct Reverser {
  37.     void operator()(string& value) const {
  38.         reverse(value.begin(), value.end());
  39.     }
  40. };
  41.  
  42. template <typename Container, typename Function>
  43. void Test(string_view mark, Container keys, Function function) {
  44.     LOG_DURATION(mark);
  45.     function(keys, Reverser{});
  46. }
  47.  
  48. #define TEST(function) Test(#function, keys, function<remove_const_t<decltype(keys)>, Reverser>)
  49.  
  50. template <typename ForwardRange, typename Function>
  51. void ForEach(ForwardRange& range, Function function) {
  52.     // ускорьте эту реализацию
  53.     int AsyncPart=20000;
  54.  
  55.     if (!range.empty()){
  56.         int divisions = range.size()/AsyncPart;
  57.         if (!divisions){
  58.             for_each(range.begin(),range.end(),function);
  59.         }
  60.         else{
  61.             auto first = range.begin(); //0
  62.             auto last = range.begin(); //0
  63.             last = next(first,range.size()/divisions); //0 + size/divisions
  64.             vector<future<void>> futures;
  65.             for(int i=0;i<divisions;++i){
  66.                 futures.push_back(async([first,last,function]{
  67.                     for_each(first,last,function);
  68.                 }));
  69.                 first = next(last);
  70.                 advance(first,range.size()/divisions);
  71.             }
  72.  
  73.             for_each(futures.begin(),futures.end(),[](auto& asyncFunc){
  74.                 asyncFunc.get();
  75.             });
  76.         }
  77.     }
  78.     //for_each(execution::par,range.begin(),range.end(),function);
  79. }
  80.  
  81. int main() {
  82.     // для итераторов с произвольным доступом тоже должно работать
  83.     vector<string> strings = {"cat", "dog", "code"};
  84.  
  85.     ForEach(strings, [](string& s) {
  86.         reverse(s.begin(), s.end());
  87.     });
  88.  
  89.     for (string_view s : strings) {
  90.         cout << s << " ";
  91.     }
  92.     cout << endl;
  93.     // вывод: tac god edoc
  94.  
  95.     mt19937 generator;
  96.     const auto keys = GenerateDictionary<list>(generator, 50'000, 5'000);
  97.  
  98.     TEST(ForEach);
  99.  
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement