[libcxx-commits] [libcxx] [libc++] Optimize ranges::rotate for vector<bool>::iterator (PR #121168)
Peng Liu via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Dec 26 16:11:50 PST 2024
https://github.com/winner245 created https://github.com/llvm/llvm-project/pull/121168
This PR optimizes the performance of `std::ranges::rotate` for `vector<bool>::iterator`, addressing a subtask outlined in issue #64038. The optimization yields a performance improvement of up to **x**.
>From 53938b025c86b2c70e9198f477aa7a1d4810b39e Mon Sep 17 00:00:00 2001
From: Peng Liu <winner245 at hotmail.com>
Date: Mon, 23 Dec 2024 15:34:15 -0500
Subject: [PATCH 1/2] Optimize ranges::move{,_backward} for
vector<bool>::iterator
---
libcxx/include/__algorithm/move.h | 10 ++
libcxx/include/__algorithm/move_backward.h | 10 ++
libcxx/include/__bit_reference | 16 ---
.../test/benchmarks/algorithms/move.bench.cpp | 55 +++++++++
.../algorithms/move_backward.bench.cpp | 55 +++++++++
.../alg.move/move.pass.cpp | 53 ++++++--
.../alg.move/move_backward.pass.cpp | 60 +++++++---
.../alg.move/ranges.move.pass.cpp | 102 +++++++++++-----
.../alg.move/ranges.move_backward.pass.cpp | 113 ++++++++++++------
9 files changed, 364 insertions(+), 110 deletions(-)
create mode 100644 libcxx/test/benchmarks/algorithms/move.bench.cpp
create mode 100644 libcxx/test/benchmarks/algorithms/move_backward.bench.cpp
diff --git a/libcxx/include/__algorithm/move.h b/libcxx/include/__algorithm/move.h
index 005099dcac06e3..dbea92c067ccb4 100644
--- a/libcxx/include/__algorithm/move.h
+++ b/libcxx/include/__algorithm/move.h
@@ -9,11 +9,13 @@
#ifndef _LIBCPP___ALGORITHM_MOVE_H
#define _LIBCPP___ALGORITHM_MOVE_H
+#include <__algorithm/copy.h>
#include <__algorithm/copy_move_common.h>
#include <__algorithm/for_each_segment.h>
#include <__algorithm/iterator_operations.h>
#include <__algorithm/min.h>
#include <__config>
+#include <__fwd/bit_reference.h>
#include <__iterator/iterator_traits.h>
#include <__iterator/segmented_iterator.h>
#include <__type_traits/common_type.h>
@@ -98,6 +100,14 @@ struct __move_impl {
}
}
+ template <class _Cp, bool _IsConst>
+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<__bit_iterator<_Cp, _IsConst>, __bit_iterator<_Cp, false> >
+ operator()(__bit_iterator<_Cp, _IsConst> __first,
+ __bit_iterator<_Cp, _IsConst> __last,
+ __bit_iterator<_Cp, false> __result) {
+ return std::__copy(__first, __last, __result);
+ }
+
// At this point, the iterators have been unwrapped so any `contiguous_iterator` has been unwrapped to a pointer.
template <class _In, class _Out, __enable_if_t<__can_lower_move_assignment_to_memmove<_In, _Out>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_In*, _Out*>
diff --git a/libcxx/include/__algorithm/move_backward.h b/libcxx/include/__algorithm/move_backward.h
index 24a8d9b24527a7..14482fee181147 100644
--- a/libcxx/include/__algorithm/move_backward.h
+++ b/libcxx/include/__algorithm/move_backward.h
@@ -9,10 +9,12 @@
#ifndef _LIBCPP___ALGORITHM_MOVE_BACKWARD_H
#define _LIBCPP___ALGORITHM_MOVE_BACKWARD_H
+#include <__algorithm/copy_backward.h>
#include <__algorithm/copy_move_common.h>
#include <__algorithm/iterator_operations.h>
#include <__algorithm/min.h>
#include <__config>
+#include <__fwd/bit_reference.h>
#include <__iterator/iterator_traits.h>
#include <__iterator/segmented_iterator.h>
#include <__type_traits/common_type.h>
@@ -107,6 +109,14 @@ struct __move_backward_impl {
}
}
+ template <class _Cp, bool _IsConst>
+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<__bit_iterator<_Cp, _IsConst>, __bit_iterator<_Cp, false> >
+ operator()(__bit_iterator<_Cp, _IsConst> __first,
+ __bit_iterator<_Cp, _IsConst> __last,
+ __bit_iterator<_Cp, false> __result) {
+ return std::__copy_backward<_ClassicAlgPolicy>(__first, __last, __result);
+ }
+
// At this point, the iterators have been unwrapped so any `contiguous_iterator` has been unwrapped to a pointer.
template <class _In, class _Out, __enable_if_t<__can_lower_move_assignment_to_memmove<_In, _Out>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_In*, _Out*>
diff --git a/libcxx/include/__bit_reference b/libcxx/include/__bit_reference
index 9fa24c98d493fd..a86513d81f1820 100644
--- a/libcxx/include/__bit_reference
+++ b/libcxx/include/__bit_reference
@@ -421,22 +421,6 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cp, f
return std::__copy_backward_unaligned(__first, __last, __result);
}
-// move
-
-template <class _Cp, bool _IsConst>
-inline _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, false>
-move(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, __bit_iterator<_Cp, false> __result) {
- return std::copy(__first, __last, __result);
-}
-
-// move_backward
-
-template <class _Cp, bool _IsConst>
-inline _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, false> move_backward(
- __bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, __bit_iterator<_Cp, false> __result) {
- return std::copy_backward(__first, __last, __result);
-}
-
// swap_ranges
template <class _Cl, class _Cr>
diff --git a/libcxx/test/benchmarks/algorithms/move.bench.cpp b/libcxx/test/benchmarks/algorithms/move.bench.cpp
new file mode 100644
index 00000000000000..8fe42c5964fb88
--- /dev/null
+++ b/libcxx/test/benchmarks/algorithms/move.bench.cpp
@@ -0,0 +1,55 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+#include <algorithm>
+#include <benchmark/benchmark.h>
+#include <vector>
+
+static void bm_ranges_move(benchmark::State& state, bool aligned) {
+ auto n = state.range();
+ std::vector<bool> in(n, true);
+ std::vector<bool> out(aligned ? n : n + 8);
+ benchmark::DoNotOptimize(&in);
+ auto dst = aligned ? out.begin() : out.begin() + 4;
+ for (auto _ : state) {
+ benchmark::DoNotOptimize(std::ranges::move(in, dst));
+ benchmark::DoNotOptimize(&out);
+ }
+}
+
+static void bm_move(benchmark::State& state, bool aligned) {
+ auto n = state.range();
+ std::vector<bool> in(n, true);
+ std::vector<bool> out(aligned ? n : n + 8);
+ benchmark::DoNotOptimize(&in);
+ auto beg = in.begin();
+ auto end = in.end();
+ auto dst = aligned ? out.begin() : out.begin() + 4;
+ for (auto _ : state) {
+ benchmark::DoNotOptimize(std::move(beg, end, dst));
+ benchmark::DoNotOptimize(&out);
+ }
+}
+
+static void bm_ranges_move_aligned(benchmark::State& state) { bm_ranges_move(state, true); }
+static void bm_ranges_move_unaligned(benchmark::State& state) { bm_ranges_move(state, false); }
+
+static void bm_move_aligned(benchmark::State& state) { bm_move(state, true); }
+static void bm_move_unaligned(benchmark::State& state) { bm_move(state, false); }
+
+// Test std::ranges::move for vector<bool>::iterator
+BENCHMARK(bm_ranges_move_aligned)->Range(8, 1 << 20);
+BENCHMARK(bm_ranges_move_unaligned)->Range(8, 1 << 20);
+
+// Test std::move for vector<bool>::iterator
+BENCHMARK(bm_move_aligned)->Range(8, 1 << 20);
+BENCHMARK(bm_move_unaligned)->Range(8, 1 << 20);
+
+BENCHMARK_MAIN();
diff --git a/libcxx/test/benchmarks/algorithms/move_backward.bench.cpp b/libcxx/test/benchmarks/algorithms/move_backward.bench.cpp
new file mode 100644
index 00000000000000..c1c420f1d5426d
--- /dev/null
+++ b/libcxx/test/benchmarks/algorithms/move_backward.bench.cpp
@@ -0,0 +1,55 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+#include <algorithm>
+#include <benchmark/benchmark.h>
+#include <vector>
+
+static void bm_ranges_move_backward(benchmark::State& state, bool aligned) {
+ auto n = state.range();
+ std::vector<bool> in(n, true);
+ std::vector<bool> out(aligned ? n : n + 8);
+ benchmark::DoNotOptimize(&in);
+ auto dst = aligned ? out.end() : out.end() - 4;
+ for (auto _ : state) {
+ benchmark::DoNotOptimize(std::ranges::move_backward(in, dst));
+ benchmark::DoNotOptimize(&out);
+ }
+}
+
+static void bm_move_backward(benchmark::State& state, bool aligned) {
+ auto n = state.range();
+ std::vector<bool> in(n, true);
+ std::vector<bool> out(aligned ? n : n + 8);
+ benchmark::DoNotOptimize(&in);
+ auto beg = in.begin();
+ auto end = in.end();
+ auto dst = aligned ? out.end() : out.end() - 4;
+ for (auto _ : state) {
+ benchmark::DoNotOptimize(std::move_backward(beg, end, dst));
+ benchmark::DoNotOptimize(&out);
+ }
+}
+
+static void bm_ranges_move_backward_aligned(benchmark::State& state) { bm_ranges_move_backward(state, true); }
+static void bm_ranges_move_backward_unaligned(benchmark::State& state) { bm_ranges_move_backward(state, false); }
+
+static void bm_move_backward_aligned(benchmark::State& state) { bm_move_backward(state, true); }
+static void bm_move_backward_unaligned(benchmark::State& state) { bm_move_backward(state, false); }
+
+// Test std::ranges::move_backward for vector<bool>::iterator
+BENCHMARK(bm_ranges_move_backward_aligned)->Range(8, 1 << 20);
+BENCHMARK(bm_ranges_move_backward_unaligned)->Range(8, 1 << 20);
+
+// Test std::move_backward for vector<bool>::iterator
+BENCHMARK(bm_move_backward_aligned)->Range(8, 1 << 20);
+BENCHMARK(bm_move_backward_unaligned)->Range(8, 1 << 20);
+
+BENCHMARK_MAIN();
diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/move.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/move.pass.cpp
index b1ad6873bc5e5a..824c52c63439e4 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/move.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/move.pass.cpp
@@ -20,6 +20,7 @@
#include <cassert>
#include <iterator>
#include <memory>
+#include <vector>
#include "MoveOnly.h"
#include "test_iterators.h"
@@ -45,15 +46,15 @@ struct Test {
template <class OutIter>
TEST_CONSTEXPR_CXX20 void operator()() {
const unsigned N = 1000;
- int ia[N] = {};
+ int ia[N] = {};
for (unsigned i = 0; i < N; ++i)
- ia[i] = i;
+ ia[i] = i;
int ib[N] = {0};
- OutIter r = std::move(InIter(ia), InIter(ia+N), OutIter(ib));
- assert(base(r) == ib+N);
+ OutIter r = std::move(InIter(ia), InIter(ia + N), OutIter(ib));
+ assert(base(r) == ib + N);
for (unsigned i = 0; i < N; ++i)
- assert(ia[i] == ib[i]);
+ assert(ia[i] == ib[i]);
}
};
@@ -73,13 +74,13 @@ struct Test1 {
const unsigned N = 100;
std::unique_ptr<int> ia[N];
for (unsigned i = 0; i < N; ++i)
- ia[i].reset(new int(i));
+ ia[i].reset(new int(i));
std::unique_ptr<int> ib[N];
- OutIter r = std::move(InIter(ia), InIter(ia+N), OutIter(ib));
- assert(base(r) == ib+N);
+ OutIter r = std::move(InIter(ia), InIter(ia + N), OutIter(ib));
+ assert(base(r) == ib + N);
for (unsigned i = 0; i < N; ++i)
- assert(*ib[i] == static_cast<int>(i));
+ assert(*ib[i] == static_cast<int>(i));
}
};
@@ -92,6 +93,28 @@ struct Test1OutIters {
}
};
+template <std::size_t N>
+struct TestBitIter {
+ std::vector<bool> in;
+ TEST_CONSTEXPR TestBitIter() : in(N, false) {
+ for (std::size_t i = 0; i < N; i += 2)
+ in[i] = true;
+ }
+ TEST_CONSTEXPR_CXX20 void operator()() {
+ { // Test move with aligned bytes
+ std::vector<bool> out(N);
+ std::move(in.begin(), in.end(), out.begin());
+ assert(in == out);
+ }
+ { // Test move with unaligned bytes
+ std::vector<bool> out(N + 8);
+ std::move(in.begin(), in.end(), out.begin() + 4);
+ for (std::size_t i = 0; i < N; ++i)
+ assert(out[i + 4] == in[i]);
+ }
+ }
+};
+
TEST_CONSTEXPR_CXX20 bool test() {
types::for_each(types::cpp17_input_iterator_list<int*>(), TestOutIters());
if (TEST_STD_AT_LEAST_23_OR_RUNTIME_EVALUATED)
@@ -118,7 +141,7 @@ TEST_CONSTEXPR_CXX20 bool test() {
// When non-trivial
{
MoveOnly from[3] = {1, 2, 3};
- MoveOnly to[3] = {};
+ MoveOnly to[3] = {};
std::move(std::begin(from), std::end(from), std::begin(to));
assert(to[0] == MoveOnly(1));
assert(to[1] == MoveOnly(2));
@@ -127,7 +150,7 @@ TEST_CONSTEXPR_CXX20 bool test() {
// When trivial
{
TrivialMoveOnly from[3] = {1, 2, 3};
- TrivialMoveOnly to[3] = {};
+ TrivialMoveOnly to[3] = {};
std::move(std::begin(from), std::end(from), std::begin(to));
assert(to[0] == TrivialMoveOnly(1));
assert(to[1] == TrivialMoveOnly(2));
@@ -135,6 +158,14 @@ TEST_CONSTEXPR_CXX20 bool test() {
}
}
+ { // Test vector<bool>::iterator optimization
+ TestBitIter<8>()();
+ TestBitIter<16>()();
+ TestBitIter<32>()();
+ TestBitIter<64>()();
+ TestBitIter<1024>()();
+ }
+
return true;
}
diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/move_backward.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/move_backward.pass.cpp
index 61dea47b510716..dcfe1cc5eeb282 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/move_backward.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/move_backward.pass.cpp
@@ -19,6 +19,7 @@
#include <cassert>
#include <iterator>
#include <memory>
+#include <vector>
#include "MoveOnly.h"
#include "test_iterators.h"
@@ -44,24 +45,22 @@ struct Test {
template <class OutIter>
TEST_CONSTEXPR_CXX20 void operator()() {
const unsigned N = 1000;
- int ia[N] = {};
+ int ia[N] = {};
for (unsigned i = 0; i < N; ++i)
- ia[i] = i;
+ ia[i] = i;
int ib[N] = {0};
- OutIter r = std::move_backward(InIter(ia), InIter(ia+N), OutIter(ib+N));
+ OutIter r = std::move_backward(InIter(ia), InIter(ia + N), OutIter(ib + N));
assert(base(r) == ib);
for (unsigned i = 0; i < N; ++i)
- assert(ia[i] == ib[i]);
+ assert(ia[i] == ib[i]);
}
};
struct TestOutIters {
template <class InIter>
TEST_CONSTEXPR_CXX20 void operator()() {
- types::for_each(
- types::concatenate_t<types::bidirectional_iterator_list<int*> >(),
- Test<InIter>());
+ types::for_each(types::concatenate_t<types::bidirectional_iterator_list<int*> >(), Test<InIter>());
}
};
@@ -72,21 +71,43 @@ struct Test1 {
const unsigned N = 100;
std::unique_ptr<int> ia[N];
for (unsigned i = 0; i < N; ++i)
- ia[i].reset(new int(i));
+ ia[i].reset(new int(i));
std::unique_ptr<int> ib[N];
- OutIter r = std::move_backward(InIter(ia), InIter(ia+N), OutIter(ib+N));
+ OutIter r = std::move_backward(InIter(ia), InIter(ia + N), OutIter(ib + N));
assert(base(r) == ib);
for (unsigned i = 0; i < N; ++i)
- assert(*ib[i] == static_cast<int>(i));
+ assert(*ib[i] == static_cast<int>(i));
}
};
struct Test1OutIters {
template <class InIter>
TEST_CONSTEXPR_CXX23 void operator()() {
- types::for_each(types::concatenate_t<types::bidirectional_iterator_list<std::unique_ptr<int>*> >(),
- Test1<InIter>());
+ types::for_each(
+ types::concatenate_t<types::bidirectional_iterator_list<std::unique_ptr<int>*> >(), Test1<InIter>());
+ }
+};
+
+template <std::size_t N>
+struct TestBitIter {
+ std::vector<bool> in;
+ TEST_CONSTEXPR TestBitIter() : in(N, false) {
+ for (std::size_t i = 0; i < N; i += 2)
+ in[i] = true;
+ }
+ TEST_CONSTEXPR_CXX20 void operator()() {
+ { // Test move_backward with aligned bytes
+ std::vector<bool> out(N);
+ std::move_backward(in.begin(), in.end(), out.end());
+ assert(in == out);
+ }
+ { // Test move_backward with unaligned bytes
+ std::vector<bool> out(N + 8);
+ std::move_backward(in.begin(), in.end(), out.end() - 4);
+ for (std::size_t i = 0; i < N; ++i)
+ assert(out[i + 4] == in[i]);
+ }
}
};
@@ -117,7 +138,7 @@ TEST_CONSTEXPR_CXX20 bool test() {
// When non-trivial
{
MoveOnly from[3] = {1, 2, 3};
- MoveOnly to[3] = {};
+ MoveOnly to[3] = {};
std::move_backward(std::begin(from), std::end(from), std::end(to));
assert(to[0] == MoveOnly(1));
assert(to[1] == MoveOnly(2));
@@ -126,7 +147,7 @@ TEST_CONSTEXPR_CXX20 bool test() {
// When trivial
{
TrivialMoveOnly from[3] = {1, 2, 3};
- TrivialMoveOnly to[3] = {};
+ TrivialMoveOnly to[3] = {};
std::move_backward(std::begin(from), std::end(from), std::end(to));
assert(to[0] == TrivialMoveOnly(1));
assert(to[1] == TrivialMoveOnly(2));
@@ -134,11 +155,18 @@ TEST_CONSTEXPR_CXX20 bool test() {
}
}
+ { // Test vector<bool>::iterator optimization
+ TestBitIter<8>()();
+ TestBitIter<16>()();
+ TestBitIter<32>()();
+ TestBitIter<64>()();
+ TestBitIter<1024>()();
+ }
+
return true;
}
-int main(int, char**)
-{
+int main(int, char**) {
test();
#if TEST_STD_VER >= 20
static_assert(test());
diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/ranges.move.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/ranges.move.pass.cpp
index a0d1473360a14e..5b631b176a54c6 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/ranges.move.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/ranges.move.pass.cpp
@@ -65,7 +65,7 @@ constexpr void test(std::array<int, N> in) {
{
std::array<int, N> out;
std::same_as<std::ranges::in_out_result<In, Out>> decltype(auto) ret =
- std::ranges::move(In(in.data()), Sent(In(in.data() + in.size())), Out(out.data()));
+ std::ranges::move(In(in.data()), Sent(In(in.data() + in.size())), Out(out.data()));
assert(in == out);
assert(base(ret.in) == in.data() + in.size());
assert(base(ret.out) == out.data() + out.size());
@@ -73,8 +73,7 @@ constexpr void test(std::array<int, N> in) {
{
std::array<int, N> out;
auto range = std::ranges::subrange(In(in.data()), Sent(In(in.data() + in.size())));
- std::same_as<std::ranges::in_out_result<In, Out>> decltype(auto) ret =
- std::ranges::move(range, Out(out.data()));
+ std::same_as<std::ranges::in_out_result<In, Out>> decltype(auto) ret = std::ranges::move(range, Out(out.data()));
assert(in == out);
assert(base(ret.in) == in.data() + in.size());
assert(base(ret.out) == out.data() + out.size());
@@ -84,16 +83,16 @@ constexpr void test(std::array<int, N> in) {
template <class InContainer, class OutContainer, class In, class Out, class Sent = In>
constexpr void test_containers() {
{
- InContainer in {1, 2, 3, 4};
+ InContainer in{1, 2, 3, 4};
OutContainer out(4);
std::same_as<std::ranges::in_out_result<In, Out>> auto ret =
- std::ranges::move(In(in.begin()), Sent(In(in.end())), Out(out.begin()));
+ std::ranges::move(In(in.begin()), Sent(In(in.end())), Out(out.begin()));
assert(std::ranges::equal(in, out));
assert(base(ret.in) == in.end());
assert(base(ret.out) == out.end());
}
{
- InContainer in {1, 2, 3, 4};
+ InContainer in{1, 2, 3, 4};
OutContainer out(4);
auto range = std::ranges::subrange(In(in.begin()), Sent(In(in.end())));
std::same_as<std::ranges::in_out_result<In, Out>> auto ret = std::ranges::move(range, Out(out.begin()));
@@ -165,22 +164,51 @@ constexpr void test_proxy_in_iterators() {
}
struct IteratorWithMoveIter {
- using value_type = int;
- using difference_type = int;
+ using value_type = int;
+ using difference_type = int;
explicit IteratorWithMoveIter() = default;
int* ptr;
constexpr IteratorWithMoveIter(int* ptr_) : ptr(ptr_) {}
constexpr int& operator*() const; // iterator with iter_move should not be dereferenced
- constexpr IteratorWithMoveIter& operator++() { ++ptr; return *this; }
- constexpr IteratorWithMoveIter operator++(int) { auto ret = *this; ++*this; return ret; }
+ constexpr IteratorWithMoveIter& operator++() {
+ ++ptr;
+ return *this;
+ }
+ constexpr IteratorWithMoveIter operator++(int) {
+ auto ret = *this;
+ ++*this;
+ return ret;
+ }
friend constexpr int iter_move(const IteratorWithMoveIter&) { return 42; }
constexpr bool operator==(const IteratorWithMoveIter& other) const = default;
};
+template <std::size_t N>
+struct TestBitIter {
+ std::vector<bool> in;
+ TEST_CONSTEXPR TestBitIter() : in(N, false) {
+ for (std::size_t i = 0; i < N; i += 2)
+ in[i] = true;
+ }
+ TEST_CONSTEXPR_CXX20 void operator()() {
+ { // Test move with aligned bytes
+ std::vector<bool> out(N);
+ std::ranges::move(in, out.begin());
+ assert(in == out);
+ }
+ { // Test move with unaligned bytes
+ std::vector<bool> out(N + 8);
+ std::ranges::move(in, out.begin() + 4);
+ for (std::size_t i = 0; i < N; ++i)
+ assert(out[i + 4] == in[i]);
+ }
+ }
+};
+
// cpp17_intput_iterator has a defaulted template argument
template <class Iter>
using Cpp17InIter = cpp17_input_iterator<Iter>;
@@ -267,13 +295,13 @@ constexpr bool test() {
{ // check that ranges::dangling is returned
std::array<int, 4> out;
std::same_as<std::ranges::in_out_result<std::ranges::dangling, int*>> decltype(auto) ret =
- std::ranges::move(std::array {1, 2, 3, 4}, out.data());
+ std::ranges::move(std::array{1, 2, 3, 4}, out.data());
assert(ret.out == out.data() + 4);
assert((out == std::array{1, 2, 3, 4}));
}
{ // check that an iterator is returned with a borrowing range
- std::array in {1, 2, 3, 4};
+ std::array in{1, 2, 3, 4};
std::array<int, 4> out;
std::same_as<std::ranges::in_out_result<std::array<int, 4>::iterator, int*>> decltype(auto) ret =
std::ranges::move(std::views::all(in), out.data());
@@ -284,8 +312,8 @@ constexpr bool test() {
{ // check that every element is moved exactly once
struct MoveOnce {
- bool moved = false;
- constexpr MoveOnce() = default;
+ bool moved = false;
+ constexpr MoveOnce() = default;
constexpr MoveOnce(const MoveOnce& other) = delete;
constexpr MoveOnce& operator=(MoveOnce&& other) {
assert(!other.moved);
@@ -294,16 +322,16 @@ constexpr bool test() {
}
};
{
- std::array<MoveOnce, 4> in {};
- std::array<MoveOnce, 4> out {};
+ std::array<MoveOnce, 4> in{};
+ std::array<MoveOnce, 4> out{};
auto ret = std::ranges::move(in.begin(), in.end(), out.begin());
assert(ret.in == in.end());
assert(ret.out == out.end());
assert(std::all_of(out.begin(), out.end(), [](const auto& e) { return e.moved; }));
}
{
- std::array<MoveOnce, 4> in {};
- std::array<MoveOnce, 4> out {};
+ std::array<MoveOnce, 4> in{};
+ std::array<MoveOnce, 4> out{};
auto ret = std::ranges::move(in, out.begin());
assert(ret.in == in.end());
assert(ret.out == out.end());
@@ -314,8 +342,8 @@ constexpr bool test() {
{ // check that the range is moved forwards
struct OnlyForwardsMovable {
OnlyForwardsMovable* next = nullptr;
- bool canMove = false;
- OnlyForwardsMovable() = default;
+ bool canMove = false;
+ OnlyForwardsMovable() = default;
constexpr OnlyForwardsMovable& operator=(OnlyForwardsMovable&&) {
assert(canMove);
if (next != nullptr)
@@ -324,12 +352,12 @@ constexpr bool test() {
}
};
{
- std::array<OnlyForwardsMovable, 3> in {};
- std::array<OnlyForwardsMovable, 3> out {};
- out[0].next = &out[1];
- out[1].next = &out[2];
+ std::array<OnlyForwardsMovable, 3> in{};
+ std::array<OnlyForwardsMovable, 3> out{};
+ out[0].next = &out[1];
+ out[1].next = &out[2];
out[0].canMove = true;
- auto ret = std::ranges::move(in.begin(), in.end(), out.begin());
+ auto ret = std::ranges::move(in.begin(), in.end(), out.begin());
assert(ret.in == in.end());
assert(ret.out == out.end());
assert(out[0].canMove);
@@ -337,12 +365,12 @@ constexpr bool test() {
assert(out[2].canMove);
}
{
- std::array<OnlyForwardsMovable, 3> in {};
- std::array<OnlyForwardsMovable, 3> out {};
- out[0].next = &out[1];
- out[1].next = &out[2];
+ std::array<OnlyForwardsMovable, 3> in{};
+ std::array<OnlyForwardsMovable, 3> out{};
+ out[0].next = &out[1];
+ out[1].next = &out[2];
out[0].canMove = true;
- auto ret = std::ranges::move(in, out.begin());
+ auto ret = std::ranges::move(in, out.begin());
assert(ret.in == in.end());
assert(ret.out == out.end());
assert(out[0].canMove);
@@ -358,19 +386,27 @@ constexpr bool test() {
auto ret = std::ranges::move(IteratorWithMoveIter(a), IteratorWithMoveIter(a + 4), b.data());
assert(ret.in == a + 4);
assert(ret.out == b.data() + 4);
- assert((b == std::array {42, 42, 42, 42}));
+ assert((b == std::array{42, 42, 42, 42}));
}
{
int a[] = {1, 2, 3, 4};
std::array<int, 4> b;
auto range = std::ranges::subrange(IteratorWithMoveIter(a), IteratorWithMoveIter(a + 4));
- auto ret = std::ranges::move(range, b.data());
+ auto ret = std::ranges::move(range, b.data());
assert(ret.in == a + 4);
assert(ret.out == b.data() + 4);
- assert((b == std::array {42, 42, 42, 42}));
+ assert((b == std::array{42, 42, 42, 42}));
}
}
+ { // Test vector<bool>::iterator optimization
+ TestBitIter<8>()();
+ TestBitIter<16>()();
+ TestBitIter<32>()();
+ TestBitIter<64>()();
+ TestBitIter<1024>()();
+ }
+
return true;
}
diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/ranges.move_backward.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/ranges.move_backward.pass.cpp
index 47cf178636ad13..acedcf40ac3596 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/ranges.move_backward.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/ranges.move_backward.pass.cpp
@@ -65,7 +65,7 @@ constexpr void test(std::array<int, N> in) {
{
std::array<int, N> out;
std::same_as<std::ranges::in_out_result<In, Out>> decltype(auto) ret =
- std::ranges::move_backward(In(in.data()), Sent(In(in.data() + in.size())), Out(out.data() + out.size()));
+ std::ranges::move_backward(In(in.data()), Sent(In(in.data() + in.size())), Out(out.data() + out.size()));
assert(in == out);
assert(base(ret.in) == in.data() + in.size());
assert(base(ret.out) == out.data());
@@ -92,16 +92,16 @@ constexpr void test_iterators() {
template <class InContainer, class OutContainer, class In, class Out, class Sent = In>
constexpr void test_containers() {
{
- InContainer in {1, 2, 3, 4};
+ InContainer in{1, 2, 3, 4};
OutContainer out(4);
std::same_as<std::ranges::in_out_result<In, Out>> auto ret =
- std::ranges::move_backward(In(in.begin()), Sent(In(in.end())), Out(out.end()));
+ std::ranges::move_backward(In(in.begin()), Sent(In(in.end())), Out(out.end()));
assert(std::ranges::equal(in, out));
assert(base(ret.in) == in.end());
assert(base(ret.out) == out.begin());
}
{
- InContainer in {1, 2, 3, 4};
+ InContainer in{1, 2, 3, 4};
OutContainer out(4);
auto range = std::ranges::subrange(In(in.begin()), Sent(In(in.end())));
std::same_as<std::ranges::in_out_result<In, Out>> auto ret = std::ranges::move_backward(range, Out(out.end()));
@@ -159,25 +159,61 @@ constexpr void test_proxy_in_iterators() {
}
struct IteratorWithMoveIter {
- using value_type = int;
- using difference_type = int;
+ using value_type = int;
+ using difference_type = int;
explicit IteratorWithMoveIter() = default;
int* ptr;
constexpr IteratorWithMoveIter(int* ptr_) : ptr(ptr_) {}
constexpr int& operator*() const; // iterator with iter_move should not be dereferenced
- constexpr IteratorWithMoveIter& operator++() { ++ptr; return *this; }
- constexpr IteratorWithMoveIter operator++(int) { auto ret = *this; ++*this; return ret; }
+ constexpr IteratorWithMoveIter& operator++() {
+ ++ptr;
+ return *this;
+ }
+ constexpr IteratorWithMoveIter operator++(int) {
+ auto ret = *this;
+ ++*this;
+ return ret;
+ }
- constexpr IteratorWithMoveIter& operator--() { --ptr; return *this; }
- constexpr IteratorWithMoveIter operator--(int) { auto ret = *this; --*this; return ret; }
+ constexpr IteratorWithMoveIter& operator--() {
+ --ptr;
+ return *this;
+ }
+ constexpr IteratorWithMoveIter operator--(int) {
+ auto ret = *this;
+ --*this;
+ return ret;
+ }
friend constexpr int iter_move(const IteratorWithMoveIter&) { return 42; }
constexpr bool operator==(const IteratorWithMoveIter& other) const = default;
};
+template <std::size_t N>
+struct TestBitIter {
+ std::vector<bool> in;
+ TEST_CONSTEXPR TestBitIter() : in(N, false) {
+ for (std::size_t i = 0; i < N; i += 2)
+ in[i] = true;
+ }
+ TEST_CONSTEXPR_CXX20 void operator()() {
+ { // Test move_backward with aligned bytes
+ std::vector<bool> out(N);
+ std::ranges::move_backward(in, out.end());
+ assert(in == out);
+ }
+ { // Test move_backward with unaligned bytes
+ std::vector<bool> out(N + 8);
+ std::ranges::move_backward(in, out.end() - 4);
+ for (std::size_t i = 0; i < N; ++i)
+ assert(out[i + 4] == in[i]);
+ }
+ }
+};
+
constexpr bool test() {
test_in_iterators<bidirectional_iterator>();
test_in_iterators<random_access_iterator>();
@@ -243,7 +279,8 @@ constexpr bool test() {
MoveOnly b[3];
ProxyRange proxyA{a};
ProxyRange proxyB{b};
- std::ranges::move_backward(std::begin(proxyA), std::end(proxyA), std::ranges::next(proxyB.begin(), std::end(proxyB)));
+ std::ranges::move_backward(
+ std::begin(proxyA), std::end(proxyA), std::ranges::next(proxyB.begin(), std::end(proxyB)));
assert(b[0].get() == 1);
assert(b[1].get() == 2);
assert(b[2].get() == 3);
@@ -253,13 +290,13 @@ constexpr bool test() {
{ // check that ranges::dangling is returned
std::array<int, 4> out;
std::same_as<std::ranges::in_out_result<std::ranges::dangling, int*>> auto ret =
- std::ranges::move_backward(std::array {1, 2, 3, 4}, out.data() + out.size());
+ std::ranges::move_backward(std::array{1, 2, 3, 4}, out.data() + out.size());
assert(ret.out == out.data());
assert((out == std::array{1, 2, 3, 4}));
}
{ // check that an iterator is returned with a borrowing range
- std::array in {1, 2, 3, 4};
+ std::array in{1, 2, 3, 4};
std::array<int, 4> out;
std::same_as<std::ranges::in_out_result<std::array<int, 4>::iterator, int*>> auto ret =
std::ranges::move_backward(std::views::all(in), out.data() + out.size());
@@ -270,8 +307,8 @@ constexpr bool test() {
{ // check that every element is moved exactly once
struct MoveOnce {
- bool moved = false;
- constexpr MoveOnce() = default;
+ bool moved = false;
+ constexpr MoveOnce() = default;
constexpr MoveOnce(const MoveOnce& other) = delete;
constexpr MoveOnce& operator=(const MoveOnce& other) {
assert(!other.moved);
@@ -280,16 +317,16 @@ constexpr bool test() {
}
};
{
- std::array<MoveOnce, 4> in {};
- std::array<MoveOnce, 4> out {};
+ std::array<MoveOnce, 4> in{};
+ std::array<MoveOnce, 4> out{};
auto ret = std::ranges::move_backward(in.begin(), in.end(), out.end());
assert(ret.in == in.end());
assert(ret.out == out.begin());
assert(std::all_of(out.begin(), out.end(), [](const auto& e) { return e.moved; }));
}
{
- std::array<MoveOnce, 4> in {};
- std::array<MoveOnce, 4> out {};
+ std::array<MoveOnce, 4> in{};
+ std::array<MoveOnce, 4> out{};
auto ret = std::ranges::move_backward(in, out.end());
assert(ret.in == in.end());
assert(ret.out == out.begin());
@@ -300,8 +337,8 @@ constexpr bool test() {
{ // check that the range is moved backwards
struct OnlyBackwardsMovable {
OnlyBackwardsMovable* next = nullptr;
- bool canMove = false;
- OnlyBackwardsMovable() = default;
+ bool canMove = false;
+ OnlyBackwardsMovable() = default;
constexpr OnlyBackwardsMovable& operator=(const OnlyBackwardsMovable&) {
assert(canMove);
if (next != nullptr)
@@ -310,12 +347,12 @@ constexpr bool test() {
}
};
{
- std::array<OnlyBackwardsMovable, 3> in {};
- std::array<OnlyBackwardsMovable, 3> out {};
- out[1].next = &out[0];
- out[2].next = &out[1];
+ std::array<OnlyBackwardsMovable, 3> in{};
+ std::array<OnlyBackwardsMovable, 3> out{};
+ out[1].next = &out[0];
+ out[2].next = &out[1];
out[2].canMove = true;
- auto ret = std::ranges::move_backward(in, out.end());
+ auto ret = std::ranges::move_backward(in, out.end());
assert(ret.in == in.end());
assert(ret.out == out.begin());
assert(out[0].canMove);
@@ -323,12 +360,12 @@ constexpr bool test() {
assert(out[2].canMove);
}
{
- std::array<OnlyBackwardsMovable, 3> in {};
- std::array<OnlyBackwardsMovable, 3> out {};
- out[1].next = &out[0];
- out[2].next = &out[1];
+ std::array<OnlyBackwardsMovable, 3> in{};
+ std::array<OnlyBackwardsMovable, 3> out{};
+ out[1].next = &out[0];
+ out[2].next = &out[1];
out[2].canMove = true;
- auto ret = std::ranges::move_backward(in.begin(), in.end(), out.end());
+ auto ret = std::ranges::move_backward(in.begin(), in.end(), out.end());
assert(ret.in == in.end());
assert(ret.out == out.begin());
assert(out[0].canMove);
@@ -344,19 +381,27 @@ constexpr bool test() {
auto ret = std::ranges::move_backward(IteratorWithMoveIter(a), IteratorWithMoveIter(a + 4), b.data() + b.size());
assert(ret.in == a + 4);
assert(ret.out == b.data());
- assert((b == std::array {42, 42, 42, 42}));
+ assert((b == std::array{42, 42, 42, 42}));
}
{
int a[] = {1, 2, 3, 4};
std::array<int, 4> b;
auto range = std::ranges::subrange(IteratorWithMoveIter(a), IteratorWithMoveIter(a + 4));
- auto ret = std::ranges::move_backward(range, b.data() + b.size());
+ auto ret = std::ranges::move_backward(range, b.data() + b.size());
assert(ret.in == a + 4);
assert(ret.out == b.data());
- assert((b == std::array {42, 42, 42, 42}));
+ assert((b == std::array{42, 42, 42, 42}));
}
}
+ { // Test vector<bool>::iterator optimization
+ TestBitIter<8>()();
+ TestBitIter<16>()();
+ TestBitIter<32>()();
+ TestBitIter<64>()();
+ TestBitIter<1024>()();
+ }
+
return true;
}
>From 9b81a59066f8e3551b7af5854bc06d464f127604 Mon Sep 17 00:00:00 2001
From: Peng Liu <winner245 at hotmail.com>
Date: Thu, 26 Dec 2024 16:10:54 -0500
Subject: [PATCH 2/2] Optimize ranges::equal for vector<bool>::iterator
---
libcxx/include/__algorithm/rotate.h | 74 ++
libcxx/include/__bit_reference | 80 +-
.../benchmarks/algorithms/rotate.bench.cpp | 40 +
.../alg.rotate/ranges_rotate.pass.cpp | 67 +-
.../alg.rotate/rotate.pass.cpp | 810 +++++++++---------
5 files changed, 580 insertions(+), 491 deletions(-)
create mode 100644 libcxx/test/benchmarks/algorithms/rotate.bench.cpp
diff --git a/libcxx/include/__algorithm/rotate.h b/libcxx/include/__algorithm/rotate.h
index df4ca95aac95bc..10e7fd77023790 100644
--- a/libcxx/include/__algorithm/rotate.h
+++ b/libcxx/include/__algorithm/rotate.h
@@ -9,12 +9,17 @@
#ifndef _LIBCPP___ALGORITHM_ROTATE_H
#define _LIBCPP___ALGORITHM_ROTATE_H
+#include <__algorithm/copy.h>
+#include <__algorithm/copy_backward.h>
#include <__algorithm/iterator_operations.h>
#include <__algorithm/move.h>
#include <__algorithm/move_backward.h>
#include <__algorithm/swap_ranges.h>
#include <__config>
#include <__iterator/iterator_traits.h>
+#include <__memory/construct_at.h>
+#include <__memory/pointer_traits.h>
+#include <__type_traits/is_constant_evaluated.h>
#include <__type_traits/is_trivially_assignable.h>
#include <__utility/move.h>
#include <__utility/pair.h>
@@ -185,6 +190,75 @@ __rotate(_Iterator __first, _Iterator __middle, _Sentinel __last) {
return _Ret(std::move(__result), std::move(__last_iter));
}
+template <class _Cp>
+struct __bit_array {
+ using difference_type = typename _Cp::difference_type;
+ using __storage_type = typename _Cp::__storage_type;
+ using __storage_pointer = typename _Cp::__storage_pointer;
+ using iterator = typename _Cp::iterator;
+
+ static const unsigned __bits_per_word = _Cp::__bits_per_word;
+ static const unsigned _Np = 4;
+
+ difference_type __size_;
+ __storage_type __word_[_Np];
+
+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static difference_type capacity() {
+ return static_cast<difference_type>(_Np * __bits_per_word);
+ }
+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit __bit_array(difference_type __s) : __size_(__s) {
+ if (__libcpp_is_constant_evaluated()) {
+ for (size_t __i = 0; __i != __bit_array<_Cp>::_Np; ++__i)
+ std::__construct_at(__word_ + __i, 0);
+ }
+ }
+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator begin() {
+ return iterator(pointer_traits<__storage_pointer>::pointer_to(__word_[0]), 0);
+ }
+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator end() {
+ return iterator(pointer_traits<__storage_pointer>::pointer_to(__word_[0]) + __size_ / __bits_per_word,
+ static_cast<unsigned>(__size_ % __bits_per_word));
+ }
+};
+
+template <class, class _Cp>
+_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<__bit_iterator<_Cp, false>, __bit_iterator<_Cp, false> >
+__rotate(__bit_iterator<_Cp, false> __first, __bit_iterator<_Cp, false> __middle, __bit_iterator<_Cp, false> __last) {
+ using _I1 = __bit_iterator<_Cp, false>;
+ using difference_type = typename _I1::difference_type;
+ difference_type __d1 = __middle - __first;
+ difference_type __d2 = __last - __middle;
+ _I1 __r = __first + __d2;
+ while (__d1 != 0 && __d2 != 0) {
+ if (__d1 <= __d2) {
+ if (__d1 <= __bit_array<_Cp>::capacity()) {
+ __bit_array<_Cp> __b(__d1);
+ std::copy(__first, __middle, __b.begin());
+ std::copy(__b.begin(), __b.end(), std::copy(__middle, __last, __first));
+ break;
+ } else {
+ __bit_iterator<_Cp, false> __mp = std::swap_ranges(__first, __middle, __middle);
+ __first = __middle;
+ __middle = __mp;
+ __d2 -= __d1;
+ }
+ } else {
+ if (__d2 <= __bit_array<_Cp>::capacity()) {
+ __bit_array<_Cp> __b(__d2);
+ std::copy(__middle, __last, __b.begin());
+ std::copy_backward(__b.begin(), __b.end(), std::copy_backward(__first, __middle, __last));
+ break;
+ } else {
+ __bit_iterator<_Cp, false> __mp = __first + __d2;
+ std::swap_ranges(__first, __mp, __middle);
+ __first = __mp;
+ __d1 -= __d2;
+ }
+ }
+ }
+ return std::make_pair(__r, __last);
+}
+
template <class _ForwardIterator>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last) {
diff --git a/libcxx/include/__bit_reference b/libcxx/include/__bit_reference
index a86513d81f1820..6a824c0d23188d 100644
--- a/libcxx/include/__bit_reference
+++ b/libcxx/include/__bit_reference
@@ -12,6 +12,7 @@
#include <__algorithm/copy_n.h>
#include <__algorithm/min.h>
+#include <__algorithm/rotate.h>
#include <__bit/countr.h>
#include <__compare/ordering.h>
#include <__config>
@@ -22,6 +23,7 @@
#include <__memory/pointer_traits.h>
#include <__type_traits/conditional.h>
#include <__type_traits/is_constant_evaluated.h>
+#include <__utility/pair.h>
#include <__utility/swap.h>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -567,78 +569,6 @@ inline _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cr, false> swap_ranges(
return std::__swap_ranges_unaligned(__first1, __last1, __first2);
}
-// rotate
-
-template <class _Cp>
-struct __bit_array {
- using difference_type = typename _Cp::difference_type;
- using __storage_type = typename _Cp::__storage_type;
- using __storage_pointer = typename _Cp::__storage_pointer;
- using iterator = typename _Cp::iterator;
-
- static const unsigned __bits_per_word = _Cp::__bits_per_word;
- static const unsigned _Np = 4;
-
- difference_type __size_;
- __storage_type __word_[_Np];
-
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static difference_type capacity() {
- return static_cast<difference_type>(_Np * __bits_per_word);
- }
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit __bit_array(difference_type __s) : __size_(__s) {
- if (__libcpp_is_constant_evaluated()) {
- for (size_t __i = 0; __i != __bit_array<_Cp>::_Np; ++__i)
- std::__construct_at(__word_ + __i, 0);
- }
- }
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator begin() {
- return iterator(pointer_traits<__storage_pointer>::pointer_to(__word_[0]), 0);
- }
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator end() {
- return iterator(pointer_traits<__storage_pointer>::pointer_to(__word_[0]) + __size_ / __bits_per_word,
- static_cast<unsigned>(__size_ % __bits_per_word));
- }
-};
-
-template <class _Cp>
-_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, false>
-rotate(__bit_iterator<_Cp, false> __first, __bit_iterator<_Cp, false> __middle, __bit_iterator<_Cp, false> __last) {
- using _I1 = __bit_iterator<_Cp, false>;
- using difference_type = typename _I1::difference_type;
-
- difference_type __d1 = __middle - __first;
- difference_type __d2 = __last - __middle;
- _I1 __r = __first + __d2;
- while (__d1 != 0 && __d2 != 0) {
- if (__d1 <= __d2) {
- if (__d1 <= __bit_array<_Cp>::capacity()) {
- __bit_array<_Cp> __b(__d1);
- std::copy(__first, __middle, __b.begin());
- std::copy(__b.begin(), __b.end(), std::copy(__middle, __last, __first));
- break;
- } else {
- __bit_iterator<_Cp, false> __mp = std::swap_ranges(__first, __middle, __middle);
- __first = __middle;
- __middle = __mp;
- __d2 -= __d1;
- }
- } else {
- if (__d2 <= __bit_array<_Cp>::capacity()) {
- __bit_array<_Cp> __b(__d2);
- std::copy(__middle, __last, __b.begin());
- std::copy_backward(__b.begin(), __b.end(), std::copy_backward(__first, __middle, __last));
- break;
- } else {
- __bit_iterator<_Cp, false> __mp = __first + __d2;
- std::swap_ranges(__first, __mp, __middle);
- __first = __mp;
- __d1 -= __d2;
- }
- }
- }
- return __r;
-}
-
// equal
template <class _Cp, bool _IC1, bool _IC2>
@@ -979,9 +909,9 @@ private:
template <class _Cl, class _Cr>
friend __bit_iterator<_Cr, false>
swap_ranges(__bit_iterator<_Cl, false>, __bit_iterator<_Cl, false>, __bit_iterator<_Cr, false>);
- template <class _Dp>
- _LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator<_Dp, false>
- rotate(__bit_iterator<_Dp, false>, __bit_iterator<_Dp, false>, __bit_iterator<_Dp, false>);
+ template <class, class _Dp>
+ _LIBCPP_CONSTEXPR_SINCE_CXX20 friend pair<__bit_iterator<_Cp, false>, __bit_iterator<_Cp, false> >
+ __rotate(__bit_iterator<_Dp, false>, __bit_iterator<_Dp, false>, __bit_iterator<_Dp, false>);
template <class _Dp, bool _IC1, bool _IC2>
_LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool
__equal_aligned(__bit_iterator<_Dp, _IC1>, __bit_iterator<_Dp, _IC1>, __bit_iterator<_Dp, _IC2>);
diff --git a/libcxx/test/benchmarks/algorithms/rotate.bench.cpp b/libcxx/test/benchmarks/algorithms/rotate.bench.cpp
new file mode 100644
index 00000000000000..48843d00b84f5f
--- /dev/null
+++ b/libcxx/test/benchmarks/algorithms/rotate.bench.cpp
@@ -0,0 +1,40 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+#include <algorithm>
+#include <benchmark/benchmark.h>
+#include <vector>
+
+static void bm_ranges_rotate(benchmark::State& state) {
+ auto n = state.range();
+ std::vector<bool> v(n);
+ auto mid = std::ranges::begin(v) + n / 2;
+ for (auto _ : state) {
+ benchmark::DoNotOptimize(std::ranges::rotate(v, mid));
+ benchmark::DoNotOptimize(&v);
+ }
+}
+
+static void bm_rotate(benchmark::State& state) {
+ auto n = state.range();
+ std::vector<bool> v(n);
+ auto beg = v.begin();
+ auto mid = v.begin() + n / 2;
+ auto end = v.end();
+ for (auto _ : state) {
+ benchmark::DoNotOptimize(std::rotate(beg, mid, end));
+ benchmark::DoNotOptimize(&v);
+ }
+}
+
+BENCHMARK(bm_ranges_rotate)->Range(8, 1 << 20);
+BENCHMARK(bm_rotate)->Range(8, 1 << 20);
+
+BENCHMARK_MAIN();
diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/ranges_rotate.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/ranges_rotate.pass.cpp
index 1506ce79b5dbd2..04df4bda00f7e7 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/ranges_rotate.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/ranges_rotate.pass.cpp
@@ -21,6 +21,7 @@
#include <array>
#include <cassert>
#include <ranges>
+#include <vector>
#include "almost_satisfies_types.h"
#include "test_iterators.h"
@@ -29,10 +30,9 @@
// ======================================================
template <class Iter = int*, class Sent = int*>
-concept HasRotateIter =
- requires(Iter&& iter, Sent&& sent) {
- std::ranges::rotate(std::forward<Iter>(iter), std::forward<Iter>(iter), std::forward<Sent>(sent));
- };
+concept HasRotateIter = requires(Iter&& iter, Sent&& sent) {
+ std::ranges::rotate(std::forward<Iter>(iter), std::forward<Iter>(iter), std::forward<Sent>(sent));
+};
static_assert(HasRotateIter<int*, int*>);
@@ -48,10 +48,9 @@ static_assert(!HasRotateIter<int*, SentinelForNotWeaklyEqualityComparableWith>);
// =========================================
template <class Range>
-concept HasRotateRange =
- requires(Range&& range, std::ranges::iterator_t<Range> iter) {
- std::ranges::rotate(std::forward<Range>(range), iter);
- };
+concept HasRotateRange = requires(Range&& range, std::ranges::iterator_t<Range> iter) {
+ std::ranges::rotate(std::forward<Range>(range), iter);
+};
template <class T>
using R = UncheckedRange<T>;
@@ -73,10 +72,10 @@ constexpr void test_one(const std::array<int, N> input, std::size_t mid_index, s
assert(mid_index <= N);
{ // (iterator, sentinel) overload.
- auto in = input;
+ auto in = input;
auto begin = Iter(in.data());
- auto mid = Iter(in.data() + mid_index);
- auto end = Sent(Iter(in.data() + in.size()));
+ auto mid = Iter(in.data() + mid_index);
+ auto end = Sent(Iter(in.data() + in.size()));
std::same_as<std::ranges::subrange<Iter>> decltype(auto) result = std::ranges::rotate(begin, mid, end);
assert(base(result.begin()) == in.data() + in.size() - mid_index);
@@ -85,10 +84,10 @@ constexpr void test_one(const std::array<int, N> input, std::size_t mid_index, s
}
{ // (range) overload.
- auto in = input;
+ auto in = input;
auto begin = Iter(in.data());
- auto mid = Iter(in.data() + mid_index);
- auto end = Sent(Iter(in.data() + in.size()));
+ auto mid = Iter(in.data() + mid_index);
+ auto end = Sent(Iter(in.data() + in.size()));
auto range = std::ranges::subrange(std::move(begin), std::move(end));
std::same_as<std::ranges::subrange<Iter>> decltype(auto) result = std::ranges::rotate(range, mid);
@@ -146,18 +145,39 @@ constexpr void test_iterators() {
test_iter<int*>();
}
+template <std::size_t N>
+TEST_CONSTEXPR_CXX20 bool test_bit_iterator() {
+ for (int offset = -1; offset <= 1; ++offset) {
+ std::vector<bool> a(N, false);
+ std::size_t mid = N / 2 + offset;
+ for (std::size_t i = mid; i < N; ++i)
+ a[i] = true;
+
+ // (iterator, sentinel)-overload
+ std::ranges::rotate(std::ranges::begin(a), std::ranges::begin(a) + mid, std::ranges::end(a));
+ for (std::size_t i = 0; i < N; ++i)
+ assert(a[i] == (i < N - mid));
+
+ // range-overload
+ std::ranges::rotate(a, std::ranges::begin(a) + (N - mid));
+ for (std::size_t i = 0; i < N; ++i)
+ assert(a[i] == (i >= mid));
+ }
+ return true;
+};
+
constexpr bool test() {
test_iterators();
{ // Complexity: at most `last - first` swaps.
const std::array input = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
- auto expected = static_cast<int>(input.size());
+ auto expected = static_cast<int>(input.size());
{
- auto in = input;
- int swaps = 0;
+ auto in = input;
+ int swaps = 0;
auto begin = adl::Iterator::TrackSwaps(in.data(), swaps);
- auto end = adl::Iterator::TrackSwaps(in.data() + in.size(), swaps);
+ auto end = adl::Iterator::TrackSwaps(in.data() + in.size(), swaps);
for (std::size_t mid = 0; mid != input.size(); ++mid) {
std::ranges::rotate(begin, begin + mid, end);
@@ -166,10 +186,10 @@ constexpr bool test() {
}
{
- auto in = input;
- int swaps = 0;
+ auto in = input;
+ int swaps = 0;
auto begin = adl::Iterator::TrackSwaps(in.data(), swaps);
- auto end = adl::Iterator::TrackSwaps(in.data() + in.size(), swaps);
+ auto end = adl::Iterator::TrackSwaps(in.data() + in.size(), swaps);
auto range = std::ranges::subrange(begin, end);
for (std::size_t mid = 0; mid != input.size(); ++mid) {
@@ -179,6 +199,11 @@ constexpr bool test() {
}
}
+ test_bit_iterator<8>();
+ test_bit_iterator<16>();
+ test_bit_iterator<64>();
+ test_bit_iterator<100>();
+
return true;
}
diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/rotate.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/rotate.pass.cpp
index 7bbf3abd43dc57..ce4a3c2211def8 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/rotate.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/rotate.pass.cpp
@@ -15,433 +15,453 @@
#include <algorithm>
#include <cassert>
#include <memory>
+#include <vector>
#include "test_macros.h"
#include "test_iterators.h"
template <class Iter>
-TEST_CONSTEXPR_CXX17 bool
-test()
-{
- int ia[] = {0};
- const int sa = static_cast<int>(sizeof(ia)/sizeof(ia[0]));
- Iter r = std::rotate(Iter(ia), Iter(ia), Iter(ia));
- assert(base(r) == ia);
- assert(ia[0] == 0);
- r = std::rotate(Iter(ia), Iter(ia), Iter(ia+sa));
- assert(base(r) == ia+sa);
- assert(ia[0] == 0);
- r = std::rotate(Iter(ia), Iter(ia+sa), Iter(ia+sa));
- assert(base(r) == ia);
- assert(ia[0] == 0);
+TEST_CONSTEXPR_CXX17 bool test() {
+ int ia[] = {0};
+ const int sa = static_cast<int>(sizeof(ia) / sizeof(ia[0]));
+ Iter r = std::rotate(Iter(ia), Iter(ia), Iter(ia));
+ assert(base(r) == ia);
+ assert(ia[0] == 0);
+ r = std::rotate(Iter(ia), Iter(ia), Iter(ia + sa));
+ assert(base(r) == ia + sa);
+ assert(ia[0] == 0);
+ r = std::rotate(Iter(ia), Iter(ia + sa), Iter(ia + sa));
+ assert(base(r) == ia);
+ assert(ia[0] == 0);
- int ib[] = {0, 1};
- const int sb = static_cast<int>(sizeof(ib)/sizeof(ib[0]));
- r = std::rotate(Iter(ib), Iter(ib), Iter(ib+sb));
- assert(base(r) == ib+sb);
- assert(ib[0] == 0);
- assert(ib[1] == 1);
- r = std::rotate(Iter(ib), Iter(ib+1), Iter(ib+sb));
- assert(base(r) == ib+1);
- assert(ib[0] == 1);
- assert(ib[1] == 0);
- r = std::rotate(Iter(ib), Iter(ib+sb), Iter(ib+sb));
- assert(base(r) == ib);
- assert(ib[0] == 1);
- assert(ib[1] == 0);
+ int ib[] = {0, 1};
+ const int sb = static_cast<int>(sizeof(ib) / sizeof(ib[0]));
+ r = std::rotate(Iter(ib), Iter(ib), Iter(ib + sb));
+ assert(base(r) == ib + sb);
+ assert(ib[0] == 0);
+ assert(ib[1] == 1);
+ r = std::rotate(Iter(ib), Iter(ib + 1), Iter(ib + sb));
+ assert(base(r) == ib + 1);
+ assert(ib[0] == 1);
+ assert(ib[1] == 0);
+ r = std::rotate(Iter(ib), Iter(ib + sb), Iter(ib + sb));
+ assert(base(r) == ib);
+ assert(ib[0] == 1);
+ assert(ib[1] == 0);
- int ic[] = {0, 1, 2};
- const int sc = static_cast<int>(sizeof(ic)/sizeof(ic[0]));
- r = std::rotate(Iter(ic), Iter(ic), Iter(ic+sc));
- assert(base(r) == ic+sc);
- assert(ic[0] == 0);
- assert(ic[1] == 1);
- assert(ic[2] == 2);
- r = std::rotate(Iter(ic), Iter(ic+1), Iter(ic+sc));
- assert(base(r) == ic+2);
- assert(ic[0] == 1);
- assert(ic[1] == 2);
- assert(ic[2] == 0);
- r = std::rotate(Iter(ic), Iter(ic+2), Iter(ic+sc));
- assert(base(r) == ic+1);
- assert(ic[0] == 0);
- assert(ic[1] == 1);
- assert(ic[2] == 2);
- r = std::rotate(Iter(ic), Iter(ic+sc), Iter(ic+sc));
- assert(base(r) == ic);
- assert(ic[0] == 0);
- assert(ic[1] == 1);
- assert(ic[2] == 2);
+ int ic[] = {0, 1, 2};
+ const int sc = static_cast<int>(sizeof(ic) / sizeof(ic[0]));
+ r = std::rotate(Iter(ic), Iter(ic), Iter(ic + sc));
+ assert(base(r) == ic + sc);
+ assert(ic[0] == 0);
+ assert(ic[1] == 1);
+ assert(ic[2] == 2);
+ r = std::rotate(Iter(ic), Iter(ic + 1), Iter(ic + sc));
+ assert(base(r) == ic + 2);
+ assert(ic[0] == 1);
+ assert(ic[1] == 2);
+ assert(ic[2] == 0);
+ r = std::rotate(Iter(ic), Iter(ic + 2), Iter(ic + sc));
+ assert(base(r) == ic + 1);
+ assert(ic[0] == 0);
+ assert(ic[1] == 1);
+ assert(ic[2] == 2);
+ r = std::rotate(Iter(ic), Iter(ic + sc), Iter(ic + sc));
+ assert(base(r) == ic);
+ assert(ic[0] == 0);
+ assert(ic[1] == 1);
+ assert(ic[2] == 2);
- int id[] = {0, 1, 2, 3};
- const int sd = static_cast<int>(sizeof(id)/sizeof(id[0]));
- r = std::rotate(Iter(id), Iter(id), Iter(id+sd));
- assert(base(r) == id+sd);
- assert(id[0] == 0);
- assert(id[1] == 1);
- assert(id[2] == 2);
- assert(id[3] == 3);
- r = std::rotate(Iter(id), Iter(id+1), Iter(id+sd));
- assert(base(r) == id+3);
- assert(id[0] == 1);
- assert(id[1] == 2);
- assert(id[2] == 3);
- assert(id[3] == 0);
- r = std::rotate(Iter(id), Iter(id+2), Iter(id+sd));
- assert(base(r) == id+2);
- assert(id[0] == 3);
- assert(id[1] == 0);
- assert(id[2] == 1);
- assert(id[3] == 2);
- r = std::rotate(Iter(id), Iter(id+3), Iter(id+sd));
- assert(base(r) == id+1);
- assert(id[0] == 2);
- assert(id[1] == 3);
- assert(id[2] == 0);
- assert(id[3] == 1);
- r = std::rotate(Iter(id), Iter(id+sd), Iter(id+sd));
- assert(base(r) == id);
- assert(id[0] == 2);
- assert(id[1] == 3);
- assert(id[2] == 0);
- assert(id[3] == 1);
+ int id[] = {0, 1, 2, 3};
+ const int sd = static_cast<int>(sizeof(id) / sizeof(id[0]));
+ r = std::rotate(Iter(id), Iter(id), Iter(id + sd));
+ assert(base(r) == id + sd);
+ assert(id[0] == 0);
+ assert(id[1] == 1);
+ assert(id[2] == 2);
+ assert(id[3] == 3);
+ r = std::rotate(Iter(id), Iter(id + 1), Iter(id + sd));
+ assert(base(r) == id + 3);
+ assert(id[0] == 1);
+ assert(id[1] == 2);
+ assert(id[2] == 3);
+ assert(id[3] == 0);
+ r = std::rotate(Iter(id), Iter(id + 2), Iter(id + sd));
+ assert(base(r) == id + 2);
+ assert(id[0] == 3);
+ assert(id[1] == 0);
+ assert(id[2] == 1);
+ assert(id[3] == 2);
+ r = std::rotate(Iter(id), Iter(id + 3), Iter(id + sd));
+ assert(base(r) == id + 1);
+ assert(id[0] == 2);
+ assert(id[1] == 3);
+ assert(id[2] == 0);
+ assert(id[3] == 1);
+ r = std::rotate(Iter(id), Iter(id + sd), Iter(id + sd));
+ assert(base(r) == id);
+ assert(id[0] == 2);
+ assert(id[1] == 3);
+ assert(id[2] == 0);
+ assert(id[3] == 1);
- int ie[] = {0, 1, 2, 3, 4};
- const int se = static_cast<int>(sizeof(ie)/sizeof(ie[0]));
- r = std::rotate(Iter(ie), Iter(ie), Iter(ie+se));
- assert(base(r) == ie+se);
- assert(ie[0] == 0);
- assert(ie[1] == 1);
- assert(ie[2] == 2);
- assert(ie[3] == 3);
- assert(ie[4] == 4);
- r = std::rotate(Iter(ie), Iter(ie+1), Iter(ie+se));
- assert(base(r) == ie+4);
- assert(ie[0] == 1);
- assert(ie[1] == 2);
- assert(ie[2] == 3);
- assert(ie[3] == 4);
- assert(ie[4] == 0);
- r = std::rotate(Iter(ie), Iter(ie+2), Iter(ie+se));
- assert(base(r) == ie+3);
- assert(ie[0] == 3);
- assert(ie[1] == 4);
- assert(ie[2] == 0);
- assert(ie[3] == 1);
- assert(ie[4] == 2);
- r = std::rotate(Iter(ie), Iter(ie+3), Iter(ie+se));
- assert(base(r) == ie+2);
- assert(ie[0] == 1);
- assert(ie[1] == 2);
- assert(ie[2] == 3);
- assert(ie[3] == 4);
- assert(ie[4] == 0);
- r = std::rotate(Iter(ie), Iter(ie+4), Iter(ie+se));
- assert(base(r) == ie+1);
- assert(ie[0] == 0);
- assert(ie[1] == 1);
- assert(ie[2] == 2);
- assert(ie[3] == 3);
- assert(ie[4] == 4);
- r = std::rotate(Iter(ie), Iter(ie+se), Iter(ie+se));
- assert(base(r) == ie);
- assert(ie[0] == 0);
- assert(ie[1] == 1);
- assert(ie[2] == 2);
- assert(ie[3] == 3);
- assert(ie[4] == 4);
+ int ie[] = {0, 1, 2, 3, 4};
+ const int se = static_cast<int>(sizeof(ie) / sizeof(ie[0]));
+ r = std::rotate(Iter(ie), Iter(ie), Iter(ie + se));
+ assert(base(r) == ie + se);
+ assert(ie[0] == 0);
+ assert(ie[1] == 1);
+ assert(ie[2] == 2);
+ assert(ie[3] == 3);
+ assert(ie[4] == 4);
+ r = std::rotate(Iter(ie), Iter(ie + 1), Iter(ie + se));
+ assert(base(r) == ie + 4);
+ assert(ie[0] == 1);
+ assert(ie[1] == 2);
+ assert(ie[2] == 3);
+ assert(ie[3] == 4);
+ assert(ie[4] == 0);
+ r = std::rotate(Iter(ie), Iter(ie + 2), Iter(ie + se));
+ assert(base(r) == ie + 3);
+ assert(ie[0] == 3);
+ assert(ie[1] == 4);
+ assert(ie[2] == 0);
+ assert(ie[3] == 1);
+ assert(ie[4] == 2);
+ r = std::rotate(Iter(ie), Iter(ie + 3), Iter(ie + se));
+ assert(base(r) == ie + 2);
+ assert(ie[0] == 1);
+ assert(ie[1] == 2);
+ assert(ie[2] == 3);
+ assert(ie[3] == 4);
+ assert(ie[4] == 0);
+ r = std::rotate(Iter(ie), Iter(ie + 4), Iter(ie + se));
+ assert(base(r) == ie + 1);
+ assert(ie[0] == 0);
+ assert(ie[1] == 1);
+ assert(ie[2] == 2);
+ assert(ie[3] == 3);
+ assert(ie[4] == 4);
+ r = std::rotate(Iter(ie), Iter(ie + se), Iter(ie + se));
+ assert(base(r) == ie);
+ assert(ie[0] == 0);
+ assert(ie[1] == 1);
+ assert(ie[2] == 2);
+ assert(ie[3] == 3);
+ assert(ie[4] == 4);
- int ig[] = {0, 1, 2, 3, 4, 5};
- const int sg = static_cast<int>(sizeof(ig)/sizeof(ig[0]));
- r = std::rotate(Iter(ig), Iter(ig), Iter(ig+sg));
- assert(base(r) == ig+sg);
- assert(ig[0] == 0);
- assert(ig[1] == 1);
- assert(ig[2] == 2);
- assert(ig[3] == 3);
- assert(ig[4] == 4);
- assert(ig[5] == 5);
- r = std::rotate(Iter(ig), Iter(ig+1), Iter(ig+sg));
- assert(base(r) == ig+5);
- assert(ig[0] == 1);
- assert(ig[1] == 2);
- assert(ig[2] == 3);
- assert(ig[3] == 4);
- assert(ig[4] == 5);
- assert(ig[5] == 0);
- r = std::rotate(Iter(ig), Iter(ig+2), Iter(ig+sg));
- assert(base(r) == ig+4);
- assert(ig[0] == 3);
- assert(ig[1] == 4);
- assert(ig[2] == 5);
- assert(ig[3] == 0);
- assert(ig[4] == 1);
- assert(ig[5] == 2);
- r = std::rotate(Iter(ig), Iter(ig+3), Iter(ig+sg));
- assert(base(r) == ig+3);
- assert(ig[0] == 0);
- assert(ig[1] == 1);
- assert(ig[2] == 2);
- assert(ig[3] == 3);
- assert(ig[4] == 4);
- assert(ig[5] == 5);
- r = std::rotate(Iter(ig), Iter(ig+4), Iter(ig+sg));
- assert(base(r) == ig+2);
- assert(ig[0] == 4);
- assert(ig[1] == 5);
- assert(ig[2] == 0);
- assert(ig[3] == 1);
- assert(ig[4] == 2);
- assert(ig[5] == 3);
- r = std::rotate(Iter(ig), Iter(ig+5), Iter(ig+sg));
- assert(base(r) == ig+1);
- assert(ig[0] == 3);
- assert(ig[1] == 4);
- assert(ig[2] == 5);
- assert(ig[3] == 0);
- assert(ig[4] == 1);
- assert(ig[5] == 2);
- r = std::rotate(Iter(ig), Iter(ig+sg), Iter(ig+sg));
- assert(base(r) == ig);
- assert(ig[0] == 3);
- assert(ig[1] == 4);
- assert(ig[2] == 5);
- assert(ig[3] == 0);
- assert(ig[4] == 1);
- assert(ig[5] == 2);
+ int ig[] = {0, 1, 2, 3, 4, 5};
+ const int sg = static_cast<int>(sizeof(ig) / sizeof(ig[0]));
+ r = std::rotate(Iter(ig), Iter(ig), Iter(ig + sg));
+ assert(base(r) == ig + sg);
+ assert(ig[0] == 0);
+ assert(ig[1] == 1);
+ assert(ig[2] == 2);
+ assert(ig[3] == 3);
+ assert(ig[4] == 4);
+ assert(ig[5] == 5);
+ r = std::rotate(Iter(ig), Iter(ig + 1), Iter(ig + sg));
+ assert(base(r) == ig + 5);
+ assert(ig[0] == 1);
+ assert(ig[1] == 2);
+ assert(ig[2] == 3);
+ assert(ig[3] == 4);
+ assert(ig[4] == 5);
+ assert(ig[5] == 0);
+ r = std::rotate(Iter(ig), Iter(ig + 2), Iter(ig + sg));
+ assert(base(r) == ig + 4);
+ assert(ig[0] == 3);
+ assert(ig[1] == 4);
+ assert(ig[2] == 5);
+ assert(ig[3] == 0);
+ assert(ig[4] == 1);
+ assert(ig[5] == 2);
+ r = std::rotate(Iter(ig), Iter(ig + 3), Iter(ig + sg));
+ assert(base(r) == ig + 3);
+ assert(ig[0] == 0);
+ assert(ig[1] == 1);
+ assert(ig[2] == 2);
+ assert(ig[3] == 3);
+ assert(ig[4] == 4);
+ assert(ig[5] == 5);
+ r = std::rotate(Iter(ig), Iter(ig + 4), Iter(ig + sg));
+ assert(base(r) == ig + 2);
+ assert(ig[0] == 4);
+ assert(ig[1] == 5);
+ assert(ig[2] == 0);
+ assert(ig[3] == 1);
+ assert(ig[4] == 2);
+ assert(ig[5] == 3);
+ r = std::rotate(Iter(ig), Iter(ig + 5), Iter(ig + sg));
+ assert(base(r) == ig + 1);
+ assert(ig[0] == 3);
+ assert(ig[1] == 4);
+ assert(ig[2] == 5);
+ assert(ig[3] == 0);
+ assert(ig[4] == 1);
+ assert(ig[5] == 2);
+ r = std::rotate(Iter(ig), Iter(ig + sg), Iter(ig + sg));
+ assert(base(r) == ig);
+ assert(ig[0] == 3);
+ assert(ig[1] == 4);
+ assert(ig[2] == 5);
+ assert(ig[3] == 0);
+ assert(ig[4] == 1);
+ assert(ig[5] == 2);
- return true;
+ return true;
}
#if TEST_STD_VER >= 11
template <class Iter>
-void
-test1()
-{
- std::unique_ptr<int> ia[1];
- const int sa = static_cast<int>(sizeof(ia)/sizeof(ia[0]));
- for (int i = 0; i < sa; ++i)
- ia[i].reset(new int(i));
- Iter r = std::rotate(Iter(ia), Iter(ia), Iter(ia));
- assert(base(r) == ia);
- assert(*ia[0] == 0);
- r = std::rotate(Iter(ia), Iter(ia), Iter(ia+sa));
- assert(base(r) == ia+sa);
- assert(*ia[0] == 0);
- r = std::rotate(Iter(ia), Iter(ia+sa), Iter(ia+sa));
- assert(base(r) == ia);
- assert(*ia[0] == 0);
+void test1() {
+ std::unique_ptr<int> ia[1];
+ const int sa = static_cast<int>(sizeof(ia) / sizeof(ia[0]));
+ for (int i = 0; i < sa; ++i)
+ ia[i].reset(new int(i));
+ Iter r = std::rotate(Iter(ia), Iter(ia), Iter(ia));
+ assert(base(r) == ia);
+ assert(*ia[0] == 0);
+ r = std::rotate(Iter(ia), Iter(ia), Iter(ia + sa));
+ assert(base(r) == ia + sa);
+ assert(*ia[0] == 0);
+ r = std::rotate(Iter(ia), Iter(ia + sa), Iter(ia + sa));
+ assert(base(r) == ia);
+ assert(*ia[0] == 0);
- std::unique_ptr<int> ib[2];
- const int sb = static_cast<int>(sizeof(ib)/sizeof(ib[0]));
- for (int i = 0; i < sb; ++i)
- ib[i].reset(new int(i));
- r = std::rotate(Iter(ib), Iter(ib), Iter(ib+sb));
- assert(base(r) == ib+sb);
- assert(*ib[0] == 0);
- assert(*ib[1] == 1);
- r = std::rotate(Iter(ib), Iter(ib+1), Iter(ib+sb));
- assert(base(r) == ib+1);
- assert(*ib[0] == 1);
- assert(*ib[1] == 0);
- r = std::rotate(Iter(ib), Iter(ib+sb), Iter(ib+sb));
- assert(base(r) == ib);
- assert(*ib[0] == 1);
- assert(*ib[1] == 0);
+ std::unique_ptr<int> ib[2];
+ const int sb = static_cast<int>(sizeof(ib) / sizeof(ib[0]));
+ for (int i = 0; i < sb; ++i)
+ ib[i].reset(new int(i));
+ r = std::rotate(Iter(ib), Iter(ib), Iter(ib + sb));
+ assert(base(r) == ib + sb);
+ assert(*ib[0] == 0);
+ assert(*ib[1] == 1);
+ r = std::rotate(Iter(ib), Iter(ib + 1), Iter(ib + sb));
+ assert(base(r) == ib + 1);
+ assert(*ib[0] == 1);
+ assert(*ib[1] == 0);
+ r = std::rotate(Iter(ib), Iter(ib + sb), Iter(ib + sb));
+ assert(base(r) == ib);
+ assert(*ib[0] == 1);
+ assert(*ib[1] == 0);
- std::unique_ptr<int> ic[3];
- const int sc = static_cast<int>(sizeof(ic)/sizeof(ic[0]));
- for (int i = 0; i < sc; ++i)
- ic[i].reset(new int(i));
- r = std::rotate(Iter(ic), Iter(ic), Iter(ic+sc));
- assert(base(r) == ic+sc);
- assert(*ic[0] == 0);
- assert(*ic[1] == 1);
- assert(*ic[2] == 2);
- r = std::rotate(Iter(ic), Iter(ic+1), Iter(ic+sc));
- assert(base(r) == ic+2);
- assert(*ic[0] == 1);
- assert(*ic[1] == 2);
- assert(*ic[2] == 0);
- r = std::rotate(Iter(ic), Iter(ic+2), Iter(ic+sc));
- assert(base(r) == ic+1);
- assert(*ic[0] == 0);
- assert(*ic[1] == 1);
- assert(*ic[2] == 2);
- r = std::rotate(Iter(ic), Iter(ic+sc), Iter(ic+sc));
- assert(base(r) == ic);
- assert(*ic[0] == 0);
- assert(*ic[1] == 1);
- assert(*ic[2] == 2);
+ std::unique_ptr<int> ic[3];
+ const int sc = static_cast<int>(sizeof(ic) / sizeof(ic[0]));
+ for (int i = 0; i < sc; ++i)
+ ic[i].reset(new int(i));
+ r = std::rotate(Iter(ic), Iter(ic), Iter(ic + sc));
+ assert(base(r) == ic + sc);
+ assert(*ic[0] == 0);
+ assert(*ic[1] == 1);
+ assert(*ic[2] == 2);
+ r = std::rotate(Iter(ic), Iter(ic + 1), Iter(ic + sc));
+ assert(base(r) == ic + 2);
+ assert(*ic[0] == 1);
+ assert(*ic[1] == 2);
+ assert(*ic[2] == 0);
+ r = std::rotate(Iter(ic), Iter(ic + 2), Iter(ic + sc));
+ assert(base(r) == ic + 1);
+ assert(*ic[0] == 0);
+ assert(*ic[1] == 1);
+ assert(*ic[2] == 2);
+ r = std::rotate(Iter(ic), Iter(ic + sc), Iter(ic + sc));
+ assert(base(r) == ic);
+ assert(*ic[0] == 0);
+ assert(*ic[1] == 1);
+ assert(*ic[2] == 2);
- std::unique_ptr<int> id[4];
- const int sd = static_cast<int>(sizeof(id)/sizeof(id[0]));
- for (int i = 0; i < sd; ++i)
- id[i].reset(new int(i));
- r = std::rotate(Iter(id), Iter(id), Iter(id+sd));
- assert(base(r) == id+sd);
- assert(*id[0] == 0);
- assert(*id[1] == 1);
- assert(*id[2] == 2);
- assert(*id[3] == 3);
- r = std::rotate(Iter(id), Iter(id+1), Iter(id+sd));
- assert(base(r) == id+3);
- assert(*id[0] == 1);
- assert(*id[1] == 2);
- assert(*id[2] == 3);
- assert(*id[3] == 0);
- r = std::rotate(Iter(id), Iter(id+2), Iter(id+sd));
- assert(base(r) == id+2);
- assert(*id[0] == 3);
- assert(*id[1] == 0);
- assert(*id[2] == 1);
- assert(*id[3] == 2);
- r = std::rotate(Iter(id), Iter(id+3), Iter(id+sd));
- assert(base(r) == id+1);
- assert(*id[0] == 2);
- assert(*id[1] == 3);
- assert(*id[2] == 0);
- assert(*id[3] == 1);
- r = std::rotate(Iter(id), Iter(id+sd), Iter(id+sd));
- assert(base(r) == id);
- assert(*id[0] == 2);
- assert(*id[1] == 3);
- assert(*id[2] == 0);
- assert(*id[3] == 1);
+ std::unique_ptr<int> id[4];
+ const int sd = static_cast<int>(sizeof(id) / sizeof(id[0]));
+ for (int i = 0; i < sd; ++i)
+ id[i].reset(new int(i));
+ r = std::rotate(Iter(id), Iter(id), Iter(id + sd));
+ assert(base(r) == id + sd);
+ assert(*id[0] == 0);
+ assert(*id[1] == 1);
+ assert(*id[2] == 2);
+ assert(*id[3] == 3);
+ r = std::rotate(Iter(id), Iter(id + 1), Iter(id + sd));
+ assert(base(r) == id + 3);
+ assert(*id[0] == 1);
+ assert(*id[1] == 2);
+ assert(*id[2] == 3);
+ assert(*id[3] == 0);
+ r = std::rotate(Iter(id), Iter(id + 2), Iter(id + sd));
+ assert(base(r) == id + 2);
+ assert(*id[0] == 3);
+ assert(*id[1] == 0);
+ assert(*id[2] == 1);
+ assert(*id[3] == 2);
+ r = std::rotate(Iter(id), Iter(id + 3), Iter(id + sd));
+ assert(base(r) == id + 1);
+ assert(*id[0] == 2);
+ assert(*id[1] == 3);
+ assert(*id[2] == 0);
+ assert(*id[3] == 1);
+ r = std::rotate(Iter(id), Iter(id + sd), Iter(id + sd));
+ assert(base(r) == id);
+ assert(*id[0] == 2);
+ assert(*id[1] == 3);
+ assert(*id[2] == 0);
+ assert(*id[3] == 1);
- std::unique_ptr<int> ie[5];
- const int se = static_cast<int>(sizeof(ie)/sizeof(ie[0]));
- for (int i = 0; i < se; ++i)
- ie[i].reset(new int(i));
- r = std::rotate(Iter(ie), Iter(ie), Iter(ie+se));
- assert(base(r) == ie+se);
- assert(*ie[0] == 0);
- assert(*ie[1] == 1);
- assert(*ie[2] == 2);
- assert(*ie[3] == 3);
- assert(*ie[4] == 4);
- r = std::rotate(Iter(ie), Iter(ie+1), Iter(ie+se));
- assert(base(r) == ie+4);
- assert(*ie[0] == 1);
- assert(*ie[1] == 2);
- assert(*ie[2] == 3);
- assert(*ie[3] == 4);
- assert(*ie[4] == 0);
- r = std::rotate(Iter(ie), Iter(ie+2), Iter(ie+se));
- assert(base(r) == ie+3);
- assert(*ie[0] == 3);
- assert(*ie[1] == 4);
- assert(*ie[2] == 0);
- assert(*ie[3] == 1);
- assert(*ie[4] == 2);
- r = std::rotate(Iter(ie), Iter(ie+3), Iter(ie+se));
- assert(base(r) == ie+2);
- assert(*ie[0] == 1);
- assert(*ie[1] == 2);
- assert(*ie[2] == 3);
- assert(*ie[3] == 4);
- assert(*ie[4] == 0);
- r = std::rotate(Iter(ie), Iter(ie+4), Iter(ie+se));
- assert(base(r) == ie+1);
- assert(*ie[0] == 0);
- assert(*ie[1] == 1);
- assert(*ie[2] == 2);
- assert(*ie[3] == 3);
- assert(*ie[4] == 4);
- r = std::rotate(Iter(ie), Iter(ie+se), Iter(ie+se));
- assert(base(r) == ie);
- assert(*ie[0] == 0);
- assert(*ie[1] == 1);
- assert(*ie[2] == 2);
- assert(*ie[3] == 3);
- assert(*ie[4] == 4);
+ std::unique_ptr<int> ie[5];
+ const int se = static_cast<int>(sizeof(ie) / sizeof(ie[0]));
+ for (int i = 0; i < se; ++i)
+ ie[i].reset(new int(i));
+ r = std::rotate(Iter(ie), Iter(ie), Iter(ie + se));
+ assert(base(r) == ie + se);
+ assert(*ie[0] == 0);
+ assert(*ie[1] == 1);
+ assert(*ie[2] == 2);
+ assert(*ie[3] == 3);
+ assert(*ie[4] == 4);
+ r = std::rotate(Iter(ie), Iter(ie + 1), Iter(ie + se));
+ assert(base(r) == ie + 4);
+ assert(*ie[0] == 1);
+ assert(*ie[1] == 2);
+ assert(*ie[2] == 3);
+ assert(*ie[3] == 4);
+ assert(*ie[4] == 0);
+ r = std::rotate(Iter(ie), Iter(ie + 2), Iter(ie + se));
+ assert(base(r) == ie + 3);
+ assert(*ie[0] == 3);
+ assert(*ie[1] == 4);
+ assert(*ie[2] == 0);
+ assert(*ie[3] == 1);
+ assert(*ie[4] == 2);
+ r = std::rotate(Iter(ie), Iter(ie + 3), Iter(ie + se));
+ assert(base(r) == ie + 2);
+ assert(*ie[0] == 1);
+ assert(*ie[1] == 2);
+ assert(*ie[2] == 3);
+ assert(*ie[3] == 4);
+ assert(*ie[4] == 0);
+ r = std::rotate(Iter(ie), Iter(ie + 4), Iter(ie + se));
+ assert(base(r) == ie + 1);
+ assert(*ie[0] == 0);
+ assert(*ie[1] == 1);
+ assert(*ie[2] == 2);
+ assert(*ie[3] == 3);
+ assert(*ie[4] == 4);
+ r = std::rotate(Iter(ie), Iter(ie + se), Iter(ie + se));
+ assert(base(r) == ie);
+ assert(*ie[0] == 0);
+ assert(*ie[1] == 1);
+ assert(*ie[2] == 2);
+ assert(*ie[3] == 3);
+ assert(*ie[4] == 4);
- std::unique_ptr<int> ig[6];
- const int sg = static_cast<int>(sizeof(ig)/sizeof(ig[0]));
- for (int i = 0; i < sg; ++i)
- ig[i].reset(new int(i));
- r = std::rotate(Iter(ig), Iter(ig), Iter(ig+sg));
- assert(base(r) == ig+sg);
- assert(*ig[0] == 0);
- assert(*ig[1] == 1);
- assert(*ig[2] == 2);
- assert(*ig[3] == 3);
- assert(*ig[4] == 4);
- assert(*ig[5] == 5);
- r = std::rotate(Iter(ig), Iter(ig+1), Iter(ig+sg));
- assert(base(r) == ig+5);
- assert(*ig[0] == 1);
- assert(*ig[1] == 2);
- assert(*ig[2] == 3);
- assert(*ig[3] == 4);
- assert(*ig[4] == 5);
- assert(*ig[5] == 0);
- r = std::rotate(Iter(ig), Iter(ig+2), Iter(ig+sg));
- assert(base(r) == ig+4);
- assert(*ig[0] == 3);
- assert(*ig[1] == 4);
- assert(*ig[2] == 5);
- assert(*ig[3] == 0);
- assert(*ig[4] == 1);
- assert(*ig[5] == 2);
- r = std::rotate(Iter(ig), Iter(ig+3), Iter(ig+sg));
- assert(base(r) == ig+3);
- assert(*ig[0] == 0);
- assert(*ig[1] == 1);
- assert(*ig[2] == 2);
- assert(*ig[3] == 3);
- assert(*ig[4] == 4);
- assert(*ig[5] == 5);
- r = std::rotate(Iter(ig), Iter(ig+4), Iter(ig+sg));
- assert(base(r) == ig+2);
- assert(*ig[0] == 4);
- assert(*ig[1] == 5);
- assert(*ig[2] == 0);
- assert(*ig[3] == 1);
- assert(*ig[4] == 2);
- assert(*ig[5] == 3);
- r = std::rotate(Iter(ig), Iter(ig+5), Iter(ig+sg));
- assert(base(r) == ig+1);
- assert(*ig[0] == 3);
- assert(*ig[1] == 4);
- assert(*ig[2] == 5);
- assert(*ig[3] == 0);
- assert(*ig[4] == 1);
- assert(*ig[5] == 2);
- r = std::rotate(Iter(ig), Iter(ig+sg), Iter(ig+sg));
- assert(base(r) == ig);
- assert(*ig[0] == 3);
- assert(*ig[1] == 4);
- assert(*ig[2] == 5);
- assert(*ig[3] == 0);
- assert(*ig[4] == 1);
- assert(*ig[5] == 2);
+ std::unique_ptr<int> ig[6];
+ const int sg = static_cast<int>(sizeof(ig) / sizeof(ig[0]));
+ for (int i = 0; i < sg; ++i)
+ ig[i].reset(new int(i));
+ r = std::rotate(Iter(ig), Iter(ig), Iter(ig + sg));
+ assert(base(r) == ig + sg);
+ assert(*ig[0] == 0);
+ assert(*ig[1] == 1);
+ assert(*ig[2] == 2);
+ assert(*ig[3] == 3);
+ assert(*ig[4] == 4);
+ assert(*ig[5] == 5);
+ r = std::rotate(Iter(ig), Iter(ig + 1), Iter(ig + sg));
+ assert(base(r) == ig + 5);
+ assert(*ig[0] == 1);
+ assert(*ig[1] == 2);
+ assert(*ig[2] == 3);
+ assert(*ig[3] == 4);
+ assert(*ig[4] == 5);
+ assert(*ig[5] == 0);
+ r = std::rotate(Iter(ig), Iter(ig + 2), Iter(ig + sg));
+ assert(base(r) == ig + 4);
+ assert(*ig[0] == 3);
+ assert(*ig[1] == 4);
+ assert(*ig[2] == 5);
+ assert(*ig[3] == 0);
+ assert(*ig[4] == 1);
+ assert(*ig[5] == 2);
+ r = std::rotate(Iter(ig), Iter(ig + 3), Iter(ig + sg));
+ assert(base(r) == ig + 3);
+ assert(*ig[0] == 0);
+ assert(*ig[1] == 1);
+ assert(*ig[2] == 2);
+ assert(*ig[3] == 3);
+ assert(*ig[4] == 4);
+ assert(*ig[5] == 5);
+ r = std::rotate(Iter(ig), Iter(ig + 4), Iter(ig + sg));
+ assert(base(r) == ig + 2);
+ assert(*ig[0] == 4);
+ assert(*ig[1] == 5);
+ assert(*ig[2] == 0);
+ assert(*ig[3] == 1);
+ assert(*ig[4] == 2);
+ assert(*ig[5] == 3);
+ r = std::rotate(Iter(ig), Iter(ig + 5), Iter(ig + sg));
+ assert(base(r) == ig + 1);
+ assert(*ig[0] == 3);
+ assert(*ig[1] == 4);
+ assert(*ig[2] == 5);
+ assert(*ig[3] == 0);
+ assert(*ig[4] == 1);
+ assert(*ig[5] == 2);
+ r = std::rotate(Iter(ig), Iter(ig + sg), Iter(ig + sg));
+ assert(base(r) == ig);
+ assert(*ig[0] == 3);
+ assert(*ig[1] == 4);
+ assert(*ig[2] == 5);
+ assert(*ig[3] == 0);
+ assert(*ig[4] == 1);
+ assert(*ig[5] == 2);
}
#endif // TEST_STD_VER >= 11
-int main(int, char**)
-{
- test<forward_iterator<int*> >();
- test<bidirectional_iterator<int*> >();
- test<random_access_iterator<int*> >();
- test<int*>();
+template <std::size_t N>
+TEST_CONSTEXPR_CXX20 bool test_bit_iterator() {
+ for (int offset = -1; offset <= 1; ++offset) {
+ std::vector<bool> a(N, false);
+ std::size_t mid = N / 2 + offset;
+ for (std::size_t i = mid; i < N; ++i)
+ a[i] = true;
+ std::rotate(a.begin(), a.begin() + mid, a.end());
+ for (std::size_t i = 0; i < N; ++i)
+ assert(a[i] == (i < N - mid));
+ }
+ return true;
+};
+
+int main(int, char**) {
+ test<forward_iterator<int*> >();
+ test<bidirectional_iterator<int*> >();
+ test<random_access_iterator<int*> >();
+ test<int*>();
+
+ test_bit_iterator<8>();
+ test_bit_iterator<16>();
+ test_bit_iterator<64>();
+ test_bit_iterator<100>();
#if TEST_STD_VER >= 11
- test1<forward_iterator<std::unique_ptr<int>*> >();
- test1<bidirectional_iterator<std::unique_ptr<int>*> >();
- test1<random_access_iterator<std::unique_ptr<int>*> >();
- test1<std::unique_ptr<int>*>();
+ test1<forward_iterator<std::unique_ptr<int>*> >();
+ test1<bidirectional_iterator<std::unique_ptr<int>*> >();
+ test1<random_access_iterator<std::unique_ptr<int>*> >();
+ test1<std::unique_ptr<int>*>();
#endif
#if TEST_STD_VER > 17
- static_assert(test<forward_iterator<int*> >());
- static_assert(test<bidirectional_iterator<int*> >());
- static_assert(test<random_access_iterator<int*> >());
- static_assert(test<int*>());
+ static_assert(test<forward_iterator<int*> >());
+ static_assert(test<bidirectional_iterator<int*> >());
+ static_assert(test<random_access_iterator<int*> >());
+ static_assert(test<int*>());
+
+ static_assert(test_bit_iterator<8>());
+ static_assert(test_bit_iterator<16>());
+ static_assert(test_bit_iterator<64>());
+ static_assert(test_bit_iterator<100>());
#endif // TEST_STD_VER > 17
return 0;
More information about the libcxx-commits
mailing list