Projektdateien hinzufügen.

This commit is contained in:
Karsten Laux 2021-12-15 15:43:30 +01:00
parent 135afb3a9e
commit 25555097ba
4 changed files with 138 additions and 0 deletions

11
CMakeLists.txt Normal file
View File

@ -0,0 +1,11 @@
# CMakeList.txt: CMake-Projekt für "ParallelSearch". Schließen Sie die Quelle ein, und definieren Sie
# projektspezifische Logik hier.
#
cmake_minimum_required (VERSION 3.8)
project ("ParallelSearch")
# Fügen Sie der ausführbaren Datei dieses Projekts eine Quelle hinzu.
add_executable (ParallelSearch "main.cpp" "SearchJob.hpp")
# TODO: Fügen Sie bei Bedarf Tests hinzu, und installieren Sie Ziele.

15
CMakeSettings.json Normal file
View File

@ -0,0 +1,15 @@
{
"configurations": [
{
"name": "x64-Debug",
"generator": "Ninja",
"configurationType": "Debug",
"inheritEnvironments": [ "msvc_x64_x64" ],
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": ""
}
]
}

54
SearchJob.hpp Normal file
View File

@ -0,0 +1,54 @@
#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

58
main.cpp Normal file
View File

@ -0,0 +1,58 @@
#include <iostream>
#include <string>
#include <vector>
#include <chrono>
#include "SearchJob.hpp"
using namespace std::chrono;
// Type of our hay stack: a list of strings
typedef std::vector<std::string> WordList;
void create_testdata(WordList& words)
{
const char start = 'A';
const char end = 'Z';
char str[4] = { 'A', 'A', 'A', 'A' };
for (str[0] = start; str[0] <= end; str[0]++)
for (str[1] = start; str[1] <= end; str[1]++)
for (str[2] = start; str[2] <= end; str[2]++)
for (str[3] = start; str[3] <= end; str[3]++)
words.push_back(std::string(str, 4));
}
int main()
{
std::cout << "Hello World." << std::endl;
WordList test_data;
{
steady_clock::time_point t1 = steady_clock::now();
create_testdata(test_data);
steady_clock::time_point t2 = steady_clock::now();
duration<double> time_span = duration_cast<duration<double>>(t2 - t1);
std::cout << "It took me " << time_span.count() << " seconds to create " << test_data.size() << " words." << std::endl;
}
{
SearchJob<WordList::const_iterator> job(test_data.begin(), test_data.end(), "ABC");
steady_clock::time_point t1 = steady_clock::now();
job.run();
steady_clock::time_point t2 = steady_clock::now();
duration<double> time_span = duration_cast<duration<double>>(t2 - t1);
std::cout << "It took me " << time_span.count() << " seconds to find " << job.GetResult().size() << " matches." << std::endl;
}
std::cout << std::endl;
return 0;
}