ParallelSearch/ParallelSearch.hpp

62 lines
1.6 KiB
C++

#ifndef PARALLELSEARCH_HPP
#define PARALLELSEARCH_HPP
#include <vector>
#include "WordList.hpp"
class ParallelSearch
{
public:
// Iterator type used inside the class
typedef WordList::const_iterator Iterator;
// Type of the search result
typedef std::vector<Iterator> ResultList;
class SearchJob
{
public:
// initially reserve n entries in result-list (to cut down number of reallocations)
enum { INITIAL_RESERVE = 128 };
// Create partial search job for provided range
SearchJob(Iterator begin, Iterator end, const std::string& pattern);
// Destructor
~SearchJob() {}
// run the search job and collect results internally
void execute();
// return the result
const ResultList& get_result() const { return result_; };
private:
// pattern to search for
std::string pattern_;
// start of search range
Iterator begin_;
// end of search range
Iterator end_;
// result of the search
ResultList result_;
};
// construct parallel search by splitting the search range across <workerCount> SearchJob objects
ParallelSearch(unsigned workerCount, Iterator begin, Iterator end, const std::string& pattern);
// Destructor
~ParallelSearch() {}
// run the search workers and collect results
void run();
// return the result
const ResultList& get_result() const { return result_; };
private:
// jobs to execute
std::vector<SearchJob> jobList_;
// result of the search
ResultList result_;
};
#endif