Add default constructor and std::hash to v2/id_types

Reviewers: mferencevic, ipaljak

Reviewed By: mferencevic

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D2349
This commit is contained in:
Teon Banek 2019-09-03 15:35:46 +02:00
parent 7213bec886
commit 02722a1ed5

View File

@ -6,41 +6,44 @@
namespace storage { namespace storage {
#define STORAGE_DEFINE_ID_TYPE(name) \ #define STORAGE_DEFINE_ID_TYPE(name) \
class name final { \ class name final { \
private: \ private: \
explicit name(uint64_t id) : id_(id) {} \ explicit name(uint64_t id) : id_(id) {} \
\ \
public: \ public: \
static name FromUint(uint64_t id) { return name{id}; } \ /* Default constructor to allow serialization or preallocation. */ \
static name FromInt(int64_t id) { \ name() = default; \
return name{utils::MemcpyCast<uint64_t>(id)}; \ \
} \ static name FromUint(uint64_t id) { return name{id}; } \
uint64_t AsUint() const { return id_; } \ static name FromInt(int64_t id) { \
int64_t AsInt() const { return utils::MemcpyCast<int64_t>(id_); } \ return name{utils::MemcpyCast<uint64_t>(id)}; \
\ } \
private: \ uint64_t AsUint() const { return id_; } \
uint64_t id_; \ int64_t AsInt() const { return utils::MemcpyCast<int64_t>(id_); } \
}; \ \
static_assert(std::is_trivially_copyable<name>::value, \ private: \
"storage::" #name " must be trivially copyable!"); \ uint64_t id_; \
inline bool operator==(const name &first, const name &second) { \ }; \
return first.AsUint() == second.AsUint(); \ static_assert(std::is_trivially_copyable<name>::value, \
} \ "storage::" #name " must be trivially copyable!"); \
inline bool operator!=(const name &first, const name &second) { \ inline bool operator==(const name &first, const name &second) { \
return first.AsUint() != second.AsUint(); \ return first.AsUint() == second.AsUint(); \
} \ } \
inline bool operator<(const name &first, const name &second) { \ inline bool operator!=(const name &first, const name &second) { \
return first.AsUint() < second.AsUint(); \ return first.AsUint() != second.AsUint(); \
} \ } \
inline bool operator>(const name &first, const name &second) { \ inline bool operator<(const name &first, const name &second) { \
return first.AsUint() > second.AsUint(); \ return first.AsUint() < second.AsUint(); \
} \ } \
inline bool operator<=(const name &first, const name &second) { \ inline bool operator>(const name &first, const name &second) { \
return first.AsUint() <= second.AsUint(); \ return first.AsUint() > second.AsUint(); \
} \ } \
inline bool operator>=(const name &first, const name &second) { \ inline bool operator<=(const name &first, const name &second) { \
return first.AsUint() >= second.AsUint(); \ return first.AsUint() <= second.AsUint(); \
} \
inline bool operator>=(const name &first, const name &second) { \
return first.AsUint() >= second.AsUint(); \
} }
STORAGE_DEFINE_ID_TYPE(Gid); STORAGE_DEFINE_ID_TYPE(Gid);
@ -51,3 +54,35 @@ STORAGE_DEFINE_ID_TYPE(EdgeTypeId);
#undef STORAGE_DEFINE_ID_TYPE #undef STORAGE_DEFINE_ID_TYPE
} // namespace storage } // namespace storage
namespace std {
template <>
struct hash<storage::Gid> {
size_t operator()(const storage::Gid &id) const noexcept {
return id.AsUint();
}
};
template <>
struct hash<storage::LabelId> {
size_t operator()(const storage::LabelId &id) const noexcept {
return id.AsUint();
}
};
template <>
struct hash<storage::PropertyId> {
size_t operator()(const storage::PropertyId &id) const noexcept {
return id.AsUint();
}
};
template <>
struct hash<storage::EdgeTypeId> {
size_t operator()(const storage::EdgeTypeId &id) const noexcept {
return id.AsUint();
}
};
} // namespace std