diff options
author | Gil <mcg@google.com> | 2018-05-01 08:57:31 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-01 08:57:31 -0700 |
commit | 51be76700c3b71a0924e8eaf7bac4b02e0130e62 (patch) | |
tree | 06cc3b2501c09507d4394f120fb97ea955b3db5a /Firestore/core/src/firebase/firestore/model | |
parent | 421cd3c24811f2b112fb01a17102e22a914d6673 (diff) |
Define a general hashing utility in C++ (#1195)
This is good enough to make it possible for the new C++ code to
interoperate with existing Objective-C code where `-hash` is required if
you override `-isEqual:`.
Diffstat (limited to 'Firestore/core/src/firebase/firestore/model')
3 files changed, 8 insertions, 11 deletions
diff --git a/Firestore/core/src/firebase/firestore/model/base_path.h b/Firestore/core/src/firebase/firestore/model/base_path.h index bc1f89d..59a9b5a 100644 --- a/Firestore/core/src/firebase/firestore/model/base_path.h +++ b/Firestore/core/src/firebase/firestore/model/base_path.h @@ -25,6 +25,7 @@ #include <vector> #include "Firestore/core/src/firebase/firestore/util/firebase_assert.h" +#include "Firestore/core/src/firebase/firestore/util/hashing.h" namespace firebase { namespace firestore { @@ -162,13 +163,8 @@ class BasePath { #if defined(__OBJC__) // For Objective-C++ hash; to be removed after migration. // Do NOT use in C++ code. - NSUInteger Hash() const { - std::hash<std::string> hash_fn; - NSUInteger hash_result = 0; - for (const std::string& segment : segments_) { - hash_result = hash_result * 31u + hash_fn(segment); - } - return hash_result; + size_t Hash() const { + return util::Hash(segments_); } #endif // defined(__OBJC__) diff --git a/Firestore/core/src/firebase/firestore/model/database_id.h b/Firestore/core/src/firebase/firestore/model/database_id.h index 0c0e0ec..c432b8f 100644 --- a/Firestore/core/src/firebase/firestore/model/database_id.h +++ b/Firestore/core/src/firebase/firestore/model/database_id.h @@ -20,6 +20,7 @@ #include <cstdint> #include <string> +#include "Firestore/core/src/firebase/firestore/util/hashing.h" #include "absl/strings/string_view.h" namespace firebase { @@ -62,9 +63,8 @@ class DatabaseId { #if defined(__OBJC__) // For objective-c++ hash; to be removed after migration. // Do NOT use in C++ code. - NSUInteger Hash() const { - std::hash<std::string> hash_fn; - return hash_fn(project_id_) * 31u + hash_fn(database_id_); + size_t Hash() const { + return util::Hash(project_id_, database_id_); } #endif // defined(__OBJC__) diff --git a/Firestore/core/src/firebase/firestore/model/document_key.h b/Firestore/core/src/firebase/firestore/model/document_key.h index 4bdc04b..7f6478f 100644 --- a/Firestore/core/src/firebase/firestore/model/document_key.h +++ b/Firestore/core/src/firebase/firestore/model/document_key.h @@ -26,6 +26,7 @@ #endif // defined(__OBJC__) #include "Firestore/core/src/firebase/firestore/model/resource_path.h" +#include "Firestore/core/src/firebase/firestore/util/hashing.h" #include "absl/strings/string_view.h" namespace firebase { @@ -61,7 +62,7 @@ class DocumentKey { } NSUInteger Hash() const { - return std::hash<std::string>{}(ToString()); + return util::Hash(ToString()); } #endif |