Shadowrun: Awakened 29 September 2011 - Build 871
sub_string_finder_pretty.cpp
Go to the documentation of this file.
00001 /*
00002     Copyright 2005-2010 Intel Corporation.  All Rights Reserved.
00003 
00004     This file is part of Threading Building Blocks.
00005 
00006     Threading Building Blocks is free software; you can redistribute it
00007     and/or modify it under the terms of the GNU General Public License
00008     version 2 as published by the Free Software Foundation.
00009 
00010     Threading Building Blocks is distributed in the hope that it will be
00011     useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00012     of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with Threading Building Blocks; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 
00019     As a special exception, you may use this file as part of a free software
00020     library without restriction.  Specifically, if other files instantiate
00021     templates or use macros or inline functions from this file, or you compile
00022     this file and link it with other files to produce an executable, this
00023     file does not by itself cause the resulting executable to be covered by
00024     the GNU General Public License.  This exception does not however
00025     invalidate any other reasons why the executable file might be covered by
00026     the GNU General Public License.
00027 */
00028 
00029 #include <iostream>
00030 #include <string>
00031 #include <algorithm>
00032 
00033 #include "tbb/parallel_for.h"
00034 #include "tbb/blocked_range.h"
00035 
00036 using namespace tbb;
00037 static const size_t N = 9;
00038 
00039 class SubStringFinder {
00040  const std::string str;
00041  size_t *max_array;
00042  size_t *pos_array;
00043  public:
00044  void operator() ( const blocked_range<size_t>& r ) const {
00045   for ( size_t i = r.begin(); i != r.end(); ++i ) {
00046    size_t max_size = 0, max_pos = 0;
00047    for (size_t j = 0; j < str.size(); ++j) 
00048     if (j != i) {
00049      size_t limit = str.size()-( i > j ? i : j );
00050      for (size_t k = 0; k < limit; ++k) {
00051       if (str[i + k] != str[j + k]) break;
00052       if (k+1 > max_size) {
00053        max_size = k+1;
00054        max_pos = j;
00055       }
00056      }
00057     }
00058    max_array[i] = max_size;
00059    pos_array[i] = max_pos;
00060   }
00061  }
00062  SubStringFinder(std::string &s, size_t *m, size_t *p) :
00063   str(s), max_array(m), pos_array(p) { }
00064 };
00065 
00066 int main(int argc, char *argv[]) {
00067  
00068 
00069  std::string str[N] = { std::string("a"), std::string("b") };
00070  for (size_t i = 2; i < N; ++i) str[i] = str[i-1]+str[i-2];
00071  std::string &to_scan = str[N-1]; 
00072  std::cout << "String to scan: " << to_scan << std::endl;
00073 
00074  size_t *max = new size_t[to_scan.size()];
00075  size_t *pos = new size_t[to_scan.size()];
00076 
00077  parallel_for(blocked_range<size_t>(0, to_scan.size(), 100),
00078        SubStringFinder( to_scan, max, pos ) );
00079 
00080  for (size_t i = 0; i < to_scan.size(); ++i) {
00081    for (size_t j = 0; j < to_scan.size(); ++j) {
00082      if (j >= i && j < i + max[i]) std::cout << "_";
00083      else std::cout << " ";
00084    } 
00085    std::cout << std::endl << to_scan << std::endl;
00086 
00087    for (size_t j = 0; j < to_scan.size(); ++j) {
00088      if (j >= pos[i] && j < pos[i] + max[i]) std::cout << "*";
00089      else std::cout << " ";
00090    }
00091    std::cout << std::endl;
00092  }
00093  delete[] max;
00094  delete[] pos;
00095  return 0;
00096 }
00097 

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net