[libcxx-commits] [libcxx] [libc++][ranges] Implement `ranges::stride_view`. (PR #65200)
Konstantin Varlamov via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Nov 21 20:44:20 PST 2023
================
@@ -0,0 +1,133 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// ranges
+
+// std::views::stride_view
+
+#include "__ranges/stride_view.h"
+#include "test.h"
+#include "test_iterators.h"
+#include <concepts>
+#include <iterator>
+#include <ranges>
+#include <utility>
+
+template <class View, class T>
+concept CanBePiped = requires(View&& view, T&& t) {
+ { std::forward<View>(view) | std::forward<T>(t) };
+};
+
+constexpr bool test() {
+ constexpr int array_n = 3;
+ int arr[array_n] = {1, 2, 3};
+
+ // Test that `std::views::stride` is a range adaptor.
+ { // Check various forms of
+ // view | stride
+ {
+ {
+ using View = InputView<bidirectional_iterator<int*>>;
+ View view(bidirectional_iterator<int*>(arr), bidirectional_iterator<int*>(arr + array_n));
+ std::same_as<std::ranges::stride_view<View>> decltype(auto) strided = view | std::views::stride(1);
+ auto strided_iter = strided.begin();
+
+ // Check that the begin() iter views arr[0]
+ assert(*strided_iter == arr[0]);
+
+ // Check that the strided_iter, after advancing it 2 * 1 steps, views arr[2].
+ std::ranges::advance(strided_iter, 2);
+ assert(*strided_iter == arr[2]);
+ }
+ {
+ using View = InputView<bidirectional_iterator<int*>>;
+ View view(bidirectional_iterator<int*>(arr), bidirectional_iterator<int*>(arr + array_n));
+ std::same_as<std::ranges::stride_view<View>> decltype(auto) strided = view | std::views::stride(2);
+ auto strided_iter = strided.begin();
+
+ assert(*strided_iter == arr[0]);
+
+ // Same test as above, just advance one time with a bigger step (1 * 2 steps).
+ std::ranges::advance(strided_iter, 1);
+ assert(*strided_iter == arr[2]);
+ }
+ }
+ }
+
+ // Check various forms of
+ // adaptor | stride
+ {
+ // Parallels the two tests from above.
+ constexpr const auto identity_lambda = [](const int i) { return i * 2; };
----------------
var-const wrote:
Some nits:
1. I don't think this variable needs to be `constexpr`.
2. I'd omit the `_lambda` part from the name, the fact that it's a lambda is really an implementation detail and not very important to the code that uses this variable.
3. We generally avoid "shallow" const, so (optionally) I'd make `i` just an `int` variable (especially in a function so short, `const` doesn't really provide any benefit).
4. `identity` seems to imply this function returns its argument unchanged, similar to `std::identity`. I often name this kind of functor `x2` (`x` imitating the multiplication sign); if you prefer a less funky name, another alternative is to presume that the functor's exact implementation is unimportant and just name it `func` or similar.
https://github.com/llvm/llvm-project/pull/65200
More information about the libcxx-commits
mailing list