Shadowrun: Awakened 29 September 2011 - Build 871
sub_string_finder.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 #include "tbb/parallel_for.h"
00033 #include "tbb/blocked_range.h"
00034 
00035 using namespace tbb;
00036 using namespace std;
00037 static const size_t N = 23;
00038 
00039 class SubStringFinder {
00040   const 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()-max(i,j);
00050         for (size_t k = 0; k < limit; ++k) {
00051           if (str[i + k] != str[j + k]) break;
00052           if (k > max_size) {
00053             max_size = k;
00054             max_pos = j;
00055           }
00056         }
00057       }
00058       max_array[i] = max_size;
00059       pos_array[i] = max_pos;
00060     }
00061   }
00062   SubStringFinder(string &s, size_t *m, size_t *p) :
00063     str(s), max_array(m), pos_array(p) { }
00064 };
00065 
00066 int main() {
00067 
00068   string str[N] = { string("a"), string("b") };
00069   for (size_t i = 2; i < N; ++i) str[i] = str[i-1]+str[i-2];
00070   string &to_scan = str[N-1]; 
00071 
00072   size_t *max = new size_t[to_scan.size()];
00073   size_t *pos = new size_t[to_scan.size()];
00074 
00075   parallel_for(blocked_range<size_t>(0, to_scan.size() ),
00076                SubStringFinder( to_scan, max, pos ) );
00077 
00078   for (size_t i = 0; i < to_scan.size(); ++i)
00079     cout << " " << max[i] << "(" << pos[i] << ")" << endl;
00080   delete[] pos;
00081   delete[] max;
00082   return 0;
00083 }
00084 

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