[libcxx-commits] [libcxx] [libc++] Implement P2442R1 `std::views::slide` (PR #172948)

via libcxx-commits libcxx-commits at lists.llvm.org
Thu Dec 25 02:00:40 PST 2025


================
@@ -0,0 +1,396 @@
+// -*- 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_SLIDE_VIEW_H
+#define _LIBCPP___RANGES_SLIDE_VIEW_H
+
+#include <__algorithm/ranges_min.h>
+#include <__assert>
+#include <__concepts/constructible.h>
+#include <__concepts/convertible_to.h>
+#include <__config>
+#include <__functional/bind_back.h>
+#include <__iterator/advance.h>
+#include <__iterator/concepts.h>
+#include <__iterator/default_sentinel.h>
+#include <__iterator/distance.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/all.h>
+#include <__ranges/concepts.h>
+#include <__ranges/counted.h>
+#include <__ranges/enable_borrowed_range.h>
+#include <__ranges/non_propagating_cache.h>
+#include <__ranges/range_adaptor.h>
+#include <__ranges/take_view.h>
+#include <__ranges/view_interface.h>
+#include <__type_traits/conditional.h>
+#include <__type_traits/decay.h>
+#include <__type_traits/is_nothrow_constructible.h>
+#include <__type_traits/make_unsigned.h>
+#include <__utility/forward.h>
+#include <__utility/move.h>
+
+#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 _View>
+concept __slide_caches_nothing = random_access_range<_View> && sized_range<_View>;
+
+template <class _View>
+concept __slide_caches_last = !__slide_caches_nothing<_View> && bidirectional_range<_View> && common_range<_View>;
+
+template <class _View>
+concept __slide_caches_first = !__slide_caches_nothing<_View> && !__slide_caches_last<_View>;
+
+template <forward_range _View>
+  requires view<_View>
+class slide_view : public view_interface<slide_view<_View>> {
+public:
+  _LIBCPP_NO_UNIQUE_ADDRESS _View __base_;
+  _LIBCPP_NO_UNIQUE_ADDRESS range_difference_t<_View> __n_;
+  _LIBCPP_NO_UNIQUE_ADDRESS _If<__slide_caches_first<_View>, __non_propagating_cache<iterator_t<_View>>, __empty_cache>
+      __cached_begin_;
+  _LIBCPP_NO_UNIQUE_ADDRESS _If<__slide_caches_last<_View>, __non_propagating_cache<iterator_t<_View>>, __empty_cache>
+      __cached_end_;
+
+  template <bool _Const>
+  class __iterator;
+  class __sentinel;
+
+public:
+  _LIBCPP_HIDE_FROM_ABI constexpr explicit slide_view(_View __base, range_difference_t<_View> __n)
+      : __base_(std::move(__base)), __n_(__n) {}
+
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _View base() const&
+    requires copy_constructible<_View>
+  {
+    return __base_;
+  }
+
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }
+
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto begin()
+    requires(!(__simple_view<_View> && __slide_caches_nothing<const _View>))
+  {
+    if constexpr (__slide_caches_first<_View>) {
+      __cached_begin_ = __iterator<false>(
+          ranges::begin(__base_), ranges::next(ranges::begin(__base_), __n_ - 1, ranges::end(__base_)), __n_);
+      return __cached_begin_;
+    } else
+      return __iterator<false>(ranges::begin(__base_), __n_);
+  }
+
+  [[nodiscard]] _LIBCPP_HIDDEN constexpr auto begin() const
----------------
anonymouspc wrote:

Thanks for pointing out this typo.

https://github.com/llvm/llvm-project/pull/172948


More information about the libcxx-commits mailing list