-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActor.cpp
More file actions
405 lines (340 loc) · 13.6 KB
/
Copy pathActor.cpp
File metadata and controls
405 lines (340 loc) · 13.6 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include "Actor.h"
#include <iterator>
// constructors
Actor::Actor()
{
Actor("", 0);
}
Actor::Actor(string n)
{
Actor(n, 0);
}
Actor::Actor(string n, int fb)
{
name = n;
fbLikesForActor = fb;
}
//setters
void Actor::setName(string n)
{
name = n;
}
void Actor::setLikes(int likes)
{
fbLikesForActor = likes;
}
void Actor::addMovie(Movie *m)
{
forward_list<Movie *> mov = movieList[m->getTitleYear()];
mov.emplace_front(m);
movieList[m->getTitleYear()] = mov;
}
// getters
string Actor::getName()
{
return name;
}
int Actor::getLikes()
{
return fbLikesForActor;
}
map<short int, forward_list<Movie *>> Actor::getMovie()
{
return movieList;
}
int Actor::getCountOfMovies()
{
int count = 0;
map<short int, forward_list<Movie *>>::iterator it;
for (it = movieList.begin(); it != movieList.end(); it++)
{
forward_list<Movie *> movies = it->second;
count += distance(movies.begin(), movies.end());
}
return count;
}
// helper method to display all actors
void Actor::displayAllActors(unordered_map<string, ActorAVL> allActors)
{
unordered_map<string, ActorAVL>::iterator it; //iterator
for (it = allActors.begin(); it != allActors.end(); it++)
{
cout << "----------- " << it->first << " -----------" << endl;
ActorAVL avl = it->second;
avl.traverse();
}
}
// helper functions
void Actor::displayAllMovies()
{
cout << "Movies: " << endl;
map<short int, forward_list<Movie *>>::iterator it; //iterator
// traversing through the map of movies of given actor (movieList)
for (it = this->movieList.begin(); it != this->movieList.end(); it++)
{
if (it->first != 0)
cout << "----------- " << it->first << " -----------" << endl; // year
forward_list<Movie *> movies = it->second; // all movies of certain year
for (auto it = movies.begin(); it != movies.end(); ++it) // traverse thorugh the forward_list of movies
{
Movie *m = (*it); // pointer to movie
cout << m->getTitle() << endl; // name of movie
}
cout << endl;
}
cout << endl;
}
void Actor::display()
{
/*display all the details of the actor such as actor name , fb likes,total movies &
names of the movies actor has acted in */
cout << "Name: " << name << endl;
cout << "FB Likes: " << getLikes() << endl;
cout << "Total Movies: " << getCountOfMovies() << endl
<< endl;
displayAllMovies();
}
// methods related to actors specified in document provided
Actor *Actor::searchActor(string name, unordered_map<string, ActorAVL> allActors, bool display)
{
/*
The method will take the actor name as input and search for the actor in the unordered map,
'allActors', with key as the first two alphabets of actor name and value contain an avl
of pointers to actor nodes named as 'ActorAVL'.
this method will search for the input actor name by searching the first two alphabets of the name
in map if the key exist it will find the actor in the avl and return the actor pointer if found
otherwise it will return null. if display parameter is true it will print the details of the actor.
Time Complexity:
Best Case: O(1)
Worst Case: O(nlog(n))
*/
Actor *actor = NULL;
// finding actor, returns an avl of pointers to Actor
if (allActors.find(name.substr(0, 2)) != allActors.end()) // if key exists, search for actor in ActorAVL
actor = allActors[name.substr(0, 2)].search(name); // find actor in avl
if (display)
{
if (!actor) //if actor is not in the records
cout << "Actor not found. Try Again" << endl;
else // if actor is present, print the details of actor
actor->display();
}
return actor;
}
//second method of the list
forward_list<Actor *> Actor::getCoActors(string name, unordered_map<string, ActorAVL> allActors)
{
/*
This method will take the name of the actor as input and search for actor in the map ,'allActors',
with key as the first two alphabets of actor name and value contain an avl of *Actor
named as 'ActorAVL'.
In this method first we will search for the actor
case1: If actor not found then it will return an empty list of coactor
case2:
1. If the actor is found in the map then we will traverse through the movies map
(that has year as key and values as forward list of pointer to Movie)
2. For each movie traverse through the array of actors, these are the coactors.
3. Add coactors to list of coactors
4. Return the list of coactors
Time Complexity:
Best Case: O(n)
Worst Case: O(n^2)
*/
forward_list<Actor *> coactorsList; // forward_list for storing coactors
Actor *actor = searchActor(name, allActors, false); // finding the actor in allActors map
// actor not found
if (!actor)
{
cout << "Actor not found" << endl;
return coactorsList;
}
// actor found: traverse thorugh the map of movies and find coactors
map<short int, forward_list<Movie *>>::iterator it; //iterator
// traversing through the map of movies of given actor (movieList)
for (it = actor->movieList.begin(); it != actor->movieList.end(); it++)
{
forward_list<Movie *> movies = it->second; // all movies of certain year
for (auto it = movies.begin(); it != movies.end(); ++it) // traverse thorugh the forward_list of movies
{
Movie *m = (*it); // pointer to movie
// finding coactors
Actor **coActors = (*it)->getActor(); // all actors of this movie
for (Actor **i = coActors; i < coActors + 3; i++)
{
if ((*(i))->getName() != actor->getName()) // if actor is not the actor itself
coactorsList.emplace_front(*(i));
}
}
}
return coactorsList;
}
//second method of the list
void Actor::displayCoActors(string name, unordered_map<string, ActorAVL> allActors)
{
/*
This method will take the name of the actor as input and search for actor in the map ,'allActors',
with key as the first two alphabets of actor name and value contain an avl of *actor
named as 'ActorAVL'.
In this method first we will search for the actor
case1: If actor not found then it an error message is displayed
case2:
1. If the actor is found in the map then we will traverse through the movies map
(that has year as key and values as forward list of pointer to Movie)
2. For each movie traverse through the array of actors, these are the coactors.
3. Print the name of coactors
Time Complexity:
Best Case: O(n)
Worst Case: O(n^2)
*/
Actor *actor = searchActor(name, allActors, false); // finding the actor in allActors map
// actor not found
if (!actor)
{
cout << "Actor not found" << endl;
return;
}
// actor found: traverse thorugh the map of movies and find coactors
map<short int, forward_list<Movie *>>::iterator it; //iterator
// traversing through the map of movies of given actor (movieList)
for (it = actor->movieList.begin(); it != actor->movieList.end(); it++)
{
forward_list<Movie *> movies = it->second; // all movies of certain year
for (auto it = movies.begin(); it != movies.end(); ++it) // traverse thorugh the forward_list of movies
{
Movie *m = (*it); // pointer to movie
cout << "MOVIE: " << m->getTitle() << endl; // name of movie
// finding coactors
Actor **coActors = (*it)->getActor(); // all actors of this movie
cout << "\tCoActors: ";
for (Actor **i = coActors; i < coActors + 3; i++) // actors of this movie
{
if ((*(i))->getName() != actor->getName()) // if actor is not himself
cout << (*(i))->getName() << ", ";
}
cout << endl
<< endl;
}
}
}
void Actor::getUniqueCoActors(string name, unordered_map<string, ActorAVL> allActors)
{
/* Note: This Method uses unordered_map<string, ActorAVL> allActors that stores all actors
Print the name of all co-actors of an actor only once. For each co-actor,
print title of the movies in which both acted in.
Methodology:
Implementaton using unordered map of Actor having movies as value
1. find coactors
2. insert name of coactor in map as a key, if not present
3. add movie title to the forward_list of movies coresponding to this coactor (value of map)
Time Complexity:
Best Case: O(n)
Worst Case: O(n^2)
*/
Actor *actor = searchActor(name, allActors, false); // finding the actor in allActors map
// actor not found
if (!actor)
{
cout << "Actor not found" << endl;
return;
}
// actor found: traverse thorugh the map of movies and find coactors
// map for storing coactors and corresponding movies
unordered_map<string, forward_list<string>> coactorsMap; // key: name of coactor, value: forward_list of title of movie
map<short int, forward_list<Movie *>>::iterator it; //iterator
// traversing through the map of movies of given actor (movieList)
for (it = actor->movieList.begin(); it != actor->movieList.end(); it++)
{
forward_list<Movie *> movies = it->second; // all movies of certain year
for (auto it = movies.begin(); it != movies.end(); ++it) // traverse thorugh the forward_list of movies
{
Movie *m = (*it); // pointer to movie
// finding coactors
Actor **coActors = (*it)->getActor(); // all actors of this movie
for (Actor **a = coActors; a < coActors + 3; a++) //traversing through three actors of every movie
{
Actor *coAct = (*a);
if (coAct->getName() != actor->getName()) // coActor is not the one we are searching coactors for
{
forward_list<string> movies = coactorsMap[coAct->getName()]; // get key
movies.emplace_front(m->getTitle()); // insert to movies
coactorsMap[coAct->getName()] = movies; // insert to map
}
}
}
}
// display
unordered_map<string, forward_list<string>>::iterator itCoactors; //iterator
// traversing through the map of coactors
for (itCoactors = coactorsMap.begin(); itCoactors != coactorsMap.end(); itCoactors++)
{
cout << itCoactors->first << endl; // name of coactor
cout << "Movies: ";
forward_list<string> movies = itCoactors->second; // all common movies
for (auto mov = movies.begin(); mov != movies.end(); ++mov) // traverse thorugh the forward_list of movies
{
cout << (*mov) << "\t "; //print movie title
}
cout << endl
<< endl;
}
}
bool inCoactors(forward_list<Actor *> coactors, string name)
// check if the given actor name is in coactor list or not
{
for (auto it = coactors.begin(); it != coactors.end(); ++it) // traverse thorugh the forward_list of movies
{
if ((*it)->getName() == name)
return true;
}
return false;
}
void Actor::getCoActorsOfCoActors(string name, unordered_map<string, ActorAVL> allActors)
{
/*
This method will take the actor name as input and
1) Find the list of coactors of given actor
2) Traverse the list and find the coactors of each coactor (subactors)
3) Add to avl,'allCoactors', if subactor is not coactor of actor and actor itself
4) Impelmentation of AVL is such that it does not take duplicates
4) Traverse and print allCoactors
Time Complexity:
Best Case: O(n^2)
Worst Case: O(n^2)
*/
forward_list<Actor *> coactors = getCoActors(name, allActors); // coactors of actor
ActorAVL allCoactor; // avl to store coactors of coactors
if (!coactors.empty())
{
for (auto it1 = coactors.begin(); it1 != coactors.end(); ++it1) // traverse thorugh the forward_list of movies
{
forward_list<Actor *> subCoactors = getCoActors((*it1)->name, allActors); // finding coactors of coactors
for (auto it2 = subCoactors.begin(); it2 != subCoactors.end(); ++it2) // traverse thorugh the forward_list of coactors
{
Actor *subActor = *it2;
// add to avl if subactor is not coactor of actor and actor itself
if (subActor->getName() != name && !inCoactors(coactors, subActor->getName()))
allCoactor.insert(subActor);
}
}
allCoactor.traverse(); // printing all coactors of coactors
}
}
bool Actor::isCoActor(string nameA, string nameB, unordered_map<string, ActorAVL> allActors)
{
/* check if the two actors( A & B) are coactors or not
1)find the coactors of A
2)if B is in list of coactors of A then both are coactors
Best Case: O(n)
Worst Case: O(n^2)
*/
forward_list<Actor *> coactors = getCoActors(nameA, allActors); //coactors of A
if (!coactors.empty())
{
for (auto it = coactors.begin(); it != coactors.end(); ++it) // traverse thorugh the forward_list of movies
{
if ((*it)->getName() == nameB) // B is present in list
return true;
}
}
return false; // if B not found in the list
}