File: sub_string_finder_pretty.cpp

package info (click to toggle)
tbb 2018~U6-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 19,920 kB
  • sloc: cpp: 131,295; ansic: 9,211; makefile: 1,343; asm: 1,061; python: 838; sh: 395; lisp: 198; objc: 176; pascal: 69
file content (98 lines) | stat: -rw-r--r-- 3,009 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
    Copyright (c) 2005-2018 Intel Corporation

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.




*/

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <algorithm>    //std::max

#include "tbb/parallel_for.h"
#include "tbb/blocked_range.h"

static const std::size_t N = 9;

class SubStringFinder {
    const std::string &str;
    std::vector<std::size_t> &max_array;
    std::vector<std::size_t> &pos_array;
public:
    void operator() ( const tbb::blocked_range<std::size_t>& r ) const {
        for (std::size_t i = r.begin(); i != r.end(); ++i) {
            std::size_t max_size = 0, max_pos = 0;
            for (std::size_t j = 0; j < str.size(); ++j) {
                if (j != i) {
                    std::size_t limit = str.size()-(std::max)(i,j);
                    for (std::size_t k = 0; k < limit; ++k) {
                        if (str[i + k] != str[j + k])
                            break;
                        if (k+1 > max_size) {
                            max_size = k+1;
                            max_pos = j;
                        }
                    }
                }
            }
            max_array[i] = max_size;
            pos_array[i] = max_pos;
        }
    }

    SubStringFinder( const std::string &s, std::vector<std::size_t> &m, std::vector<std::size_t> &p ) :
        str(s), max_array(m), pos_array(p) { }
};

int main() {
    using namespace tbb;

    std::string str[N] = { std::string("a"), std::string("b") };
    for (std::size_t i = 2; i < N; ++i)
        str[i] = str[i-1]+str[i-2];
    std::string &to_scan = str[N-1];
    const std::size_t num_elem = to_scan.size();
    std::cout << "String to scan: " << to_scan << std::endl;

    std::vector<std::size_t> max( num_elem );
    std::vector<std::size_t> pos( num_elem );

    parallel_for( blocked_range<std::size_t>( 0, num_elem, 100 ),
            SubStringFinder( to_scan, max, pos ) );

    for (std::size_t i = 0; i < num_elem; ++i) {
        for (std::size_t j = 0; j < num_elem; ++j) {
            if (j >= i && j < i + max[i])
                std::cout << "_";
            else
                std::cout << " ";
        }
        std::cout << std::endl << to_scan << std::endl;

        for (std::size_t j = 0; j < num_elem; ++j) {
            if (j >= pos[i] && j < pos[i] + max[i])
                std::cout << "*";
            else
                std::cout << " ";
        }
        std::cout << std::endl;
    }

    return 0;
}