Skip to content

Conversation

@huixie90
Copy link
Member

  • [libc++] implement adjacent_view
  • ci
  • ci
  • [libc++] adjacent_transform

@huixie90 huixie90 requested a review from a team as a code owner November 15, 2025 14:54
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Nov 15, 2025
@llvmbot
Copy link
Member

llvmbot commented Nov 15, 2025

@llvm/pr-subscribers-libcxx

Author: Hui (huixie90)

Changes
  • [libc++] implement adjacent_view
  • ci
  • ci
  • [libc++] adjacent_transform

Patch is 269.82 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/168208.diff

66 Files Affected:

  • (modified) libcxx/docs/ReleaseNotes/22.rst (+1-2)
  • (modified) libcxx/include/CMakeLists.txt (+3)
  • (added) libcxx/include/__ranges/adjacent_transform_view.h (+411)
  • (added) libcxx/include/__ranges/adjacent_view.h (+416)
  • (added) libcxx/include/__ranges/zip_utils.h (+49)
  • (modified) libcxx/include/__ranges/zip_view.h (+4-12)
  • (modified) libcxx/include/module.modulemap.in (+5)
  • (modified) libcxx/include/ranges (+27)
  • (modified) libcxx/modules/std/ranges.inc (+3-3)
  • (modified) libcxx/test/std/library/description/conventions/customization.point.object/cpo.compile.pass.cpp (+2-1)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent.transform/adaptor.pass.cpp (+275)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent.transform/base.pass.cpp (+114)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent.transform/begin.pass.cpp (+133)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent.transform/ctor.default.pass.cpp (+82)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent.transform/ctor.views.pass.cpp (+99)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent.transform/end.pass.cpp (+183)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent.transform/general.pass.cpp (+47)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent.transform/helpers.h (+110)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent.transform/iterator/arithmetic.pass.cpp (+164)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent.transform/iterator/compare.pass.cpp (+162)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent.transform/iterator/ctor.default.pass.cpp (+58)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent.transform/iterator/ctor.other.pass.cpp (+83)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent.transform/iterator/decrement.pass.cpp (+100)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent.transform/iterator/deref.pass.cpp (+143)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent.transform/iterator/increment.pass.cpp (+90)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent.transform/iterator/member_types.compile.pass.cpp (+147)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent.transform/iterator/singular.pass.cpp (+107)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent.transform/iterator/subscript.pass.cpp (+76)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent.transform/range.concept.compile.pass.cpp (+158)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent.transform/sentinel/ctor.default.pass.cpp (+70)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent.transform/sentinel/ctor.other.pass.cpp (+125)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent.transform/sentinel/eq.pass.cpp (+206)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent.transform/sentinel/minus.pass.cpp (+195)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent.transform/size.pass.cpp (+131)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent/adaptor.pass.cpp (+242)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent/base.pass.cpp (+104)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent/begin.pass.cpp (+136)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent/borrowing.compile.pass.cpp (+39)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent/ctor.default.pass.cpp (+73)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent/ctor.views.pass.cpp (+102)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent/end.pass.cpp (+143)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent/general.pass.cpp (+57)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent/helpers.h (+43)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent/iterator/arithmetic.pass.cpp (+165)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent/iterator/compare.pass.cpp (+153)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent/iterator/ctor.default.pass.cpp (+94)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent/iterator/ctor.other.pass.cpp (+82)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent/iterator/decrement.pass.cpp (+102)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent/iterator/deref.pass.cpp (+105)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent/iterator/increment.pass.cpp (+92)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent/iterator/iter_move.pass.cpp (+154)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent/iterator/iter_swap.pass.cpp (+136)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent/iterator/member_types.compile.pass.cpp (+150)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent/iterator/singular.pass.cpp (+106)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent/iterator/subscript.pass.cpp (+67)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent/range.concept.compile.pass.cpp (+135)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent/sentinel/ctor.default.pass.cpp (+60)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent/sentinel/ctor.other.pass.cpp (+116)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent/sentinel/eq.pass.cpp (+195)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent/sentinel/minus.pass.cpp (+185)
  • (added) libcxx/test/std/ranges/range.adaptors/range.adjacent/size.pass.cpp (+122)
  • (modified) libcxx/test/std/ranges/range.adaptors/range.join/adaptor.pass.cpp (+13-13)
  • (modified) libcxx/test/std/ranges/range.adaptors/range.zip.transform/sentinel/minus.pass.cpp (+1-77)
  • (modified) libcxx/test/std/ranges/range.adaptors/range.zip/sentinel/minus.pass.cpp (+8-85)
  • (modified) libcxx/test/std/ranges/range.adaptors/range_adaptor_types.h (+99-10)
  • (modified) libcxx/test/std/ranges/ranges_robust_against_no_unique_address.pass.cpp (+2)
