-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapper.cpp
More file actions
62 lines (50 loc) · 1.61 KB
/
Copy pathmapper.cpp
File metadata and controls
62 lines (50 loc) · 1.61 KB
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
#include "utils.hpp"
// This function creates a new string from the given one, where
// all letters are converted to their corresponding lower-case char
// and all non-alphabetic chars not included.
std::string mapper::uniformize_string(std::string &in_string) {
std::string rez;
for (char c : in_string) {
if (std::isalpha(c)) {
rez += std::tolower(c);
}
}
return rez;
}
// The function executed by a mapper
void *mapper::mapper_func(void *arg) {
int file_nr = 0;
int idx = MARG->idx;
int &file_idx = *MARG->file_idx;
auto &fin = *MARG->fin;
// The set of words associated with the mapper with ID "idx"
auto &set = (*(MARG->map_arr))[idx];
std::string file_name;
std::string word;
while (1) {
pthread_mutex_lock(MARG->mutex_fin);
if (fin.peek() != EOF) { // A file is available for processing
file_nr = (file_idx++);
fin >> file_name;
} else { // No more files left to process
pthread_mutex_unlock(MARG->mutex_fin);
break;
}
pthread_mutex_unlock(MARG->mutex_fin);
// Open the file and read it word by word
std::ifstream temp_fin(file_name);
while (temp_fin.peek() != EOF) {
temp_fin >> word;
if (word.size() < 1) {
continue;
}
word = uniformize_string(word);
set.insert({word, file_nr});
}
temp_fin.close();
}
// Wait for all mappers to finish execution
pthread_barrier_wait(MARG->barrier);
free(arg);
return NULL;
}