-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapLoader.cpp
149 lines (117 loc) · 3.54 KB
/
MapLoader.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
#include "provided.h"
#include "MyMap.h"
#include <string>
#include <queue>
#include <vector>
#include <iostream>
#include <fstream>
using namespace std;
class MapLoaderImpl
{
public:
MapLoaderImpl();
~MapLoaderImpl();
bool load(string mapFile);
size_t getNumSegments() const;
bool getSegment(size_t segNum, StreetSegment& seg) const;
private:
vector<StreetSegment> vectorOfStreetSegs;
};
MapLoaderImpl::MapLoaderImpl()
{
}
MapLoaderImpl::~MapLoaderImpl()
{
}
bool MapLoaderImpl::load(string mapFile)
{
ifstream infile(mapFile);
if ( ! infile ) // Did opening the file fail?
{
cerr << "Error: Cannot open data.txt!" << endl;
return false;
}
string s;
queue<string> queueOfStrings;
// push all the strings into the queue
// O(N)
while (getline(infile,s))
queueOfStrings.push(s);
// constructing items and putting them into MyMap
//O(N)
while(!queueOfStrings.empty())
{
StreetSegment streetSeg;
// load the name
streetSeg.streetName = queueOfStrings.front();
queueOfStrings.pop();
// load the geosegment
string temp = queueOfStrings.front();
size_t t1 = temp.find(','); // the end of the start lat
string lat1 = temp.substr(0,t1);
size_t t2 = temp.find(' ',t1+2); // the end of the start long
string lon1 = temp.substr(t1+2,t2-t1-2);
size_t t3 = temp.find(',',t2); // the end of the end lat
string lat2 = temp.substr(t2+1,t3-t2-1);
string lon2 = temp.substr(t3+1);
streetSeg.segment = GeoSegment(GeoCoord(lat1, lon1), GeoCoord(lat2,lon2));
queueOfStrings.pop();
// load the attractions
int numOfAttractions = stoi(queueOfStrings.front());
queueOfStrings.pop();
while (numOfAttractions > 0)
{
Attraction attraction;
string temp2 = queueOfStrings.front();
queueOfStrings.pop();
size_t pos = temp2.find('|');
attraction.name = temp2.substr(0,pos);
size_t pos1 = temp2.find(',',pos);
string attractionLat = temp2.substr(pos+1,pos1-pos-1);
string attractionLon = temp2.substr(pos1+2);
attraction.geocoordinates = GeoCoord(attractionLat, attractionLon);
streetSeg.attractions.push_back(attraction);
numOfAttractions--;
}
// loading MyMap
vectorOfStreetSegs.push_back(streetSeg);
}
return true;
}
size_t MapLoaderImpl::getNumSegments() const
{
//return m_map->size();
return vectorOfStreetSegs.size();
}
// O(1)
bool MapLoaderImpl::getSegment(size_t segNum, StreetSegment &seg) const
{
if(segNum < 0 || segNum >= getNumSegments())
return false;
// this should be doing the right thing
seg = vectorOfStreetSegs[segNum];
return true;
}
//******************** MapLoader functions ************************************
// These functions simply delegate to MapLoaderImpl's functions.
// You probably don't want to change any of this code.
MapLoader::MapLoader()
{
m_impl = new MapLoaderImpl;
}
MapLoader::~MapLoader()
{
delete m_impl;
}
bool MapLoader::load(string mapFile)
{
return m_impl->load(mapFile);
}
size_t MapLoader::getNumSegments() const
{
return m_impl->getNumSegments();
}
bool MapLoader::getSegment(size_t segNum, StreetSegment& seg) const
{
return m_impl->getSegment(segNum, seg);
}