diff --git a/libcxx/docs/ReleaseNotes/22.rst b/libcxx/docs/ReleaseNotes/22.rst
index ec23ba9d1e3a1..326da58897dcd 100644
--- a/libcxx/docs/ReleaseNotes/22.rst
+++ b/libcxx/docs/ReleaseNotes/22.rst
@@ -38,8 +38,7 @@ What's New in Libc++ 22.0.0?
 Implemented Papers
 ------------------
 
-- P2321R2: ``zip`` (`Github <https://llvm.org/PR105169>`__) (The paper is partially implemented. ``zip_transform_view``
-  is implemented in this release)
+- P2321R2: ``zip`` (`Github <https://llvm.org/PR105169>`__) (The paper is partially implemented. ``zip_transform_view``, ``adjacent_view``, and ``adjacent_transform_view`` are implemented in this release)
 - P3044R2: sub-``string_view`` from ``string`` (`Github <https://llvm.org/PR148140>`__)
 - P3223R2: Making ``std::istream::ignore`` less surprising (`Github <https://llvm.org/PR148178>`__)
 - P3060R3: Add ``std::views::indices(n)`` (`Github <https://llvm.org/PR148175>`__)
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index ddace8bf8c728..cac0b1285e0bf 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -696,6 +696,8 @@ set(files
   __random/uniform_real_distribution.h
   __random/weibull_distribution.h
   __ranges/access.h
+  __ranges/adjacent_transform_view.h
+  __ranges/adjacent_view.h
   __ranges/all.h
   __ranges/as_rvalue_view.h
   __ranges/chunk_by_view.h
@@ -739,6 +741,7 @@ set(files
   __ranges/view_interface.h
   __ranges/views.h
   __ranges/zip_transform_view.h
+  __ranges/zip_utils.h
   __ranges/zip_view.h
   __split_buffer
   __std_mbstate_t.h
diff --git a/libcxx/include/__ranges/adjacent_transform_view.h b/libcxx/include/__ranges/adjacent_transform_view.h
new file mode 100644
index 0000000000000..fe8defb5cb56f
--- /dev/null
+++ b/libcxx/include/__ranges/adjacent_transform_view.h
@@ -0,0 +1,411 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___RANGES_ADJACENT_TRANSFORM_VIEW_H
+#define _LIBCPP___RANGES_ADJACENT_TRANSFORM_VIEW_H
+
+#include <__config>
+
+#include <__algorithm/min.h>
+#include <__compare/three_way_comparable.h>
+#include <__concepts/constructible.h>
+#include <__concepts/convertible_to.h>
+#include <__concepts/equality_comparable.h>
+#include <__cstddef/size_t.h>
+#include <__functional/invoke.h>
+#include <__functional/operations.h>
+#include <__iterator/concepts.h>
+#include <__iterator/incrementable_traits.h>
+#include <__iterator/iter_move.h>
+#include <__iterator/iter_swap.h>
+#include <__iterator/iterator_traits.h>
+#include <__iterator/next.h>
+#include <__iterator/prev.h>
+#include <__memory/addressof.h>
+#include <__ranges/access.h>
+#include <__ranges/adjacent_view.h>
+#include <__ranges/all.h>
+#include <__ranges/concepts.h>
+#include <__ranges/empty_view.h>
+#include <__ranges/movable_box.h>
+#include <__ranges/range_adaptor.h>
+#include <__ranges/size.h>
+#include <__ranges/view_interface.h>
+#include <__ranges/zip_transform_view.h>
+#include <__ranges/zip_utils.h>
+#include <__type_traits/common_type.h>
+#include <__type_traits/is_nothrow_constructible.h>
+#include <__type_traits/is_referenceable.h>
+#include <__type_traits/make_unsigned.h>
+#include <__type_traits/maybe_const.h>
+#include <__utility/declval.h>
+#include <__utility/forward.h>
+#include <__utility/in_place.h>
+#include <__utility/integer_sequence.h>
+#include <__utility/move.h>
+#include <concepts>
+#include <ranges>
+#include <tuple>
+#include <type_traits>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER >= 23
+
+namespace ranges {
+
+template <class _Fn, size_t _Np>
+struct __apply_n {
+  template <class... _Ts>
+  static auto __apply(tuple<_Ts...>&&) -> invoke_result_t<_Fn, _Ts...>;
+
+  template <class _Tp, size_t... _Is>
+  static auto __make_tuple_impl(index_sequence<_Is...>) -> tuple<decltype((_Is, std::declval<_Tp (*)()>()()))...>;
+
+  template <class _Tp>
+  static constexpr auto operator()(_Tp&&) -> decltype(__apply(__make_tuple_impl<_Tp&&>(make_index_sequence<_Np>{})));
+};
+
+template <forward_range _View, move_constructible _Fn, size_t _Np>
+  requires view<_View> && (_Np > 0) && is_object_v<_Fn> &&
+           regular_invocable<__apply_n<_Fn&, _Np>, range_reference_t<_View>> &&
+           __referenceable<invoke_result_t<__apply_n<_Fn&, _Np>, range_reference_t<_View>>>
+class adjacent_transform_view : public view_interface<adjacent_transform_view<_View, _Fn, _Np>> {
+private:
+  _LIBCPP_NO_UNIQUE_ADDRESS adjacent_view<_View, _Np> __inner_;
+  _LIBCPP_NO_UNIQUE_ADDRESS __movable_box<_Fn> __fun_;
+
+  using _InnerView _LIBCPP_NODEBUG = adjacent_view<_View, _Np>;
+
+  template <bool _Const>
+  using __inner_iterator _LIBCPP_NODEBUG = iterator_t<__maybe_const<_Const, _InnerView>>;
+
+  template <bool _Const>
+  using __inner_sentinel _LIBCPP_NODEBUG = sentinel_t<__maybe_const<_Const, _InnerView>>;
+
+  template <bool>
+  class __iterator;
+
+  template <bool>
+  class __sentinel;
+
+public:
+  _LIBCPP_HIDE_FROM_ABI adjacent_transform_view() = default;
+
+  _LIBCPP_HIDE_FROM_ABI constexpr explicit adjacent_transform_view(_View __base, _Fn __fun)
+      : __inner_(std::move(__base)), __fun_(std::in_place, std::move(__fun)) {}
+
+  _LIBCPP_HIDE_FROM_ABI constexpr _View base() const&
+    requires copy_constructible<_View>
+  {
+    return __inner_.base();
+  }
+  _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__inner_).base(); }
+
+  _LIBCPP_HIDE_FROM_ABI constexpr auto begin() { return __iterator<false>(*this, __inner_.begin()); }
+
+  _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const
+    requires range<const _InnerView> && regular_invocable<__apply_n<const _Fn&, _Np>, range_reference_t<const _View>>
+  {
+    return __iterator<true>(*this, __inner_.begin());
+  }
+
+  _LIBCPP_HIDE_FROM_ABI constexpr auto end() {
+    if constexpr (common_range<_InnerView>) {
+      return __iterator<false>(*this, __inner_.end());
+    } else {
+      return __sentinel<false>(__inner_.end());
+    }
+  }
+
+  _LIBCPP_HIDE_FROM_ABI constexpr auto end() const
+    requires range<const _InnerView> && regular_invocable<__apply_n<const _Fn&, _Np>, range_reference_t<const _View>>
+  {
+    if constexpr (common_range<const _InnerView>) {
+      return __iterator<true>(*this, __inner_.end());
+    } else {
+      return __sentinel<true>(__inner_.end());
+    }
+  }
+
+  _LIBCPP_HIDE_FROM_ABI constexpr auto size()
+    requires sized_range<_InnerView>
+  {
+    return __inner_.size();
+  }
+
+  _LIBCPP_HIDE_FROM_ABI constexpr auto size() const
+    requires sized_range<const _InnerView>
+  {
+    return __inner_.size();
+  }
+};
+
+template <forward_range _View, move_constructible _Fn, size_t _Np>
+  requires view<_View> && (_Np > 0) && is_object_v<_Fn> &&
+           regular_invocable<__apply_n<_Fn&, _Np>, range_reference_t<_View>> &&
+           __referenceable<invoke_result_t<__apply_n<_Fn&, _Np>, range_reference_t<_View>>>
+template <bool _Const>
+class adjacent_transform_view<_View, _Fn, _Np>::__iterator {
+  friend adjacent_transform_view;
+
+  using _Parent _LIBCPP_NODEBUG = __maybe_const<_Const, adjacent_transform_view>;
+  using _Base _LIBCPP_NODEBUG   = __maybe_const<_Const, _View>;
+
+  _Parent* __parent_ = nullptr;
+  __inner_iterator<_Const> __inner_;
+
+  _LIBCPP_HIDE_FROM_ABI constexpr __iterator(_Parent& __parent, __inner_iterator<_Const> __inner)
+      : __parent_(std::addressof(__parent)), __inner_(std::move(__inner)) {}
+
+  static consteval auto __get_iterator_category() {
+    using _Cat = iterator_traits<iterator_t<_Base>>::iterator_category;
+    if constexpr (!is_reference_v<
+                      invoke_result_t<__apply_n<__maybe_const<_Const, _Fn>&, _Np>, range_reference_t<_Base>>>)
+      return input_iterator_tag{};
+    else if constexpr (derived_from<_Cat, random_access_iterator_tag>)
+      return random_access_iterator_tag{};
+    else if constexpr (derived_from<_Cat, bidirectional_iterator_tag>)
+      return bidirectional_iterator_tag{};
+    else if constexpr (derived_from<_Cat, forward_iterator_tag>)
+      return forward_iterator_tag{};
+    else
+      return input_iterator_tag{};
+  }
+
+  template <size_t... _Is>
+  static consteval bool __noexcept_dereference(index_sequence<_Is...>) {
+    return noexcept(std::invoke(
+        std::declval<__maybe_const<_Const, _Fn>&>(),
+        *std::get<_Is>(
+            __adjacent_view_iter_access::__get_current(std::declval<const __inner_iterator<_Const>&>()))...));
+  }
+
+public:
+  using iterator_category = decltype(__get_iterator_category());
+  using iterator_concept  = typename __inner_iterator<_Const>::iterator_concept;
+  using value_type =
+      remove_cvref_t<invoke_result_t<__apply_n<__maybe_const<_Const, _Fn>&, _Np>, range_reference_t<_Base>>>;
+  using difference_type = range_difference_t<_Base>;
+
+  _LIBCPP_HIDE_FROM_ABI __iterator() = default;
+
+  _LIBCPP_HIDE_FROM_ABI constexpr __iterator(__iterator<!_Const> __i)
+    requires _Const && convertible_to<__inner_iterator<false>, __inner_iterator<true>>
+      : __parent_(__i.__parent_), __inner_(std::move(__i.__inner_)) {}
+
+  _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator*() const
+      noexcept(__noexcept_dereference(make_index_sequence<_Np>{})) {
+    return std::apply(
+        [&](const auto&... __iters) -> decltype(auto) { return std::invoke(*__parent_->__fun_, *__iters...); },
+        __adjacent_view_iter_access::__get_current(__inner_));
+  }
+
+  _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator++() {
+    ++__inner_;
+    return *this;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI constexpr __iterator operator++(int) {
+    auto __tmp = *this;
+    ++*this;
+    return __tmp;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator--()
+    requires bidirectional_range<_Base>
+  {
+    --__inner_;
+    return *this;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI constexpr __iterator operator--(int)
+    requires bidirectional_range<_Base>
+  {
+    auto __tmp = *this;
+    --*this;
+    return __tmp;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator+=(difference_type __x)
+    requires random_access_range<_Base>
+  {
+    __inner_ += __x;
+    return *this;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator-=(difference_type __x)
+    requires random_access_range<_Base>
+  {
+    __inner_ -= __x;
+    return *this;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator[](difference_type __n) const
+    requires random_access_range<_Base>
+  {
+    return std::apply(
+        [&](const auto&... __iters) -> decltype(auto) { return std::invoke(*__parent_->__fun_, __iters[__n]...); },
+        __adjacent_view_iter_access::__get_current(__inner_));
+  }
+
+  _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator& __x, const __iterator& __y) {
+    return __x.__inner_ == __y.__inner_;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<(const __iterator& __x, const __iterator& __y)
+    requires random_access_range<_Base>
+  {
+    return __x.__inner_ < __y.__inner_;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>(const __iterator& __x, const __iterator& __y)
+    requires random_access_range<_Base>
+  {
+    return __x.__inner_ > __y.__inner_;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<=(const __iterator& __x, const __iterator& __y)
+    requires random_access_range<_Base>
+  {
+    return __x.__inner_ <= __y.__inner_;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>=(const __iterator& __x, const __iterator& __y)
+    requires random_access_range<_Base>
+  {
+    return __x.__inner_ >= __y.__inner_;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI friend constexpr auto operator<=>(const __iterator& __x, const __iterator& __y)
+    requires random_access_range<_Base> && three_way_comparable<__inner_iterator<_Const>>
+  {
+    return __x.__inner_ <=> __y.__inner_;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator+(const __iterator& __i, difference_type __n)
+    requires random_access_range<_Base>
+  {
+    return __iterator(*__i.__parent_, __i.__inner_ + __n);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator+(difference_type __n, const __iterator& __i)
+    requires random_access_range<_Base>
+  {
+    return __iterator(*__i.__parent_, __i.__inner_ + __n);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator-(const __iterator& __i, difference_type __n)
+    requires random_access_range<_Base>
+  {
+    return __iterator(*__i.__parent_, __i.__inner_ - __n);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI friend constexpr difference_type operator-(const __iterator& __x, const __iterator& __y)
+    requires sized_sentinel_for<__inner_iterator<_Const>, __inner_iterator<_Const>>
+  {
+    return __x.__inner_ - __y.__inner_;
+  }
+};
+
+template <forward_range _View, move_constructible _Fn, size_t _Np>
+  requires view<_View> && (_Np > 0) && is_object_v<_Fn> &&
+           regular_invocable<__apply_n<_Fn&, _Np>, range_reference_t<_View>> &&
+           __referenceable<invoke_result_t<__apply_n<_Fn&, _Np>, range_reference_t<_View>>>
+template <bool _Const>
+class adjacent_transform_view<_View, _Fn, _Np>::__sentinel {
+  friend adjacent_transform_view;
+
+  __inner_sentinel<_Const> __inner_;
+
+  _LIBCPP_HIDE_FROM_ABI constexpr explicit __sentinel(__inner_sentinel<_Const> __inner)
+      : __inner_(std::move(__inner)) {}
+
+public:
+  _LIBCPP_HIDE_FROM_ABI __sentinel() = default;
+
+  _LIBCPP_HIDE_FROM_ABI constexpr __sentinel(__sentinel<!_Const> __i)
+    requires _Const && convertible_to<__inner_sentinel<false>, __inner_sentinel<_Const>>
+      : __inner_(std::move(__i.__inner_)) {}
+
+  template <bool _OtherConst>
+    requires sentinel_for<__inner_sentinel<_Const>, __inner_iterator<_OtherConst>>
+  _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator<_OtherConst>& __x, const __sentinel& __y) {
+    return __x.__inner_ == __y.__inner_;
+  }
+
+  template <bool _OtherConst>
+    requires sized_sentinel_for<__inner_sentinel<_Const>, __inner_iterator<_OtherConst>>
+  _LIBCPP_HIDE_FROM_ABI friend constexpr range_difference_t<__maybe_const<_OtherConst, _InnerView>>
+  operator-(const __iterator<_OtherConst>& __x, const __sentinel& __y) {
+    return __x.__inner_ - __y.__inner_;
+  }
+
+  template <bool _OtherConst>
+    requires sized_sentinel_for<__inner_sentinel<_Const>, __inner_iterator<_OtherConst>>
+  _LIBCPP_HIDE_FROM_ABI friend constexpr range_difference_t<__maybe_const<_OtherConst, _InnerView>>
+  operator-(const __sentinel& __x, const __iterator<_OtherConst>& __y) {
+    return __x.__inner_ - __y.__inner_;
+  }
+};
+
+namespace views {
+namespace __adjacent_transform {
+
+template <size_t _Np>
+struct __fn : __range_adaptor_closure<__fn<_Np>> {
+  template <class _Range, class _Fn>
+    requires(_Np == 0 && forward_range<_Range &&>)
+  _LIBCPP_HIDE_FROM_ABI static constexpr auto
+  operator()(_Range&&, _Fn&& __fn) noexcept(noexcept(views::zip_transform(std::forward<_Fn>(__fn))))
+      -> decltype(views::zip_transform(std::forward<_Fn>(__fn))) {
+    return views::zip_transform(std::forward<_Fn>(__fn));
+  }
+
+  template <class _Range, class _Fn>
+  _LIBCPP_HIDE_FROM_ABI static constexpr auto operator()(_Range&& __range, _Fn&& __fn) noexcept(
+      noexcept(adjacent_transform_view<views::all_t<_Range&&>, decay_t<_Fn>, _Np>(
+          std::forward<_Range>(__range), std::forward<_Fn>(__fn))))
+      -> decltype(adjacent_transform_view<views::all_t<_Range&&>, decay_t<_Fn>, _Np>(
+          std::forward<_Range>(__range), std::forward<_Fn>(__fn))) {
+    return adjacent_transform_view<views::all_t<_Range&&>, decay_t<_Fn>, _Np>(
+        std::forward<_Range>(__range), std::forward<_Fn>(__fn));
+  }
+
+  template <class _Fn>
+    requires constructible_from<decay_t<_Fn>, _Fn>
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Fn&& __f) const
+      noexcept(is_nothrow_constructible_v<decay_t<_Fn>, _Fn>) {
+    return __pipeable(std::__bind_back(*this, std::forward<_Fn>(__f)));
+  }
+};
+
+} // namespace __adjacent_transform
+inline namespace __cpo {
+template <size_t _Np>
+inline constexpr auto adjacent_transform = __adjacent_transform::__fn<_Np>{};
+inline constexpr auto pairwise_transform = adjacent_transform<2>;
+} // namespace __cpo
+} // namespace views
+} // namespace ranges
+
+#endif // _LIBCPP_STD_VER >= 23
+
+_LIBCPP_END_NAMESPACE_STD
+
+_LIBCPP_POP_MACROS
+
+#endif // _LIBCPP___RANGES_ADJACENT_TRANSFORM_VIEW_H
diff --git a/libcxx/include/__ranges/adjacent_view.h b/libcxx/include/__ranges/adjacent_view.h
new file mode 100644
index 0000000000000..9eefe4020925e
--- /dev/null
+++ b/libcxx/include/__ranges/adjacent_view.h
@@ -0,0 +1,416 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___RANGES_ADJACENT_VIEW_H
+#define _LIBCPP___RANGES_ADJACENT_VIEW_H
+
+#include <__config>
+
+#include <__algorithm/min.h>
+#include <__compare/three_way_comparable.h>
+#include <__concepts/constructible.h>
+#include <__concepts/convertible_to.h>
+#include <__concepts/equality_comparable.h>
+#include <__cstddef/size_t.h>
+#include <__functional/invoke.h>
+#include <__functional/operations.h>
+#include <__iterator/concepts.h>
+#include <__iterator/incrementable_traits.h>
+#include <__iterator/iter_move.h>
+#include <__iterator/iter_swap.h>
+#include <__iterator/iterator_traits.h>
+#include <__iterator/next.h>
+#include <__iterator/prev.h>
+#include <__ranges/access.h>
+#include <__ranges/all.h>
+#include <__ranges/concepts.h>
+#include <__ranges/empty_view.h>
+#include <__ranges/enable_borrowed_range.h>
+#include <__ranges/range_adaptor.h>
+#include <__ranges/size.h>
+#include <__ranges/view_interface.h>
+#include <__ranges/zip_utils.h>
+#include <__type_traits/common_type.h>
+#include <__type_traits/is_nothrow_constructible.h>
+#include <__type_traits/make_unsigned.h>
+#include <__type_traits/maybe_const.h>
+#include <__utility/declval.h>
+#include <__utility/forward.h>
+#include <__utility/integer_sequence.h>
+#include <__utility/move.h>
+#include <array>
+#include <tuple>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER >= 23
+
+namespace ranges {
+
+template <forward_range _View, size_t _Np>
+  requires view<_View> && (_Np > 0)
+class adjacent_view : public view_interface<adjacent_view<_View, _Np>> {
+private:
+  _LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View();
+
+  template <bool>
+  class __iterator;
+
+  template <bool>
+  class __sentinel;
+
+  struct __as_sentinel {};
+
+public:
+  _LIBCPP_HIDE_FROM_ABI adjacent_view()
+    requires default_initializable<_View>
+  = default;
+
+  _LIBCPP_HIDE_FROM_ABI constexpr explicit adjacent_view(_View __base) : __base_(std::move(__base)) {}
+
+  _LIBCPP_HIDE_FROM_ABI constexpr _View base() const&
+    requires copy_constructible<_View>
+  {
+    return __base_;
+  }
+  _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }
+
+  _LIBCPP_HIDE_FROM_ABI constexpr auto begin()
+    requires(!__simple_view<_View>)
+  {
+    return __iterator<false>(ranges::begin(__base_), ranges::end(__base_));
+  }
+
+  _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const
+    requires range<const _View> // todo: this seems under-constrained. lwg issue?
+  {
+    return __iterator<true>(ranges::begin(__base_), ranges::end(__base_));
+  }
+
+  _LIBCPP_HIDE_FROM_ABI constexpr auto end()
+    requires(!__simple_view<_View>)
+  {
+    if constexpr (common_range<_View>) {
+      return __iterator<false>(__as_sentinel{}, ranges::begin(__base_), ...
[truncated]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants