From 02722a1ed54fcd6016725dafef7e2316885f491e Mon Sep 17 00:00:00 2001 From: Teon Banek Date: Tue, 3 Sep 2019 15:35:46 +0200 Subject: [PATCH] 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 --- src/storage/v2/id_types.hpp | 105 ++++++++++++++++++++++++------------ 1 file changed, 70 insertions(+), 35 deletions(-) diff --git a/src/storage/v2/id_types.hpp b/src/storage/v2/id_types.hpp index 21191c109..7b159de2c 100644 --- a/src/storage/v2/id_types.hpp +++ b/src/storage/v2/id_types.hpp @@ -6,41 +6,44 @@ namespace storage { -#define STORAGE_DEFINE_ID_TYPE(name) \ - class name final { \ - private: \ - explicit name(uint64_t id) : id_(id) {} \ - \ - public: \ - static name FromUint(uint64_t id) { return name{id}; } \ - static name FromInt(int64_t id) { \ - return name{utils::MemcpyCast(id)}; \ - } \ - uint64_t AsUint() const { return id_; } \ - int64_t AsInt() const { return utils::MemcpyCast(id_); } \ - \ - private: \ - uint64_t id_; \ - }; \ - static_assert(std::is_trivially_copyable::value, \ - "storage::" #name " must be trivially copyable!"); \ - inline bool operator==(const name &first, const name &second) { \ - return first.AsUint() == second.AsUint(); \ - } \ - inline bool operator!=(const name &first, const name &second) { \ - return first.AsUint() != second.AsUint(); \ - } \ - inline bool operator<(const name &first, const name &second) { \ - return first.AsUint() < second.AsUint(); \ - } \ - inline bool operator>(const name &first, const name &second) { \ - return first.AsUint() > second.AsUint(); \ - } \ - inline bool operator<=(const name &first, const name &second) { \ - return first.AsUint() <= second.AsUint(); \ - } \ - inline bool operator>=(const name &first, const name &second) { \ - return first.AsUint() >= second.AsUint(); \ +#define STORAGE_DEFINE_ID_TYPE(name) \ + class name final { \ + private: \ + explicit name(uint64_t id) : id_(id) {} \ + \ + public: \ + /* Default constructor to allow serialization or preallocation. */ \ + name() = default; \ + \ + static name FromUint(uint64_t id) { return name{id}; } \ + static name FromInt(int64_t id) { \ + return name{utils::MemcpyCast(id)}; \ + } \ + uint64_t AsUint() const { return id_; } \ + int64_t AsInt() const { return utils::MemcpyCast(id_); } \ + \ + private: \ + uint64_t id_; \ + }; \ + static_assert(std::is_trivially_copyable::value, \ + "storage::" #name " must be trivially copyable!"); \ + inline bool operator==(const name &first, const name &second) { \ + return first.AsUint() == second.AsUint(); \ + } \ + inline bool operator!=(const name &first, const name &second) { \ + return first.AsUint() != second.AsUint(); \ + } \ + inline bool operator<(const name &first, const name &second) { \ + return first.AsUint() < second.AsUint(); \ + } \ + inline bool operator>(const name &first, const name &second) { \ + return first.AsUint() > second.AsUint(); \ + } \ + inline bool operator<=(const name &first, const name &second) { \ + return first.AsUint() <= second.AsUint(); \ + } \ + inline bool operator>=(const name &first, const name &second) { \ + return first.AsUint() >= second.AsUint(); \ } STORAGE_DEFINE_ID_TYPE(Gid); @@ -51,3 +54,35 @@ STORAGE_DEFINE_ID_TYPE(EdgeTypeId); #undef STORAGE_DEFINE_ID_TYPE } // namespace storage + +namespace std { + +template <> +struct hash { + size_t operator()(const storage::Gid &id) const noexcept { + return id.AsUint(); + } +}; + +template <> +struct hash { + size_t operator()(const storage::LabelId &id) const noexcept { + return id.AsUint(); + } +}; + +template <> +struct hash { + size_t operator()(const storage::PropertyId &id) const noexcept { + return id.AsUint(); + } +}; + +template <> +struct hash { + size_t operator()(const storage::EdgeTypeId &id) const noexcept { + return id.AsUint(); + } +}; + +} // namespace std