[libc++] Implement ranges::replace{, _if}

Reviewed By: var-const, #libc

Spies: libcxx-commits, mgorny

Differential Revision: https://reviews.llvm.org/D126283
diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm
index 6f542be..f3436b7 100644
--- a/libcxx/include/algorithm
+++ b/libcxx/include/algorithm
@@ -401,6 +401,29 @@
                                      projected<iterator_t<R>, Proj>> Pred = ranges::equal_to>
     constexpr borrowed_iterator_t<R> ranges::adjacent_find(R&& r, Pred pred = {}, Proj proj = {});  // since C++20
 
+  template<input_iterator I, sentinel_for<I> S, class T1, class T2, class Proj = identity>
+    requires indirectly_writable<I, const T2&> &&
+             indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T1*>
+    constexpr I
+      ranges::replace(I first, S last, const T1& old_value, const T2& new_value, Proj proj = {});   // since C++20
+
+  template<input_range R, class T1, class T2, class Proj = identity>
+    requires indirectly_writable<iterator_t<R>, const T2&> &&
+             indirect_binary_predicate<ranges::equal_to, projected<iterator_t<R>, Proj>, const T1*>
+    constexpr borrowed_iterator_t<R>
+      ranges::replace(R&& r, const T1& old_value, const T2& new_value, Proj proj = {});             // since C++20
+
+  template<input_iterator I, sentinel_for<I> S, class T, class Proj = identity,
+           indirect_unary_predicate<projected<I, Proj>> Pred>
+    requires indirectly_writable<I, const T&>
+    constexpr I ranges::replace_if(I first, S last, Pred pred, const T& new_value, Proj proj = {}); // since C++20
+
+  template<input_range R, class T, class Proj = identity,
+           indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
+    requires indirectly_writable<iterator_t<R>, const T&>
+    constexpr borrowed_iterator_t<R>
+      ranges::replace_if(R&& r, Pred pred, const T& new_value, Proj proj = {});                     // since C++20
+
 }
 
     constexpr bool     // constexpr in C++20
@@ -1148,6 +1171,8 @@
 #include <__algorithm/ranges_minmax_element.h>
 #include <__algorithm/ranges_mismatch.h>
 #include <__algorithm/ranges_none_of.h>
+#include <__algorithm/ranges_replace.h>
+#include <__algorithm/ranges_replace_if.h>
 #include <__algorithm/ranges_reverse.h>
 #include <__algorithm/ranges_swap_ranges.h>
 #include <__algorithm/ranges_transform.h>