| 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 <vector> | 
|---|
| 20 | #include <algorithm>    //std::max | 
|---|
| 21 | #include "tbb/parallel_for.h" | 
|---|
| 22 | #include "tbb/blocked_range.h" | 
|---|
| 23 |  | 
|---|
| 24 | static const std::size_t N = 23; | 
|---|
| 25 |  | 
|---|
| 26 | class SubStringFinder { | 
|---|
| 27 | const std::string &str; | 
|---|
| 28 | std::vector<std::size_t> &max_array; | 
|---|
| 29 | std::vector<std::size_t> &pos_array; | 
|---|
| 30 | public: | 
|---|
| 31 | void operator() ( const tbb::blocked_range<std::size_t> &r ) const { | 
|---|
| 32 | for (std::size_t i = r.begin(); i != r.end(); ++i) { | 
|---|
| 33 | std::size_t max_size = 0, max_pos = 0; | 
|---|
| 34 | for (std::size_t j = 0; j < str.size(); ++j) { | 
|---|
| 35 | if (j != i) { | 
|---|
| 36 | std::size_t limit = str.size()-(std::max)(i,j); | 
|---|
| 37 | for (std::size_t k = 0; k < limit; ++k) { | 
|---|
| 38 | if (str[i + k] != str[j + k]) | 
|---|
| 39 | break; | 
|---|
| 40 | if (k > max_size) { | 
|---|
| 41 | max_size = k; | 
|---|
| 42 | max_pos = j; | 
|---|
| 43 | } | 
|---|
| 44 | } | 
|---|
| 45 | } | 
|---|
| 46 | } | 
|---|
| 47 | max_array[i] = max_size; | 
|---|
| 48 | pos_array[i] = max_pos; | 
|---|
| 49 | } | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 | SubStringFinder( const std::string &s, std::vector<std::size_t> &m, std::vector<std::size_t> &p ) : | 
|---|
| 53 | str(s), max_array(m), pos_array(p) { } | 
|---|
| 54 | }; | 
|---|
| 55 |  | 
|---|
| 56 | int main() { | 
|---|
| 57 | std::string str[N] = { std::string( "a"), std::string( "b") }; | 
|---|
| 58 | for (std::size_t i = 2; i < N; ++i) | 
|---|
| 59 | str[i] = str[i-1]+str[i-2]; | 
|---|
| 60 | std::string &to_scan = str[N-1]; | 
|---|
| 61 | const std::size_t num_elem = to_scan.size(); | 
|---|
| 62 |  | 
|---|
| 63 | std::vector<std::size_t> max(num_elem); | 
|---|
| 64 | std::vector<std::size_t> pos(num_elem); | 
|---|
| 65 |  | 
|---|
| 66 | tbb::parallel_for( tbb::blocked_range<std::size_t>( 0, num_elem ), | 
|---|
| 67 | SubStringFinder( to_scan, max, pos ) ); | 
|---|
| 68 |  | 
|---|
| 69 | for (std::size_t i = 0; i < num_elem; ++i) | 
|---|
| 70 | std::cout << " "<< max[i] << "("<< pos[i] << ")"<< std::endl; | 
|---|
| 71 |  | 
|---|
| 72 | return 0; | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 |  | 
|---|