[base] Minor refactoring of osm::Id.

This commit is contained in:
Maxim Pimenov 2018-07-11 11:57:26 +03:00 committed by Sergey Yershov
parent a646652ae5
commit eee40270f3
9 changed files with 101 additions and 69 deletions

View file

@ -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

View file

@ -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", ());
}

View file

@ -1,41 +1,43 @@
#include "base/osm_id.hpp"
#include "base/assert.hpp"
#include <sstream>
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

View file

@ -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<uint64_t>

View file

@ -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();
}

View file

@ -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

View file

@ -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", ());
}

View file

@ -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 = "<group>"; };
39B89C391FD1898A001104AF /* control_flow_tests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = control_flow_tests.cpp; sourceTree = "<group>"; };
39BC0FCF1FD057F900B6C276 /* control_flow.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = control_flow.hpp; sourceTree = "<group>"; };
39BC707420F55B6700A6EC20 /* clustering_map_tests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = clustering_map_tests.cpp; sourceTree = "<group>"; };
39BC707520F55B6700A6EC20 /* bwt_tests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = bwt_tests.cpp; sourceTree = "<group>"; };
39F995DE20F55B8A0034F977 /* worker_thread_tests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = worker_thread_tests.cpp; sourceTree = "<group>"; };
39F995DF20F55B8A0034F977 /* move_to_front_tests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = move_to_front_tests.cpp; sourceTree = "<group>"; };
39F995E020F55B8A0034F977 /* osm_id_test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = osm_id_test.cpp; sourceTree = "<group>"; };
39F995E120F55B8A0034F977 /* suffix_array_tests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = suffix_array_tests.cpp; sourceTree = "<group>"; };
39F995E220F55B8A0034F977 /* visitor_tests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = visitor_tests.cpp; sourceTree = "<group>"; };
39FD26C81CC65A0E00AFF551 /* assert_test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = assert_test.cpp; sourceTree = "<group>"; };
39FD26CA1CC65A0E00AFF551 /* bits_test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = bits_test.cpp; sourceTree = "<group>"; };
39FD26CB1CC65A0E00AFF551 /* buffer_vector_test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = buffer_vector_test.cpp; sourceTree = "<group>"; };
@ -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;
};

View file

@ -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 = "<group>"; };
6726C1E61A4C28D5005EEA39 /* feature_builder_test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = feature_builder_test.cpp; sourceTree = "<group>"; };
6726C1E71A4C28D5005EEA39 /* feature_merger_test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = feature_merger_test.cpp; sourceTree = "<group>"; };
6726C1E81A4C28D5005EEA39 /* osm_id_test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = osm_id_test.cpp; sourceTree = "<group>"; };
6726C1EA1A4C28D5005EEA39 /* osm_type_test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = osm_type_test.cpp; sourceTree = "<group>"; };
6726C1EB1A4C28D5005EEA39 /* tesselator_test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = tesselator_test.cpp; sourceTree = "<group>"; };
6726C1EC1A4C28D5005EEA39 /* triangles_tree_coding_test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = triangles_tree_coding_test.cpp; sourceTree = "<group>"; };
@ -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 */,