cxx20_erase_unordered_map.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_CONTAINERS_CXX20_ERASE_UNORDERED_MAP_H_
  5. #define BASE_CONTAINERS_CXX20_ERASE_UNORDERED_MAP_H_
  6. #include <unordered_map>
  7. #include "base/containers/cxx20_erase_internal.h"
  8. namespace base {
  9. // EraseIf is based on C++20's uniform container erasure API:
  10. // - https://eel.is/c++draft/libraryindex#:erase
  11. // - https://eel.is/c++draft/libraryindex#:erase_if
  12. // They provide a generic way to erase elements from a container.
  13. // The functions here implement these for the standard containers until those
  14. // functions are available in the C++ standard.
  15. // Note: there is no std::erase for standard associative containers so we don't
  16. // have it either.
  17. template <class Key,
  18. class T,
  19. class Hash,
  20. class KeyEqual,
  21. class Allocator,
  22. class Predicate>
  23. size_t EraseIf(std::unordered_map<Key, T, Hash, KeyEqual, Allocator>& container,
  24. Predicate pred) {
  25. return internal::IterateAndEraseIf(container, pred);
  26. }
  27. template <class Key,
  28. class T,
  29. class Hash,
  30. class KeyEqual,
  31. class Allocator,
  32. class Predicate>
  33. size_t EraseIf(
  34. std::unordered_multimap<Key, T, Hash, KeyEqual, Allocator>& container,
  35. Predicate pred) {
  36. return internal::IterateAndEraseIf(container, pred);
  37. }
  38. } // namespace base
  39. #endif // BASE_CONTAINERS_CXX20_ERASE_UNORDERED_MAP_H_