-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
280 lines (245 loc) · 6.4 KB
/
main.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
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
#include <string>
#include "json.hpp"
#include <iostream>
#include <fstream>
#include <stack>
#include <queue>
using json = nlohmann::json;
struct Movie
{
//Movie values
std::string name; //title
std::string imgName; //path for image
std::string overview;
long id;
//filter values
int budget;
float rating;
long revenue;
float popularity;
Movie(int budget, std::string name, float rating, std::string imgName, std::string overview, long revenue, float popularity, long id) : overview(overview), revenue(revenue), popularity(popularity), id(id), budget(budget), name(name), rating(rating), imgName(imgName) {}
};
//Comparison operators for building the Trees and Heaps
struct CompareBudget
{
bool operator()(Movie const &p1, Movie const &p2)
{
return p1.budget < p2.budget;
}
};
struct CompareRating
{
bool operator()(Movie const &p1, Movie const &p2)
{
return p1.rating < p2.rating;
}
};
struct CompareRevenue
{
bool operator()(Movie const &p1, Movie const &p2)
{
return p1.revenue < p2.revenue;
}
};
struct ComparePopularity
{
bool operator()(Movie const &p1, Movie const &p2)
{
return p1.popularity < p2.popularity;
}
};
struct Node
{
Movie obj;
Node *left;
Node *right;
Node(Movie newMovie) : obj(newMovie), left(nullptr), right(nullptr) {}
};
class BST
{
Node *root = nullptr;
int filter = 0;
public:
void outputTree();
void setFilter(int num);
void insertNodeObj(Movie obj);
};
template <typename T>
Node *insertNode(Node *node, Movie newMovie, T compare) //generic binary search insert with comparison operator
{
if (node == nullptr)
{
node = new Node(newMovie);
}
else if (compare.operator()(node->obj, newMovie))
{ //compare depends on the object put in
node->right = insertNode(node->right, newMovie, compare);
}
else
{
node->left = insertNode(node->left, newMovie, compare);
}
return node;
}
void BST::setFilter(int num)
{
filter = num;
}
void BST::outputTree()
{
std::stack<Node *> stack;
Node *node = root;
json jsonObjects = json::array();
std::ofstream o("moviesOut.json");
//iterative traversal so I can stream all the movieObjects to jsonfile
while (node != nullptr || !stack.empty())
{
while (node != nullptr)
{
stack.push(node);
node = node->right;
}
node = stack.top();
stack.pop();
json j; //outputs to stream
j["title"] = node->obj.name;
j["image path"] = node->obj.imgName;
j["budget"] = node->obj.budget;
j["rating"] = node->obj.rating;
j["overview"] = node->obj.overview;
j["id"] = node->obj.id;
j["popularity"] = node->obj.popularity;
j["revenue"] = node->obj.revenue;
jsonObjects.push_back(j);
node = node->left;
}
o << jsonObjects; //stream out
std::cout << "done" << std::endl;
}
void BST::insertNodeObj(Movie obj)
{
switch (filter) //determines comparison operator needed
{
case 1:
{
ComparePopularity r1;
root = insertNode(root, obj, r1);
break;
}
case 2:
{
CompareRevenue r1;
root = insertNode(root, obj, r1);
break;
}
case 3:
{
CompareRating r1;
root = insertNode(root, obj, r1);
break;
}
default:
{
CompareBudget r1;
root = insertNode(root, obj, r1);
break;
}
}
}
//Heaps
template <typename T>
void HeapifyObjects(std::vector<Movie> mList, T priorityQueue)
{
std::ofstream o("../moviesOut.json");
json jsonObjects = json::array(); //create json array for sorted movie objects
for (auto i : mList)
priorityQueue.push(i); //priority push to sort
while (!priorityQueue.empty())
{
Movie movie = priorityQueue.top();
priorityQueue.pop();
json j; //outputs to stream
j["title"] = movie.name;
j["image path"] = movie.imgName;
j["budget"] = movie.budget;
j["rating"] = movie.rating;
j["overview"] = movie.overview;
j["id"] = movie.id;
j["popularity"] = movie.popularity;
j["revenue"] = movie.revenue;
jsonObjects.push_back(j);
}
o << jsonObjects; //stream out
std::cout << "done" << std::endl;
}
std::vector<Movie> jsonRead()
{ //reads the Json file
std::vector<Movie> movieList;
std::ifstream i("../data.json");
json j = json::parse(i); //parses through Json
for (unsigned int i = 0; i < j.size(); i++)
{
std::string poster_path;
if (j[i].at("poster_path") != nullptr)
{
poster_path = j[i].at("poster_path");
}
Movie mov(j[i].at("budget"), j[i].at("title"), j[i].at("vote_average"), poster_path, j[i].at("overview"), j[i].at("revenue"), j[i].at("popularity"), j[i].at("id"));
movieList.push_back(mov);
}
return movieList;
}
void HeapifyController(std::vector<Movie> movieList, int filter)
{
switch (filter) //controls the comparison operator needed for objects
{
case 1:
{
std::priority_queue<Movie, std::vector<Movie>, ComparePopularity> pq;
HeapifyObjects(movieList, pq);
break;
}
case 2:
{
std::priority_queue<Movie, std::vector<Movie>, CompareRevenue> pq;
HeapifyObjects(movieList, pq);
break;
}
case 3:
{
std::priority_queue<Movie, std::vector<Movie>, CompareRating> pq;
HeapifyObjects(movieList, pq);
break;
}
default:
{
std::priority_queue<Movie, std::vector<Movie>, CompareBudget> pq;
HeapifyObjects(movieList, pq);
break;
}
}
}
void BSTController(std::vector<Movie> movieList, int filter)
{
BST movieTree;
movieTree.setFilter(filter); //sets filter for BST
for (unsigned int i = 0; i < movieList.size(); i++)
{
movieTree.insertNodeObj(movieList[i]);
}
movieTree.outputTree(); //outputs back to JSON for sending
}
int main(int argc, char **argv)
{
int filter = atoi(argv[1]); //grabs command line arguments from node
int dataStructure = atoi(argv[2]);
std::vector<Movie> movieList = jsonRead(); //reads the json from the movie list
if (dataStructure == 1)
{
BSTController(movieList, filter);
}
else
{
HeapifyController(movieList, filter);
}
}