Thread and atomic tester.
This commit is contained in:
parent
ca5e6e8331
commit
f61a96cfac
@ -33,8 +33,6 @@
|
|||||||
<Add option="-fexceptions" />
|
<Add option="-fexceptions" />
|
||||||
</Compiler>
|
</Compiler>
|
||||||
<Unit filename="main.cpp" />
|
<Unit filename="main.cpp" />
|
||||||
<Unit filename="stats.cpp" />
|
|
||||||
<Unit filename="stats.h" />
|
|
||||||
<Extensions />
|
<Extensions />
|
||||||
</Project>
|
</Project>
|
||||||
</CodeBlocks_project_file>
|
</CodeBlocks_project_file>
|
||||||
|
89
main.cpp
89
main.cpp
@ -1,45 +1,82 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <algorithm>
|
#include <thread>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <atomic>
|
||||||
|
|
||||||
|
using atomic_bool = std::atomic_bool;
|
||||||
|
using thread = std::thread;
|
||||||
using string = std::string;
|
using string = std::string;
|
||||||
using svector = std::vector<string>;
|
using svector = std::vector<string>;
|
||||||
|
using tvector = std::vector<thread>;
|
||||||
|
|
||||||
|
void exec_containsSubstring(string&,string&,int,int, atomic_bool&);
|
||||||
|
bool containsSubstring(string&,string&,int,int);
|
||||||
string prompts(const string&,bool*);
|
string prompts(const string&,bool*);
|
||||||
void sortVStringsAsc(svector &v);
|
int prompti(const string&,bool*);
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
tvector threads;
|
||||||
svector inputs;
|
svector inputs;
|
||||||
bool s{true};
|
bool s{};
|
||||||
while (s) {
|
int numt = prompti("Enter the number of threads for searching: ", &s);
|
||||||
auto inp = prompts("Enter a string (Invalid to begin processing): ",&s);
|
if (!s)
|
||||||
if (s) inputs.push_back(inp);
|
return 0;
|
||||||
|
if (numt < 1) {
|
||||||
|
std::cout << "Invalid Thread Count!\n";
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
sortVStringsAsc(inputs);
|
string toTest = prompts("Enter the string to search in: ", &s);
|
||||||
for (const auto &c : inputs) std::cout << c << "\n";
|
if (!s)
|
||||||
|
return 0;
|
||||||
|
if (static_cast<int>(toTest.length())/numt == 0) {
|
||||||
|
std::cout << "Invalid Search String Length!\n";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
string testFor = prompts("Enter the string to search for: ", &s);
|
||||||
|
if (!s)
|
||||||
|
return 0;
|
||||||
|
if (testFor.length() == 0 || testFor.length() > toTest.length()) {
|
||||||
|
std::cout << "Invalid Searching String Length!\n";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
atomic_bool res{false};
|
||||||
|
for (int i = 0; i < numt; ++i)
|
||||||
|
threads.push_back(thread {exec_containsSubstring, std::ref(toTest), std::ref(testFor), i*static_cast<int>(toTest.length())/numt, (i+1)*static_cast<int>(toTest.length())/numt, std::ref(res)});
|
||||||
|
for (auto &t : threads)
|
||||||
|
t.join();
|
||||||
|
std::cout << "String Contained state: " << res.load() << "\n" << std::flush;
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void sortVStringsAsc(svector &v) {
|
void exec_containsSubstring(string& toTest,string& testFor, int startIndex, int endIndex, atomic_bool& res) {
|
||||||
sort(v.begin(), v.end(), [] (const string &x, const string &y) {return x.length() < y.length();});
|
bool hm {containsSubstring(toTest,testFor, startIndex, endIndex)};
|
||||||
|
if (hm)
|
||||||
|
res.store(hm);
|
||||||
}
|
}
|
||||||
|
|
||||||
double sumList(const std::vector<double> &l) {
|
bool containsSubstring(string& toTest,string& testFor, int startIndex, int endIndex) {
|
||||||
double sum = 0.0;
|
bool matches {true};
|
||||||
for (const auto &c : l) sum += c;
|
while (startIndex < endIndex && startIndex < static_cast<int>(toTest.length())) {
|
||||||
return sum;
|
for (int i = 0; i < static_cast<int>(testFor.length()); ++i) {
|
||||||
|
matches &= toTest[(long long unsigned int)(i+startIndex)] == testFor[(long long unsigned int)i];
|
||||||
|
if (!matches)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (matches)
|
||||||
|
return true;
|
||||||
|
matches = true;
|
||||||
|
++startIndex;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
class A {
|
|
||||||
std::vector<string> s;
|
|
||||||
};
|
|
||||||
|
|
||||||
string prompts(const string &message, bool *success) {
|
string prompts(const string &message, bool *success) {
|
||||||
string tr{""};
|
string tr{""};
|
||||||
*success = false;
|
*success = false;
|
||||||
std::cout << message;
|
std::cout << message;
|
||||||
if (!(std::cin >> tr)) {
|
if (!(std::getline(std::cin,tr))) {
|
||||||
std::cin.clear();
|
std::cin.clear();
|
||||||
std::cin.ignore();
|
std::cin.ignore();
|
||||||
return "";
|
return "";
|
||||||
@ -48,3 +85,17 @@ string prompts(const string &message, bool *success) {
|
|||||||
return tr;
|
return tr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int prompti(const string &message, bool *success) {
|
||||||
|
int tr{0};
|
||||||
|
*success = false;
|
||||||
|
std::cout << message;
|
||||||
|
if (!(std::cin >> tr)) {
|
||||||
|
std::cin.clear();
|
||||||
|
std::cin.ignore();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
*success = true;
|
||||||
|
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
|
||||||
|
return tr;
|
||||||
|
}
|
||||||
|
|
||||||
|
21
stats.cpp
21
stats.cpp
@ -1,21 +0,0 @@
|
|||||||
#include "stats.h"
|
|
||||||
#include <vector>
|
|
||||||
#include <algorithm>
|
|
||||||
|
|
||||||
double med(std::vector<double> &scores,bool ignoreExtremes) {
|
|
||||||
const auto len = scores.size() - (ignoreExtremes && scores.size() > 2) ? 2 : 0;
|
|
||||||
sort(scores.begin(), scores.end());
|
|
||||||
using vec_size = std::vector<double>::size_type;
|
|
||||||
const vec_size middle = (vec_size) (len / 2);
|
|
||||||
if (middle%2 == 1)
|
|
||||||
return scores[middle];
|
|
||||||
else
|
|
||||||
return (scores[middle - 1] + scores[middle]) / 2.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
double avg(const std::vector<double> &scores,bool ignoreExtremes) {
|
|
||||||
auto sum = 0.0;
|
|
||||||
using vec_size = std::vector<double>::size_type;
|
|
||||||
for (vec_size i = (ignoreExtremes && scores.size() > 2) ? 1 : 0; i < scores.size() - (ignoreExtremes && scores.size() > 2) ? 1 : 0; i++) sum += scores[i];
|
|
||||||
return sum / static_cast<double>(scores.size() - (ignoreExtremes && scores.size() > 2) ? 2 : 0);
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user