[libcxx-commits] [libcxx] [libc++] Implement adjacent_view (PR #165089)
via libcxx-commits
libcxx-commits at lists.llvm.org
Sat Dec 6 10:43:27 PST 2025
================
@@ -0,0 +1,408 @@
+// -*- 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_), ranges::end(__base_));
+ } else {
+ return __sentinel<false>(ranges::end(__base_));
+ }
+ }
+
+ _LIBCPP_HIDE_FROM_ABI constexpr auto end() const
+ requires range<const _View>
+ {
+ if constexpr (common_range<const _View>) {
+ return __iterator<true>(__as_sentinel{}, ranges::begin(__base_), ranges::end(__base_));
+ } else {
+ return __sentinel<true>(ranges::end(__base_));
+ }
+ }
+
+ _LIBCPP_HIDE_FROM_ABI constexpr auto size()
+ requires sized_range<_View>
+ {
+ using _ST = decltype(ranges::size(__base_));
+ using _CT = common_type_t<_ST, size_t>;
+ auto __sz = static_cast<_CT>(ranges::size(__base_));
+ __sz -= std::min<_CT>(__sz, _Np - 1);
+ return static_cast<_ST>(__sz);
+ }
+
+ _LIBCPP_HIDE_FROM_ABI constexpr auto size() const
+ requires sized_range<const _View>
+ {
+ using _ST = decltype(ranges::size(__base_));
+ using _CT = common_type_t<_ST, size_t>;
+ auto __sz = static_cast<_CT>(ranges::size(__base_));
+ __sz -= std::min<_CT>(__sz, _Np - 1);
+ return static_cast<_ST>(__sz);
+ }
+};
+
+template <forward_range _View, size_t _Np>
+ requires view<_View> && (_Np > 0)
+template <bool _Const>
+class adjacent_view<_View, _Np>::__iterator {
+ friend adjacent_view;
+ using _Base _LIBCPP_NODEBUG = __maybe_const<_Const, _View>;
+ array<iterator_t<_Base>, _Np> __current_ = array<iterator_t<_Base>, _Np>();
+
+ _LIBCPP_HIDE_FROM_ABI constexpr __iterator(iterator_t<_Base> __first, sentinel_t<_Base> __last) {
+ __current_[0] = __first;
+ for (int __i = 1; __i < static_cast<int>(_Np); ++__i) {
+ __current_[__i] = ranges::next(__current_[__i - 1], 1, __last);
----------------
huixie90 wrote:
small correction to your suggestin. `size_t __i = 0;` (it should be initialised to 0 instead of 1)
Here is the godbolt: they do generate different code
https://godbolt.org/z/q8M9hz31P
here is the benchmark results:
```
Comparing ../../../build_test/hui.json to ../../../build_test/ldionne.json
Benchmark Time CPU Time Old Time New CPU Old CPU New
----------------------------------------------------------------------------------------------------------------------
BM_adjacent_full<2> +0.1249 +0.0828 0 0 0 0
BM_adjacent_full<3> -0.1366 -0.1309 0 0 0 0
BM_adjacent_full<4> -0.4258 -0.4177 0 0 0 0
BM_adjacent_full<5> +0.6505 +0.6697 0 1 0 1
BM_adjacent_full<6> -0.4113 -0.4138 1 0 1 0
BM_adjacent_full<7> +0.6383 +0.6381 1 1 1 1
BM_adjacent_full<8> -0.3788 -0.3788 1 0 1 0
BM_adjacent_full<9> +0.9281 +0.9281 1 1 1 1
BM_adjacent_full<10> -0.2923 -0.2923 1 1 1 1
BM_adjacent_full<100> -0.7548 -0.7531 56 14 56 14
BM_adjacent_full<1000> -0.8338 -0.8338 722 120 721 120
BM_adjacent_empty<2> +0.0183 +0.0139 0 0 0 0
BM_adjacent_empty<3> +0.0035 +0.0034 0 0 0 0
BM_adjacent_empty<4> -0.0025 -0.0025 0 0 0 0
BM_adjacent_empty<5> -0.0157 -0.0157 0 0 0 0
BM_adjacent_empty<6> -0.0050 -0.0050 0 0 0 0
BM_adjacent_empty<7> -0.0171 -0.0139 1 1 1 1
BM_adjacent_empty<8> -0.0004 -0.0005 0 0 0 0
BM_adjacent_empty<9> +0.0046 +0.0009 1 1 1 1
BM_adjacent_empty<10> +0.0093 +0.0093 1 1 1 1
BM_adjacent_empty<100> -0.5744 -0.5744 14 6 14 6
BM_adjacent_empty<1000> -0.1526 -0.1526 142 120 142 120
OVERALL_GEOMEAN -0.1854 -0.1857 0 0 0 0
```
My interpretation is that when N is even, your version is better and when N is odd, my version is better. so conclusion ... (btw, N is likely to be small number. big N means large `tuple` and not practical to use)
https://github.com/llvm/llvm-project/pull/165089
More information about the libcxx-commits
mailing list