1 /*
2 Copyright (c) 2005-2021 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 "oneapi/tbb/parallel_for.h"
24 #include "oneapi/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
33 public:
operator ()(const oneapi::tbb::blocked_range<std::size_t> & r) const34 void operator()(const oneapi::tbb::blocked_range<std::size_t> &r) const {
35 for (std::size_t i = r.begin(); i != r.end(); ++i) {
36 std::size_t max_size = 0, max_pos = 0;
37 for (std::size_t j = 0; j < str.size(); ++j) {
38 if (j != i) {
39 std::size_t limit = str.size() - (std::max)(i, j);
40 for (std::size_t k = 0; k < limit; ++k) {
41 if (str[i + k] != str[j + k])
42 break;
43 if (k + 1 > max_size) {
44 max_size = k + 1;
45 max_pos = j;
46 }
47 }
48 }
49 }
50 max_array[i] = max_size;
51 pos_array[i] = max_pos;
52 }
53 }
54
SubStringFinder(const std::string & s,std::vector<std::size_t> & m,std::vector<std::size_t> & p)55 SubStringFinder(const std::string &s, std::vector<std::size_t> &m, std::vector<std::size_t> &p)
56 : str(s),
57 max_array(m),
58 pos_array(p) {}
59 };
60
main()61 int main() {
62 std::string str[N] = { std::string("a"), std::string("b") };
63 for (std::size_t i = 2; i < N; ++i)
64 str[i] = str[i - 1] + str[i - 2];
65 std::string &to_scan = str[N - 1];
66 const std::size_t num_elem = to_scan.size();
67 std::cout << "String to scan: " << to_scan << "\n";
68
69 std::vector<std::size_t> max(num_elem);
70 std::vector<std::size_t> pos(num_elem);
71
72 oneapi::tbb::parallel_for(oneapi::tbb::blocked_range<std::size_t>(0, num_elem, 100),
73 SubStringFinder(to_scan, max, pos));
74
75 for (std::size_t i = 0; i < num_elem; ++i) {
76 for (std::size_t j = 0; j < num_elem; ++j) {
77 if (j >= i && j < i + max[i])
78 std::cout << "_";
79 else
80 std::cout << " ";
81 }
82 std::cout << "\n" << to_scan << "\n";
83
84 for (std::size_t j = 0; j < num_elem; ++j) {
85 if (j >= pos[i] && j < pos[i] + max[i])
86 std::cout << "*";
87 else
88 std::cout << " ";
89 }
90 std::cout << "\n";
91 }
92
93 return 0;
94 }
95