Advertisement
AyratT

Untitled

Feb 27th, 2023
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.53 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. #include "log_duration.h"
  12.  
  13. using namespace std;
  14.  
  15. string GenerateWord(mt19937& generator, int max_length) {
  16.     const int length = uniform_int_distribution(1, max_length)(generator);
  17.     string word;
  18.     word.reserve(length);
  19.     for (int i = 0; i < length; ++i) {
  20.         word.push_back(uniform_int_distribution('a', 'z')(generator));
  21.     }
  22.     return word;
  23. }
  24.  
  25. template <template <typename> typename Container>
  26. Container<string> GenerateDictionary(mt19937& generator, int word_count, int max_length) {
  27.     vector<string> words;
  28.     words.reserve(word_count);
  29.     for (int i = 0; i < word_count; ++i) {
  30.         words.push_back(GenerateWord(generator, max_length));
  31.     }
  32.     return Container(words.begin(), words.end());
  33. }
  34.  
  35. struct Reverser {
  36.     void operator()(string& value) const {
  37.         reverse(value.begin(), value.end());
  38.     }
  39. };
  40.  
  41. template <typename Container, typename Function>
  42. void Test(string_view mark, Container keys, Function function) {
  43.     LOG_DURATION(mark);
  44.     function(keys, Reverser{});
  45. }
  46.  
  47. #define TEST(function) Test(#function, keys, function<remove_const_t<decltype(keys)>, Reverser>)
  48.  
  49. template <typename ForwardRange, typename Function>
  50. void ForEach(ForwardRange& range, Function function) {
  51.     const int number_of_tasks = 4;
  52.     const int size_of_part = range.size() / number_of_tasks;
  53.     auto part_begin = range.begin();
  54.     auto part_end = next(part_begin, size_of_part);
  55.  
  56.     vector<future<void>> to;
  57.     int i = 0;
  58.     while ( i < number_of_tasks) {
  59.         part_begin = part_end;
  60.         part_end = (i == number_of_tasks - 1) ? range.end() : next(part_begin, size_of_part);
  61.         ++i;
  62.        
  63.         to.push_back(async([&function, part_begin, part_end] {
  64.             for_each(part_begin, part_end, function);
  65.             }));
  66.     }
  67.  
  68. }
  69.  
  70. int main() {
  71.     // для итераторов с произвольным доступом тоже должно работать
  72.     vector<string> strings = { "cat", "dog", "code" };
  73.  
  74.     ForEach(strings, [](string& s) {
  75.         reverse(s.begin(), s.end());
  76.         });
  77.  
  78.     for (string_view s : strings) {
  79.         cout << s << " ";
  80.     }
  81.     cout << endl;
  82.     // вывод: tac god edoc
  83.  
  84.     mt19937 generator;
  85.     const auto keys = GenerateDictionary<list>(generator, 50'000, 5'000);
  86.  
  87.     TEST(ForEach);
  88.  
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement