-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAttractionMapper.cpp
86 lines (74 loc) · 1.96 KB
/
AttractionMapper.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
#include "provided.h"
#include "MyMap.h"
#include <string>
using namespace std;
class AttractionMapperImpl
{
public:
AttractionMapperImpl();
~AttractionMapperImpl();
void init(const MapLoader& ml);
bool getGeoCoord(string attraction, GeoCoord& gc) const;
private:
MyMap<string,GeoCoord>* m_attractionMap;
};
AttractionMapperImpl::AttractionMapperImpl()
{
m_attractionMap = new MyMap<string, GeoCoord>;
}
AttractionMapperImpl::~AttractionMapperImpl()
{
delete m_attractionMap;
}
// O(N+A*log(A))
void AttractionMapperImpl::init(const MapLoader& ml)
{
StreetSegment ss;
for(size_t t = 0; t < ml.getNumSegments(); t++) // O(N)
{
ml.getSegment(t, ss);
if(!ss.attractions.empty())
{
for(size_t k = 0; k < ss.attractions.size(); k++) // O(A)
{
string nm = ss.attractions[k].name;
for(size_t i = 0; i < nm.size(); i++)
nm[i] = tolower(nm[i]);
GeoCoord gc = ss.attractions[k].geocoordinates;
m_attractionMap->associate(nm, gc); //O(logA)
}
}
}
}
bool AttractionMapperImpl::getGeoCoord(string attraction, GeoCoord& gc) const
{
string dup = attraction;
for(size_t t = 0; t < dup.size(); t++)
dup[t] = tolower(dup[t]);
GeoCoord* gcPtr = m_attractionMap->find(dup);
if(gcPtr != nullptr)
{
gc = *gcPtr;
return true;
}
return false;
}
//******************** AttractionMapper functions *****************************
// These functions simply delegate to AttractionMapperImpl's functions.
// You probably don't want to change any of this code.
AttractionMapper::AttractionMapper()
{
m_impl = new AttractionMapperImpl;
}
AttractionMapper::~AttractionMapper()
{
delete m_impl;
}
void AttractionMapper::init(const MapLoader& ml)
{
m_impl->init(ml);
}
bool AttractionMapper::getGeoCoord(string attraction, GeoCoord& gc) const
{
return m_impl->getGeoCoord(attraction, gc);
}