-
Notifications
You must be signed in to change notification settings - Fork 0
/
recover.cpp
114 lines (95 loc) · 2.96 KB
/
recover.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <omp.h>
#include <stdio.h>
#include <time.h>
#include <sstream>
// #include "Cloud_lab1.cpp"
#include<mutex>
#include <thread>
using namespace std;
#define NTHREADS 10
mutex mtx;
map<string,map<string,int> > dic;
void InvertedIndex(string filename){
// void InvertedIndex(string filename,map<string,map<string,int> > &dic){
ifstream infile;
infile.open (filename);
// map<string,map<string,int> > dic;
// map<string,int>::iterator it = dic.begin();
string word;
// clock_t begin = clock();
if (infile.is_open()){
//bool flag=true;
#pragma omp parrallel for default(none) num_threads(4) private(infile,word)
{
for (;infile>>word;)
{
// int tid = omp_get_thread_num();
// cout << "hilo_id:"<<tid<<" "<<word<<endl;
#pragma omp critical
if(dic.find(word) == dic.end()){
// cout<<"nueva: "<<word<<" añado: "<<filename<<endl;
map<string,int> tmp;
tmp.insert(make_pair(filename,1));
dic.insert(make_pair(word,tmp));
}else{
// cout<<" ya_puesta: "<<word<<endl;
if(dic[word].find(filename) == dic[word].end()){
// cout<<" añadiendo_txt = "<<filename<<endl;
dic[word].insert(make_pair(filename,1));
}
// else{
// cout<<" ya_esta = "<<filename<<endl;
// }
}
}
}
infile.close();
}else {
cout << "Error al abrir archivo"<<endl;
}
}
// void PrintDic(map<string,map<string,int> > &dic){
void PrintDic(){
map<string,map<string,int> >::iterator it;
for (it=dic.begin(); it!=dic.end(); ++it){
cout << it->first << ": ";
// map<string,int> tmp = it;
map<string,int>::iterator it1;
cout<<"("<<it->second.size()<<") ";
for(it1 = it->second.begin();it1 != it->second.end(); it1++){
cout << it1->first << " ";
}
cout<<endl;
}
}
void ReadFiles(){
string infile = "";
// pthread_t thread_id[NTHREADS];
map<string,map<string,int> > dic;
thread *thread_id = new thread[NTHREADS];
int i,j;
clock_t start= clock();
for(i=0; i < NTHREADS; i++){
stringstream out;
out << i + 1;
infile = "file" + out.str()+".txt";
cout<<infile<<endl;
// pthread_create(&thread_id[i], NULL, thread_funtion,NULL);
mtx.lock();
thread_id[i] = thread(InvertedIndex,infile);
mtx.unlock();
}
clock_t fin =clock();
double time=(double(fin-start)/CLOCKS_PER_SEC);
cout<<"time:"<<time<<endl;
PrintDic();
}
int main(){
// ReadFiles(5);
ReadFiles();
// fun_count_words("example1.txt");
}