aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firestore/core
diff options
context:
space:
mode:
authorGravatar Gil <mcg@google.com>2018-05-05 08:10:51 -0700
committerGravatar GitHub <noreply@github.com>2018-05-05 08:10:51 -0700
commita080e481b5e6fcbc2b645920051cf20fc8cad7a7 (patch)
tree5d5ce3d39be316ac82a2fd34c92b05fc9e67f83a /Firestore/core
parentd158e420c6fa04997ee3d2a6c44fd53a52883d81 (diff)
Port FSTDocumentKeySet to C++ DocumentKeySet (#1229)
* Define a Comparator for DocumentKey * Automated migration from FSTDocumentKeySet to DocumentKeySet * Manual fixups for DocumentKeySet * Delete FSTDocumentKeySet
Diffstat (limited to 'Firestore/core')
-rw-r--r--Firestore/core/src/firebase/firestore/immutable/sorted_set.h16
-rw-r--r--Firestore/core/src/firebase/firestore/model/document_key.h9
-rw-r--r--Firestore/core/test/firebase/firestore/model/document_key_test.cc23
3 files changed, 40 insertions, 8 deletions
diff --git a/Firestore/core/src/firebase/firestore/immutable/sorted_set.h b/Firestore/core/src/firebase/firestore/immutable/sorted_set.h
index 6828106..d78fd61 100644
--- a/Firestore/core/src/firebase/firestore/immutable/sorted_set.h
+++ b/Firestore/core/src/firebase/firestore/immutable/sorted_set.h
@@ -61,6 +61,13 @@ class SortedSet {
explicit SortedSet(M&& map) : map_{std::move(map)} {
}
+ SortedSet(std::initializer_list<value_type> entries, const C& comparator = {})
+ : map_{comparator} {
+ for (auto&& value : entries) {
+ map_ = map_.insert(value, {});
+ }
+ }
+
bool empty() const {
return map_.empty();
}
@@ -124,7 +131,14 @@ class SortedSet {
}
friend bool operator==(const SortedSet& lhs, const SortedSet& rhs) {
- return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
+ if (lhs.size() != rhs.size()) {
+ return false;
+ }
+ return std::equal(lhs.begin(), lhs.end(), rhs.begin());
+ }
+
+ friend bool operator!=(const SortedSet& lhs, const SortedSet& rhs) {
+ return !(lhs == rhs);
}
private:
diff --git a/Firestore/core/src/firebase/firestore/model/document_key.h b/Firestore/core/src/firebase/firestore/model/document_key.h
index 7f6478f..6532b2e 100644
--- a/Firestore/core/src/firebase/firestore/model/document_key.h
+++ b/Firestore/core/src/firebase/firestore/model/document_key.h
@@ -17,6 +17,7 @@
#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_DOCUMENT_KEY_H_
#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_DOCUMENT_KEY_H_
+#include <functional>
#include <initializer_list>
#include <memory>
#include <string>
@@ -26,6 +27,7 @@
#endif // defined(__OBJC__)
#include "Firestore/core/src/firebase/firestore/model/resource_path.h"
+#include "Firestore/core/src/firebase/firestore/util/comparison.h"
#include "Firestore/core/src/firebase/firestore/util/hashing.h"
#include "absl/strings/string_view.h"
@@ -118,6 +120,13 @@ inline bool operator>=(const DocumentKey& lhs, const DocumentKey& rhs) {
}
} // namespace model
+
+namespace util {
+
+template <>
+struct Comparator<model::DocumentKey> : public std::less<model::DocumentKey> {};
+
+} // namespace util
} // namespace firestore
} // namespace firebase
diff --git a/Firestore/core/test/firebase/firestore/model/document_key_test.cc b/Firestore/core/test/firebase/firestore/model/document_key_test.cc
index 619ee7f..71b78d1 100644
--- a/Firestore/core/test/firebase/firestore/model/document_key_test.cc
+++ b/Firestore/core/test/firebase/firestore/model/document_key_test.cc
@@ -21,8 +21,11 @@
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
#include "Firestore/core/src/firebase/firestore/model/resource_path.h"
+#include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
#include "gtest/gtest.h"
+using firebase::firestore::testutil::Key;
+
namespace firebase {
namespace firestore {
namespace model {
@@ -112,16 +115,16 @@ TEST(DocumentKey, IsDocumentKey) {
}
TEST(DocumentKey, Comparison) {
- const DocumentKey abcd({"a", "b", "c", "d"});
- const DocumentKey abcd_too({"a", "b", "c", "d"});
- const DocumentKey xyzw({"x", "y", "z", "w"});
+ DocumentKey abcd = Key("a/b/c/d");
+ DocumentKey abcd_too = Key("a/b/c/d");
+ DocumentKey xyzw = Key("x/y/z/w");
EXPECT_EQ(abcd, abcd_too);
EXPECT_NE(abcd, xyzw);
- const DocumentKey empty;
- const DocumentKey a({"a", "a"});
- const DocumentKey b({"b", "b"});
- const DocumentKey ab({"a", "a", "b", "b"});
+ DocumentKey empty;
+ DocumentKey a = Key("a/a");
+ DocumentKey b = Key("b/b");
+ DocumentKey ab = Key("a/a/b/b");
EXPECT_FALSE(empty < empty);
EXPECT_TRUE(empty <= empty);
@@ -148,6 +151,12 @@ TEST(DocumentKey, Comparison) {
EXPECT_TRUE(ab >= a);
}
+TEST(DocumentKey, Comparator) {
+ DocumentKey abcd = Key("a/b/c/d");
+ DocumentKey xyzw = Key("x/y/z/w");
+ EXPECT_TRUE(util::Comparator<DocumentKey>{}(abcd, xyzw));
+}
+
} // namespace model
} // namespace firestore
} // namespace firebase