1
0

Thread and atomic tester.

This commit is contained in:
Captain ALM 2023-08-21 23:49:03 +01:00
parent ca5e6e8331
commit f61a96cfac
Signed by: alfred
GPG Key ID: 4E4ADD02609997B1
4 changed files with 70 additions and 51 deletions

View File

@ -33,8 +33,6 @@
<Add option="-fexceptions" />
</Compiler>
<Unit filename="main.cpp" />
<Unit filename="stats.cpp" />
<Unit filename="stats.h" />
<Extensions />
</Project>
</CodeBlocks_project_file>

View File

@ -1,45 +1,82 @@
#include <string>
#include <vector>
#include <algorithm>
#include <thread>
#include <iostream>
#include <atomic>
using atomic_bool = std::atomic_bool;
using thread = std::thread;
using string = std::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*);
void sortVStringsAsc(svector &v);
int prompti(const string&,bool*);
int main() {
tvector threads;
svector inputs;
bool s{true};
while (s) {
auto inp = prompts("Enter a string (Invalid to begin processing): ",&s);
if (s) inputs.push_back(inp);
bool s{};
int numt = prompti("Enter the number of threads for searching: ", &s);
if (!s)
return 0;
if (numt < 1) {
std::cout << "Invalid Thread Count!\n";
return 1;
}
sortVStringsAsc(inputs);
for (const auto &c : inputs) std::cout << c << "\n";
string toTest = prompts("Enter the string to search in: ", &s);
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;
}
void sortVStringsAsc(svector &v) {
sort(v.begin(), v.end(), [] (const string &x, const string &y) {return x.length() < y.length();});
void exec_containsSubstring(string& toTest,string& testFor, int startIndex, int endIndex, atomic_bool& res) {
bool hm {containsSubstring(toTest,testFor, startIndex, endIndex)};
if (hm)
res.store(hm);
}
double sumList(const std::vector<double> &l) {
double sum = 0.0;
for (const auto &c : l) sum += c;
return sum;
bool containsSubstring(string& toTest,string& testFor, int startIndex, int endIndex) {
bool matches {true};
while (startIndex < endIndex && startIndex < static_cast<int>(toTest.length())) {
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 tr{""};
*success = false;
std::cout << message;
if (!(std::cin >> tr)) {
if (!(std::getline(std::cin,tr))) {
std::cin.clear();
std::cin.ignore();
return "";
@ -48,3 +85,17 @@ string prompts(const string &message, bool *success) {
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;
}

View File

@ -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);
}

View File

@ -1,9 +0,0 @@
#ifndef STATS_H_INCLUDED
#define STATS_H_INCLUDED
#include <vector>
double med(std::vector<double> &scores,bool ignoreExtremes);
double avg(const std::vector<double> &scores,bool ignoreExtremes);
#endif // STATS_H_INCLUDED