Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <execution>
- #include <iostream>
- #include <future>
- #include <iterator>
- using namespace std;
- template <typename RandomAccessIterator, typename Value>
- RandomAccessIterator LowerBound(RandomAccessIterator range_begin, RandomAccessIterator range_end,
- const Value& value) {
- return std::lower_bound(range_begin, range_end, value);
- }
- template <typename RandomAccessIterator, typename Value>
- RandomAccessIterator LowerBound(const execution::sequenced_policy&, RandomAccessIterator range_begin,
- RandomAccessIterator range_end, const Value& value) {
- return LowerBound(range_begin, range_end, value);
- }
- template <typename RandomAccessIterator, typename Value>
- RandomAccessIterator LowerBound(const execution::parallel_policy&, RandomAccessIterator range_begin,
- RandomAccessIterator range_end, const Value& value) {
- RandomAccessIterator left_bound = range_begin;
- RandomAccessIterator right_bound = range_end;
- while (left_bound < right_bound - 1) {
- int size_path = max(1, int(right_bound - left_bound) / 3);
- RandomAccessIterator middle_left = left_bound + size_path;
- RandomAccessIterator middle_right = right_bound - size_path;
- future<bool> hasInRight = async([&middle_right, &value] {
- return *middle_right < value;
- });
- if (value < *middle_left) {
- right_bound = middle_left;
- }
- else if (hasInRight.get()) {
- left_bound = middle_right + 1;
- }
- else {
- left_bound = middle_left - 1;
- right_bound = middle_right;
- }
- /*
- future<bool> hasInMid = async([&middle_right, &value] {
- return *middle_right < value;
- });
- if (value < *middle_left) {
- right_bound = middle_left;
- }
- else if (hasInMid.get()) {
- left_bound = middle_right + 1;
- }
- else {
- left_bound = middle_left - 1;
- right_bound = middle_right;
- }*/
- /* future<bool> hasInMid = async([middle_left, middle_right, &value] {
- return !(value < *middle_left) && !(*middle_right < value);
- });
- if (value < *middle_left) {
- right_bound = middle_left;
- }
- else if (hasInMid.get()) {
- left_bound = middle_left - 1;
- right_bound = middle_right;
- }
- else if (*middle_right < value) {
- left_bound = middle_right + 1;
- }*/
- }
- if (left_bound == range_begin && !(*left_bound < value)) {
- return left_bound;
- }
- else {
- return right_bound;
- }
- }
- int main() {
- const vector<string> strings = { "cat", "dog", "dog", "horse" };
- const vector<string> requests = { "bear", "cat", "deer", "dog", "dogs", "horses" };
- // последовательные версии
- cout << "Request [" << requests[0] << "] → position "
- << LowerBound(strings.begin(), strings.end(), requests[0]) - strings.begin() << endl;
- cout << "Request [" << requests[1] << "] → position "
- << LowerBound(execution::par, strings.begin(), strings.end(), requests[1])
- - strings.begin()
- << endl;
- cout << "Request [" << requests[2] << "] → position "
- << LowerBound(execution::par, strings.begin(), strings.end(), requests[2])
- - strings.begin()
- << endl;
- // параллельные
- cout << "Request [" << requests[3] << "] → position "
- << LowerBound(execution::par, strings.begin(), strings.end(), requests[3])
- - strings.begin()
- << endl;
- cout << "Request [" << requests[4] << "] → position "
- << LowerBound(execution::par, strings.begin(), strings.end(), requests[4])
- - strings.begin()
- << endl;
- cout << "Request [" << requests[5] << "] → position "
- << LowerBound(execution::par, strings.begin(), strings.end(), requests[5])
- - strings.begin()
- << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement