This repository has been archived on 2021-10-31. You can view files and clone it, but cannot push or open issues or pull requests.
sys_prog/roadmap/roadmap.cpp

45 lines
976 B
C++

#include "roadmap.h"
#include <map>
#include <set>
using std::map;
using std::set;
class ConcreteRoadMap : public AbstractRoadMap {
private:
int cities;
map< string, set<string> > adj;
/*
* Adds in the map for key city1 a new set with city2 inside if there is no
* prior set, otherwise it appends city2 into the existing set
*/
void addPair(const string& city1, const string& city2) {
this->adj[city1].insert(city2);
}
public:
ConcreteRoadMap() : adj(), cities(0) {};
void addRoad(const string& city1, const string& city2) override {
addPair(city1, city2);
addPair(city2, city1);
}
void addRoad(const vector<string>& cities) override {
for (size_t i = 0; i < cities.size() - 1; i++) {
addRoad(cities[i], cities[i + 1]);
}
}
void clear() override {
this->adj.clear();
}
int neighborCountForCity(const string& city) const override {
auto i = this->adj.find(city);
return i->second.size();
}
};