generate-shapes/src/pfaedle/router/RoutingAttrs.h

109 lines
3 KiB
C
Raw Normal View History

2018-06-09 17:14:08 +02:00
// Copyright 2018, University of Freiburg,
// Chair of Algorithms and Data Structures.
// Authors: Patrick Brosi <brosi@informatik.uni-freiburg.de>
#ifndef PFAEDLE_ROUTER_ROUTINGATTRS_H_
#define PFAEDLE_ROUTER_ROUTINGATTRS_H_
#include <string>
#include <unordered_map>
#include <vector>
#include "pfaedle/statsimi-classifier/StatsimiClassifier.h"
2018-06-09 17:14:08 +02:00
#include "pfaedle/trgraph/EdgePL.h"
using pfaedle::trgraph::TransitEdgeLine;
namespace pfaedle {
namespace router {
struct LineSimilarity {
bool nameSimilar : 1;
bool fromSimilar : 1;
bool toSimilar : 1;
};
inline bool operator<(const LineSimilarity& a, const LineSimilarity& b) {
return (a.nameSimilar + a.fromSimilar + a.toSimilar) <
(b.nameSimilar + b.fromSimilar + b.toSimilar);
}
2018-06-09 17:14:08 +02:00
struct RoutingAttrs {
RoutingAttrs()
: lineFrom(""), lineTo(), shortName(""), classifier(0), _simiCache() {}
RoutingAttrs(const std::string& shortName, const std::string& lineFrom,
const std::string& lineTo)
: lineFrom(lineFrom),
lineTo({lineTo}),
shortName(shortName),
classifier(0),
_simiCache() {}
std::string lineFrom;
std::vector<std::string> lineTo;
2018-06-09 17:14:08 +02:00
std::string shortName;
const pfaedle::statsimiclassifier::StatsimiClassifier* classifier;
mutable std::unordered_map<const TransitEdgeLine*, LineSimilarity> _simiCache;
LineSimilarity simi(const TransitEdgeLine* line) const {
// shortcut, if we don't have a line information, classify as similar
if (line->shortName.empty() && line->toStr.empty() && line->fromStr.empty())
return {true, true, true};
2018-06-09 17:14:08 +02:00
auto i = _simiCache.find(line);
if (i != _simiCache.end()) return i->second;
LineSimilarity ret{false, false, false};
2019-02-03 12:48:48 +01:00
if (shortName.empty() || router::lineSimi(line->shortName, shortName) > 0.5)
ret.nameSimilar = true;
if (lineTo.size() == 0) {
ret.toSimilar = true;
} else {
for (const auto& lTo : lineTo) {
if (lTo.empty() || classifier->similar(line->toStr, lTo)) {
ret.toSimilar = true;
break;
}
}
}
2018-06-09 17:14:08 +02:00
if (lineFrom.empty() || classifier->similar(line->fromStr, lineFrom))
ret.fromSimilar = true;
2018-06-09 17:14:08 +02:00
_simiCache[line] = ret;
return ret;
}
2018-06-09 17:14:08 +02:00
void merge(const RoutingAttrs& other) {
assert(other.lineFrom == lineFrom);
assert(other.shortName == shortName);
2018-06-09 17:14:08 +02:00
for (const auto& l : other.lineTo) {
auto i = std::lower_bound(lineTo.begin(), lineTo.end(), l);
if (i != lineTo.end() && (*i) == l) continue; // already present
lineTo.insert(i, l);
}
2018-06-09 17:14:08 +02:00
}
};
inline bool operator==(const RoutingAttrs& a, const RoutingAttrs& b) {
return a.shortName == b.shortName && a.lineFrom == b.lineFrom;
2018-06-09 17:14:08 +02:00
}
inline bool operator!=(const RoutingAttrs& a, const RoutingAttrs& b) {
return !(a == b);
}
inline bool operator<(const RoutingAttrs& a, const RoutingAttrs& b) {
return a.lineFrom < b.lineFrom ||
(a.lineFrom == b.lineFrom && a.shortName < b.shortName);
2018-06-09 17:14:08 +02:00
}
} // namespace router
} // namespace pfaedle
#endif // PFAEDLE_ROUTER_ROUTINGATTRS_H_