[libcxx-commits] [libcxx] WIP: [libc++][ranges] Implement `ranges::slide_view` (PR #67146)
Xiaoyang Liu via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Mar 19 23:13:13 PDT 2024
================
@@ -0,0 +1,315 @@
+// -*- 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 <__assert>
+#include <__concepts/constructible.h>
+#include <__concepts/convertible_to.h>
+#include <__config>
+#include <__functional/bind_back.h>
+#include <__iterator/concepts.h>
+#include <__iterator/default_sentinel.h>
+#include <__iterator/distance.h>
+#include <__iterator/iterator_traits.h>
+#include <__iterator/next.h>
+#include <__iterator/prev.h>
+#include <__ranges/all.h>
+#include <__ranges/concepts.h>
+#include <__ranges/counted.h>
+#include <__ranges/empty_view.h>
+#include <__ranges/non_propagating_cache.h>
+#include <__ranges/range_adaptor.h>
+#include <__ranges/view_interface.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 <typename _V>
+concept __slide_caches_nothing = random_access_range<_V> && sized_range<_V>;
+template <typename _V>
+concept __slide_caches_last = !__slide_caches_nothing<_V> && bidirectional_range<_V> && common_range<_V>;
+template <typename _V>
+concept __slide_caches_first = !__slide_caches_nothing<_V> && !__slide_caches_last<_V>;
+
+template <forward_range _View>
+ requires view<_View>
+class slide_view : public view_interface<slide_view<_View>> {
+public:
+ template <bool>
+ class __iterator;
+ class __sentinel;
+
+private:
+ _LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View();
+ range_difference_t<_View> __n_ = 0;
+ using _Cache = _If<!(__slide_caches_nothing<_View>), __non_propagating_cache<iterator_t<_View>>, __empty_cache>;
+ _Cache __cached_begin_;
+ _Cache __cached_end_;
+
+public:
+ _LIBCPP_HIDE_FROM_ABI constexpr explicit slide_view(_View __base, range_difference_t<_View> __n)
+ : __base_(std::move(__base)), __n_(__n) {}
+
+ _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> && __slide_caches_nothing<const _View>) && __slide_caches_last<_View>)
+ {
+ auto __first = ranges::begin(__base_);
+ if (!__cached_begin_.__has_value()) {
+ __cached_begin_.__emplace(ranges::next(__first, __n_ - 1, ranges::end(__base_)));
+ }
+ return __iterator<false>(__first, __cached_begin_, __n_);
+ }
+
+ _LIBCPP_HIDE_FROM_ABI constexpr auto begin()
+ requires(!(__simple_view<_View> && __slide_caches_nothing<const _View>))
+ {
+ return __iterator<false>(ranges::begin(__base_), __n_);
+ }
----------------
xiaoyang-sde wrote:
These methods can be merged with `if constexpr`.
https://github.com/llvm/llvm-project/pull/67146
More information about the libcxx-commits
mailing list