diff options
author | Sergey Yershov <yershov@corp.mail.ru> | 2015-08-14 13:38:21 +0300 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-23 03:01:26 +0300 |
commit | 1253cba0070a4484ba2ae2269871ab6894bdd195 (patch) | |
tree | 4bd464787cd80d6cd4df1c78ef63f93b4d63fa83 /generator | |
parent | 52411f4a24faf1491a1fc75a3ee1f5545f02a885 (diff) |
Make separate 'tag' tag processing
Diffstat (limited to 'generator')
-rw-r--r-- | generator/generator_tests/osm_type_test.cpp | 2 | ||||
-rw-r--r-- | generator/osm2type.cpp | 58 | ||||
-rw-r--r-- | generator/osm_element.hpp | 21 | ||||
-rw-r--r-- | generator/osm_source.cpp | 10 | ||||
-rw-r--r-- | generator/xml_element.cpp | 48 | ||||
-rw-r--r-- | generator/xml_element.hpp | 93 |
6 files changed, 101 insertions, 131 deletions
diff --git a/generator/generator_tests/osm_type_test.cpp b/generator/generator_tests/osm_type_test.cpp index 89a5d48190..813fe22f33 100644 --- a/generator/generator_tests/osm_type_test.cpp +++ b/generator/generator_tests/osm_type_test.cpp @@ -17,7 +17,7 @@ namespace void FillXmlElement(char const * arr[][2], size_t count, XMLElement * p) { for (size_t i = 0; i < count; ++i) - p->AddKV(arr[i][0], arr[i][1]); + p->AddTag(arr[i][0], arr[i][1]); } template <size_t N> uint32_t GetType(char const * (&arr)[N]) diff --git a/generator/osm2type.cpp b/generator/osm2type.cpp index 7a764460ca..b2bc46818b 100644 --- a/generator/osm2type.cpp +++ b/generator/osm2type.cpp @@ -66,21 +66,18 @@ namespace ftype TResult for_each_tag(XMLElement * p, ToDo toDo) { TResult res = TResult(); - for (auto & e : p->childs) + for (auto & e : p->m_tags) { - if (e.type == XMLElement::EntityType::Tag) - { - if (e.k.empty() || is_skip_tag(e.k)) - continue; + if (e.key.empty() || is_skip_tag(e.key)) + continue; - // this means "no" - if (get_mark_value(e.k, e.v) == -1) - continue; + // this means "no" + if (get_mark_value(e.key, e.value) == -1) + continue; - res = toDo(e.k, e.v); - if (res) - return res; - } + res = toDo(e.key, e.value); + if (res) + return res; } return res; } @@ -318,22 +315,25 @@ namespace ftype template <typename FuncT = void()> void ApplyRules(initializer_list<Rule<FuncT>> const & rules) const { - for (auto & e : m_element->childs) - if (e.type == XMLElement::EntityType::Tag) - for (auto const & rule: rules) - if (e.k == rule.key) - { - bool take = false; - if (rule.value[0] == '*') - take = true; - else if (rule.value[0] == '!') - take = IsNegative(e.v); - else if (rule.value[0] == '~') - take = !IsNegative(e.v); - - if (take || e.v == rule.value) - call(rule.func, e.k, e.v); - } + for (auto & e : m_element->m_tags) + { + for (auto const & rule: rules) + { + if (e.key == rule.key) + { + bool take = false; + if (rule.value[0] == '*') + take = true; + else if (rule.value[0] == '!') + take = IsNegative(e.value); + else if (rule.value[0] == '~') + take = !IsNegative(e.value); + + if (take || e.value == rule.value) + call(rule.func, e.key, e.value); + } + } + } } protected: @@ -361,7 +361,7 @@ namespace ftype }); if (!hasLayer && layer) - p->AddKV("layer", layer); + p->AddTag("layer", layer); } //#ifdef DEBUG diff --git a/generator/osm_element.hpp b/generator/osm_element.hpp index dc98456cde..5260d08733 100644 --- a/generator/osm_element.hpp +++ b/generator/osm_element.hpp @@ -124,12 +124,9 @@ class SecondPassParser typedef unordered_set<string> NameKeysT; void GetNameKeys(NameKeysT & keys) const { - for (auto const & p : m_current->childs) - if (p.type == XMLElement::EntityType::Tag) - { - if (strings::StartsWith(p.k, "name")) - keys.insert(p.k); - } + for (auto const & p : m_current->m_tags) + if (strings::StartsWith(p.key, "name")) + keys.insert(p.key); } void Process(RelationElement const & e) @@ -167,7 +164,7 @@ class SecondPassParser if (isWay && p.first == "place") continue; - m_current->AddKV(p.first, p.second); + m_current->AddTag(p.first, p.second); } } @@ -407,7 +404,7 @@ public: { if (p->type == XMLElement::EntityType::Node) { - if (p->childs.empty()) + if (p->m_tags.empty()) return; FeatureParams params; @@ -456,15 +453,11 @@ public: { // 1. Check, if this is our processable relation. Here we process only polygon relations. size_t i = 0; - size_t const count = p->childs.size(); + size_t const count = p->m_tags.size(); for (; i < count; ++i) { - if (p->childs[i].type == XMLElement::EntityType::Tag && - p->childs[i].k == "type" && - p->childs[i].v == "multipolygon") - { + if (p->m_tags[i].key == "type" && p->m_tags[i].value == "multipolygon") break; - } } if (i == count) return; diff --git a/generator/osm_source.cpp b/generator/osm_source.cpp index af0ee65ea4..d513464f6f 100644 --- a/generator/osm_source.cpp +++ b/generator/osm_source.cpp @@ -451,7 +451,7 @@ void BuildFeaturesFromO5M(SourceReader & stream, function<void(XMLElement *)> pr { p.type = XMLElement::EntityType::Way; for (uint64_t nd : em.Nodes()) - p.AddND(nd); + p.AddNd(nd); break; } case TType::Relation: @@ -462,13 +462,13 @@ void BuildFeaturesFromO5M(SourceReader & stream, function<void(XMLElement *)> pr switch (member.type) { case TType::Node: - p.AddMEMBER(member.ref, XMLElement::EntityType::Node, member.role); + p.AddMember(member.ref, XMLElement::EntityType::Node, member.role); break; case TType::Way: - p.AddMEMBER(member.ref, XMLElement::EntityType::Way, member.role); + p.AddMember(member.ref, XMLElement::EntityType::Way, member.role); break; case TType::Relation: - p.AddMEMBER(member.ref, XMLElement::EntityType::Relation, member.role); + p.AddMember(member.ref, XMLElement::EntityType::Relation, member.role); break; default: break; @@ -480,7 +480,7 @@ void BuildFeaturesFromO5M(SourceReader & stream, function<void(XMLElement *)> pr } for (auto const & tag : em.Tags()) - p.AddKV(tag.key, tag.value); + p.AddTag(tag.key, tag.value); processor(&p); } diff --git a/generator/xml_element.cpp b/generator/xml_element.cpp index 28646dff38..41637d89eb 100644 --- a/generator/xml_element.cpp +++ b/generator/xml_element.cpp @@ -6,46 +6,26 @@ #include "std/algorithm.hpp" -void XMLElement::AddKV(string const & k, string const & v) -{ - childs.push_back(XMLElement()); - XMLElement & e = childs.back(); - - e.type = EntityType::Tag; - e.k = k; - e.v = v; -} - -void XMLElement::AddND(uint64_t ref) -{ - m_nds.push_back(ref); -} - -void XMLElement::AddMEMBER(uint64_t ref, EntityType type, string const & role) -{ - m_members.push_back({ref, type, role}); -} - string DebugPrint(XMLElement::EntityType e) { switch (e) { case XMLElement::EntityType::Unknown: - return "Unknown"; + return "unknown"; case XMLElement::EntityType::Way: - return "Way"; + return "way"; case XMLElement::EntityType::Tag: - return "Tag"; + return "tag"; case XMLElement::EntityType::Relation: - return "Relation"; + return "relation"; case XMLElement::EntityType::Osm: - return "Osm"; + return "osm"; case XMLElement::EntityType::Node: - return "Node"; + return "node"; case XMLElement::EntityType::Nd: - return "Nd"; + return "nd"; case XMLElement::EntityType::Member: - return "Member"; + return "member"; } } @@ -56,13 +36,13 @@ string XMLElement::ToString(string const & shift) const switch (type) { case EntityType::Node: - ss << "Node: " << id << " (" << fixed << setw(7) << lat << ", " << lon << ")" << " subelements: " << childs.size(); + ss << "Node: " << id << " (" << fixed << setw(7) << lat << ", " << lon << ")" << " tags: " << m_tags.size(); break; case EntityType::Nd: ss << "Nd ref: " << ref; break; case EntityType::Way: - ss << "Way: " << id << " nds: " << m_nds.size() << " subelements: " << childs.size(); + ss << "Way: " << id << " nds: " << m_nds.size() << " tags: " << m_tags.size(); if (!m_nds.empty()) { string shift2 = shift; @@ -72,7 +52,7 @@ string XMLElement::ToString(string const & shift) const } break; case EntityType::Relation: - ss << "Relation: " << id << " members: " << m_members.size() << " subelements: " << childs.size(); + ss << "Relation: " << id << " members: " << m_members.size() << " tags: " << m_tags.size(); if (!m_members.empty()) { string shift2 = shift; @@ -90,12 +70,12 @@ string XMLElement::ToString(string const & shift) const default: ss << "Unknown element"; } - if (!childs.empty()) + if (!m_tags.empty()) { string shift2 = shift; shift2 += shift2.empty() ? "\n " : " "; - for ( auto const & e : childs ) - ss << e.ToString(shift2); + for (auto const & e : m_tags) + ss << shift2 << e.key << " = " << e.value; } return ss.str(); } diff --git a/generator/xml_element.hpp b/generator/xml_element.hpp index 0f3c1c391c..fee3961137 100644 --- a/generator/xml_element.hpp +++ b/generator/xml_element.hpp @@ -25,32 +25,50 @@ struct XMLElement Osm = 0x736F, // "os" }; - EntityType type = EntityType::Unknown; - uint64_t id = 0; - double lon = 0; - double lat = 0; - uint64_t ref = 0; - string k; - string v; - EntityType memberType = EntityType::Unknown; - string role; - struct Member { - uint64_t ref; - EntityType type; + uint64_t ref = 0; + EntityType type = EntityType::Unknown; string role; + Member() = default; + Member(uint64_t ref, EntityType type, string const & role) + : ref(ref), type(type), role(role) + {} + bool operator == (Member const & e) const { return ref == e.ref && type == e.type && role == e.role; } }; + struct Tag + { + string key; + string value; + + Tag() = default; + Tag(string const & k, string const & v) : key(k), value(v) {} + + bool operator == (Tag const & e) const + { + return key == e.key && value == e.value; + } + }; + + EntityType type = EntityType::Unknown; + uint64_t id = 0; + double lon = 0; + double lat = 0; + uint64_t ref = 0; + string k; + string v; + EntityType memberType = EntityType::Unknown; + string role; vector<uint64_t> m_nds; vector<Member> m_members; - vector<XMLElement> childs; + vector<Tag> m_tags; void Clear() { @@ -66,16 +84,14 @@ struct XMLElement m_nds.clear(); m_members.clear(); - childs.clear(); + m_tags.clear(); } string ToString(string const & shift = string()) const; - vector<uint64_t> const & Nodes() const - { - return m_nds; - } - + inline vector<uint64_t> const & Nodes() const { return m_nds; } + inline vector<Member> const & Members() const { return m_members; } + inline vector<Tag> const & Tags() const { return m_tags; } static EntityType StringToEntityType(string const & t) { @@ -89,28 +105,6 @@ struct XMLElement return EntityType::Unknown; } - vector<Member> const & Members() const - { - return m_members; - } - - struct Tag - { - string key; - string value; - }; - - vector<Tag> Tags() const - { - vector<Tag> tags; - for (auto const & e : childs) - { - if (e.type == EntityType::Tag) - tags.push_back({e.k, e.v}); - } - return move(tags); - } - bool operator == (XMLElement const & e) const { return ( @@ -125,13 +119,16 @@ struct XMLElement && role == e.role && m_nds == e.m_nds && m_members == e.m_members - && childs == e.childs + && m_tags == e.m_tags ); } - void AddKV(string const & k, string const & v); - void AddND(uint64_t ref); - void AddMEMBER(uint64_t ref, EntityType type, string const & role); + void AddTag(string const & k, string const & v) { m_tags.emplace_back(k, v); } + void AddNd(uint64_t ref) { m_nds.emplace_back(ref); } + void AddMember(uint64_t ref, EntityType type, string const & role) + { + m_members.emplace_back(ref, type, role); + } }; string DebugPrint(XMLElement const & e); @@ -214,13 +211,13 @@ public: switch (m_child.type) { case XMLElement::EntityType::Member: - m_parent.AddMEMBER(m_child.ref, m_child.memberType, m_child.role); + m_parent.AddMember(m_child.ref, m_child.memberType, m_child.role); break; case XMLElement::EntityType::Tag: - m_parent.AddKV(m_child.k, m_child.v); + m_parent.AddTag(m_child.k, m_child.v); break; case XMLElement::EntityType::Nd: - m_parent.AddND(m_child.ref); + m_parent.AddNd(m_child.ref); default: break; } m_current = &m_parent; |