-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainSH.cpp
141 lines (117 loc) · 4.25 KB
/
mainSH.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
#include <iostream>
#include "hash.h"
#include <thread>
#include <mutex>
mutex constructor_first;
using namespace std;
int main(int argc, char const *argv[]) {
Hash* myhash;
bool hash_exists;
ifstream infile("hash.txt");
hash_exists = infile.good();
if(!hash_exists){
string file_name;
cout << endl << "Enter csv file name: ";
cin >> file_name;
cout << endl << "Enter the number of buckets: ";
int num_buckets;
cin >> num_buckets;
cout << endl << "Enter number of registers per bucket: ";
int fd;
cin >> fd;
myhash = new Hash(num_buckets, fd, file_name);
}
else{
cout << "Opening hash.txt...";
myhash = new Hash("hash.txt");
}
bool run_program = true;
while(run_program){
bool run_parallel=true;
cout << endl << "Do you want to run it in parallel?(0/1): ";
cin >> run_parallel;
if(run_parallel){
bool instruct_seq=true;
vector<string> insert_inputs;
vector<int> search_inputs;
while(instruct_seq){
int instruct_type;
cout << "Type 1 to add insert, type 2 to add searches, type 3 to deploy threads, 0 to quit ";
cin >> instruct_type;
if(instruct_type == 1){
string row;
cout << endl << "Enter register to insert: ";
cin >> row;
cout << endl;
insert_inputs.push_back(row);
}
else if(instruct_type == 2){
int key;
cout << endl << "Enter id to search: ";
cin >> key;
cout << endl;
search_inputs.push_back(key);
}
else if( instruct_type == 3){
cout << insert_inputs.size() << endl;
thread threads[insert_inputs.size()];
for(int i=0; i<insert_inputs.size(); i++){
threads[i] = thread(&Hash::insert, myhash, insert_inputs[i]);
}
thread threads2[search_inputs.size()];
for(int i=0; i<search_inputs.size(); i++){
threads2[i] = thread(&Hash::search, myhash, search_inputs[i]);
}
for(int i=0; i<insert_inputs.size(); i++){
threads[i].join();
}
for(int i=0; i<search_inputs.size(); i++){
threads2[i].join();
}
instruct_seq = false;
run_program = false;
}
else if(instruct_type == 0){
instruct_seq = false;
}
}
}
else{
bool instruct_seq = true;
while(instruct_seq){
int instruct_type;
cout << "Type 1 to insert, type 2 to search, type 3 to remove hash, 0 to quit ";
cin >> instruct_type;
if(instruct_type == 1){
string row;
cout << endl << "Enter register to insert: ";
cin >> row;
cout << endl;
myhash -> insert(row);
cout << "inserted" << endl;
}
else if(instruct_type == 2){
int key;
cout << endl << "Enter id to search: ";
cin >> key;
cout << endl;
cout << myhash -> search(key) << endl;
}
else if(instruct_type == 3){
myhash -> removeHash();
instruct_seq = false;
run_program = false;
}
else if(instruct_type == 0){
instruct_seq = false;
}
}
if(run_program){
cout << endl << "Do you want keep going?(0/1): ";
cin >> run_program;
cout << "run_program: " << run_program << endl;
}
}
}
return 0;
}