-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathget_possible_data_structures.cpp
More file actions
150 lines (134 loc) · 6.79 KB
/
Copy pathget_possible_data_structures.cpp
File metadata and controls
150 lines (134 loc) · 6.79 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
#include <iostream>
#include <vector>
#include <string>
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;
}