From 0c03c28a3b7609d218a9acdff099fc0bda0f4ae6 Mon Sep 17 00:00:00 2001 From: Gil Date: Fri, 20 Apr 2018 12:11:19 -0700 Subject: 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 --- .../firestore/immutable/array_sorted_map.h | 39 +++++++++++++++------- 1 file changed, 27 insertions(+), 12 deletions(-) (limited to 'Firestore/core/src/firebase/firestore/immutable/array_sorted_map.h') 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(found - begin()); } /** -- cgit v1.2.3