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
#include <cstddef>
#include <iterator>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/index_tuple.hpp>
#include <sprout/fixed_container/traits.hpp>
@ -13,10 +15,10 @@
namespace sprout {
namespace fixed {
namespace detail {
template<typename Iterator, typename Result, std::ptrdiff_t... Indexes>
SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm<Result>::type copy_impl(
Iterator first,
Iterator last,
template<typename RandomAccessIterator, typename Result, std::ptrdiff_t... Indexes>
SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm<Result>::type copy_impl_ra(
RandomAccessIterator first,
RandomAccessIterator last,
Result const& result,
sprout::index_tuple<Indexes...>,
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
//
// copy
//
template<typename Iterator, typename Result>
template<typename InputIterator, typename Result>
SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm<Result>::type copy(
Iterator first,
Iterator last,
InputIterator first,
InputIterator last,
Result const& result
)
{
return sprout::fixed::detail::copy_impl(
typedef typename std::iterator_traits<InputIterator>::iterator_category* category;
return sprout::fixed::detail::copy(
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)
category()
);
}
} // namespace fixed

View file

@ -33,13 +33,13 @@ namespace sprout {
{
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::fixed_container_traits<Result>::fixed_size == sizeof...(Args),
typename sprout::fixed::result_of::algorithm<Result>::type
>::type copy_if_impl_2(
Iterator first,
Iterator last,
InputIterator first,
InputIterator last,
Result const& result,
Predicate pred,
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...);
}
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::fixed_container_traits<Result>::fixed_size != sizeof...(Args),
typename sprout::fixed::result_of::algorithm<Result>::type
>::type copy_if_impl_2(
Iterator first,
Iterator last,
InputIterator first,
InputIterator last,
Result const& result,
Predicate pred,
typename sprout::fixed_container_traits<Result>::difference_type offset,
Args const&... args
)
{
return first != last && sizeof...(Args) < offset
return first != last && sizeof...(Args) < static_cast<std::size_t>(offset)
? 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...)
: 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::fixed_container_traits<Result>::fixed_size == sizeof...(Args),
typename sprout::fixed::result_of::algorithm<Result>::type
>::type copy_if_impl_1(
Iterator first,
Iterator last,
InputIterator first,
InputIterator last,
Result const& result,
Predicate pred,
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...);
}
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::fixed_container_traits<Result>::fixed_size != sizeof...(Args),
typename sprout::fixed::result_of::algorithm<Result>::type
>::type copy_if_impl_1(
Iterator first,
Iterator last,
InputIterator first,
InputIterator last,
Result const& result,
Predicate pred,
typename sprout::fixed_container_traits<Result>::difference_type offset,
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_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(
Iterator first,
Iterator last,
InputIterator first,
InputIterator last,
Result const& result,
Predicate pred
)
@ -115,10 +115,10 @@ namespace sprout {
//
// 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(
Iterator first,
Iterator last,
InputIterator first,
InputIterator last,
Result const& result,
Predicate pred
)

View file

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

View file

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

View file

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

View file

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