diff options
author | Gil <mcg@google.com> | 2018-04-20 12:11:19 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-20 12:11:19 -0700 |
commit | 0c03c28a3b7609d218a9acdff099fc0bda0f4ae6 (patch) | |
tree | 944ffbf8158b1faf75d8baa09abaec427b699222 /Firestore/core/src/firebase/firestore/immutable/array_sorted_map.h | |
parent | ede57f84bb4ddcc438bba34ef4407ee86fbaf2f9 (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/array_sorted_map.h')
-rw-r--r-- | Firestore/core/src/firebase/firestore/immutable/array_sorted_map.h | 39 |
1 files changed, 27 insertions, 12 deletions
diff --git a/Firestore/core/src/firebase/firestore/immutable/array_sorted_map.h b/Firestore/core/src/firebase/firestore/immutable/array_sorted_map.h index 6808297..6ffe017 100644 --- a/Firestore/core/src/firebase/firestore/immutable/array_sorted_map.h +++ b/Firestore/core/src/firebase/firestore/immutable/array_sorted_map.h @@ -152,6 +152,20 @@ class ArraySortedMap : public SortedMapBase { key_comparator_{comparator} { } + /** Returns true if the map contains no elements. */ + bool empty() const { + return size() == 0; + } + + /** Returns the number of items in this map. */ + size_type size() const { + return array_->size(); + } + + const key_comparator_type& comparator() const { + return key_comparator_; + } + /** * Creates a new map identical to this one, but with a key-value pair added or * updated. @@ -212,6 +226,10 @@ class ArraySortedMap : public SortedMapBase { } } + bool contains(const K& key) const { + return find(key) != end(); + } + /** * Finds a value in the map. * @@ -229,18 +247,15 @@ class ArraySortedMap : public SortedMapBase { } } - const key_comparator_type& comparator() const { - return key_comparator_; - } - - /** Returns true if the map contains no elements. */ - bool empty() const { - return size() == 0; - } - - /** Returns the number of items in this map. */ - size_type size() const { - return array_->size(); + /** + * 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 { + auto found = find(key); + return found == end() ? npos : static_cast<size_type>(found - begin()); } /** |