remove_if.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef _LIBCPP___ALGORITHM_REMOVE_IF_H
  9. #define _LIBCPP___ALGORITHM_REMOVE_IF_H
  10. #include <__algorithm/find_if.h>
  11. #include <__config>
  12. #include <__utility/move.h>
  13. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  14. # pragma GCC system_header
  15. #endif
  16. _LIBCPP_PUSH_MACROS
  17. #include <__undef_macros>
  18. _LIBCPP_BEGIN_NAMESPACE_STD
  19. template <class _ForwardIterator, class _Predicate>
  20. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
  21. remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) {
  22. __first = std::find_if<_ForwardIterator, _Predicate&>(__first, __last, __pred);
  23. if (__first != __last) {
  24. _ForwardIterator __i = __first;
  25. while (++__i != __last) {
  26. if (!__pred(*__i)) {
  27. *__first = std::move(*__i);
  28. ++__first;
  29. }
  30. }
  31. }
  32. return __first;
  33. }
  34. _LIBCPP_END_NAMESPACE_STD
  35. _LIBCPP_POP_MACROS
  36. #endif // _LIBCPP___ALGORITHM_REMOVE_IF_H