-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.cpp
153 lines (128 loc) · 3.95 KB
/
code.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include <thread>
#include <mutex>
using namespace std;
struct FileStatistics {
string name;
int id;
int count;
};
char ch;
int threshold;
long long totalFound = 0;
int aboveThreshold = 0;
int equalsThreshold = 0;
int belowThreshold = 0;
mutex m1, m2, m3, m4, m5;
vector<string> files;
vector<FileStatistics> sortedFiles;
void increaseTotalFound(int x) {
m1.lock();
totalFound += x;
m1.unlock();
}
void increaseAboveThreshold() {
m2.lock();
aboveThreshold++;
m2.unlock();
}
void increaseEqualsThreshold() {
m3.lock();
equalsThreshold++;
m3.unlock();
}
void increaseBelowThreshold() {
m4.lock();
belowThreshold++;
m4.unlock();
}
void secrchInFiles(int threadId, int leftFile, int rightFile) {
printf("TID%d --> Starting thread firstItem=%d, lastItem=%d\n", threadId, leftFile, rightFile + 1);
for (int i = leftFile; i <= rightFile; i++) {
ifstream curFile(files[i]);
int count = 0;
char c;
while (curFile >> c) {
if (c == ch) {
count++;
}
}
printf("TID%d --> File: %s, (%c) found=%d\n", threadId, files[i].c_str(), ch, count);
m5.lock();
sortedFiles.push_back({files[i], i, count});
m5.unlock();
increaseTotalFound(count);
if (count < threshold) increaseBelowThreshold();
else if (count == threshold) increaseEqualsThreshold();
else increaseAboveThreshold();
}
printf("TID%d --> Ending thread firstItem=%d, lastItem=%d\n", threadId, leftFile, rightFile);
}
void distribute(int leftThread, int rightTread, int filesPerThread) {
if (filesPerThread == 0) {
for (int i = leftThread; i <= rightTread; i++) {
printf("TID%d --> I have nothing to do\n", i);
}
return;
}
int numThreads = rightTread - leftThread + 1;
thread threads[numThreads];
for (int i = leftThread; i <= rightTread; i++) {
threads[i] = thread(secrchInFiles, i, i * filesPerThread, i * filesPerThread + filesPerThread - 1);
}
for (int i = leftThread; i <= rightTread; i++) {
threads[i].join();
}
}
int main(int argc, char *argv[]) {
freopen("in.txt", "r", stdin);
// take input from stdin
int numFiles, numThreads;
cin >> numFiles >> threshold;
files.resize(numFiles);
for (int i = 0; i < numFiles; i++) {
cin >> files[i];
}
// take arguments input
if (argc == 3) {
numThreads = atoi(argv[1]);
ch = *argv[2];
} else {
printf("Please provide the number of threads and the character to search for\n");
return 0;
}
printf("Main --> Search Engine searching for (%c) in %d files, using %d threads (with threshold=%d)\n", ch, numFiles, numThreads, threshold);
if (numThreads > numFiles) {
distribute(0, numFiles - 1, 1);
distribute(numFiles, numThreads - 1, 0);
}
else if (numThreads <= numFiles && numFiles % numThreads == 0) {
int filesPerThread = numFiles / numThreads;
distribute(0, numThreads - 1, filesPerThread);
}
else {
printf("Unexpected input :))\n");
return 0;
}
printf("Main --> TotalFound=%lld, AboveThreshold=%d, EqualsThreshold=%d, BelowThreshold=%d\n", totalFound, aboveThreshold, equalsThreshold, belowThreshold);
printf("Main --> Sorted list of files in out.txt\n");
sort(
sortedFiles.begin(),
sortedFiles.end(),
[&](const FileStatistics &a, const FileStatistics &b) {
if (a.count == b.count) {
return a.id > b.id;
}
return a.count > b.count;
}
);
freopen("out.txt", "w", stdout);
printf("Sorted list of files:\n");
for (int i = 0; i < numFiles; i++) {
printf("%d.[ %s ] (found = %d)\n", i + 1, sortedFiles[i].name.c_str(), sortedFiles[i].count);
}
return 0;
}