shushus

Спринт 9. Асинхронные вычисления.

Mar 6th, 2023 (edited)
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.69 KB | None | 0 0
  1. #include <algorithm>
  2. #include <execution>
  3. #include <iostream>
  4. #include <future>
  5.  
  6. using namespace std;
  7.  
  8. template <typename RandomAccessIterator, typename Value>
  9. RandomAccessIterator LowerBound(const execution::sequenced_policy&,
  10.                                 RandomAccessIterator range_begin,
  11.                                 RandomAccessIterator range_end,
  12.                                 const Value& value) {
  13.     auto left_bound = range_begin;
  14.     auto right_bound = range_end;
  15.    
  16.     while (left_bound + 1 < right_bound) {
  17.         const auto middle = left_bound + (right_bound - left_bound) / 2;
  18.         if (*middle < value) {
  19.             left_bound = middle;
  20.         } else {
  21.             right_bound = middle;
  22.         }
  23.     }
  24.     if (left_bound == range_begin && !(*left_bound < value)) {
  25.         return left_bound;
  26.     } else {
  27.         return right_bound;
  28.     }
  29. }
  30.  
  31. template <typename RandomAccessIterator, typename Value>
  32. RandomAccessIterator LowerBound(const execution::parallel_policy&,
  33.                                 RandomAccessIterator range_begin,
  34.                                 RandomAccessIterator range_end,
  35.                                 const Value& value) {
  36.     RandomAccessIterator left_bound = range_begin;
  37.     RandomAccessIterator right_bound = range_end;
  38.    
  39.     while (left_bound + 1 < right_bound) {
  40.        
  41.         const int distance = right_bound - left_bound;
  42.         const int part_length = max(1, distance / 3);
  43.         const auto middle_left = left_bound + part_length;
  44.         const auto middle_right = right_bound - part_length;
  45.  
  46.         auto left_less_future = async([middle_left, &value] { return *middle_left < value; });
  47.  
  48.         if (*middle_right < value) {
  49.             left_bound = middle_right;
  50.         } else if (left_less_future.get()) {
  51.             left_bound = middle_left;
  52.             right_bound = middle_right;
  53.         } else {
  54.             right_bound = middle_left;
  55.         }
  56.     }
  57.    
  58.     if (left_bound == range_begin && !(*left_bound < value)) {
  59.         return left_bound;
  60.     } else {
  61.         return right_bound;
  62.     }
  63. }
  64.  
  65. template <typename RandomAccessIterator, typename Value>
  66. RandomAccessIterator LowerBound(RandomAccessIterator range_begin,
  67.                                 RandomAccessIterator range_end,
  68.                                 const Value& value) {
  69.     return LowerBound(execution::seq, range_begin, range_end, value);
  70. }
  71.  
  72. int main() {
  73.     const vector<string> strings = {"cat", "dog", "dog", "horse"};
  74.  
  75.     const vector<string> requests = {"bear", "cat", "deer", "dog", "dogs", "horses"};
  76.  
  77.     cout << "Request [" << requests[0] << "] → position "
  78.          << LowerBound(strings.begin(), strings.end(), requests[0]) - strings.begin() << endl;
  79.     cout << "Request [" << requests[1] << "] → position "
  80.          << LowerBound(execution::seq, strings.begin(), strings.end(), requests[1])
  81.             - strings.begin()
  82.          << endl;
  83.     cout << "Request [" << requests[2] << "] → position "
  84.          << LowerBound(execution::seq, strings.begin(), strings.end(), requests[2])
  85.             - strings.begin()
  86.          << endl;
  87.  
  88.     cout << "Request [" << requests[3] << "] → position "
  89.          << LowerBound(execution::par, strings.begin(), strings.end(), requests[3])
  90.             - strings.begin()
  91.          << endl;
  92.     cout << "Request [" << requests[4] << "] → position "
  93.          << LowerBound(execution::par, strings.begin(), strings.end(), requests[4])
  94.             - strings.begin()
  95.          << endl;
  96.     cout << "Request [" << requests[5] << "] → position "
  97.          << LowerBound(execution::par, strings.begin(), strings.end(), requests[5])
  98.             - strings.begin()
  99.          << endl;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment