From 25555097baf3ca0e89fe10d52200dd20bc8064c6 Mon Sep 17 00:00:00 2001 From: karsten Date: Wed, 15 Dec 2021 15:43:30 +0100 Subject: [PATCH] =?UTF-8?q?Projektdateien=20hinzuf=C3=BCgen.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 11 +++++++++ CMakeSettings.json | 15 ++++++++++++ SearchJob.hpp | 54 ++++++++++++++++++++++++++++++++++++++++++ main.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 138 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 CMakeSettings.json create mode 100644 SearchJob.hpp create mode 100644 main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..c452f4d --- /dev/null +++ b/CMakeLists.txt @@ -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. diff --git a/CMakeSettings.json b/CMakeSettings.json new file mode 100644 index 0000000..9204f06 --- /dev/null +++ b/CMakeSettings.json @@ -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": "" + } + ] +} \ No newline at end of file diff --git a/SearchJob.hpp b/SearchJob.hpp new file mode 100644 index 0000000..409c006 --- /dev/null +++ b/SearchJob.hpp @@ -0,0 +1,54 @@ +#ifndef SEARCHJOB_HPP +#define SEARCHJOB_HPP + +#include +#include + +template +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 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 \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..0dbba8b --- /dev/null +++ b/main.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include + +#include "SearchJob.hpp" + +using namespace std::chrono; + +// Type of our hay stack: a list of strings +typedef std::vector 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 time_span = duration_cast>(t2 - t1); + + std::cout << "It took me " << time_span.count() << " seconds to create " << test_data.size() << " words." << std::endl; + } + + + { + SearchJob 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 time_span = duration_cast>(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; +}