Loading include/ftl/small_map.h +6 −0 Original line number Diff line number Diff line Loading @@ -237,6 +237,12 @@ class SmallMap final { // bool erase(const key_type& key) { return erase(key, begin()); } // Removes all mappings. // // All iterators are invalidated. // void clear() { map_.clear(); } private: iterator find(const key_type& key, iterator first) { return std::find_if(first, end(), [&key](const auto& pair) { return pair.first == key; }); Loading include/ftl/small_vector.h +10 −9 Original line number Diff line number Diff line Loading @@ -151,8 +151,6 @@ class SmallVector final : ArrayTraits<T>, ArrayComparators<SmallVector> { DISPATCH(reference, back, noexcept) DISPATCH(const_reference, back, const) #undef DISPATCH reference operator[](size_type i) { return dynamic() ? std::get<Dynamic>(vector_)[i] : std::get<Static>(vector_)[i]; } Loading Loading @@ -214,13 +212,15 @@ class SmallVector final : ArrayTraits<T>, ArrayComparators<SmallVector> { // // The last() and end() iterators are invalidated. // void pop_back() { if (dynamic()) { std::get<Dynamic>(vector_).pop_back(); } else { std::get<Static>(vector_).pop_back(); } } DISPATCH(void, pop_back, noexcept) // Removes all elements. // // All iterators are invalidated. // DISPATCH(void, clear, noexcept) #undef DISPATCH // Erases an element, but does not preserve order. Rather than shifting subsequent elements, // this moves the last element to the slot of the erased element. Loading Loading @@ -345,6 +345,7 @@ class SmallVector<T, 0> final : ArrayTraits<T>, return true; } using Impl::clear; using Impl::pop_back; void unstable_erase(iterator it) { Loading include/ftl/static_vector.h +10 −2 Original line number Diff line number Diff line Loading @@ -189,8 +189,7 @@ class StaticVector final : ArrayTraits<T>, } StaticVector& operator=(StaticVector&& other) { std::destroy(begin(), end()); size_ = 0; clear(); swap<true>(other); return *this; } Loading Loading @@ -280,6 +279,15 @@ class StaticVector final : ArrayTraits<T>, // void pop_back() { unstable_erase(last()); } // Removes all elements. // // All iterators are invalidated. // void clear() { std::destroy(begin(), end()); size_ = 0; } // Erases an element, but does not preserve order. Rather than shifting subsequent elements, // this moves the last element to the slot of the erased element. // Loading libs/ftl/small_map_test.cpp +17 −0 Original line number Diff line number Diff line Loading @@ -345,4 +345,21 @@ TEST(SmallMap, Erase) { } } TEST(SmallMap, Clear) { SmallMap map = ftl::init::map(1, '1')(2, '2')(3, '3'); map.clear(); EXPECT_TRUE(map.empty()); EXPECT_FALSE(map.dynamic()); map = ftl::init::map(1, '1')(2, '2')(3, '3'); map.try_emplace(4, '4'); map.clear(); EXPECT_TRUE(map.empty()); EXPECT_TRUE(map.dynamic()); } } // namespace android::test libs/ftl/small_vector_test.cpp +30 −0 Original line number Diff line number Diff line Loading @@ -460,4 +460,34 @@ TEST(SmallVector, Destroy) { EXPECT_EQ(0, dead); } TEST(SmallVector, Clear) { int live = 0; int dead = 0; SmallVector<DestroyCounts, 2> counts; counts.emplace_back(live, dead); counts.emplace_back(live, dead); counts.clear(); EXPECT_TRUE(counts.empty()); EXPECT_FALSE(counts.dynamic()); EXPECT_EQ(2, live); EXPECT_EQ(0, dead); live = 0; counts.emplace_back(live, dead); counts.emplace_back(live, dead); counts.emplace_back(live, dead); counts.clear(); EXPECT_TRUE(counts.empty()); EXPECT_TRUE(counts.dynamic()); EXPECT_EQ(3, live); EXPECT_EQ(2, dead); } } // namespace android::test Loading
include/ftl/small_map.h +6 −0 Original line number Diff line number Diff line Loading @@ -237,6 +237,12 @@ class SmallMap final { // bool erase(const key_type& key) { return erase(key, begin()); } // Removes all mappings. // // All iterators are invalidated. // void clear() { map_.clear(); } private: iterator find(const key_type& key, iterator first) { return std::find_if(first, end(), [&key](const auto& pair) { return pair.first == key; }); Loading
include/ftl/small_vector.h +10 −9 Original line number Diff line number Diff line Loading @@ -151,8 +151,6 @@ class SmallVector final : ArrayTraits<T>, ArrayComparators<SmallVector> { DISPATCH(reference, back, noexcept) DISPATCH(const_reference, back, const) #undef DISPATCH reference operator[](size_type i) { return dynamic() ? std::get<Dynamic>(vector_)[i] : std::get<Static>(vector_)[i]; } Loading Loading @@ -214,13 +212,15 @@ class SmallVector final : ArrayTraits<T>, ArrayComparators<SmallVector> { // // The last() and end() iterators are invalidated. // void pop_back() { if (dynamic()) { std::get<Dynamic>(vector_).pop_back(); } else { std::get<Static>(vector_).pop_back(); } } DISPATCH(void, pop_back, noexcept) // Removes all elements. // // All iterators are invalidated. // DISPATCH(void, clear, noexcept) #undef DISPATCH // Erases an element, but does not preserve order. Rather than shifting subsequent elements, // this moves the last element to the slot of the erased element. Loading Loading @@ -345,6 +345,7 @@ class SmallVector<T, 0> final : ArrayTraits<T>, return true; } using Impl::clear; using Impl::pop_back; void unstable_erase(iterator it) { Loading
include/ftl/static_vector.h +10 −2 Original line number Diff line number Diff line Loading @@ -189,8 +189,7 @@ class StaticVector final : ArrayTraits<T>, } StaticVector& operator=(StaticVector&& other) { std::destroy(begin(), end()); size_ = 0; clear(); swap<true>(other); return *this; } Loading Loading @@ -280,6 +279,15 @@ class StaticVector final : ArrayTraits<T>, // void pop_back() { unstable_erase(last()); } // Removes all elements. // // All iterators are invalidated. // void clear() { std::destroy(begin(), end()); size_ = 0; } // Erases an element, but does not preserve order. Rather than shifting subsequent elements, // this moves the last element to the slot of the erased element. // Loading
libs/ftl/small_map_test.cpp +17 −0 Original line number Diff line number Diff line Loading @@ -345,4 +345,21 @@ TEST(SmallMap, Erase) { } } TEST(SmallMap, Clear) { SmallMap map = ftl::init::map(1, '1')(2, '2')(3, '3'); map.clear(); EXPECT_TRUE(map.empty()); EXPECT_FALSE(map.dynamic()); map = ftl::init::map(1, '1')(2, '2')(3, '3'); map.try_emplace(4, '4'); map.clear(); EXPECT_TRUE(map.empty()); EXPECT_TRUE(map.dynamic()); } } // namespace android::test
libs/ftl/small_vector_test.cpp +30 −0 Original line number Diff line number Diff line Loading @@ -460,4 +460,34 @@ TEST(SmallVector, Destroy) { EXPECT_EQ(0, dead); } TEST(SmallVector, Clear) { int live = 0; int dead = 0; SmallVector<DestroyCounts, 2> counts; counts.emplace_back(live, dead); counts.emplace_back(live, dead); counts.clear(); EXPECT_TRUE(counts.empty()); EXPECT_FALSE(counts.dynamic()); EXPECT_EQ(2, live); EXPECT_EQ(0, dead); live = 0; counts.emplace_back(live, dead); counts.emplace_back(live, dead); counts.emplace_back(live, dead); counts.clear(); EXPECT_TRUE(counts.empty()); EXPECT_TRUE(counts.dynamic()); EXPECT_EQ(3, live); EXPECT_EQ(2, dead); } } // namespace android::test