fix algorithm: copy for InputIterator

This commit is contained in:
bolero-MURAKAMI 2013-08-13 01:55:26 +09:00
parent 72fe72a623
commit 6ae41ef6de
4 changed files with 443 additions and 4 deletions

View file

@ -101,6 +101,249 @@ namespace testspr {
array<int, 10>{{0, 0, 3, 4, 5, 6, 7, 8, 0, 0}} array<int, 10>{{0, 0, 3, 4, 5, 6, 7, 8, 0, 0}}
)); ));
} }
// [2 .. 8) の範囲をコピー
{
SPROUT_STATIC_CONSTEXPR auto copied = sprout::copy(
testspr::reduct_input(sprout::begin(arr1) + 2),
testspr::reduct_input(sprout::begin(arr1) + 8),
arr2
);
TESTSPR_BOTH_ASSERT(testspr::equal(
copied,
array<int, 10>{{3, 4, 5, 6, 7, 8, 0, 0, 0, 0}}
));
}
// !!!
// {
// SPROUT_STATIC_CONSTEXPR auto copied = sprout::fit::copy(
// testspr::reduct_input(sprout::begin(arr1) + 2),
// testspr::reduct_input(sprout::begin(arr1) + 8),
// arr2
// );
// TESTSPR_BOTH_ASSERT(testspr::equal(
// copied,
// array<int, 6>{{3, 4, 5, 6, 7, 8}}
// ));
// }
// [2 .. 8) の範囲をコピー
// 出力範囲をオーバーする場合
{
SPROUT_STATIC_CONSTEXPR auto copied = sprout::copy(
testspr::reduct_input(sprout::begin(arr1) + 2),
testspr::reduct_input(sprout::begin(arr1) + 8),
arr3
);
TESTSPR_BOTH_ASSERT(testspr::equal(
copied,
array<int, 4>{{3, 4, 5, 6}}
));
}
// !!!
// {
// SPROUT_STATIC_CONSTEXPR auto copied = sprout::fit::copy(
// testspr::reduct_input(sprout::begin(arr1) + 2),
// testspr::reduct_input(sprout::begin(arr1) + 8),
// arr3
// );
// TESTSPR_BOTH_ASSERT(testspr::equal(
// copied,
// array<int, 4>{{3, 4, 5, 6}}
// ));
// }
// [2 .. 8) の範囲をコピー
// 出力範囲の切り出し
{
SPROUT_STATIC_CONSTEXPR auto copied = sprout::copy(
testspr::reduct_input(sprout::begin(arr1) + 2),
testspr::reduct_input(sprout::begin(arr1) + 8),
sprout::sub(arr2, 2, 8)
);
TESTSPR_BOTH_ASSERT(testspr::equal(
copied,
array<int, 6>{{3, 4, 5, 6, 7, 8}}
));
TESTSPR_BOTH_ASSERT(testspr::equal(
sprout::get_internal(copied),
array<int, 10>{{0, 0, 3, 4, 5, 6, 7, 8, 0, 0}}
));
}
// !!!
// {
// SPROUT_STATIC_CONSTEXPR auto copied = sprout::fit::copy(
// testspr::reduct_input(sprout::begin(arr1) + 2),
// testspr::reduct_input(sprout::begin(arr1) + 8),
// sprout::sub(arr2, 2, 8)
// );
// TESTSPR_BOTH_ASSERT(testspr::equal(
// copied,
// array<int, 6>{{3, 4, 5, 6, 7, 8}}
// ));
// TESTSPR_BOTH_ASSERT(testspr::equal(
// sprout::get_internal(copied),
// array<int, 10>{{0, 0, 3, 4, 5, 6, 7, 8, 0, 0}}
// ));
// }
// [2 .. 8) の範囲をコピー
{
SPROUT_STATIC_CONSTEXPR auto copied = sprout::copy(
testspr::reduct_forward(sprout::begin(arr1) + 2),
testspr::reduct_forward(sprout::begin(arr1) + 8),
arr2
);
TESTSPR_BOTH_ASSERT(testspr::equal(
copied,
array<int, 10>{{3, 4, 5, 6, 7, 8, 0, 0, 0, 0}}
));
}
{
SPROUT_STATIC_CONSTEXPR auto copied = sprout::fit::copy(
testspr::reduct_forward(sprout::begin(arr1) + 2),
testspr::reduct_forward(sprout::begin(arr1) + 8),
arr2
);
TESTSPR_BOTH_ASSERT(testspr::equal(
copied,
array<int, 6>{{3, 4, 5, 6, 7, 8}}
));
}
// [2 .. 8) の範囲をコピー
// 出力範囲をオーバーする場合
{
SPROUT_STATIC_CONSTEXPR auto copied = sprout::copy(
testspr::reduct_forward(sprout::begin(arr1) + 2),
testspr::reduct_forward(sprout::begin(arr1) + 8),
arr3
);
TESTSPR_BOTH_ASSERT(testspr::equal(
copied,
array<int, 4>{{3, 4, 5, 6}}
));
}
{
SPROUT_STATIC_CONSTEXPR auto copied = sprout::fit::copy(
testspr::reduct_forward(sprout::begin(arr1) + 2),
testspr::reduct_forward(sprout::begin(arr1) + 8),
arr3
);
TESTSPR_BOTH_ASSERT(testspr::equal(
copied,
array<int, 4>{{3, 4, 5, 6}}
));
}
// [2 .. 8) の範囲をコピー
// 出力範囲の切り出し
{
SPROUT_STATIC_CONSTEXPR auto copied = sprout::copy(
testspr::reduct_forward(sprout::begin(arr1) + 2),
testspr::reduct_forward(sprout::begin(arr1) + 8),
sprout::sub(arr2, 2, 8)
);
TESTSPR_BOTH_ASSERT(testspr::equal(
copied,
array<int, 6>{{3, 4, 5, 6, 7, 8}}
));
TESTSPR_BOTH_ASSERT(testspr::equal(
sprout::get_internal(copied),
array<int, 10>{{0, 0, 3, 4, 5, 6, 7, 8, 0, 0}}
));
}
{
SPROUT_STATIC_CONSTEXPR auto copied = sprout::fit::copy(
testspr::reduct_forward(sprout::begin(arr1) + 2),
testspr::reduct_forward(sprout::begin(arr1) + 8),
sprout::sub(arr2, 2, 8)
);
TESTSPR_BOTH_ASSERT(testspr::equal(
copied,
array<int, 6>{{3, 4, 5, 6, 7, 8}}
));
TESTSPR_BOTH_ASSERT(testspr::equal(
sprout::get_internal(copied),
array<int, 10>{{0, 0, 3, 4, 5, 6, 7, 8, 0, 0}}
));
}
// [2 .. 8) の範囲をコピー
{
SPROUT_STATIC_CONSTEXPR auto copied = sprout::copy(
testspr::reduct_random_access(sprout::begin(arr1) + 2),
testspr::reduct_random_access(sprout::begin(arr1) + 8),
arr2
);
TESTSPR_BOTH_ASSERT(testspr::equal(
copied,
array<int, 10>{{3, 4, 5, 6, 7, 8, 0, 0, 0, 0}}
));
}
{
SPROUT_STATIC_CONSTEXPR auto copied = sprout::fit::copy(
testspr::reduct_random_access(sprout::begin(arr1) + 2),
testspr::reduct_random_access(sprout::begin(arr1) + 8),
arr2
);
TESTSPR_BOTH_ASSERT(testspr::equal(
copied,
array<int, 6>{{3, 4, 5, 6, 7, 8}}
));
}
// [2 .. 8) の範囲をコピー
// 出力範囲をオーバーする場合
{
SPROUT_STATIC_CONSTEXPR auto copied = sprout::copy(
testspr::reduct_random_access(sprout::begin(arr1) + 2),
testspr::reduct_random_access(sprout::begin(arr1) + 8),
arr3
);
TESTSPR_BOTH_ASSERT(testspr::equal(
copied,
array<int, 4>{{3, 4, 5, 6}}
));
}
{
SPROUT_STATIC_CONSTEXPR auto copied = sprout::fit::copy(
testspr::reduct_random_access(sprout::begin(arr1) + 2),
testspr::reduct_random_access(sprout::begin(arr1) + 8),
arr3
);
TESTSPR_BOTH_ASSERT(testspr::equal(
copied,
array<int, 4>{{3, 4, 5, 6}}
));
}
// [2 .. 8) の範囲をコピー
// 出力範囲の切り出し
{
SPROUT_STATIC_CONSTEXPR auto copied = sprout::copy(
testspr::reduct_random_access(sprout::begin(arr1) + 2),
testspr::reduct_random_access(sprout::begin(arr1) + 8),
sprout::sub(arr2, 2, 8)
);
TESTSPR_BOTH_ASSERT(testspr::equal(
copied,
array<int, 6>{{3, 4, 5, 6, 7, 8}}
));
TESTSPR_BOTH_ASSERT(testspr::equal(
sprout::get_internal(copied),
array<int, 10>{{0, 0, 3, 4, 5, 6, 7, 8, 0, 0}}
));
}
{
SPROUT_STATIC_CONSTEXPR auto copied = sprout::fit::copy(
testspr::reduct_random_access(sprout::begin(arr1) + 2),
testspr::reduct_random_access(sprout::begin(arr1) + 8),
sprout::sub(arr2, 2, 8)
);
TESTSPR_BOTH_ASSERT(testspr::equal(
copied,
array<int, 6>{{3, 4, 5, 6, 7, 8}}
));
TESTSPR_BOTH_ASSERT(testspr::equal(
sprout::get_internal(copied),
array<int, 10>{{0, 0, 3, 4, 5, 6, 7, 8, 0, 0}}
));
}
} }
} }
} // namespace testspr } // namespace testspr

View file

@ -16,6 +16,7 @@
#include <sprout/container/functions.hpp> #include <sprout/container/functions.hpp>
#include <sprout/container/indexes.hpp> #include <sprout/container/indexes.hpp>
#include <sprout/iterator/operation.hpp> #include <sprout/iterator/operation.hpp>
#include <sprout/iterator/next_iterator.hpp>
#include <sprout/algorithm/fixed/result_of.hpp> #include <sprout/algorithm/fixed/result_of.hpp>
#include <sprout/pit/pit.hpp> #include <sprout/pit/pit.hpp>
#include <sprout/math/comparison.hpp> #include <sprout/math/comparison.hpp>
@ -63,7 +64,7 @@ namespace sprout {
sprout::container_traits<Result>::static_size == sizeof...(Args), sprout::container_traits<Result>::static_size == sizeof...(Args),
typename sprout::fixed::result_of::algorithm<Result>::type typename sprout::fixed::result_of::algorithm<Result>::type
>::type >::type
copy_impl( copy_impl_fw(
InputIterator, InputIterator, Result const& result, InputIterator, InputIterator, Result const& result,
typename sprout::container_traits<Result>::size_type, typename sprout::container_traits<Result>::size_type,
Args const&... args Args const&... args
@ -76,14 +77,14 @@ namespace sprout {
sprout::container_traits<Result>::static_size != sizeof...(Args), sprout::container_traits<Result>::static_size != sizeof...(Args),
typename sprout::fixed::result_of::algorithm<Result>::type typename sprout::fixed::result_of::algorithm<Result>::type
>::type >::type
copy_impl( copy_impl_fw(
InputIterator first, InputIterator last, Result const& result, InputIterator first, InputIterator last, Result const& result,
typename sprout::container_traits<Result>::size_type size, typename sprout::container_traits<Result>::size_type size,
Args const&... args Args const&... args
) )
{ {
return first != last && sizeof...(Args) < size return first != last && sizeof...(Args) < size
? sprout::fixed::detail::copy_impl(sprout::next(first), last, result, size, args..., *first) ? sprout::fixed::detail::copy_impl_fw(sprout::next(first), last, result, size, args..., *first)
: sprout::detail::container_complate(result, args...) : sprout::detail::container_complate(result, args...)
; ;
} }
@ -91,10 +92,67 @@ namespace sprout {
inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm<Result>::type inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm<Result>::type
copy( copy(
InputIterator first, InputIterator last, Result const& result, InputIterator first, InputIterator last, Result const& result,
std::forward_iterator_tag*
)
{
return sprout::fixed::detail::copy_impl_fw(first, last, result, sprout::size(result));
}
template<typename InputIterator, typename Result, typename... Args>
inline SPROUT_CONSTEXPR typename std::enable_if<
sprout::container_traits<Result>::static_size == sizeof...(Args) + 1,
typename sprout::fixed::result_of::algorithm<Result>::type
>::type
copy_impl_in(
sprout::next_iterator<InputIterator> const& first, InputIterator, Result const& result,
typename sprout::container_traits<Result>::size_type,
Args const&... args
)
{
return sprout::remake<Result>(result, sprout::size(result), args..., *first);
}
template<typename InputIterator, typename Result, typename... Args>
inline SPROUT_CONSTEXPR typename std::enable_if<
sprout::container_traits<Result>::static_size != sizeof...(Args) + 1,
typename sprout::fixed::result_of::algorithm<Result>::type
>::type
copy_impl_in(
sprout::next_iterator<InputIterator> const& first, InputIterator last, Result const& result,
typename sprout::container_traits<Result>::size_type size,
Args const&... args
)
{
return first.base() != last && sizeof...(Args) + 1 < size
? sprout::fixed::detail::copy_impl_in(sprout::next(first), last, result, size, args..., *first)
: sprout::detail::container_complate(result, args..., *first)
;
}
template<typename InputIterator, typename Result>
inline SPROUT_CONSTEXPR typename std::enable_if<
sprout::container_traits<Result>::static_size == 0,
typename sprout::fixed::result_of::algorithm<Result>::type
>::type
copy(
InputIterator, InputIterator, Result const& result,
std::input_iterator_tag* std::input_iterator_tag*
) )
{ {
return sprout::fixed::detail::copy_impl(first, last, result, sprout::size(result)); return sprout::remake<Result>(result, sprout::size(result));
}
template<typename InputIterator, typename Result>
inline SPROUT_CONSTEXPR typename std::enable_if<
sprout::container_traits<Result>::static_size != 0,
typename sprout::fixed::result_of::algorithm<Result>::type
>::type
copy(
InputIterator first, InputIterator last, Result const& result,
std::input_iterator_tag*
)
{
return first != last
? sprout::fixed::detail::copy_impl_in(sprout::make_next_iterator(first), last, result, sprout::size(result))
: sprout::detail::container_complate(result)
;
} }
template<typename InputIterator, typename Result> template<typename InputIterator, typename Result>

View file

@ -33,5 +33,6 @@
#include <sprout/iterator/size_enum_iterator.hpp> #include <sprout/iterator/size_enum_iterator.hpp>
#include <sprout/iterator/bytes_iterator.hpp> #include <sprout/iterator/bytes_iterator.hpp>
#include <sprout/iterator/remake_iterator.hpp> #include <sprout/iterator/remake_iterator.hpp>
#include <sprout/iterator/next_iterator.hpp>
#endif // #ifndef SPROUT_ITERATOR_ADAPTOR_HPP #endif // #ifndef SPROUT_ITERATOR_ADAPTOR_HPP

View file

@ -0,0 +1,137 @@
/*=============================================================================
Copyright (c) 2011-2013 Bolero MURAKAMI
https://github.com/bolero-MURAKAMI/Sprout
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#ifndef SPROUT_ITERATOR_NEXT_ITERATOR_HPP
#define SPROUT_ITERATOR_NEXT_ITERATOR_HPP
#include <iterator>
#include <utility>
#include <sprout/config.hpp>
#include <sprout/iterator/next.hpp>
#include <sprout/utility/value_holder/value_holder.hpp>
#include <sprout/utility/swap.hpp>
namespace sprout {
//
// next_iterator
//
template<typename Iterator>
struct next_iterator
: public std::iterator<
std::forward_iterator_tag,
typename std::iterator_traits<Iterator>::value_type,
typename std::iterator_traits<Iterator>::difference_type,
typename std::iterator_traits<Iterator>::pointer,
typename std::iterator_traits<Iterator>::reference
>
{
public:
typedef Iterator iterator_type;
typedef std::forward_iterator_tag iterator_category;
typedef typename std::iterator_traits<iterator_type>::value_type value_type;
typedef typename std::iterator_traits<iterator_type>::difference_type difference_type;
typedef typename std::iterator_traits<iterator_type>::pointer pointer;
typedef typename std::iterator_traits<iterator_type>::reference reference;
private:
typedef sprout::value_holder<reference> holder_type;
private:
holder_type holder_;
protected:
iterator_type current;
public:
next_iterator() = default;
SPROUT_CONSTEXPR next_iterator(next_iterator const& other)
: holder_(other.holder_), current(other.current)
{}
SPROUT_CONSTEXPR next_iterator(iterator_type it)
: holder_(*it), current(sprout::next(it))
{}
template<typename U>
SPROUT_CONSTEXPR next_iterator(next_iterator<U> const& it)
: holder_(*it), current(it.base())
{}
template<typename U>
next_iterator& operator=(next_iterator<U> const& it) {
next_iterator temp(it);
temp.swap(*this);
return *this;
}
SPROUT_CONSTEXPR iterator_type base() const {
return current;
}
SPROUT_CONSTEXPR reference operator*() const {
return *holder_;
}
SPROUT_CONSTEXPR pointer operator->() const {
return &*(*this);
}
next_iterator& operator++() {
holder_ = holder_type(*current);
++current;
return *this;
}
next_iterator operator++(int) {
next_iterator result(*this);
holder_ = holder_type(*current);
++current;
return result;
}
SPROUT_CONSTEXPR next_iterator next() const {
return next_iterator(current);
}
void swap(next_iterator& other)
SPROUT_NOEXCEPT_EXPR(
SPROUT_NOEXCEPT_EXPR(swap(holder_, other.holder_)) && SPROUT_NOEXCEPT_EXPR(swap(current, other.current))
)
{
swap(holder_, other.holder_);
swap(current, other.current);
}
};
template<typename Iterator1, typename Iterator2>
inline SPROUT_CONSTEXPR bool
operator==(sprout::next_iterator<Iterator1> const& lhs, sprout::next_iterator<Iterator2> const& rhs) {
return lhs.base() == rhs.base();
}
template<typename Iterator1, typename Iterator2>
inline SPROUT_CONSTEXPR bool
operator!=(sprout::next_iterator<Iterator1> const& lhs, sprout::next_iterator<Iterator2> const& rhs) {
return !(lhs == rhs);
}
//
// make_next_iterator
//
template<typename Iterator>
inline SPROUT_CONSTEXPR sprout::next_iterator<Iterator>
make_next_iterator(Iterator it) {
return sprout::next_iterator<Iterator>(it);
}
//
// swap
//
template<typename Iterator>
inline void
swap(sprout::next_iterator<Iterator>& lhs, sprout::next_iterator<Iterator>& rhs)
SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(lhs.swap(rhs)))
{
lhs.swap(rhs);
}
//
// iterator_next
//
template<typename Iterator>
inline SPROUT_CONSTEXPR sprout::next_iterator<Iterator>
iterator_next(sprout::next_iterator<Iterator> const& it) {
return it.next();
}
} // namespace sprout
#endif // #ifndef SPROUT_ITERATOR_NEXT_ITERATOR_HPP