From eee40270f3adc9ea0f6b2db3791f86a7dc36b5c6 Mon Sep 17 00:00:00 2001 From: Maxim Pimenov Date: Wed, 11 Jul 2018 11:57:26 +0300 Subject: [PATCH] [base] Minor refactoring of osm::Id. --- base/base_tests/CMakeLists.txt | 1 + base/base_tests/osm_id_test.cpp | 30 ++++++++++ base/osm_id.cpp | 60 ++++++++++--------- base/osm_id.hpp | 21 ++++--- generator/feature_builder.cpp | 2 +- generator/generator_tests/CMakeLists.txt | 1 - generator/generator_tests/osm_id_test.cpp | 23 ------- xcode/base/base.xcodeproj/project.pbxproj | 28 +++++++++ .../generator_tool.xcodeproj/project.pbxproj | 4 -- 9 files changed, 101 insertions(+), 69 deletions(-) create mode 100644 base/base_tests/osm_id_test.cpp delete mode 100644 generator/generator_tests/osm_id_test.cpp diff --git a/base/base_tests/CMakeLists.txt b/base/base_tests/CMakeLists.txt index 253173f2ee..095e83de9a 100644 --- a/base/base_tests/CMakeLists.txt +++ b/base/base_tests/CMakeLists.txt @@ -23,6 +23,7 @@ set( move_to_front_tests.cpp newtype_test.cpp observer_list_test.cpp + osm_id_test.cpp range_iterator_test.cpp ref_counted_tests.cpp regexp_test.cpp diff --git a/base/base_tests/osm_id_test.cpp b/base/base_tests/osm_id_test.cpp new file mode 100644 index 0000000000..d3cd7e7285 --- /dev/null +++ b/base/base_tests/osm_id_test.cpp @@ -0,0 +1,30 @@ +#include "testing/testing.hpp" + +#include "base/osm_id.hpp" + +using namespace osm; + +UNIT_TEST(OsmId) +{ + Id const node = Id::Node(12345); + TEST_EQUAL(node.OsmId(), 12345ULL, ()); + TEST(node.IsNode(), ()); + TEST(!node.IsWay(), ()); + TEST(!node.IsRelation(), ()); + TEST_EQUAL(DebugPrint(node), "node 12345", ()); + + Id const way = Id::Way(93245123456332ULL); + TEST_EQUAL(way.OsmId(), 93245123456332ULL, ()); + TEST(!way.IsNode(), ()); + TEST(way.IsWay(), ()); + TEST(!way.IsRelation(), ()); + TEST_EQUAL(DebugPrint(way), "way 93245123456332", ()); + + Id const relation = Id::Relation(5); + TEST_EQUAL(relation.OsmId(), 5ULL, ()); + // sic! + TEST(relation.IsNode(), ()); + TEST(relation.IsWay(), ()); + TEST(relation.IsRelation(), ()); + TEST_EQUAL(DebugPrint(relation), "relation 5", ()); +} diff --git a/base/osm_id.cpp b/base/osm_id.cpp index da21db1b45..70c588911f 100644 --- a/base/osm_id.cpp +++ b/base/osm_id.cpp @@ -1,41 +1,43 @@ #include "base/osm_id.hpp" -#include "base/assert.hpp" - #include +namespace +{ +// Use 2 higher bits to encode type. +// +// todo The masks are not disjoint for some reason +// since the commit 60414dc86254aed22ac9e66fed49eba554260a2c. +uint64_t const kNode = 0x4000000000000000ULL; +uint64_t const kWay = 0x8000000000000000ULL; +uint64_t const kRelation = 0xC000000000000000ULL; +uint64_t const kReset = ~(kNode | kWay | kRelation); +} // namespace namespace osm { - -// Use 3 higher bits to encode type -static const uint64_t NODE = 0x4000000000000000ULL; -static const uint64_t WAY = 0x8000000000000000ULL; -static const uint64_t RELATION = 0xC000000000000000ULL; -static const uint64_t RESET = ~(NODE | WAY | RELATION); - Id::Id(uint64_t encodedId) : m_encodedId(encodedId) { } Id Id::Node(uint64_t id) { - return Id( id | NODE ); + return Id(id | kNode); } Id Id::Way(uint64_t id) { - return Id( id | WAY ); + return Id(id | kWay); } Id Id::Relation(uint64_t id) { - return Id( id | RELATION ); + return Id(id | kRelation); } uint64_t Id::OsmId() const { - return m_encodedId & RESET; + return m_encodedId & kReset; } uint64_t Id::EncodedId() const @@ -45,35 +47,35 @@ uint64_t Id::EncodedId() const bool Id::IsNode() const { - return ((m_encodedId & NODE) == NODE); + return (m_encodedId & kNode) == kNode; } bool Id::IsWay() const { - return ((m_encodedId & WAY) == WAY); + return (m_encodedId & kWay) == kWay; } bool Id::IsRelation() const { - return ((m_encodedId & RELATION) == RELATION); -} - -std::string Id::Type() const -{ - if ((m_encodedId & RELATION) == RELATION) - return "relation"; - else if ((m_encodedId & NODE) == NODE) - return "node"; - else if ((m_encodedId & WAY) == WAY) - return "way"; - else - return "ERROR: Not initialized Osm ID"; + return (m_encodedId & kRelation) == kRelation; } std::string DebugPrint(osm::Id const & id) { + std::string typeStr; + // Note that with current encoding all relations are also at the + // same time nodes and ways. Therefore, the relation check must go first. + if (id.IsRelation()) + typeStr = "relation"; + else if (id.IsNode()) + typeStr = "node"; + else if (id.IsWay()) + typeStr = "way"; + else + typeStr = "ERROR: Not initialized Osm ID"; + std::ostringstream stream; - stream << id.Type() << " " << id.OsmId(); + stream << typeStr << " " << id.OsmId(); return stream.str(); } } // namespace osm diff --git a/base/osm_id.hpp b/base/osm_id.hpp index 597f81eb05..c38f471ef0 100644 --- a/base/osm_id.hpp +++ b/base/osm_id.hpp @@ -8,12 +8,10 @@ namespace osm { class Id { - uint64_t m_encodedId; - - static const uint64_t INVALID = 0ULL; - public: - explicit Id(uint64_t encodedId = INVALID); + static const uint64_t kInvalid = 0ULL; + + explicit Id(uint64_t encodedId = kInvalid); static Id Node(uint64_t osmId); static Id Way(uint64_t osmId); @@ -21,17 +19,18 @@ public: uint64_t OsmId() const; uint64_t EncodedId() const; + bool IsNode() const; bool IsWay() const; bool IsRelation() const; - /// For debug output - std::string Type() const; - - inline bool operator<(Id const & other) const { return m_encodedId < other.m_encodedId; } - inline bool operator==(Id const & other) const { return m_encodedId == other.m_encodedId; } - inline bool operator!=(Id const & other) const { return !(*this == other); } + bool operator<(Id const & other) const { return m_encodedId < other.m_encodedId; } + bool operator==(Id const & other) const { return m_encodedId == other.m_encodedId; } + bool operator!=(Id const & other) const { return !(*this == other); } bool operator==(uint64_t other) const { return OsmId() == other; } + +private: + uint64_t m_encodedId; }; struct HashId : private std::hash diff --git a/generator/feature_builder.cpp b/generator/feature_builder.cpp index 0fcd0bc00e..6cbc55db90 100644 --- a/generator/feature_builder.cpp +++ b/generator/feature_builder.cpp @@ -526,7 +526,7 @@ string FeatureBuilder1::GetOsmIdsString() const ostringstream out; for (auto const & id : m_osmIds) - out << id.Type() << " id=" << id.OsmId() << " "; + out << DebugPrint(id) << " "; return out.str(); } diff --git a/generator/generator_tests/CMakeLists.txt b/generator/generator_tests/CMakeLists.txt index 6ea75f6896..ac1aae6593 100644 --- a/generator/generator_tests/CMakeLists.txt +++ b/generator/generator_tests/CMakeLists.txt @@ -10,7 +10,6 @@ set( metadata_parser_test.cpp node_mixer_test.cpp osm2meta_test.cpp - osm_id_test.cpp osm_o5m_source_test.cpp osm_type_test.cpp road_access_test.cpp diff --git a/generator/generator_tests/osm_id_test.cpp b/generator/generator_tests/osm_id_test.cpp deleted file mode 100644 index ff0d225245..0000000000 --- a/generator/generator_tests/osm_id_test.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include "testing/testing.hpp" - -#include "generator/feature_builder.hpp" - -#include "base/logging.hpp" -#include "base/osm_id.hpp" - -using namespace osm; - -UNIT_TEST(OsmId) -{ - Id const node = Id::Node(12345); - TEST_EQUAL(node.OsmId(), 12345ULL, ()); - TEST_EQUAL(node.Type(), "node", ()); - - Id const way = Id::Way(93245123456332ULL); - TEST_EQUAL(way.OsmId(), 93245123456332ULL, ()); - TEST_EQUAL(way.Type(), "way", ()); - - Id const relation = Id::Relation(5); - TEST_EQUAL(relation.OsmId(), 5ULL, ()); - TEST_EQUAL(relation.Type(), "relation", ()); -} diff --git a/xcode/base/base.xcodeproj/project.pbxproj b/xcode/base/base.xcodeproj/project.pbxproj index e63cda2597..94930b4700 100644 --- a/xcode/base/base.xcodeproj/project.pbxproj +++ b/xcode/base/base.xcodeproj/project.pbxproj @@ -18,6 +18,13 @@ 3446C6831DDCAA7800146687 /* ref_counted_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3446C67F1DDCAA6E00146687 /* ref_counted_tests.cpp */; }; 39B89C3A1FD1898A001104AF /* control_flow_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 39B89C391FD1898A001104AF /* control_flow_tests.cpp */; }; 39BC0FD01FD057FA00B6C276 /* control_flow.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 39BC0FCF1FD057F900B6C276 /* control_flow.hpp */; }; + 39BC707620F55B6700A6EC20 /* clustering_map_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 39BC707420F55B6700A6EC20 /* clustering_map_tests.cpp */; }; + 39BC707720F55B6700A6EC20 /* bwt_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 39BC707520F55B6700A6EC20 /* bwt_tests.cpp */; }; + 39F995E320F55B8A0034F977 /* worker_thread_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 39F995DE20F55B8A0034F977 /* worker_thread_tests.cpp */; }; + 39F995E420F55B8A0034F977 /* move_to_front_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 39F995DF20F55B8A0034F977 /* move_to_front_tests.cpp */; }; + 39F995E520F55B8A0034F977 /* osm_id_test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 39F995E020F55B8A0034F977 /* osm_id_test.cpp */; }; + 39F995E620F55B8A0034F977 /* suffix_array_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 39F995E120F55B8A0034F977 /* suffix_array_tests.cpp */; }; + 39F995E720F55B8A0034F977 /* visitor_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 39F995E220F55B8A0034F977 /* visitor_tests.cpp */; }; 39FD271E1CC65AD000AFF551 /* testingmain.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 39FD27011CC65A2800AFF551 /* testingmain.cpp */; }; 39FD271F1CC65AD000AFF551 /* assert_test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 39FD26C81CC65A0E00AFF551 /* assert_test.cpp */; }; 39FD27201CC65AD000AFF551 /* bits_test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 39FD26CA1CC65A0E00AFF551 /* bits_test.cpp */; }; @@ -148,6 +155,13 @@ 34BA2D6B1DBE169E00FAB345 /* common-release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = "common-release.xcconfig"; path = "../common-release.xcconfig"; sourceTree = ""; }; 39B89C391FD1898A001104AF /* control_flow_tests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = control_flow_tests.cpp; sourceTree = ""; }; 39BC0FCF1FD057F900B6C276 /* control_flow.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = control_flow.hpp; sourceTree = ""; }; + 39BC707420F55B6700A6EC20 /* clustering_map_tests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = clustering_map_tests.cpp; sourceTree = ""; }; + 39BC707520F55B6700A6EC20 /* bwt_tests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = bwt_tests.cpp; sourceTree = ""; }; + 39F995DE20F55B8A0034F977 /* worker_thread_tests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = worker_thread_tests.cpp; sourceTree = ""; }; + 39F995DF20F55B8A0034F977 /* move_to_front_tests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = move_to_front_tests.cpp; sourceTree = ""; }; + 39F995E020F55B8A0034F977 /* osm_id_test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = osm_id_test.cpp; sourceTree = ""; }; + 39F995E120F55B8A0034F977 /* suffix_array_tests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = suffix_array_tests.cpp; sourceTree = ""; }; + 39F995E220F55B8A0034F977 /* visitor_tests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = visitor_tests.cpp; sourceTree = ""; }; 39FD26C81CC65A0E00AFF551 /* assert_test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = assert_test.cpp; sourceTree = ""; }; 39FD26CA1CC65A0E00AFF551 /* bits_test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = bits_test.cpp; sourceTree = ""; }; 39FD26CB1CC65A0E00AFF551 /* buffer_vector_test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = buffer_vector_test.cpp; sourceTree = ""; }; @@ -292,6 +306,13 @@ 39FD26C71CC659D200AFF551 /* base_tests */ = { isa = PBXGroup; children = ( + 39F995DF20F55B8A0034F977 /* move_to_front_tests.cpp */, + 39F995E020F55B8A0034F977 /* osm_id_test.cpp */, + 39F995E120F55B8A0034F977 /* suffix_array_tests.cpp */, + 39F995E220F55B8A0034F977 /* visitor_tests.cpp */, + 39F995DE20F55B8A0034F977 /* worker_thread_tests.cpp */, + 39BC707520F55B6700A6EC20 /* bwt_tests.cpp */, + 39BC707420F55B6700A6EC20 /* clustering_map_tests.cpp */, 564BB446206E8A4D00BDD211 /* fifo_cache_test.cpp */, 39B89C391FD1898A001104AF /* control_flow_tests.cpp */, 67E40EC71E4DC0D500A6D200 /* small_set_test.cpp */, @@ -674,9 +695,12 @@ 675342061A3F57E400A0A8C3 /* thread_pool.cpp in Sources */, 670E39441C46C76900E9C0A6 /* sunrise_sunset.cpp in Sources */, 67E40EC81E4DC0D500A6D200 /* small_set_test.cpp in Sources */, + 39F995E320F55B8A0034F977 /* worker_thread_tests.cpp in Sources */, 56DE23051FCD8AB4008FEFD5 /* osm_id.cpp in Sources */, 6753420E1A3F57E400A0A8C3 /* timer.cpp in Sources */, + 39F995E520F55B8A0034F977 /* osm_id_test.cpp in Sources */, 675341F61A3F57E400A0A8C3 /* shared_buffer_manager.cpp in Sources */, + 39F995E420F55B8A0034F977 /* move_to_front_tests.cpp in Sources */, 56B1A0741E69DE4D00395022 /* random.cpp in Sources */, 675341DA1A3F57E400A0A8C3 /* exception.cpp in Sources */, 3D74EF111F8B902C0081202C /* suffix_array.cpp in Sources */, @@ -688,6 +712,7 @@ 564BB447206E8A4D00BDD211 /* fifo_cache_test.cpp in Sources */, 3D74EF141F8B902C0081202C /* bwt.cpp in Sources */, 3D74EF131F8B902C0081202C /* move_to_front.cpp in Sources */, + 39F995E620F55B8A0034F977 /* suffix_array_tests.cpp in Sources */, 675341CD1A3F57E400A0A8C3 /* base.cpp in Sources */, 675342011A3F57E400A0A8C3 /* string_utils.cpp in Sources */, 674A7E2E1C0DB03D003D48E1 /* timegm.cpp in Sources */, @@ -699,7 +724,10 @@ 67B52B601AD3C84E00664C17 /* thread_checker.cpp in Sources */, 675341E71A3F57E400A0A8C3 /* normalize_unicode.cpp in Sources */, 675341E11A3F57E400A0A8C3 /* lower_case.cpp in Sources */, + 39F995E720F55B8A0034F977 /* visitor_tests.cpp in Sources */, + 39BC707620F55B6700A6EC20 /* clustering_map_tests.cpp in Sources */, 672DD4C11E0425600078E13C /* condition.cpp in Sources */, + 39BC707720F55B6700A6EC20 /* bwt_tests.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/xcode/generator_tool/generator_tool.xcodeproj/project.pbxproj b/xcode/generator_tool/generator_tool.xcodeproj/project.pbxproj index 2e6e9db9bf..71bf8d1907 100644 --- a/xcode/generator_tool/generator_tool.xcodeproj/project.pbxproj +++ b/xcode/generator_tool/generator_tool.xcodeproj/project.pbxproj @@ -143,7 +143,6 @@ 67AB92D01B75156400AB5194 /* coasts_test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6726C1E51A4C28D5005EEA39 /* coasts_test.cpp */; }; 67AB92D11B75156700AB5194 /* feature_builder_test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6726C1E61A4C28D5005EEA39 /* feature_builder_test.cpp */; }; 67AB92D21B75156B00AB5194 /* feature_merger_test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6726C1E71A4C28D5005EEA39 /* feature_merger_test.cpp */; }; - 67AB92D31B75156E00AB5194 /* osm_id_test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6726C1E81A4C28D5005EEA39 /* osm_id_test.cpp */; }; 67AB92D51B75157400AB5194 /* osm_type_test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6726C1EA1A4C28D5005EEA39 /* osm_type_test.cpp */; }; 67AB92D61B75157700AB5194 /* tesselator_test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6726C1EB1A4C28D5005EEA39 /* tesselator_test.cpp */; }; 67AB92D71B75157A00AB5194 /* triangles_tree_coding_test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6726C1EC1A4C28D5005EEA39 /* triangles_tree_coding_test.cpp */; }; @@ -241,7 +240,6 @@ 6726C1E51A4C28D5005EEA39 /* coasts_test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = coasts_test.cpp; sourceTree = ""; }; 6726C1E61A4C28D5005EEA39 /* feature_builder_test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = feature_builder_test.cpp; sourceTree = ""; }; 6726C1E71A4C28D5005EEA39 /* feature_merger_test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = feature_merger_test.cpp; sourceTree = ""; }; - 6726C1E81A4C28D5005EEA39 /* osm_id_test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = osm_id_test.cpp; sourceTree = ""; }; 6726C1EA1A4C28D5005EEA39 /* osm_type_test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = osm_type_test.cpp; sourceTree = ""; }; 6726C1EB1A4C28D5005EEA39 /* tesselator_test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = tesselator_test.cpp; sourceTree = ""; }; 6726C1EC1A4C28D5005EEA39 /* triangles_tree_coding_test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = triangles_tree_coding_test.cpp; sourceTree = ""; }; @@ -495,7 +493,6 @@ 671F58B71B86109B0032311E /* intermediate_data_test.cpp */, 677792501C1B2E9300EC9499 /* metadata_parser_test.cpp */, 671ED3B720D4098000D4317E /* node_mixer_test.cpp */, - 6726C1E81A4C28D5005EEA39 /* osm_id_test.cpp */, 6764B8931ADD6FC100DD8B15 /* osm_o5m_source_test.cpp */, 6726C1EA1A4C28D5005EEA39 /* osm_type_test.cpp */, 677E2A0B1CAAC7CB001DC42A /* osm2meta_test.cpp */, @@ -733,7 +730,6 @@ 670E7BC51EF9860100A8E9ED /* ugc_test.cpp in Sources */, 67AB92D21B75156B00AB5194 /* feature_merger_test.cpp in Sources */, 67AB92C31B73C29000AB5194 /* source_to_element_test.cpp in Sources */, - 67AB92D31B75156E00AB5194 /* osm_id_test.cpp in Sources */, 671ED3C520D4098100D4317E /* sponsored_storage_tests.cpp in Sources */, 671ED3C120D4098100D4317E /* srtm_parser_test.cpp in Sources */, 671ED3C220D4098100D4317E /* restriction_collector_test.cpp in Sources */,