giGii

atomic

Jan 8th, 2023
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <numeric>
  4. #include <random>
  5. #include <string>
  6. #include <string_view>
  7. #include <vector>
  8. #include <execution>
  9.  
  10. #include "log_duration.h"
  11.  
  12. using namespace std;
  13.  
  14. string GenerateWord(mt19937& generator, int max_length) {
  15.     const int length = uniform_int_distribution(1, max_length)(generator);
  16.     string word;
  17.     word.reserve(length);
  18.     for (int i = 0; i < length; ++i) {
  19.         word.push_back(uniform_int_distribution('a', 'z')(generator));
  20.     }
  21.     return word;
  22. }
  23.  
  24. template <template <typename> typename Container>
  25. Container<string> GenerateDictionary(mt19937& generator, int word_count, int max_length) {
  26.     vector<string> words;
  27.     words.reserve(word_count);
  28.     for (int i = 0; i < word_count; ++i) {
  29.         words.push_back(GenerateWord(generator, max_length));
  30.     }
  31.     return Container(words.begin(), words.end());
  32. }
  33.  
  34. template <typename Strings, typename Predicate, typename Function>
  35. void Test(string_view mark, const Strings& strings, Predicate predicate, Function function) {
  36.     LOG_DURATION(mark);
  37.     const auto result = function(strings, predicate);
  38.     cout << result.size() << " " << result[5].substr(0, 5) << endl;
  39. }
  40.  
  41. #define TEST(function) \
  42.     Test(#function, strings, predicate, function<vector<string>, decltype(predicate)>)
  43.  
  44. template <typename Container, typename Predicate, typename Type = typename Container::value_type>
  45. vector<Type> CopyIfUnordered(const Container& container, Predicate predicate) {
  46.     vector<Type> result;
  47.     result.reserve(container.size());
  48.     // atomic_int size = -1;
  49.     atomic<Type*> destination;
  50.  
  51. for_each(execution::par, container.begin(), container.end(),
  52.                     [&predicate, &result, &destination](const Type& item) {
  53.                         if (predicate(item)) {
  54.                             destination = &result.emplace_back();
  55.                             *destination = item;
  56.                             // size.fetch_add(1);
  57.                             // result[size] = item;
  58.                         }
  59.                     });
  60.     return result;
  61. }
  62.  
  63. int main() {
  64.     vector<int> numbers(1'000);
  65.    iota(numbers.begin(), numbers.end(), 0);
  66.  
  67.    const vector<int> even_numbers = CopyIfUnordered(numbers, [](int number) {
  68.        return number % 2 == 0;
  69.    });
  70.    for (const int number : even_numbers) {
  71.        cout << number << " "s;
  72.    }
  73.    cout << endl;
  74.    // выведет все чётные числа от 0 до 999
  75.  
  76.    mt19937 generator;
  77.  
  78.    const auto strings = GenerateDictionary<vector>(generator, 50'000, 3000);
  79.     auto predicate = [](const string& s) {
  80.         return count(s.begin(), s.end(), 'a') < 100;
  81.     };
  82.  
  83.     TEST(CopyIfUnordered);
  84. }
Advertisement
Add Comment
Please, Sign In to add comment