shushus

Спринт 9. Концепция MapReduce, функции reduce и transform_reduce. Задание 2.

Mar 6th, 2023 (edited)
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <random>
  4. #include <string>
  5. #include <string_view>
  6. #include <execution>
  7.  
  8. #include "log_duration.h"
  9.  
  10. using namespace std;
  11.  
  12. string GenerateQuery(mt19937& generator, int max_length, int space_rate) {
  13.     const int length = uniform_int_distribution(0, max_length)(generator);
  14.     string query(length, ' ');
  15.     for (char& c : query) {
  16.         const int rnd = uniform_int_distribution(0, space_rate - 1)(generator);
  17.         if (rnd > 0) {
  18.             c = 'a' + (rnd - 1);
  19.         }
  20.     }
  21.     return query;
  22. }
  23.  
  24. template <typename Solver>
  25. void Test(string_view mark, string_view s, Solver solver) {
  26.     int result;
  27.     {
  28.         LOG_DURATION(mark);
  29.         result = solver(s);
  30.     }
  31.     cout << result << endl;
  32. }
  33.  
  34. #define TEST(solver) Test(#solver, s, solver)
  35.  
  36. int CountWords(string_view str) {
  37.     return transform_reduce(execution::par, next(str.begin()), str.end(), str.begin(),
  38.                             0, plus<>{}, [](char lhs, char rhs) {
  39.                             return lhs !=' ' && rhs == ' ';}) + (str[0] != ' ');
  40. }
  41.  
  42. int main() {
  43.     cout << CountWords("  pretty  little octopus "sv) << endl;
  44.  
  45.     mt19937 generator;
  46.  
  47.     const string s = GenerateQuery(generator, 100'000'000, 4);
  48.  
  49.     TEST(CountWords);
  50.  
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment