-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreetMap.cpp
127 lines (112 loc) · 2.97 KB
/
StreetMap.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
#include "provided.h"
#include <string>
#include <vector>
#include <functional>
#include <fstream>
#include <sstream>
#include "ExpandableHashMap.h"
using namespace std;
unsigned int hasher(const GeoCoord& g)
{
return hash<string>()(g.latitudeText + g.longitudeText);
}
class StreetMapImpl
{
public:
StreetMapImpl();
~StreetMapImpl();
bool load(string mapFile);
bool getSegmentsThatStartWith(const GeoCoord& gc, vector<StreetSegment>& segs) const;
private:
ExpandableHashMap<GeoCoord, vector<StreetSegment>> m_map;
};
StreetMapImpl::StreetMapImpl():m_map()
{
}
StreetMapImpl::~StreetMapImpl()
{
}
bool StreetMapImpl::load(string mapFile)
{
ifstream in(mapFile);
if (!in) // If can't open file.
{
cerr << "Failed to load map data." << endl;
return false;
}
string line; // String that will temporarily store coordinate text.
while (getline(in, line))
{
string streetName = line;
getline(in, line);
string s_numSeg = line;
int numSeg = stod(s_numSeg);
// cerr << "Street name: " << streetName << " – Number of segments in this street = " <<numSeg << endl;
for (int i = 0; i < numSeg; i++)
{
getline(in, line);
// double x1, y1, x2, y2;
string x1, x2, y1, y2;
stringstream ss;
ss << line;
ss >> x1;
ss >> y1;
ss >> x2;
ss >> y2;
GeoCoord a(x1, y1);
GeoCoord b(x2, y2);
StreetSegment c(a, b, streetName);
StreetSegment d(b, a, streetName);
vector<StreetSegment> *x = m_map.find(a);
if( x == nullptr)
{
vector<StreetSegment> v;
v.push_back(c);
m_map.associate(a, v);
}
else
{
x->push_back(c);
}
x = m_map.find(b);
if( x == nullptr)
{
vector<StreetSegment> v;
v.push_back(d);
m_map.associate(b, v);
}
else
{
x->push_back(d);
}
}
}
return true;
}
bool StreetMapImpl::getSegmentsThatStartWith(const GeoCoord& gc, vector<StreetSegment>& segs) const
{
const vector<StreetSegment>* result = m_map.find(gc);
if(result == nullptr)
return false;
segs = *result;
return true;
}
//******************** StreetMap functions ************************************
// These functions simply delegate to StreetMapImpl's functions.
// You probably don't want to change any of this code.
StreetMap::StreetMap()
{
m_impl = new StreetMapImpl;
}
StreetMap::~StreetMap()
{
delete m_impl;
}
bool StreetMap::load(string mapFile)
{
return m_impl->load(mapFile);
}
bool StreetMap::getSegmentsThatStartWith(const GeoCoord& gc, vector<StreetSegment>& segs) const
{
return m_impl->getSegmentsThatStartWith(gc, segs);
}