Rewriting bridge and tunnel checkers to be able to check with masks

This commit is contained in:
Vladimir Byko-Ianko 2014-12-22 16:27:00 +03:00 committed by Alex Zolotarev
parent 18bf67689c
commit d26accc4e9
4 changed files with 72 additions and 59 deletions

View file

@ -141,35 +141,8 @@ IsLocalityChecker::IsLocalityChecker()
m_types.push_back(c.GetTypeByPath(vector<string>(arr[i], arr[i] + 2)));
}
IsBridgeChecker::IsBridgeChecker() : BaseChecker(3), m_typeMask(0)
IsBridgeChecker::IsBridgeChecker() : BaseChecker(3)
{
Classificator const & c = classif();
uint32_t type = c.GetTypeByPath({"highway", "road", "bridge"});
uint8_t highwayType, bridgeType;
ftype::GetValue(type, 0, highwayType);
ftype::GetValue(type, 2, bridgeType);
size_t const recordSz = 3;
char const * arr[][recordSz] = {
{ "highway", "living_street", "bridge" },
{ "highway", "motorway", "bridge" },
{ "highway", "motorway_link", "bridge" },
{ "highway", "primary", "bridge" },
{ "highway", "primary_link", "bridge" },
{ "highway", "residential", "bridge" },
{ "highway", "road", "bridge" },
{ "highway", "secondary", "bridge" },
{ "highway", "secondary_link", "bridge" },
{ "highway", "service", "bridge" },
{ "highway", "tertiary", "bridge" },
{ "highway", "tertiary_link", "bridge" },
{ "highway", "trunk", "bridge" },
{ "highway", "trunk_link", "bridge" },
{ "highway", "unclassified", "bridge" }
};
for (size_t i = 0; i < ARRAY_SIZE(arr); ++i)
m_types.push_back(c.GetTypeByPath(vector<string>(arr[i], arr[i] + recordSz)));
}
IsBridgeChecker const & IsBridgeChecker::Instance()
@ -178,30 +151,13 @@ IsBridgeChecker const & IsBridgeChecker::Instance()
return inst;
}
IsTunnelChecker::IsTunnelChecker() : BaseChecker(3), m_typeMask(0)
bool IsBridgeChecker::IsMatched(uint32_t type) const
{
Classificator const & c = classif();
size_t const recordSz = 3;
char const * arr[][recordSz] = {
{ "highway", "living_street", "tunnel" },
{ "highway", "motorway", "tunnel" },
{ "highway", "motorway_link", "tunnel" },
{ "highway", "primary", "tunnel" },
{ "highway", "primary_link", "tunnel" },
{ "highway", "residential", "tunnel" },
{ "highway", "road", "tunnel" },
{ "highway", "secondary", "tunnel" },
{ "highway", "secondary_link", "tunnel" },
{ "highway", "service", "tunnel" },
{ "highway", "tertiary", "tunnel" },
{ "highway", "tertiary_link", "tunnel" },
{ "highway", "trunk", "tunnel" },
{ "highway", "trunk_link", "tunnel" },
{ "highway", "unclassified", "tunnel" }
};
return IsTypeConformed(type, {"highway", "*", "bridge"});
}
for (size_t i = 0; i < ARRAY_SIZE(arr); ++i)
m_types.push_back(c.GetTypeByPath(vector<string>(arr[i], arr[i] + recordSz)));
IsTunnelChecker::IsTunnelChecker() : BaseChecker(3)
{
}
IsTunnelChecker const & IsTunnelChecker::Instance()
@ -210,6 +166,11 @@ IsTunnelChecker const & IsTunnelChecker::Instance()
return inst;
}
bool IsTunnelChecker::IsMatched(uint32_t type) const
{
return IsTypeConformed(type, {"highway", "*", "tunnel"});
}
Type IsLocalityChecker::GetType(feature::TypesHolder const & types) const
{
for (size_t i = 0; i < types.Size(); ++i)
@ -275,4 +236,25 @@ uint32_t GetPopulationByRadius(double r)
return my::rounds(pow(r / 550.0, 3.6));
}
bool IsTypeConformed(uint32_t type, vector<string> const & path)
{
Classificator const & c = classif();
ClassifObject const * p = c.GetRoot();
ASSERT(p, ());
uint8_t val = 0, i = 0;
for (auto n : path)
{
if (!ftype::GetValue(type, i, val))
return false;
p = p->GetObject(val);
if (p == 0)
return false;
const string name = p->GetName();
if (n != name && n != "*")
return false;
++i;
}
return true;
}
}

View file

