cxx20_erase_unordered_set.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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_SET_H_
  5. #define BASE_CONTAINERS_CXX20_ERASE_UNORDERED_SET_H_
  6. #include <unordered_set>
  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 Hash,
  19. class KeyEqual,
  20. class Allocator,
  21. class Predicate>
  22. size_t EraseIf(std::unordered_set<Key, Hash, KeyEqual, Allocator>& container,
  23. Predicate pred) {
  24. return internal::IterateAndEraseIf(container, pred);
  25. }
  26. template <class Key,
  27. class Hash,
  28. class KeyEqual,
  29. class Allocator,
  30. class Predicate>
  31. size_t EraseIf(
  32. std::unordered_multiset<Key, Hash, KeyEqual, Allocator>& container,
  33. Predicate pred) {
  34. return internal::IterateAndEraseIf(container, pred);
  35. }
  36. } // namespace base
  37. #endif // BASE_CONTAINERS_CXX20_ERASE_UNORDERED_SET_H_