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 <vector>
20 #include <algorithm> // std::max
21 
22 #include "oneapi/tbb/parallel_for.h"
23 #include "oneapi/tbb/blocked_range.h"
24 
25 #include "common/utility/utility.hpp"
26 
27 bool silent = false;
28 
29 static const std::size_t N = 23;
30 
31 class SubStringFinder {
32     const std::string &str;
33     std::vector<std::size_t> &max_array;
34     std::vector<std::size_t> &pos_array;
35 
36 public:
operator ()(const oneapi::tbb::blocked_range<std::size_t> & r) const37     void operator()(const oneapi::tbb::blocked_range<std::size_t> &r) const {
38         for (std::size_t i = r.begin(); i != r.end(); ++i) {
39             std::size_t max_size = 0, max_pos = 0;
40             for (std::size_t j = 0; j < str.size(); ++j) {
41                 if (j != i) {
42                     std::size_t limit = str.size() - (std::max)(i, j);
43                     for (std::size_t k = 0; k < limit; ++k) {
44                         if (str[i + k] != str[j + k])
45                             break;
46                         if (k > max_size) {
47                             max_size = k;
48                             max_pos = j;
49                         }
50                     }
51                 }
52             }
53             max_array[i] = max_size;
54             pos_array[i] = max_pos;
55         }
56     }
57 
SubStringFinder(const std::string & s,std::vector<std::size_t> & m,std::vector<std::size_t> & p)58     SubStringFinder(const std::string &s, std::vector<std::size_t> &m, std::vector<std::size_t> &p)
59             : str(s),
60               max_array(m),
61               pos_array(p) {}
62 };
63 
main(int argc,char * argv[])64 int main(int argc, char *argv[]) {
65     // command line parsing
66     utility::parse_cli_arguments(argc,
67                                  argv,
68                                  utility::cli_argument_pack()
69                                      //"-h" option for displaying help is present implicitly
70                                      .arg(silent, "silent", "no output"));
71 
72     std::string str[N] = { std::string("a"), std::string("b") };
73     for (std::size_t i = 2; i < N; ++i)
74         str[i] = str[i - 1] + str[i - 2];
75     std::string &to_scan = str[N - 1];
76     const std::size_t num_elem = to_scan.size();
77 
78     std::vector<std::size_t> max(num_elem);
79     std::vector<std::size_t> pos(num_elem);
80 
81     oneapi::tbb::parallel_for(oneapi::tbb::blocked_range<std::size_t>(0, num_elem),
82                               SubStringFinder(to_scan, max, pos));
83 
84     for (std::size_t i = 0; i < num_elem; ++i)
85         if (!silent)
86             std::cout << " " << max[i] << "(" << pos[i] << ")"
87                       << "\n";
88 
89     return 0;
90 }
91