@ -4,6 +4,7 @@
#include "../std/vector.hpp"
#include "../std/string.hpp"
namespace feature { class TypesHolder; }
class FeatureType;
@ -71,7 +72,7 @@ public:
class IsBridgeChecker : public BaseChecker
{
size_t m_typeMask;
virtual bool IsMatched(uint32_t type) const;
public:
IsBridgeChecker();
static IsBridgeChecker const & Instance();
@ -79,7 +80,7 @@ public:
class IsTunnelChecker : public BaseChecker
{
size_t m_typeMask;
virtual bool IsMatched(uint32_t type) const;
public:
IsTunnelChecker();
static IsTunnelChecker const & Instance();
@ -106,5 +107,12 @@ public:
uint32_t GetPopulation(FeatureType const & ft);
double GetRadiusByPopulation(uint32_t p);
uint32_t GetPopulationByRadius(double r);
/// Check if type conforms the path. Strings in the path can be
/// feature types like "highway", "living_street", "bridge" and so on
/// or *. * means any class.
/// The root name ("world") is ignored
bool IsTypeConformed(uint32_t type, vector<string> const & path);
//@}
}

View file

@ -24,7 +24,8 @@ vector<uint32_t> GetTypes(char const * arr[][roadArrColumnCount], size_t const r
vector<uint32_t> GetStreetTypes()
{
char const * arr[][roadArrColumnCount] = {
char const * arr[][roadArrColumnCount] =
{
{ "highway", "trunk", "bridge" },
{ "highway", "tertiary", "tunnel" }
};
@ -33,7 +34,8 @@ vector<uint32_t> GetStreetTypes()
vector<uint32_t> GetStreetAndNotStreetTypes()
{
char const * arr[][roadArrColumnCount] = {
char const * arr[][roadArrColumnCount] =
{
{ "highway", "trunk", "bridge" },
{ "highway", "primary_link", "tunnel" }
};
@ -42,7 +44,8 @@ vector<uint32_t> GetStreetAndNotStreetTypes()
vector<uint32_t> GetLinkTypes()
{
char const * arr[][roadArrColumnCount] = {
char const * arr[][roadArrColumnCount] =
{
{ "highway", "secondary_link", "bridge" },
{ "highway", "motorway_link", "tunnel" }
};
@ -51,7 +54,8 @@ vector<uint32_t> GetLinkTypes()
vector<uint32_t> GetBridgeTypes()
{
char const * arr[][roadArrColumnCount] = {
char const * arr[][roadArrColumnCount] =
{
{ "highway", "trunk", "bridge" },
{ "highway", "tertiary", "bridge" }
};
@ -60,7 +64,8 @@ vector<uint32_t> GetBridgeTypes()
vector<uint32_t> GetTunnelTypes()
{
char const * arr[][roadArrColumnCount] = {
char const * arr[][roadArrColumnCount] =
{
{ "highway", "trunk", "tunnel" },
{ "highway", "motorway_link", "tunnel" }
};
@ -69,7 +74,8 @@ vector<uint32_t> GetTunnelTypes()
vector<uint32_t> GetBridgeAndTunnelTypes()
{
char const * arr[][roadArrColumnCount] = {
char const * arr[][roadArrColumnCount] =
{
{ "highway", "trunk", "bridge" },
{ "highway", "motorway_link", "tunnel" }
};
@ -77,6 +83,23 @@ vector<uint32_t> GetBridgeAndTunnelTypes()
}
}
UNIT_TEST(IsTypeConformed)
{
char const * arr[][roadArrColumnCount] =
{
{"highway", "trunk", "bridge"},
{"highway", "motorway_link", "tunnel"}
};
vector<uint32_t> types = GetTypes(arr, ARRAY_SIZE(arr));
TEST(ftypes::IsTypeConformed(types[0], {"highway", "trunk", "bridge"}), ());
TEST(ftypes::IsTypeConformed(types[0], {"highway", "trunk"}), ());
TEST(ftypes::IsTypeConformed(types[1], {"highway", "*", "tunnel"}), ());
TEST(!ftypes::IsTypeConformed(types[0], {"highway", "trunk", "tunnel"}), ());
TEST(!ftypes::IsTypeConformed(types[0], {"highway", "abcd", "tunnel"}), ());
TEST(!ftypes::IsTypeConformed(types[1], {"highway", "efgh", "*"}), ());
TEST(!ftypes::IsTypeConformed(types[1], {"*", "building", "*"}), ());
}
UNIT_TEST(IsStreetChecker)
{
TEST(ftypes::IsStreetChecker::Instance()(GetStreetTypes()), ());

View file

@ -36,4 +36,4 @@ SOURCES += \
mwm_set_test.cpp \
categories_test.cpp \
visibility_test.cpp \
checker_test.cpp
checker_test.cpp \