Advertisement
AyratT

Асинхронные вычисления

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