aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firestore/core/src/firebase/firestore/immutable/sorted_map.h
diff options
context:
space:
mode:
authorGravatar Gil <mcg@google.com>2018-04-20 12:11:19 -0700
committerGravatar GitHub <noreply@github.com>2018-04-20 12:11:19 -0700
commit0c03c28a3b7609d218a9acdff099fc0bda0f4ae6 (patch)
tree944ffbf8158b1faf75d8baa09abaec427b699222 /Firestore/core/src/firebase/firestore/immutable/sorted_map.h
parentede57f84bb4ddcc438bba34ef4407ee86fbaf2f9 (diff)
Implement find-related methods on C++ immutable maps (#1145)
* Standardize method ordering across sorted maps * Add SortedMap::find * Add SortedMap::find_index * Add SortedMap::contains
Diffstat (limited to 'Firestore/core/src/firebase/firestore/immutable/sorted_map.h')
-rw-r--r--Firestore/core/src/firebase/firestore/immutable/sorted_map.h59
1 files changed, 51 insertions, 8 deletions
diff --git a/Firestore/core/src/firebase/firestore/immutable/sorted_map.h b/Firestore/core/src/firebase/firestore/immutable/sorted_map.h
index 7c8c832..24eb5bf 100644
--- a/Firestore/core/src/firebase/firestore/immutable/sorted_map.h
+++ b/Firestore/core/src/firebase/firestore/immutable/sorted_map.h
@@ -133,6 +133,28 @@ class SortedMap : public impl::SortedMapBase {
return *this;
}
+ /** Returns true if the map contains no elements. */
+ bool empty() const {
+ switch (tag_) {
+ case Tag::Array:
+ return array_.empty();
+ case Tag::Tree:
+ return tree_.empty();
+ }
+ FIREBASE_UNREACHABLE();
+ }
+
+ /** Returns the number of items in this map. */
+ size_type size() const {
+ switch (tag_) {
+ case Tag::Array:
+ return array_.size();
+ case Tag::Tree:
+ return tree_.size();
+ }
+ FIREBASE_UNREACHABLE();
+ }
+
/**
* Creates a new map identical to this one, but with a key-value pair added or
* updated.
@@ -179,24 +201,45 @@ class SortedMap : public impl::SortedMapBase {
FIREBASE_UNREACHABLE();
}
- /** Returns true if the map contains no elements. */
- bool empty() const {
+ bool contains(const K& key) const {
switch (tag_) {
case Tag::Array:
- return array_.empty();
+ return array_.contains(key);
case Tag::Tree:
- return tree_.empty();
+ return tree_.contains(key);
}
FIREBASE_UNREACHABLE();
}
- /** Returns the number of items in this map. */
- size_type size() const {
+ /**
+ * Finds a value in the map.
+ *
+ * @param key The key to look up.
+ * @return An iterator pointing to the entry containing the key, or end() if
+ * not found.
+ */
+ const_iterator find(const K& key) const {
switch (tag_) {
case Tag::Array:
- return array_.size();
+ return const_iterator(array_.find(key));
case Tag::Tree:
- return tree_.size();
+ return const_iterator{tree_.find(key)};
+ }
+ FIREBASE_UNREACHABLE();
+ }
+
+ /**
+ * Finds the index of the given key in the map.
+ *
+ * @param key The key to look up.
+ * @return The index of the entry containing the key, or npos if not found.
+ */
+ size_type find_index(const K& key) const {
+ switch (tag_) {
+ case Tag::Array:
+ return array_.find_index(key);
+ case Tag::Tree:
+ return tree_.find_index(key);
}
FIREBASE_UNREACHABLE();
}