diff options
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 | 55 |
1 files changed, 12 insertions, 43 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 4b8acde..56da9e9 100644 --- a/Firestore/core/src/firebase/firestore/immutable/array_sorted_map.h +++ b/Firestore/core/src/firebase/firestore/immutable/array_sorted_map.h @@ -25,45 +25,15 @@ #include <utility> #include "Firestore/core/src/firebase/firestore/immutable/map_entry.h" +#include "Firestore/core/src/firebase/firestore/immutable/sorted_map_base.h" #include "Firestore/core/src/firebase/firestore/util/firebase_assert.h" namespace firebase { namespace firestore { namespace immutable { - namespace impl { /** - * A base class for implementing ArraySortedMap, containing types and constants - * that don't depend upon the template parameters to the main class. - * - * Note that this exists as a base class rather than as just a namespace in - * order to make it possible for users of ArraySortedMap to avoid needing to - * declare storage for each instantiation of the template. - */ -class ArraySortedMapBase { - public: - /** - * The type of size() methods on immutable collections. Note that this is not - * size_t specifically to save space in the TreeSortedMap implementation. - */ - using size_type = uint32_t; - - /** - * The maximum size of an ArraySortedMap. - * - * This is the size threshold where we use a tree backed sorted map instead of - * an array backed sorted map. This is a more or less arbitrary chosen value, - * that was chosen to be large enough to fit most of object kind of Firebase - * data, but small enough to not notice degradation in performance for - * inserting and lookups. Feel free to empirically determine this constant, - * but don't expect much gain in real world performance. - */ - // TODO(wilhuff): actually use this for switching implementations. - static constexpr size_type kFixedSize = 25; -}; - -/** * A bounded-size array that allocates its contents directly in itself. This * saves a heap allocation when compared with std::vector (though std::vector * can resize itself while FixedArray cannot). @@ -78,10 +48,10 @@ class ArraySortedMapBase { * @tparam T The type of an element in the array. * @tparam fixed_size the fixed size to use in creating the FixedArray. */ -template <typename T, ArraySortedMapBase::size_type fixed_size> +template <typename T, SortedMapBase::size_type fixed_size> class FixedArray { public: - using size_type = ArraySortedMapBase::size_type; + using size_type = SortedMapBase::size_type; using array_type = std::array<T, fixed_size>; using iterator = typename array_type::iterator; using const_iterator = typename array_type::const_iterator; @@ -144,14 +114,12 @@ class FixedArray { size_type size_ = 0; }; -} // namespace impl - /** * ArraySortedMap is a value type containing a map. It is immutable, but has * methods to efficiently create new maps that are mutations of it. */ template <typename K, typename V, typename C = std::less<K>> -class ArraySortedMap : public impl::ArraySortedMapBase { +class ArraySortedMap : public SortedMapBase { public: using key_comparator_type = KeyComparator<K, V, C>; @@ -163,7 +131,7 @@ class ArraySortedMap : public impl::ArraySortedMapBase { /** * The type of the fixed-size array containing entries of value_type. */ - using array_type = impl::FixedArray<value_type, kFixedSize>; + using array_type = FixedArray<value_type, kFixedSize>; using const_iterator = typename array_type::const_iterator; using array_pointer = std::shared_ptr<const array_type>; @@ -172,7 +140,7 @@ class ArraySortedMap : public impl::ArraySortedMapBase { * Creates an empty ArraySortedMap. */ explicit ArraySortedMap(const C& comparator = C()) - : array_(EmptyArray()), key_comparator_(comparator) { + : array_{EmptyArray()}, key_comparator_{comparator} { } /** @@ -180,8 +148,8 @@ class ArraySortedMap : public impl::ArraySortedMapBase { */ ArraySortedMap(std::initializer_list<value_type> entries, const C& comparator = C()) - : array_(std::make_shared<array_type>(entries.begin(), entries.end())), - key_comparator_(comparator) { + : array_{std::make_shared<array_type>(entries.begin(), entries.end())}, + key_comparator_{comparator} { } /** @@ -211,7 +179,7 @@ class ArraySortedMap : public impl::ArraySortedMapBase { auto copy = std::make_shared<array_type>(begin(), pos); // Copy the value to be inserted. - copy->append(value_type(key, value)); + copy->append({key, value}); if (replacing_entry) { // Skip the thing at pos because it compares the same as the pair above. @@ -297,11 +265,11 @@ class ArraySortedMap : public impl::ArraySortedMapBase { ArraySortedMap(const array_pointer& array, const key_comparator_type& key_comparator) noexcept - : array_(array), key_comparator_(key_comparator) { + : array_{array}, key_comparator_{key_comparator} { } ArraySortedMap wrap(const array_pointer& array) const noexcept { - return ArraySortedMap(array, key_comparator_); + return ArraySortedMap{array, key_comparator_}; } const_iterator LowerBound(const K& key) const { @@ -312,6 +280,7 @@ class ArraySortedMap : public impl::ArraySortedMapBase { key_comparator_type key_comparator_; }; +} // namespace impl } // namespace immutable } // namespace firestore } // namespace firebase |