Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Created by Tomáš on 20.12.16.
- //
- #include <set>
- #include <thread>
- #include <vector>
- #include "Controller.h"
- template <typename TimePoint>
- std::chrono::milliseconds to_ms(TimePoint tp) {
- return std::chrono::duration_cast<std::chrono::milliseconds>(tp);
- }
- Controller::Controller(std::vector<std::string> vector) {
- arguments = vector;
- }
- void Controller::control() {
- auto start = std::chrono::high_resolution_clock::now(); //timer start
- seq_or_para = seqOrParCounting(); //seq is true, para is false
- if (seq_or_para) {
- seqControl(); //call function for seq word counter
- } else {
- paraControl(); //call function for seq word counter
- }
- printResults(); //print results from map
- auto end = std::chrono::high_resolution_clock::now(); // timer end
- std::cout << "Needed " << to_ms(end - start).count() << " ms to finish.\n";
- }
- bool Controller::seqOrParCounting() {
- if("p" == arguments.front()){
- return false;
- }
- else if("s" == arguments.front()){
- return true;
- } else{
- throw "BAD argument, choose between p for parallel counting and s for sequential!";
- }
- }
- std::string Controller::validateWord(std::string word) {
- char chars[] = ".,?!:;-_\""; // add char
- for (unsigned int i = 0; i < strlen(chars); ++i) {
- word.erase(std::remove(word.begin(), word.end(), chars[i]), word.end()); // deleting specific(chars[]) chars from string
- std::transform(word.begin(), word.end(), word.begin(), ::tolower); // transfrom uppercase to lowercase
- }
- return word;
- }
- void Controller::seqControl() { // function for handling sequence reading
- while (arguments.size() != 1) {
- readFile(arguments.back(), this, true);
- arguments.pop_back();
- }
- }
- void Controller::printResults() {
- if (map.size() == 0) {
- std::cout << "\nThere are no words to print." << std::endl;
- } else {
- std::cout << "\nWords and their occurrence:" << std::endl;
- for (std::string word : set) {
- std::cout << word << ": " << map.get(word) << std::endl; // print key and value
- }
- }
- }
- void readFile(std::string path, Controller * controller, bool seq_or_para){
- Map mapForThread;
- std::vector<std::string> setForThread;
- std::ifstream file;
- file.open(path);
- if (!file.is_open()) { //if file wasnt opened sucessfully
- std::cout << "File on path: " + path + " cannot be open." << std::endl;
- } else {
- std::string word;
- while (file >> word) { // read words from file
- word = controller->validateWord(word);
- if (word != "") {
- if(seq_or_para){
- if (!controller->map.putWithoutLock(word)) { //without mutex
- controller->set.insert(word);
- }
- }else{
- if (!mapForThread.putWithoutLock(word)){
- setForThread.push_back(word);
- }
- }
- }
- }
- file.close();
- if(!seq_or_para){
- controller->map.putWithLock(&mapForThread,setForThread,&controller->set);
- }
- }
- }
- void Controller::paraControl() { //handler for para word counting
- std::vector<std::thread> threads;
- while (arguments.size() != 1) {
- std::thread thread(&readFile, arguments.back(), this, false); //create new thread
- arguments.pop_back();
- threads.push_back(std::move(thread));
- }
- for (unsigned int i=0; i<threads.size(); ++i)
- {
- if (threads[i].joinable())
- threads.at(i).join(); //wait until thread job is done
- }
- }
- //
- // Created by Tomáš on 21.12.16.
- //
- #include <set>
- #include <vector>
- #include "Map.h"
- void Map::putWithLock(Map * map1, std::vector<std::string> vector, std::set<std::string> * set) {
- // take all data from thread`s map and put them to global map
- mtx.lock();
- for(auto a:vector){
- auto it = map.find(a);
- if(it == map.end()){
- map[a] = map1->map[a];
- set->insert(a);
- }else{
- map[a] += map1->map[a];
- }
- }
- mtx.unlock();
- }
- bool Map::putWithoutLock(std::string key) {
- auto it = map.find(key);
- map[key]++;
- return it != map.end();
- }
- int Map::get(std::string key) {
- return map[key];
- }
- unsigned long Map::size() {
- return map.size();
- }
- Map::Map() {
- }
- Map::~Map() {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement