1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2025-02-04 21:33:56 +00:00

fix rotate_copy

This commit is contained in:
Mitsuru Kariya 2015-04-08 00:24:12 +09:00
parent befe29adf0
commit edc41403f6
3 changed files with 119 additions and 31 deletions

View file

@ -24,21 +24,21 @@ namespace testspr {
{ {
SPROUT_STATIC_CONSTEXPR auto rotated = sprout::rotate( SPROUT_STATIC_CONSTEXPR auto rotated = sprout::rotate(
arr1, arr1,
sprout::begin(arr1) + 5 sprout::begin(arr1) + 4
); );
TESTSPR_BOTH_ASSERT(testspr::equal( TESTSPR_BOTH_ASSERT(testspr::equal(
rotated, rotated,
array<int, 10>{{6, 7, 8, 9, 10, 1, 2, 3, 4, 5}} array<int, 10>{{5, 6, 7, 8, 9, 10, 1, 2, 3, 4}}
)); ));
} }
{ {
SPROUT_STATIC_CONSTEXPR auto rotated = sprout::fit::rotate( SPROUT_STATIC_CONSTEXPR auto rotated = sprout::fit::rotate(
arr1, arr1,
sprout::begin(arr1) + 5 sprout::begin(arr1) + 4
); );
TESTSPR_BOTH_ASSERT(testspr::equal( TESTSPR_BOTH_ASSERT(testspr::equal(
rotated, rotated,
array<int, 10>{{6, 7, 8, 9, 10, 1, 2, 3, 4, 5}} array<int, 10>{{5, 6, 7, 8, 9, 10, 1, 2, 3, 4}}
)); ));
} }
// rotate // rotate
@ -46,29 +46,29 @@ namespace testspr {
{ {
SPROUT_STATIC_CONSTEXPR auto rotated = sprout::rotate( SPROUT_STATIC_CONSTEXPR auto rotated = sprout::rotate(
sprout::sub(arr1, 2, 8), sprout::sub(arr1, 2, 8),
sprout::begin(arr1) + 5 sprout::begin(arr1) + 4
); );
TESTSPR_BOTH_ASSERT(testspr::equal( TESTSPR_BOTH_ASSERT(testspr::equal(
rotated, rotated,
array<int, 6>{{6, 7, 8, 3, 4, 5}} array<int, 6>{{5, 6, 7, 8, 3, 4}}
)); ));
TESTSPR_BOTH_ASSERT(testspr::equal( TESTSPR_BOTH_ASSERT(testspr::equal(
sprout::get_internal(rotated), sprout::get_internal(rotated),
array<int, 10>{{1, 2, 6, 7, 8, 3, 4, 5, 9, 10}} array<int, 10>{{1, 2, 5, 6, 7, 8, 3, 4, 9, 10}}
)); ));
} }
{ {
SPROUT_STATIC_CONSTEXPR auto rotated = sprout::fit::rotate( SPROUT_STATIC_CONSTEXPR auto rotated = sprout::fit::rotate(
sprout::sub(arr1, 2, 8), sprout::sub(arr1, 2, 8),
sprout::begin(arr1) + 5 sprout::begin(arr1) + 4
); );
TESTSPR_BOTH_ASSERT(testspr::equal( TESTSPR_BOTH_ASSERT(testspr::equal(
rotated, rotated,
array<int, 6>{{6, 7, 8, 3, 4, 5}} array<int, 6>{{5, 6, 7, 8, 3, 4}}
)); ));
TESTSPR_BOTH_ASSERT(testspr::equal( TESTSPR_BOTH_ASSERT(testspr::equal(
sprout::get_internal(rotated), sprout::get_internal(rotated),
array<int, 10>{{1, 2, 6, 7, 8, 3, 4, 5, 9, 10}} array<int, 10>{{1, 2, 5, 6, 7, 8, 3, 4, 9, 10}}
)); ));
} }
} }

View file

