mirror of
https://github.com/llvm/llvm-project.git
synced 2025-04-17 18:16:42 +00:00
[libc++] Optimize ranges::move{,_backward} for vector<bool>::iterator (#121109)
As a follow-up to #121013 (which optimized `ranges::copy`) and #121026 (which optimized `ranges::copy_backward`), this PR enhances the performance of `std::ranges::{move, move_backward}` for `vector<bool>::iterator`, addressing a subtask outlined in issue #64038. The optimizations bring performance improvements analogous to those achieved for the `{copy, copy_backward}` algorithms: up to 2000x for aligned moves and 60x for unaligned moves. Moreover, comprehensive tests covering up to 4 storage words (256 bytes) with odd and even bit sizes are provided, which validate the proposed optimizations in this patch.
This commit is contained in:
parent
70e693c77f
commit
ab3d793982
@ -43,8 +43,8 @@ Implemented Papers
|
||||
Improvements and New Features
|
||||
-----------------------------
|
||||
|
||||
- The ``std::ranges::{copy, copy_n, copy_backward}`` algorithms have been optimized for ``std::vector<bool>::iterator``\s,
|
||||
resulting in a performance improvement of up to 2000x.
|
||||
- The ``std::ranges::{copy, copy_n, copy_backward, move, move_backward}`` algorithms have been optimized for
|
||||
``std::vector<bool>::iterator``, resulting in a performance improvement of up to 2000x.
|
||||
|
||||
- Updated formatting library to Unicode 16.0.0.
|
||||
|
||||
|
@ -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*>
|
||||
|
@ -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*>
|
||||
|
@ -210,22 +210,6 @@ private:
|
||||
__mask_(__m) {}
|
||||
};
|
||||
|
||||
// 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>
|
||||
|
71
libcxx/test/benchmarks/algorithms/move.bench.cpp
Normal file
71
libcxx/test/benchmarks/algorithms/move.bench.cpp
Normal file
@ -0,0 +1,71 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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 <ranges>
|
||||
#include <vector>
|
||||
|
||||
template <bool aligned>
|
||||
void bm_ranges_move_vb(benchmark::State& state) {
|
||||
auto n = state.range();
|
||||
std::vector<bool> v1(n, true);
|
||||
std::vector<bool> v2(n, false);
|
||||
benchmark::DoNotOptimize(v1);
|
||||
benchmark::DoNotOptimize(v2);
|
||||
std::vector<bool>* in = &v1;
|
||||
std::vector<bool>* out = &v2;
|
||||
for (auto _ : state) {
|
||||
if constexpr (aligned) {
|
||||
benchmark::DoNotOptimize(std::ranges::move(*in, std::ranges::begin(*out)));
|
||||
} else {
|
||||
benchmark::DoNotOptimize(
|
||||
std::ranges::move(std::views::counted(in->begin() + 4, n - 4), std::ranges::begin(*out)));
|
||||
}
|
||||
std::swap(in, out);
|
||||
benchmark::DoNotOptimize(in);
|
||||
benchmark::DoNotOptimize(out);
|
||||
}
|
||||
}
|
||||
|
||||
template <bool aligned>
|
||||
void bm_move_vb(benchmark::State& state) {
|
||||
auto n = state.range();
|
||||
std::vector<bool> v1(n, true);
|
||||
std::vector<bool> v2(n, false);
|
||||
benchmark::DoNotOptimize(v1);
|
||||
benchmark::DoNotOptimize(v2);
|
||||
std::vector<bool>* in = &v1;
|
||||
std::vector<bool>* out = &v2;
|
||||
for (auto _ : state) {
|
||||
auto first1 = in->begin();
|
||||
auto last1 = in->end();
|
||||
auto first2 = out->begin();
|
||||
if constexpr (aligned) {
|
||||
benchmark::DoNotOptimize(std::move(first1, last1, first2));
|
||||
} else {
|
||||
benchmark::DoNotOptimize(std::move(first1 + 4, last1, first2));
|
||||
}
|
||||
std::swap(in, out);
|
||||
benchmark::DoNotOptimize(in);
|
||||
benchmark::DoNotOptimize(out);
|
||||
}
|
||||
}
|
||||
|
||||
BENCHMARK(bm_ranges_move_vb<true>)
|
||||
->Name("bm_ranges_move_vb_aligned")
|
||||
->Range(8, 1 << 16)
|
||||
->DenseRange(102400, 204800, 4096);
|
||||
BENCHMARK(bm_ranges_move_vb<false>)->Name("bm_ranges_move_vb_unaligned")->Range(8, 1 << 20);
|
||||
|
||||
BENCHMARK(bm_move_vb<true>)->Name("bm_move_vb_aligned")->Range(8, 1 << 20);
|
||||
BENCHMARK(bm_move_vb<false>)->Name("bm_move_vb_unaligned")->Range(8, 1 << 20);
|
||||
|
||||
BENCHMARK_MAIN();
|
71
libcxx/test/benchmarks/algorithms/move_backward.bench.cpp
Normal file
71
libcxx/test/benchmarks/algorithms/move_backward.bench.cpp
Normal file
@ -0,0 +1,71 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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 <ranges>
|
||||
#include <vector>
|
||||
|
||||
template <bool aligned>
|
||||
void bm_ranges_move_backward_vb(benchmark::State& state) {
|
||||
auto n = state.range();
|
||||
std::vector<bool> v1(n, true);
|
||||
std::vector<bool> v2(n, false);
|
||||
benchmark::DoNotOptimize(v1);
|
||||
benchmark::DoNotOptimize(v2);
|
||||
std::vector<bool>* in = &v1;
|
||||
std::vector<bool>* out = &v2;
|
||||
for (auto _ : state) {
|
||||
if constexpr (aligned) {
|
||||
benchmark::DoNotOptimize(std::ranges::move_backward(*in, std::ranges::end(*out)));
|
||||
} else {
|
||||
benchmark::DoNotOptimize(
|
||||
std::ranges::move_backward(std::views::counted(in->begin(), n - 4), std::ranges::end(*out)));
|
||||
}
|
||||
std::swap(in, out);
|
||||
benchmark::DoNotOptimize(in);
|
||||
benchmark::DoNotOptimize(out);
|
||||
}
|
||||
}
|
||||
|
||||
template <bool aligned>
|
||||
void bm_move_backward_vb(benchmark::State& state) {
|
||||
auto n = state.range();
|
||||
std::vector<bool> v1(n, true);
|
||||
std::vector<bool> v2(n, false);
|
||||
benchmark::DoNotOptimize(v1);
|
||||
benchmark::DoNotOptimize(v2);
|
||||
std::vector<bool>* in = &v1;
|
||||
std::vector<bool>* out = &v2;
|
||||
for (auto _ : state) {
|
||||
auto first1 = in->begin();
|
||||
auto last1 = in->end();
|
||||
auto last2 = out->end();
|
||||
if constexpr (aligned) {
|
||||
benchmark::DoNotOptimize(std::move_backward(first1, last1, last2));
|
||||
} else {
|
||||
benchmark::DoNotOptimize(std::move_backward(first1, last1 - 4, last2));
|
||||
}
|
||||
std::swap(in, out);
|
||||
benchmark::DoNotOptimize(in);
|
||||
benchmark::DoNotOptimize(out);
|
||||
}
|
||||
}
|
||||
|
||||
BENCHMARK(bm_ranges_move_backward_vb<true>)
|
||||
->Name("bm_ranges_move_backward_vb_aligned")
|
||||
->Range(8, 1 << 16)
|
||||
->DenseRange(102400, 204800, 4096);
|
||||
BENCHMARK(bm_ranges_move_backward_vb<false>)->Name("bm_ranges_move_backward_vb_unaligned")->Range(8, 1 << 20);
|
||||
|
||||
BENCHMARK(bm_move_backward_vb<true>)->Name("bm_move_backward_vb_aligned")->Range(8, 1 << 20);
|
||||
BENCHMARK(bm_move_backward_vb<false>)->Name("bm_move_backward_vb_unaligned")->Range(8, 1 << 20);
|
||||
|
||||
BENCHMARK_MAIN();
|
@ -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 {
|
||||
}
|
||||
};
|
||||
|
||||
TEST_CONSTEXPR_CXX20 bool test_vector_bool(std::size_t N) {
|
||||
std::vector<bool> v(N, false);
|
||||
for (std::size_t i = 0; i < N; i += 2)
|
||||
v[i] = true;
|
||||
|
||||
{ // Test move with aligned bytes
|
||||
std::vector<bool> in(v);
|
||||
std::vector<bool> out(N);
|
||||
std::move(in.begin(), in.end(), out.begin());
|
||||
assert(out == v);
|
||||
}
|
||||
{ // Test move with unaligned bytes
|
||||
std::vector<bool> in(v);
|
||||
std::vector<bool> out(N);
|
||||
std::move(in.begin() + 4, in.end(), out.begin());
|
||||
for (std::size_t i = 0; i < N - 4; ++i)
|
||||
assert(v[i + 4] == out[i]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
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,16 @@ TEST_CONSTEXPR_CXX20 bool test() {
|
||||
}
|
||||
}
|
||||
|
||||
{ // Test vector<bool>::iterator optimization
|
||||
assert(test_vector_bool(8));
|
||||
assert(test_vector_bool(19));
|
||||
assert(test_vector_bool(32));
|
||||
assert(test_vector_bool(49));
|
||||
assert(test_vector_bool(64));
|
||||
assert(test_vector_bool(199));
|
||||
assert(test_vector_bool(256));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -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,24 +71,46 @@ 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>());
|
||||
}
|
||||
};
|
||||
|
||||
TEST_CONSTEXPR_CXX20 bool test_vector_bool(std::size_t N) {
|
||||
std::vector<bool> v(N, false);
|
||||
for (std::size_t i = 0; i < N; i += 2)
|
||||
v[i] = true;
|
||||
|
||||
{ // Test move_backward with aligned bytes
|
||||
std::vector<bool> in(v);
|
||||
std::vector<bool> out(N);
|
||||
std::move_backward(in.begin(), in.end(), out.end());
|
||||
assert(out == v);
|
||||
}
|
||||
{ // Test move_backward with unaligned bytes
|
||||
std::vector<bool> in(v);
|
||||
std::vector<bool> out(N);
|
||||
std::move_backward(in.begin(), in.end() - 4, out.end());
|
||||
for (std::size_t i = 0; i < N - 4; ++i)
|
||||
assert(out[i + 4] == v[i]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
TEST_CONSTEXPR_CXX20 bool test() {
|
||||
types::for_each(types::bidirectional_iterator_list<int*>(), TestOutIters());
|
||||
if (TEST_STD_AT_LEAST_23_OR_RUNTIME_EVALUATED)
|
||||
@ -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,20 @@ TEST_CONSTEXPR_CXX20 bool test() {
|
||||
}
|
||||
}
|
||||
|
||||
{ // Test vector<bool>::iterator optimization
|
||||
assert(test_vector_bool(8));
|
||||
assert(test_vector_bool(19));
|
||||
assert(test_vector_bool(32));
|
||||
assert(test_vector_bool(49));
|
||||
assert(test_vector_bool(64));
|
||||
assert(test_vector_bool(199));
|
||||
assert(test_vector_bool(256));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int, char**)
|
||||
{
|
||||
int main(int, char**) {
|
||||
test();
|
||||
#if TEST_STD_VER >= 20
|
||||
static_assert(test());
|
||||
|
@ -31,6 +31,7 @@
|
||||
#include "almost_satisfies_types.h"
|
||||
#include "MoveOnly.h"
|
||||
#include "test_iterators.h"
|
||||
#include "test_macros.h"
|
||||
|
||||
template <class In, class Out = In, class Sent = sentinel_wrapper<In>>
|
||||
concept HasMoveIt = requires(In in, Sent sent, Out out) { std::ranges::move(in, sent, out); };
|
||||
@ -65,7 +66,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 +74,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 +84,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 +165,52 @@ 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;
|
||||
};
|
||||
|
||||
#if TEST_STD_VER >= 23
|
||||
constexpr bool test_vector_bool(std::size_t N) {
|
||||
std::vector<bool> v(N, false);
|
||||
for (std::size_t i = 0; i < N; i += 2)
|
||||
v[i] = true;
|
||||
|
||||
{ // Test move with aligned bytes
|
||||
std::vector<bool> in{v};
|
||||
std::vector<bool> out(N);
|
||||
std::ranges::move(in, out.begin());
|
||||
assert(out == v);
|
||||
}
|
||||
{ // Test move with unaligned bytes
|
||||
std::vector<bool> in{v};
|
||||
std::vector<bool> out(N);
|
||||
std::ranges::move(std::views::counted(in.begin() + 4, N - 4), out.begin());
|
||||
assert(std::ranges::equal(v | std::views::drop(4), out | std::views::take(N - 4)));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
// cpp17_intput_iterator has a defaulted template argument
|
||||
template <class Iter>
|
||||
using Cpp17InIter = cpp17_input_iterator<Iter>;
|
||||
@ -267,13 +297,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 +314,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 +324,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 +344,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 +354,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 +367,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 +388,31 @@ 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}));
|
||||
}
|
||||
}
|
||||
|
||||
#if TEST_STD_VER >= 23
|
||||
{ // Test vector<bool>::iterator optimization
|
||||
assert(test_vector_bool(8));
|
||||
assert(test_vector_bool(19));
|
||||
assert(test_vector_bool(32));
|
||||
assert(test_vector_bool(49));
|
||||
assert(test_vector_bool(64));
|
||||
assert(test_vector_bool(199));
|
||||
assert(test_vector_bool(256));
|
||||
}
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -31,6 +31,7 @@
|
||||
#include "almost_satisfies_types.h"
|
||||
#include "MoveOnly.h"
|
||||
#include "test_iterators.h"
|
||||
#include "test_macros.h"
|
||||
|
||||
template <class In, class Out = In, class Sent = sentinel_wrapper<In>>
|
||||
concept HasMoveBackwardIt = requires(In in, Sent sent, Out out) { std::ranges::move_backward(in, sent, out); };
|
||||
@ -65,7 +66,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 +93,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 +160,62 @@ 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;
|
||||
};
|
||||
|
||||
#if TEST_STD_VER >= 23
|
||||
constexpr bool test_vector_bool(std::size_t N) {
|
||||
std::vector<bool> v(N, false);
|
||||
for (std::size_t i = 0; i < N; i += 2)
|
||||
v[i] = true;
|
||||
|
||||
{ // Test move_backward with aligned bytes
|
||||
std::vector<bool> in{v};
|
||||
std::vector<bool> out(N);
|
||||
std::ranges::move_backward(in, out.end());
|
||||
assert(out == v);
|
||||
}
|
||||
{ // Test move_backward with unaligned bytes
|
||||
std::vector<bool> in{v};
|
||||
std::vector<bool> out(N);
|
||||
std::ranges::move_backward(std::views::counted(in.begin(), N - 4), out.end());
|
||||
assert(std::ranges::equal(v | std::views::take(N - 4), out | std::views::drop(4)));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
constexpr bool test() {
|
||||
test_in_iterators<bidirectional_iterator>();
|
||||
test_in_iterators<random_access_iterator>();
|
||||
@ -243,7 +281,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 +292,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 +309,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 +319,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 +339,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 +349,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 +362,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 +383,31 @@ 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}));
|
||||
}
|
||||
}
|
||||
|
||||
#if TEST_STD_VER >= 23
|
||||
{ // Test vector<bool>::iterator optimization
|
||||
assert(test_vector_bool(8));
|
||||
assert(test_vector_bool(19));
|
||||
assert(test_vector_bool(32));
|
||||
assert(test_vector_bool(49));
|
||||
assert(test_vector_bool(64));
|
||||
assert(test_vector_bool(199));
|
||||
assert(test_vector_bool(256));
|
||||
}
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user