copy, copy_if InputIterator 対応

This commit is contained in:
bolero-MURAKAMI 2011-10-06 15:00:08 +09:00
parent 9297b36d0b
commit ed469abdda
6 changed files with 181 additions and 58 deletions

View file

@ -2,6 +2,8 @@
#define SPROUT_ALGORITHM_FIXED_COPY_HPP #define SPROUT_ALGORITHM_FIXED_COPY_HPP
#include <cstddef> #include <cstddef>
#include <iterator>
#include <type_traits>
#include <sprout/config.hpp> #include <sprout/config.hpp>
#include <sprout/index_tuple.hpp> #include <sprout/index_tuple.hpp>
#include <sprout/fixed_container/traits.hpp> #include <sprout/fixed_container/traits.hpp>
@ -13,10 +15,10 @@
namespace sprout { namespace sprout {
namespace fixed { namespace fixed {
namespace detail { namespace detail {
template<typename Iterator, typename Result, std::ptrdiff_t... Indexes> template<typename RandomAccessIterator, typename Result, std::ptrdiff_t... Indexes>
SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm<Result>::type copy_impl( SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm<Result>::type copy_impl_ra(
Iterator first, RandomAccessIterator first,
Iterator last, RandomAccessIterator last,
Result const& result, Result const& result,
sprout::index_tuple<Indexes...>, sprout::index_tuple<Indexes...>,
typename sprout::fixed_container_traits<Result>::difference_type offset, typename sprout::fixed_container_traits<Result>::difference_type offset,
@ -33,25 +35,148 @@ namespace sprout {
)... )...
); );
} }
template<typename RandomAccessIterator, typename Result>
SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm<Result>::type copy(
RandomAccessIterator first,
RandomAccessIterator last,
Result const& result,
std::random_access_iterator_tag*
)
{
return sprout::fixed::detail::copy_impl_ra(
first,
last,
result,
typename sprout::index_range<0, sprout::fixed_container_traits<Result>::fixed_size>::type(),
sprout::fixed_begin_offset(result),
sprout::size(result),
NS_SSCRISK_CEL_OR_SPROUT_DETAIL::distance(first, last)
);
}
template<typename Result, typename... Args>
SPROUT_CONSTEXPR inline typename std::enable_if<
sprout::fixed_container_traits<Result>::fixed_size == sizeof...(Args),
typename sprout::fixed::result_of::algorithm<Result>::type
>::type copy_impl_3(
Result const& result,
Args const&... args
)
{
return sprout::remake_clone<Result, Result>(result, sprout::size(result), args...);
}
template<typename Result, typename... Args>
SPROUT_CONSTEXPR inline typename std::enable_if<
sprout::fixed_container_traits<Result>::fixed_size != sizeof...(Args),
typename sprout::fixed::result_of::algorithm<Result>::type
>::type copy_impl_3(
Result const& result,
Args const&... args
)
{
return copy_impl_3(result, args..., *sprout::next(sprout::fixed_begin(result), sizeof...(Args)));
}
template<typename InputIterator, typename Result, typename... Args>
SPROUT_CONSTEXPR inline typename std::enable_if<
sprout::fixed_container_traits<Result>::fixed_size == sizeof...(Args),
typename sprout::fixed::result_of::algorithm<Result>::type
>::type copy_impl_2(
InputIterator first,
InputIterator last,
Result const& result,
typename sprout::fixed_container_traits<Result>::difference_type offset,
Args const&... args
)
{
return sprout::remake_clone<Result, Result>(result, sprout::size(result), args...);
}
template<typename InputIterator, typename Result, typename... Args>
SPROUT_CONSTEXPR inline typename std::enable_if<
sprout::fixed_container_traits<Result>::fixed_size != sizeof...(Args),
typename sprout::fixed::result_of::algorithm<Result>::type
>::type copy_impl_2(
InputIterator first,
InputIterator last,
Result const& result,
typename sprout::fixed_container_traits<Result>::difference_type offset,
Args const&... args
)
{
return first != last && sizeof...(Args) < static_cast<std::size_t>(offset)
? copy_impl_2(sprout::next(first), last, result, offset, args..., *first)
: copy_impl_3(result, args...)
;
}
template<typename InputIterator, typename Result, typename... Args>
SPROUT_CONSTEXPR inline typename std::enable_if<
sprout::fixed_container_traits<Result>::fixed_size == sizeof...(Args),
typename sprout::fixed::result_of::algorithm<Result>::type
>::type copy_impl_1(
InputIterator first,
InputIterator last,
Result const& result,
typename sprout::fixed_container_traits<Result>::difference_type offset,
Args const&... args
)
{
return sprout::remake_clone<Result, Result>(result, sprout::size(result), args...);
}
template<typename InputIterator, typename Result, typename... Args>
SPROUT_CONSTEXPR inline typename std::enable_if<
sprout::fixed_container_traits<Result>::fixed_size != sizeof...(Args),
typename sprout::fixed::result_of::algorithm<Result>::type
>::type copy_impl_1(
InputIterator first,
InputIterator last,
Result const& result,
typename sprout::fixed_container_traits<Result>::difference_type offset,
Args const&... args
)
{
return sizeof...(Args) < static_cast<std::size_t>(offset)
? copy_impl_1(first, last, result, offset, args..., *sprout::next(sprout::fixed_begin(result), sizeof...(Args)))
: copy_impl_2(first, last, result, offset + sprout::size(result), args...)
;
}
template<typename InputIterator, typename Result>
SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm<Result>::type copy_impl(
InputIterator first,
InputIterator last,
Result const& result
)
{
return copy_impl_1(first, last, result, sprout::fixed_begin_offset(result));
}
template<typename InputIterator, typename Result>
SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm<Result>::type copy(
InputIterator first,
InputIterator last,
Result const& result,
void*
)
{
return sprout::fixed::detail::copy_impl(
first,
last,
result
);
}
} // namespace detail } // namespace detail
// //
// copy // copy
// //
template<typename Iterator, typename Result> template<typename InputIterator, typename Result>
SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm<Result>::type copy( SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm<Result>::type copy(
Iterator first, InputIterator first,
Iterator last, InputIterator last,
Result const& result Result const& result
) )
{ {
return sprout::fixed::detail::copy_impl( typedef typename std::iterator_traits<InputIterator>::iterator_category* category;
return sprout::fixed::detail::copy(
first, first,
last, last,
result, result,
typename sprout::index_range<0, sprout::fixed_container_traits<Result>::fixed_size>::type(), category()
sprout::fixed_begin_offset(result),
sprout::size(result),
NS_SSCRISK_CEL_OR_SPROUT_DETAIL::distance(first, last)
); );
} }
} // namespace fixed } // namespace fixed

View file

@ -5,7 +5,7 @@
#include <sprout/config.hpp> #include <sprout/config.hpp>
#include <sprout/fixed_container/traits.hpp> #include <sprout/fixed_container/traits.hpp>
#include <sprout/fixed_container/functions.hpp> #include <sprout/fixed_container/functions.hpp>
#include <sprout/iterator/operation.hpp> #include <sprout/iterator/operation.hpp>
#include <sprout/algorithm/fixed/result_of.hpp> #include <sprout/algorithm/fixed/result_of.hpp>
namespace sprout { namespace sprout {
@ -33,13 +33,13 @@ namespace sprout {
{ {
return copy_if_impl_3(result, args..., *sprout::next(sprout::fixed_begin(result), sizeof...(Args))); return copy_if_impl_3(result, args..., *sprout::next(sprout::fixed_begin(result), sizeof...(Args)));
} }
template<typename Iterator, typename Result, typename Predicate, typename... Args> template<typename InputIterator, typename Result, typename Predicate, typename... Args>
SPROUT_CONSTEXPR inline typename std::enable_if< SPROUT_CONSTEXPR inline typename std::enable_if<
sprout::fixed_container_traits<Result>::fixed_size == sizeof...(Args), sprout::fixed_container_traits<Result>::fixed_size == sizeof...(Args),
typename sprout::fixed::result_of::algorithm<Result>::type typename sprout::fixed::result_of::algorithm<Result>::type
>::type copy_if_impl_2( >::type copy_if_impl_2(
Iterator first, InputIterator first,
Iterator last, InputIterator last,
Result const& result, Result const& result,
Predicate pred, Predicate pred,
typename sprout::fixed_container_traits<Result>::difference_type offset, typename sprout::fixed_container_traits<Result>::difference_type offset,
@ -48,33 +48,33 @@ namespace sprout {
{ {
return sprout::remake_clone<Result, Result>(result, sprout::size(result), args...); return sprout::remake_clone<Result, Result>(result, sprout::size(result), args...);
} }
template<typename Iterator, typename Result, typename Predicate, typename... Args> template<typename InputIterator, typename Result, typename Predicate, typename... Args>
SPROUT_CONSTEXPR inline typename std::enable_if< SPROUT_CONSTEXPR inline typename std::enable_if<
sprout::fixed_container_traits<Result>::fixed_size != sizeof...(Args), sprout::fixed_container_traits<Result>::fixed_size != sizeof...(Args),
typename sprout::fixed::result_of::algorithm<Result>::type typename sprout::fixed::result_of::algorithm<Result>::type
>::type copy_if_impl_2( >::type copy_if_impl_2(
Iterator first, InputIterator first,
Iterator last, InputIterator last,
Result const& result, Result const& result,
Predicate pred, Predicate pred,
typename sprout::fixed_container_traits<Result>::difference_type offset, typename sprout::fixed_container_traits<Result>::difference_type offset,
Args const&... args Args const&... args
) )
{ {
return first != last && sizeof...(Args) < offset return first != last && sizeof...(Args) < static_cast<std::size_t>(offset)
? pred(*first) ? pred(*first)
? copy_if_impl_2(sprout::next(first), last, result, pred, offset, args..., *first) ? copy_if_impl_2(sprout::next(first), last, result, pred, offset, args..., *first)
: copy_if_impl_2(sprout::next(first), last, result, pred, offset, args...) : copy_if_impl_2(sprout::next(first), last, result, pred, offset, args...)
: copy_if_impl_3(result, args...) : copy_if_impl_3(result, args...)
; ;
} }
template<typename Iterator, typename Result, typename Predicate, typename... Args> template<typename InputIterator, typename Result, typename Predicate, typename... Args>
SPROUT_CONSTEXPR inline typename std::enable_if< SPROUT_CONSTEXPR inline typename std::enable_if<
sprout::fixed_container_traits<Result>::fixed_size == sizeof...(Args), sprout::fixed_container_traits<Result>::fixed_size == sizeof...(Args),
typename sprout::fixed::result_of::algorithm<Result>::type typename sprout::fixed::result_of::algorithm<Result>::type
>::type copy_if_impl_1( >::type copy_if_impl_1(
Iterator first, InputIterator first,
Iterator last, InputIterator last,
Result const& result, Result const& result,
Predicate pred, Predicate pred,
typename sprout::fixed_container_traits<Result>::difference_type offset, typename sprout::fixed_container_traits<Result>::difference_type offset,
@ -83,28 +83,28 @@ namespace sprout {
{ {
return sprout::remake_clone<Result, Result>(result, sprout::size(result), args...); return sprout::remake_clone<Result, Result>(result, sprout::size(result), args...);
} }
template<typename Iterator, typename Result, typename Predicate, typename... Args> template<typename InputIterator, typename Result, typename Predicate, typename... Args>
SPROUT_CONSTEXPR inline typename std::enable_if< SPROUT_CONSTEXPR inline typename std::enable_if<
sprout::fixed_container_traits<Result>::fixed_size != sizeof...(Args), sprout::fixed_container_traits<Result>::fixed_size != sizeof...(Args),
typename sprout::fixed::result_of::algorithm<Result>::type typename sprout::fixed::result_of::algorithm<Result>::type
>::type copy_if_impl_1( >::type copy_if_impl_1(
Iterator first, InputIterator first,
Iterator last, InputIterator last,
Result const& result, Result const& result,
Predicate pred, Predicate pred,
typename sprout::fixed_container_traits<Result>::difference_type offset, typename sprout::fixed_container_traits<Result>::difference_type offset,
Args const&... args Args const&... args
) )
{ {
return sizeof...(Args) < offset return sizeof...(Args) < static_cast<std::size_t>(offset)
? copy_if_impl_1(first, last, result, pred, offset, args..., *sprout::next(sprout::fixed_begin(result), sizeof...(Args))) ? copy_if_impl_1(first, last, result, pred, offset, args..., *sprout::next(sprout::fixed_begin(result), sizeof...(Args)))
: copy_if_impl_2(first, last, result, pred, offset + sprout::size(result), args...) : copy_if_impl_2(first, last, result, pred, offset + sprout::size(result), args...)
; ;
} }
template<typename Iterator, typename Result, typename Predicate> template<typename InputIterator, typename Result, typename Predicate>
SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm<Result>::type copy_if_impl( SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm<Result>::type copy_if_impl(
Iterator first, InputIterator first,
Iterator last, InputIterator last,
Result const& result, Result const& result,
Predicate pred Predicate pred
) )
@ -115,10 +115,10 @@ namespace sprout {
// //
// copy_if // copy_if
// //
template<typename Iterator, typename Result, typename Predicate> template<typename InputIterator, typename Result, typename Predicate>
SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm<Result>::type copy_if( SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm<Result>::type copy_if(
Iterator first, InputIterator first,
Iterator last, InputIterator last,
Result const& result, Result const& result,
Predicate pred Predicate pred
) )

View file

@ -20,14 +20,10 @@ namespace sprout {
Result const& result Result const& result
) )
{ {
return sprout::fixed::detail::copy_impl( return sprout::fixed::copy(
first, first,
sprout::next(first, n), sprout::next(first, n),
result, result
typename sprout::index_range<0, sprout::fixed_container_traits<Result>::fixed_size>::type(),
sprout::fixed_begin_offset(result),
sprout::size(result),
n
); );
} }
} // namespace fixed } // namespace fixed

View file

@ -20,12 +20,13 @@ namespace sprout {
return sprout::forward<Iterator>(it) + 1; return sprout::forward<Iterator>(it) + 1;
} }
template<typename Iterator> template<typename Iterator>
Iterator next_impl( SPROUT_CONSTEXPR typename std::decay<Iterator>::type next_impl(
Iterator&& it, Iterator&& it,
void* void*
) )
{ {
return std::next(sprout::forward<Iterator>(it)); using std::next;
return next(sprout::forward<Iterator>(it));
} }
template<typename Iterator> template<typename Iterator>
@ -41,13 +42,14 @@ namespace sprout {
return sprout::forward<Iterator>(it) + n; return sprout::forward<Iterator>(it) + n;
} }
template<typename Iterator> template<typename Iterator>
Iterator next_impl( SPROUT_CONSTEXPR typename std::decay<Iterator>::type next_impl(
Iterator it, Iterator it,
typename std::iterator_traits<typename std::decay<Iterator>::type>::difference_type n, typename std::iterator_traits<typename std::decay<Iterator>::type>::difference_type n,
void* void*
) )
{ {
return std::next(sprout::forward<Iterator>(it), n); using std::next;
return next(sprout::forward<Iterator>(it), n);
} }
} // namespace detail } // namespace detail
// //

View file

@ -306,18 +306,18 @@ namespace sprout {
{ {
return sprout::random::random_iterator<Engine>(); return sprout::random::random_iterator<Engine>();
} }
} // namespace random
// //
// next // next
// //
template<typename Engine, typename Distribution> template<typename Engine, typename Distribution>
SPROUT_CONSTEXPR sprout::random::random_iterator<Engine, Distribution> next( SPROUT_CONSTEXPR sprout::random::random_iterator<Engine, Distribution> next(
sprout::random::random_iterator<Engine, Distribution> const& it sprout::random::random_iterator<Engine, Distribution> const& it
) )
{ {
return it(); return it();
} }
} // namespace random
using sprout::random::random_iterator; using sprout::random::random_iterator;
} // namespace sprout } // namespace sprout

View file

@ -23,7 +23,7 @@ namespace sprout {
typename std::enable_if<!std::is_same<Distribution, void>::value>::type typename std::enable_if<!std::is_same<Distribution, void>::value>::type
> >
: public std::iterator< : public std::iterator<
std::forward_iterator_tag, std::input_iterator_tag,
typename Distribution::result_type, typename Distribution::result_type,
std::ptrdiff_t, std::ptrdiff_t,
typename Distribution::result_type const*, typename Distribution::result_type const*,
@ -36,7 +36,7 @@ namespace sprout {
typedef typename distribution_type::result_type result_type; typedef typename distribution_type::result_type result_type;
private: private:
typedef std::iterator< typedef std::iterator<
std::forward_iterator_tag, std::input_iterator_tag,
result_type, result_type,
std::ptrdiff_t, std::ptrdiff_t,
result_type const*, result_type const*,
@ -137,7 +137,7 @@ namespace sprout {
typename std::enable_if<std::is_same<Distribution, void>::value>::type typename std::enable_if<std::is_same<Distribution, void>::value>::type
> >
: public std::iterator< : public std::iterator<
std::forward_iterator_tag, std::input_iterator_tag,
typename Engine::result_type, typename Engine::result_type,
std::ptrdiff_t, std::ptrdiff_t,
typename Engine::result_type const*, typename Engine::result_type const*,
@ -149,7 +149,7 @@ namespace sprout {
typedef typename engine_type::result_type result_type; typedef typename engine_type::result_type result_type;
private: private:
typedef std::iterator< typedef std::iterator<
std::forward_iterator_tag, std::input_iterator_tag,
result_type, result_type,
std::ptrdiff_t, std::ptrdiff_t,
result_type const*, result_type const*,