| 1 | /* |
| 2 | Copyright (c) 2005-2019 Intel Corporation |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <iostream> |
| 18 | #include <string> |
| 19 | #include <algorithm> |
| 20 | #include <vector> |
| 21 | #include <algorithm> //std::max |
| 22 | |
| 23 | #include "tbb/parallel_for.h" |
| 24 | #include "tbb/blocked_range.h" |
| 25 | |
| 26 | static const std::size_t N = 9; |
| 27 | |
| 28 | class SubStringFinder { |
| 29 | const std::string &str; |
| 30 | std::vector<std::size_t> &max_array; |
| 31 | std::vector<std::size_t> &pos_array; |
| 32 | public: |
| 33 | void operator() ( const tbb::blocked_range<std::size_t>& r ) const { |
| 34 | for (std::size_t i = r.begin(); i != r.end(); ++i) { |
| 35 | std::size_t max_size = 0, max_pos = 0; |
| 36 | for (std::size_t j = 0; j < str.size(); ++j) { |
| 37 | if (j != i) { |
| 38 | std::size_t limit = str.size()-(std::max)(i,j); |
| 39 | for (std::size_t k = 0; k < limit; ++k) { |
| 40 | if (str[i + k] != str[j + k]) |
| 41 | break; |
| 42 | if (k+1 > max_size) { |
| 43 | max_size = k+1; |
| 44 | max_pos = j; |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | max_array[i] = max_size; |
| 50 | pos_array[i] = max_pos; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | SubStringFinder( const std::string &s, std::vector<std::size_t> &m, std::vector<std::size_t> &p ) : |
| 55 | str(s), max_array(m), pos_array(p) { } |
| 56 | }; |
| 57 | |
| 58 | int main() { |
| 59 | using namespace tbb; |
| 60 | |
| 61 | std::string str[N] = { std::string("a" ), std::string("b" ) }; |
| 62 | for (std::size_t i = 2; i < N; ++i) |
| 63 | str[i] = str[i-1]+str[i-2]; |
| 64 | std::string &to_scan = str[N-1]; |
| 65 | const std::size_t num_elem = to_scan.size(); |
| 66 | std::cout << "String to scan: " << to_scan << std::endl; |
| 67 | |
| 68 | std::vector<std::size_t> max( num_elem ); |
| 69 | std::vector<std::size_t> pos( num_elem ); |
| 70 | |
| 71 | parallel_for( blocked_range<std::size_t>( 0, num_elem, 100 ), |
| 72 | SubStringFinder( to_scan, max, pos ) ); |
| 73 | |
| 74 | for (std::size_t i = 0; i < num_elem; ++i) { |
| 75 | for (std::size_t j = 0; j < num_elem; ++j) { |
| 76 | if (j >= i && j < i + max[i]) |
| 77 | std::cout << "_" ; |
| 78 | else |
| 79 | std::cout << " " ; |
| 80 | } |
| 81 | std::cout << std::endl << to_scan << std::endl; |
| 82 | |
| 83 | for (std::size_t j = 0; j < num_elem; ++j) { |
| 84 | if (j >= pos[i] && j < pos[i] + max[i]) |
| 85 | std::cout << "*" ; |
| 86 | else |
| 87 | std::cout << " " ; |
| 88 | } |
| 89 | std::cout << std::endl; |
| 90 | } |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | |