-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsearch engine.cpp
More file actions
281 lines (243 loc) · 10.7 KB
/
Copy pathsearch engine.cpp
File metadata and controls
281 lines (243 loc) · 10.7 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include <iostream>
#include <vector>
#include <string>
#include <chrono>
#include <numeric>
#include <DynamicArray>
using namespace std;
// A function that takes an API as input and returns a list of data structures that can support it
vector<string> get_possible_data_structures(string api) {
// A vector to store the possible data structures
vector<string> result;
// A flag to indicate if the API requires random access
bool random_access = false;
// A flag to indicate if the API requires sorting
bool sorting = false;
// A flag to indicate if the API requires insertion or deletion at any position
bool insertion_deletion = false;
// A flag to indicate if the API requires ordered data
bool ordered_data = false;
// Parse the API string and set the flags accordingly
// This is a simplified example, you may need to use more sophisticated parsing techniques
if (api.find("random access") != string::npos) {
random_access = true;
}
if (api.find("sorting") != string::npos) {
sorting = true;
}
if (api.find("insertion") != string::npos || api.find("deletion") != string::npos) {
insertion_deletion = true;
}
if (api.find("ordered") != string::npos) {
ordered_data = true;
}
// Filter out the data structures that are not suitable for the API
// This is a simplified example, you may need to use more sophisticated filtering techniques
if (!random_access && !sorting && !insertion_deletion && !ordered_data) {
// No specific requirement, any data structure can be used
result.push_back("array");
result.push_back("vector");
result.push_back("linked list");
result.push_back("tree");
result.push_back("hash table");
} else if (random_access && !sorting && !insertion_deletion && !ordered_data) {
// Only random access required, array, vector, and hash table can be used
result.push_back("array");
result.push_back("vector");
result.push_back("hash table");
} else if (!random_access && sorting && !insertion_deletion && !ordered_data) {
// Only sorting required, linked list and tree can be used
result.push_back("linked list");
result.push_back("tree");
} else if (!random_access && !sorting && insertion_deletion && !ordered_data) {
// Only insertion or deletion required, linked list and hash table can be used
result.push_back("linked list");
result.push_back("hash table");
} else if (!random_access && !sorting && !insertion_deletion && ordered_data) {
// Only ordered data required, array, vector, linked list, and tree can be used
result.push_back("array");
result.push_back("vector");
result.push_back("linked list");
result.push_back("tree");
} else if (random_access && sorting && !insertion_deletion && !ordered_data) {
// Random access and sorting required, array and vector can be used
result.push_back("array");
result.push_back("vector");
} else if (random_access && !sorting && insertion_deletion && !ordered_data) {
// Random access and insertion or deletion required, vector and hash table can be used
result.push_back("vector");
result.push_back("hash table");
} else if (random_access && !sorting && !insertion_deletion && ordered_data) {
// Random access and ordered data required, array and vector can be used
result.push_back("array");
result.push_back("vector");
} else if (!random_access && sorting && insertion_deletion && !ordered_data) {
// Sorting and insertion or deletion required, linked list and tree can be used
result.push_back(“linked list”); result.push_back(“tree”);
} else if (!random_access && sorting && !insertion_deletion && ordered_data) {
// Sorting and ordered data required, linked list and tree can be used
result.push_back(“linked list”); result.push_back(“tree”);
} else if (!random_access && !sorting && insertion_deletion && ordered_data) {
// Insertion or deletion and ordered data required, linked list and tree can be used
result.push_back(“linked list”); result.push_back(“tree”);
} else if (random_access && sorting && insertion_deletion && !ordered_data) {
// Random access, sorting, and insertion or deletion required, no suitable data structure exists
result.clear();
} else if (random_access && sorting && !insertion_deletion && ordered_data) {
// Random access, sorting, and ordered data required, array and vector can be used
result.push_back(“array”); result.push_back(“vector”);
} else if (random_access && !sorting && insertion_deletion && ordered_data) {
// Random access, insertion or deletion, and ordered data required, no suitable data structure exists
result.clear();
}
// Rank the data structures based on their advantages and disadvantages for the API
// This is a simplified example, you may need to use more sophisticated ranking techniques
// Define a helper function that compares two data structures based on some criteria
bool compare(string ds1,string ds2,string api){
// For example: prefer vectors over arrays for frequent resizing,
// prefer hash tables over other data structures for fast search,
// prefer arrays over other data structures for minimal memory usage,
// prefer trees over other data structures for balanced structure
if(api.find(“resizing”)!=string::npos) {
if(ds1==“vector”&&ds2==“array”) {
return true;
} else if(ds1==“array”&&ds2==“vector”) {
return false;
}
}
if(api.find(“search”)!=string::npos) {
if(ds1==“hash table”&&ds2!=“hash table”) {
return true;
} else if(ds1!=“hash table”&&ds2==“hash table”) {
return false;
}
}
if(api.find(“memory”)!=string::npos) {
if(ds1==“array”&&ds2!=“array”) {
return true;
} else if(ds1 != “array” && ds2 ==“array”) {
return false;
}
}
if(api.find(“balanced”)!=string::npos) {
if(ds1==“tree”&&ds2!=“tree”) {
return true;
} else if(ds1!=“tree”&&ds2==“tree”) {
return false;
}
}
// If none of the criteria applies, return false by
default (no preference)
return false;
}
// Sort the vector of possible data structures using the helper function
sort(result.begin(),result.end(),[&](string ds1,string ds2) { return compare(ds1,ds2,api); });
// Return the vector of possible data structures
return result;
}
vector<vector<int>> get_time_taken(vector<string> data_structures, vector<string> api) {
vector<vector<int>> time_taken;
for(auto ds : data_structures) {
vector<int> time_for_ds;
for(auto method : api) {
auto start = chrono::high_resolution_clock::now();
if(ds == "array") {
DynamicArray array;
if(method == "add(string s)") {
array.append("hello");
}
else if(method == "remove()") {
array.remove("hello");
}
else if(method == "contains()") {
array.contains("hello");
}
else if(method == "size()") {
array.size();
}
else if(method == "print()") {
array.print();
}
}
else if(ds == "linked list") {
LinkedList list;
if(method == "add(string s)") {
list.append("hello");
}
else if(method == "remove()") {
list.remove("hello");
}
else if(method == "contains()") {
list.contains("hello");
}
else if(method == "size()") {
list.size();
}
else if(method == "print()") {
list.print();
}
}
else if(ds == "BST") {
BST tree;
if(method == "add(string s)") {
tree.append("hello");
}
else if(method == "remove()") {
tree.remove("hello");
}
else if(method == "contains()") {
tree.contains("hello");
}
else if(method == "size()") {
tree.size();
}
else if(method == "print()") {
tree.print();
}
}
else if(ds == "hash table") {
HashTable table;
if(method == "add(string s)") {
table.append("hello");
}
else if(method == "remove()") {
table.remove("hello");
}
else if(method == "contains()") {
table.contains("hello");
}
else if(method == "size()") {
table.size();
}
else if(method == "print()") {
table.print();
}
}
auto stop = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::microseconds>(stop - start);
time_for_ds.push_back(duration.count());
}
time_taken.push_back(time_for_ds);
}
return time_taken;
}
string get_best_data_structure(vector<string> data_structure, vector<vector<int>> time_taken, vector<string> api) {
string best_data_structure;
int min_avg_time = INT_MAX;
for(int i = 0; i < data_structure.size(); i++) {
string ds = data_structure[i];
vector<int> tt = time_taken[i];
int avg_time = accumulate(tt.begin(), tt.end(), 0) / tt.size();
if(avg_time <= min_avg_time) {
best_data_structure = ds;
min_avg_time = avg_time;
}
}
return best_data_structure;
}
int main() {
//vector<string> api = {"insert(int x)", "delete(int x)", "search(int x)", "sort()", "size()"};
vector<string> api = {"add(string s)", "remove()", "contains()", "size()", "print()"};
vector<string> data_structures = get_possible_data_structures(api);
vector<vector<int>> time_taken = get_time_taken(data_structures, api);
}