shushus

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

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