-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathActor.cpp
More file actions
224 lines (197 loc) · 8.07 KB
/
Actor.cpp
File metadata and controls
224 lines (197 loc) · 8.07 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
#include <list>
#include <iostream>
struct Profile
{ // This struct will maintain year wise movies of actor and also its co-actors.
std::multimap<int,Movie*> yearWiseMap;
std::map<std::string,std::list<Movie*> > co_actorMap;
};
// Container for storing Actors mapped to their profiles. Uses STL maps.
class Actor
{
private:
// Mapping from string to profile struct
std::map <std::string,Profile> map;
void createProfile(Movie* ptr, int index)
{
// If actor is not in map already, then a new mapping is created from actor name to profile
// Setting list of pointers of movies
std::list<Movie*> ptrs;
ptrs.push_back(ptr);
// Making new profile
Profile profile;
// Inserting year wise in profile map to maintain chronological order
profile.yearWiseMap.insert({(*ptr).title_year,ptr});
/* Inserting co-actors name and the linked list of pointers
in which both actor and its co-actors have worked*/
for (int ind = 0; ind < 3; ind++)
{
if(index != ind)
profile.co_actorMap.insert({(*ptr).actors[ind],ptrs});
}
this->map.insert({(*ptr).actors[index],profile});
}
void updateProfile(Movie* ptr,int index)
{
// If actor is already present as key in map, then its profile is updated.
// Finding profile of actor
auto it = this->map.find((*ptr).actors[index]);
// Inserting year and movie pointer in profile map.
it->second.yearWiseMap.insert({(*ptr).title_year,ptr});
for (int ind = 0; ind < 3; ind++)
{
if(index != ind)
{
// If co-actor is not in co-actor map, a new mapping from co-actor to linked list is made.
if (it->second.co_actorMap.count((*ptr).actors[ind]) == 0 && (*ptr).actors[ind] != "")
{
std::list<Movie*> ptrs;
ptrs.push_back(ptr);
it->second.co_actorMap.insert({(*ptr).actors[ind],ptrs});
}
/* If there is a co-actor already present in co-actorMap,
then the movie is inserted in the linked list of that co-actor*/
else if ((*ptr).actors[ind] != "")
it->second.co_actorMap.at((*ptr).actors[ind]).push_back(ptr);
}
}
}
public:
/*
Description: This function will get all the movie nodes from movieCollection and will set up profile struct.
Time Complexity:
-Best Case: O(n)
-Worst Case: O(n)
*/
void createIndex(MovieCollection* obj)
{
// Iterating through all the movie nodes and maintaing profile
for (auto &pr :obj->map)
{
// Returns movie pointer
Movie* ptr = &pr.second;
for (int index = 0; index < 3; index++)
{
if (this->map.count((*ptr).actors[index]) == 0 && (*ptr).actors[index] != "")
createProfile(ptr,index);
else if ((*ptr).actors[index] != "")
updateProfile(ptr,index);
}
}
}
/*
Description: Search and print movies of the given actor.
Time Complexity for searching:
-Best Case: O(logn)
-Worst Case: O(logn)
Time Complexity for printing:
-Best Case: O(n)
-Worst Case: O(n)
*/
void search(std::string actorName)
{
// Returns all the movie pointers of given actor in log(n) time
auto it = this->map.find(actorName);
std::cout<<"Actor Name : " << it->first<< '\n'<<"\t\tMOVIES\n\n";
// Printing the movies
for (auto &pr :it->second.yearWiseMap)
{
std::cout<<"Movie name : "<<(*pr.second).movie_title<<" | "<<"Year : "<<pr.first<<std::endl;
}
}
/*
Description: Search and print movies and co actors of the given actor.
Time Complexity for searching:
-Best Case: O(logn)
-Worst Case: O(logn)
Time Complexity for printing:
-Best Case: O(n)
-Worst Case: O(n)
*/
void searchCoActors(std::string actorName)
{
//returns all the movie pointers of given actor in log(n) time
auto it = this->map.find(actorName);
std::cout<<"Actor Name : " << it->first<< '\n'<<"\t\tMOVIES\n\n";
//printing the movies
for (auto &pr :it->second.yearWiseMap)
{
std::cout<<"Movie : "<<(*pr.second).movie_title<<" | "<<"Year : "<<pr.first<<" | Co-Actors : ";
for (int index = 0;index < 3; index++)
{
if ((*pr.second).actors[index] != actorName )
std::cout<<(*pr.second).actors[index]<<" | ";
}
std::cout<<std::endl;
}
}
/*
Description: Searches and prints unique co-actors of given actor and its movies
Time Complexity for searching:
-Best Case: O(logn)
-Worst Case: O(logn)
Time Complexity for printing:
-Best Case: O(n2)
-Worst Case: O(n2)
*/
void searchUniqueCoActors(std::string actorName)
{
auto it = this->map.find(actorName);
std::cout<<"Actor Name : " << it->first<< "\n\n"<< "CO-ACTORS\t\tMOVIE\t\t\t\t\t\t\tYEAR\n\n";
for (auto &pr :it->second.co_actorMap)
{
std::cout<<pr.first;
for (auto &pr2: pr.second)
{
std::cout<<"\t\t"<<(*pr2).movie_title<<"\t\t\t\t\t\t\t"<<(*pr2).title_year<<"\n";
}
}
}
/*
Description: Searches and prints the co-actors of co_actors of given actor
Time Complexity for searching:
-Best Case: O(logn)
-Worst Case: O(logn)
Time Complexity for printing:
-Best Case: O(n2)
-Worst Case: O(n2)
*/
void printCoActorsOfCoActors(std::string actorName)
{
auto it = this->map.find(actorName);
int count = 0;
for (auto &pr :it->second.co_actorMap)
{
std::cout<<"CO ACTOR NUMBER "<<count<<" : "<<pr.first<<" : "<<'\n'<<std::endl;
auto it2 = this->map.find(pr.first);
for (auto &pr2 :it2->second.co_actorMap)
{
std::cout<<pr2.first<<" , ";
}
count++;
std::cout<<'\n'<<std::endl;
}
}
/*
Description: Searches and tells if two actors are co-actors or not.
Time Complexity for searching:
-Best Case: O(logn)
-Worst Case: O(logn)
Time Complexity for printing:
-Best Case: O(n)
-Worst Case: O(n)
*/
void areCoActors(std::string actor1,std::string actor2)
{
auto it = this->map.find(actor1);
if (it->second.co_actorMap.count(actor2) != 0)
{
auto it2 = it->second.co_actorMap.find(actor2);
for(auto &pr: it2->second)
{
std::cout<<(*pr).movie_title<<std::endl;
}
}else{
std::cout<<"Not co-Actors"<<std::endl;
}
}
};