-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActorGraph.cpp
More file actions
executable file
·127 lines (105 loc) · 3.02 KB
/
Copy pathActorGraph.cpp
File metadata and controls
executable file
·127 lines (105 loc) · 3.02 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
/*
* ActorGraph.cpp
* Author: Nabi Ozberkman
*
*/
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <utility>
#include "ActorGraph.hpp"
using namespace std;
ActorGraph::ActorGraph(void) {}
void ActorGraph::insert(string actorName, string movieName, int year, bool weighted) {
unordered_map<string, Movie*>::iterator it;
unordered_map<string, Actor*>::iterator it2;
pair<unordered_map<string,Movie*>::iterator, bool> s;
pair<unordered_map<string,Actor*>::iterator, bool> s2;
movieName += ("#@" + to_string(year));
if((it = this->databaseMovie.find(movieName)) == this->databaseMovie.end())
{
Movie* movieObject = new Movie(movieName, year, weighted);
s = this->databaseMovie.insert(pair<string, Movie*> (movieName, movieObject));
it = s.first;
}
if((it2 = this->databaseActor.find(actorName)) != this->databaseActor.end()) {
addFriend(it2->second, it->second);
it->second->addActor(it2->second);
//it2->second->addMovie(it->first);
}
else {
s2 = this->databaseActor.insert(pair<string, Actor*> (actorName, new Actor(actorName)));
it2 = s2.first;
addFriend(it2->second, it->second);
it->second->addActor(it2->second);
//it2->second->addMovie(it->first);
}
}
void ActorGraph::addFriend(Actor* actor, Movie* movie) {
int movieWeight = movie->weight;
vector<GraphEdge*> & friends = actor->friends;
GraphEdge* current;
auto it = movie->actors.begin();
auto itE = movie->actors.end();
while(it != itE) {
current = new GraphEdge(actor, (*it), movie->name, movieWeight);
friends.push_back(current);
allEdges.push_back(current);
(*it)->addEdge(current);
it++;
}
}
bool ActorGraph::loadFromFile(const char* in_filename, bool use_weighted_edges) {
// Initialize the file stream
ifstream infile(in_filename);
// ActorGraph* graph = new ActorGraph();
if(use_weighted_edges)
this->weighted = true;
bool have_header = false;
// keep reading lines until the end of file is reached
while (infile) {
string s;
// get the next line
if (!getline( infile, s )) break;
if (!have_header) {
// skip the header
have_header = true;
continue;
}
istringstream ss( s );
vector <string> record;
while (ss) {
string next;
// get the next string before hitting a tab character and put it in 'next'
if (!getline( ss, next, '\t' )) break;
record.push_back( next );
}
if (record.size() != 3) {
// we should have exactly 3 columns
continue;
}
string actor_name(record[0]);
string movie_title(record[1]);
int movie_year = stoi(record[2]);
this->insert(actor_name, movie_title, movie_year, use_weighted_edges);
// we have an actor/movie relationship, now what?
}
if (!infile.eof()) {
cerr << "Failed to read " << in_filename << "!\n";
return false;
}
infile.close();
// this->buildAdjacent();
return true;
}
void ActorGraph::buildAdjacent() {
auto it = this->databaseActor.begin();
auto itE = this->databaseActor.end();
Movie* currentMov;
Actor* currentAct;
while(it != itE) {
currentAct = it->second;
}
}