54 lines
1.2 KiB
C++
54 lines
1.2 KiB
C++
#ifndef SEARCHJOB_HPP
|
|
#define SEARCHJOB_HPP
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
template <typename Iterator>
|
|
class SearchJob
|
|
{
|
|
public:
|
|
// initially reserve n entries in result-list (to cut down number of reallocations)
|
|
enum { INITIAL_RESERVE = 128 };
|
|
|
|
// Type of the search result
|
|
typedef std::vector<Iterator> ResultList;
|
|
|
|
// Create partial search job for provided range
|
|
SearchJob(Iterator begin, Iterator end, const std::string& pattern) : begin_(begin), end_(end), pattern_(pattern)
|
|
{
|
|
result_.reserve(INITIAL_RESERVE);
|
|
}
|
|
|
|
// Destructor
|
|
~SearchJob()
|
|
{
|
|
}
|
|
|
|
// run the search job and collect results internally
|
|
void run()
|
|
{
|
|
for (Iterator itr = begin_; itr != end_; itr++)
|
|
{
|
|
if (0 == itr->compare(0, pattern_.size(), pattern_))
|
|
{
|
|
// found a match, save the index
|
|
result_.push_back(itr);
|
|
}
|
|
}
|
|
}
|
|
|
|
// return the result
|
|
const ResultList& GetResult() const { return result_; };
|
|
|
|
private:
|
|
// pattern to search for
|
|
std::string pattern_;
|
|
// first valid index
|
|
Iterator begin_;
|
|
// last valid index
|
|
Iterator end_;
|
|
// result of the search
|
|
ResultList result_;
|
|
};
|
|
#endif |