@ -26,25 +26,25 @@ namespace testspr {
{ {
SPROUT_STATIC_CONSTEXPR auto rotated = sprout::rotate_copy( SPROUT_STATIC_CONSTEXPR auto rotated = sprout::rotate_copy(
sprout::begin(arr1) + 2, sprout::begin(arr1) + 2,
sprout::begin(arr1) + 5, sprout::begin(arr1) + 4,
sprout::begin(arr1) + 8, sprout::begin(arr1) + 8,
arr2 arr2
); );
TESTSPR_BOTH_ASSERT(testspr::equal( TESTSPR_BOTH_ASSERT(testspr::equal(
rotated, rotated,
array<int, 10>{{6, 7, 8, 3, 4, 5, 0, 0, 0, 0}} array<int, 10>{{5, 6, 7, 8, 3, 4, 0, 0, 0, 0}}
)); ));
} }
{ {
SPROUT_STATIC_CONSTEXPR auto rotated = sprout::fit::rotate_copy( SPROUT_STATIC_CONSTEXPR auto rotated = sprout::fit::rotate_copy(
sprout::begin(arr1) + 2, sprout::begin(arr1) + 2,
sprout::begin(arr1) + 5, sprout::begin(arr1) + 4,
sprout::begin(arr1) + 8, sprout::begin(arr1) + 8,
arr2 arr2
); );
TESTSPR_BOTH_ASSERT(testspr::equal( TESTSPR_BOTH_ASSERT(testspr::equal(
rotated, rotated,
array<int, 6>{{6, 7, 8, 3, 4, 5}} array<int, 6>{{5, 6, 7, 8, 3, 4}}
)); ));
} }
// rotate in range [2 .. 8) // rotate in range [2 .. 8)
@ -52,25 +52,25 @@ namespace testspr {
{ {
SPROUT_STATIC_CONSTEXPR auto rotated = sprout::rotate_copy( SPROUT_STATIC_CONSTEXPR auto rotated = sprout::rotate_copy(
sprout::begin(arr1) + 2, sprout::begin(arr1) + 2,
sprout::begin(arr1) + 5, sprout::begin(arr1) + 4,
sprout::begin(arr1) + 8, sprout::begin(arr1) + 7,
arr3 arr3
); );
TESTSPR_BOTH_ASSERT(testspr::equal( TESTSPR_BOTH_ASSERT(testspr::equal(
rotated, rotated,
array<int, 4>{{6, 7, 8, 3}} array<int, 4>{{5, 6, 7, 3}}
)); ));
} }
{ {
SPROUT_STATIC_CONSTEXPR auto rotated = sprout::fit::rotate_copy( SPROUT_STATIC_CONSTEXPR auto rotated = sprout::fit::rotate_copy(
sprout::begin(arr1) + 2, sprout::begin(arr1) + 2,
sprout::begin(arr1) + 5, sprout::begin(arr1) + 4,
sprout::begin(arr1) + 8, sprout::begin(arr1) + 7,
arr3 arr3
); );
TESTSPR_BOTH_ASSERT(testspr::equal( TESTSPR_BOTH_ASSERT(testspr::equal(
rotated, rotated,
array<int, 4>{{6, 7, 8, 3}} array<int, 4>{{5, 6, 7, 3}}
)); ));
} }
// rotate in range [2 .. 8) // rotate in range [2 .. 8)
@ -78,33 +78,119 @@ namespace testspr {
{ {
SPROUT_STATIC_CONSTEXPR auto rotated = sprout::rotate_copy( SPROUT_STATIC_CONSTEXPR auto rotated = sprout::rotate_copy(
sprout::begin(arr1) + 2, sprout::begin(arr1) + 2,
sprout::begin(arr1) + 5, sprout::begin(arr1) + 4,
sprout::begin(arr1) + 8, sprout::begin(arr1) + 8,
sprout::sub(arr2, 2, 8) sprout::sub(arr2, 2, 8)
); );
TESTSPR_BOTH_ASSERT(testspr::equal( TESTSPR_BOTH_ASSERT(testspr::equal(
rotated, rotated,
array<int, 6>{{6, 7, 8, 3, 4, 5}} array<int, 6>{{5, 6, 7, 8, 3, 4}}
)); ));
TESTSPR_BOTH_ASSERT(testspr::equal( TESTSPR_BOTH_ASSERT(testspr::equal(
sprout::get_internal(rotated), sprout::get_internal(rotated),
array<int, 10>{{0, 0, 6, 7, 8, 3, 4, 5, 0, 0}} array<int, 10>{{0, 0, 5, 6, 7, 8, 3, 4, 0, 0}}
)); ));
} }
{ {
SPROUT_STATIC_CONSTEXPR auto rotated = sprout::fit::rotate_copy( SPROUT_STATIC_CONSTEXPR auto rotated = sprout::fit::rotate_copy(
sprout::begin(arr1) + 2, sprout::begin(arr1) + 2,
sprout::begin(arr1) + 5, sprout::begin(arr1) + 4,
sprout::begin(arr1) + 8, sprout::begin(arr1) + 8,
sprout::sub(arr2, 2, 8) sprout::sub(arr2, 2, 8)
); );
TESTSPR_BOTH_ASSERT(testspr::equal( TESTSPR_BOTH_ASSERT(testspr::equal(
rotated, rotated,
array<int, 6>{{6, 7, 8, 3, 4, 5}} array<int, 6>{{5, 6, 7, 8, 3, 4}}
)); ));
TESTSPR_BOTH_ASSERT(testspr::equal( TESTSPR_BOTH_ASSERT(testspr::equal(
sprout::get_internal(rotated), sprout::get_internal(rotated),
array<int, 10>{{0, 0, 6, 7, 8, 3, 4, 5, 0, 0}} array<int, 10>{{0, 0, 5, 6, 7, 8, 3, 4, 0, 0}}
));
}
// rotate in range [2 .. 8)
{
SPROUT_STATIC_CONSTEXPR auto rotated = sprout::rotate_copy(
testspr::reduct_forward(sprout::begin(arr1) + 2),
testspr::reduct_forward(sprout::begin(arr1) + 4),
testspr::reduct_forward(sprout::begin(arr1) + 8),
arr2
);
TESTSPR_BOTH_ASSERT(testspr::equal(
rotated,
array<int, 10>{{5, 6, 7, 8, 3, 4, 0, 0, 0, 0}}
));
}
{
SPROUT_STATIC_CONSTEXPR auto rotated = sprout::fit::rotate_copy(
testspr::reduct_forward(sprout::begin(arr1) + 2),
testspr::reduct_forward(sprout::begin(arr1) + 4),
testspr::reduct_forward(sprout::begin(arr1) + 8),
arr2
);
TESTSPR_BOTH_ASSERT(testspr::equal(
rotated,
array<int, 6>{{5, 6, 7, 8, 3, 4}}
));
}
// rotate in range [2 .. 8)
// overrun from output range
{
SPROUT_STATIC_CONSTEXPR auto rotated = sprout::rotate_copy(
testspr::reduct_forward(sprout::begin(arr1) + 2),
testspr::reduct_forward(sprout::begin(arr1) + 4),
testspr::reduct_forward(sprout::begin(arr1) + 7),
arr3
);
TESTSPR_BOTH_ASSERT(testspr::equal(
rotated,
array<int, 4>{{5, 6, 7, 3}}
));
}
{
SPROUT_STATIC_CONSTEXPR auto rotated = sprout::fit::rotate_copy(
testspr::reduct_forward(sprout::begin(arr1) + 2),
testspr::reduct_forward(sprout::begin(arr1) + 4),
testspr::reduct_forward(sprout::begin(arr1) + 7),
arr3
);
TESTSPR_BOTH_ASSERT(testspr::equal(
rotated,
array<int, 4>{{5, 6, 7, 3}}
));
}
// rotate in range [2 .. 8)
// to sub range
{
SPROUT_STATIC_CONSTEXPR auto rotated = sprout::rotate_copy(
testspr::reduct_forward(sprout::begin(arr1) + 2),
testspr::reduct_forward(sprout::begin(arr1) + 4),
testspr::reduct_forward(sprout::begin(arr1) + 8),
sprout::sub(arr2, 2, 8)
);
TESTSPR_BOTH_ASSERT(testspr::equal(
rotated,
array<int, 6>{{5, 6, 7, 8, 3, 4}}
));
TESTSPR_BOTH_ASSERT(testspr::equal(
sprout::get_internal(rotated),
array<int, 10>{{0, 0, 5, 6, 7, 8, 3, 4, 0, 0}}
));
}
{
SPROUT_STATIC_CONSTEXPR auto rotated = sprout::fit::rotate_copy(
testspr::reduct_forward(sprout::begin(arr1) + 2),
testspr::reduct_forward(sprout::begin(arr1) + 4),
testspr::reduct_forward(sprout::begin(arr1) + 8),
sprout::sub(arr2, 2, 8)
);
TESTSPR_BOTH_ASSERT(testspr::equal(
rotated,
array<int, 6>{{5, 6, 7, 8, 3, 4}}
));
TESTSPR_BOTH_ASSERT(testspr::equal(
sprout::get_internal(rotated),
array<int, 10>{{0, 0, 5, 6, 7, 8, 3, 4, 0, 0}}
)); ));
} }
} }

View file

@ -29,7 +29,8 @@ namespace sprout {
template<typename RandomAccessIterator, typename Result, sprout::index_t... Indexes> template<typename RandomAccessIterator, typename Result, sprout::index_t... Indexes>
inline SPROUT_CONSTEXPR typename sprout::fixed::results::algorithm<Result>::type inline SPROUT_CONSTEXPR typename sprout::fixed::results::algorithm<Result>::type
rotate_copy_impl_ra( rotate_copy_impl_ra(
RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, RandomAccessIterator first, RandomAccessIterator middle,
typename sprout::container_traits<Result>::size_type last_half_size,
Result const& result, Result const& result,
sprout::index_tuple<Indexes...>, sprout::index_tuple<Indexes...>,
typename sprout::container_traits<Result>::difference_type offset, typename sprout::container_traits<Result>::difference_type offset,
@ -41,9 +42,9 @@ namespace sprout {
result, result,
sprout::size(result), sprout::size(result),
(Indexes >= offset && Indexes < offset + size && Indexes < offset + input_size (Indexes >= offset && Indexes < offset + size && Indexes < offset + input_size
? (Indexes < offset + sprout::distance(middle, last) ? (Indexes < offset + last_half_size
? middle[Indexes - offset] ? middle[Indexes - offset]
: first[(Indexes - offset) - sprout::distance(first, middle)] : first[(Indexes - offset) - last_half_size]
) )
: *sprout::next(sprout::internal_begin(result), Indexes) : *sprout::next(sprout::internal_begin(result), Indexes)
)... )...
@ -58,7 +59,8 @@ namespace sprout {
) )
{ {
return sprout::fixed::detail::rotate_copy_impl_ra( return sprout::fixed::detail::rotate_copy_impl_ra(
first, middle, last, first, middle,
sprout::distance(middle, last),
result, result,
sprout::container_indexes<Result>::make(), sprout::container_indexes<Result>::make(),
sprout::internal_begin_offset(result), sprout::internal_begin_offset(result),
@ -138,7 +140,7 @@ namespace sprout {
std::forward_iterator_tag* std::forward_iterator_tag*
) )
{ {
return sprout::fixed::detail::rotate_copy_impl(first, middle, last, result, sprout::size(result)); return sprout::fixed::detail::rotate_copy_impl(first, middle, middle, last, result, sprout::size(result));
} }
template<typename ForwardIterator, typename Result> template<typename ForwardIterator, typename Result>