diff options
author | Gil <mcg@google.com> | 2018-04-19 11:30:29 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-19 11:30:29 -0700 |
commit | 0df8378971553a203cc6982a298f342baecae543 (patch) | |
tree | 0db33e6bea1cb4ed6ca74ad299eb868bdb943693 /Firestore/core/src/firebase/firestore/immutable/sorted_map.h | |
parent | 81ac1761e2195aa2f16c0377471e084910ccdb35 (diff) |
Implement iterators for our immutable maps (#1132)
* Add a minimal LlrbNodeIterator
* Remove fixed_size type parameter from FixedArray
The parameter wasn't that useful and caused problems in trying to define
dependent iterator types.
* Add begin()/end() to SortedMap.
Diffstat (limited to 'Firestore/core/src/firebase/firestore/immutable/sorted_map.h')
-rw-r--r-- | Firestore/core/src/firebase/firestore/immutable/sorted_map.h | 37 |
1 files changed, 35 insertions, 2 deletions
diff --git a/Firestore/core/src/firebase/firestore/immutable/sorted_map.h b/Firestore/core/src/firebase/firestore/immutable/sorted_map.h index b7729b3..7c8c832 100644 --- a/Firestore/core/src/firebase/firestore/immutable/sorted_map.h +++ b/Firestore/core/src/firebase/firestore/immutable/sorted_map.h @@ -21,6 +21,7 @@ #include "Firestore/core/src/firebase/firestore/immutable/array_sorted_map.h" #include "Firestore/core/src/firebase/firestore/immutable/sorted_map_base.h" +#include "Firestore/core/src/firebase/firestore/immutable/sorted_map_iterator.h" #include "Firestore/core/src/firebase/firestore/immutable/tree_sorted_map.h" #include "Firestore/core/src/firebase/firestore/util/comparison.h" @@ -40,6 +41,11 @@ class SortedMap : public impl::SortedMapBase { using array_type = impl::ArraySortedMap<K, V, C>; using tree_type = impl::TreeSortedMap<K, V, C>; + using const_iterator = impl::SortedMapIterator< + value_type, + typename impl::FixedArray<value_type>::const_iterator, + typename impl::LlrbNode<K, V>::const_iterator>; + /** * Creates an empty SortedMap. */ @@ -71,7 +77,7 @@ class SortedMap : public impl::SortedMapBase { } } - SortedMap(SortedMap&& other) : tag_{other.tag_} { + SortedMap(SortedMap&& other) noexcept : tag_{other.tag_} { switch (tag_) { case Tag::Array: new (&array_) array_type{std::move(other.array_)}; @@ -110,7 +116,7 @@ class SortedMap : public impl::SortedMapBase { return *this; } - SortedMap& operator=(SortedMap&& other) { + SortedMap& operator=(SortedMap&& other) noexcept { if (tag_ == other.tag_) { switch (tag_) { case Tag::Array: @@ -195,6 +201,33 @@ class SortedMap : public impl::SortedMapBase { FIREBASE_UNREACHABLE(); } + /** + * Returns an iterator pointing to the first entry in the map. If there are + * no entries in the map, begin() == end(). + */ + const_iterator begin() const { + switch (tag_) { + case Tag::Array: + return const_iterator{array_.begin()}; + case Tag::Tree: + return const_iterator{tree_.begin()}; + } + FIREBASE_UNREACHABLE(); + } + + /** + * Returns an iterator pointing past the last entry in the map. + */ + const_iterator end() const { + switch (tag_) { + case Tag::Array: + return const_iterator{array_.end()}; + case Tag::Tree: + return const_iterator{tree_.end()}; + } + FIREBASE_UNREACHABLE(); + } + private: explicit SortedMap(array_type&& array) : tag_{Tag::Array}, array_{std::move(array)} { |