1 // Copyright 2021 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_CONTAINERS_CXX20_ERASE_INTERNAL_H_ 6 #define BASE_CONTAINERS_CXX20_ERASE_INTERNAL_H_ 7 8 #include <cstddef> 9 10 // Internal portion of base/containers/cxx20_erase_*.h. Please include those 11 // headers instead of including this directly. 12 13 namespace base { 14 15 namespace internal { 16 17 // Calls erase on iterators of matching elements and returns the number of 18 // removed elements. 19 template <typename Container, typename Predicate> IterateAndEraseIf(Container & container,Predicate pred)20size_t IterateAndEraseIf(Container& container, Predicate pred) { 21 size_t old_size = container.size(); 22 for (auto it = container.begin(), last = container.end(); it != last;) { 23 if (pred(*it)) 24 it = container.erase(it); 25 else 26 ++it; 27 } 28 return old_size - container.size(); 29 } 30 31 } // namespace internal 32 33 } // namespace base 34 35 #endif // BASE_CONTAINERS_CXX20_ERASE_INTERNAL_H_ 36