mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2024-12-23 21:25:49 +00:00
add inserter
fix type_traits is_iterator
This commit is contained in:
parent
74e83e31a7
commit
bf8c46b203
4 changed files with 235 additions and 25 deletions
|
@ -11,7 +11,9 @@
|
||||||
#include <sprout/container/internal_begin_offset_backward.hpp>
|
#include <sprout/container/internal_begin_offset_backward.hpp>
|
||||||
#include <sprout/container/internal_end_offset_backward.hpp>
|
#include <sprout/container/internal_end_offset_backward.hpp>
|
||||||
#include <sprout/iterator/remake_iterator.hpp>
|
#include <sprout/iterator/remake_iterator.hpp>
|
||||||
|
#include <sprout/iterator/type_traits/is_iterator.hpp>
|
||||||
#include <sprout/utility/forward.hpp>
|
#include <sprout/utility/forward.hpp>
|
||||||
|
#include <sprout/tpp/algorithm/all_of.hpp>
|
||||||
|
|
||||||
namespace sprout {
|
namespace sprout {
|
||||||
//
|
//
|
||||||
|
@ -50,7 +52,9 @@ namespace sprout {
|
||||||
}
|
}
|
||||||
template<typename Container, typename Cont, typename... Args>
|
template<typename Container, typename Cont, typename... Args>
|
||||||
inline SPROUT_CONSTEXPR typename std::enable_if<
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
||||||
!sprout::is_fixed_container<Container>::value && sizeof...(Args) != 2,
|
!sprout::is_fixed_container<Container>::value
|
||||||
|
&& !(sizeof...(Args) == 2 && sprout::tpp::all_of<sprout::is_input_iterator<typename std::remove_reference<Args>::type>...>::value)
|
||||||
|
,
|
||||||
typename sprout::container_construct_traits<Container>::copied_type
|
typename sprout::container_construct_traits<Container>::copied_type
|
||||||
>::type
|
>::type
|
||||||
default_remake_container(Cont&& cont, typename sprout::container_traits<Container>::difference_type size, Args&&... args) {
|
default_remake_container(Cont&& cont, typename sprout::container_traits<Container>::difference_type size, Args&&... args) {
|
||||||
|
|
175
sprout/iterator/insert_iterator.hpp
Normal file
175
sprout/iterator/insert_iterator.hpp
Normal file
|
@ -0,0 +1,175 @@
|
||||||
|
#ifndef SPROUT_ITERATOR_INSERT_ITERATOR_HPP
|
||||||
|
#define SPROUT_ITERATOR_INSERT_ITERATOR_HPP
|
||||||
|
|
||||||
|
#include <iterator>
|
||||||
|
#include <sprout/config.hpp>
|
||||||
|
#include <sprout/iterator/next.hpp>
|
||||||
|
#include <sprout/iterator/prev.hpp>
|
||||||
|
#include <sprout/iterator/distance.hpp>
|
||||||
|
#include <sprout/iterator/insert_range_iterator.hpp>
|
||||||
|
#include <sprout/container/traits.hpp>
|
||||||
|
#include <sprout/container/functions.hpp>
|
||||||
|
#include <sprout/container/container_holder.hpp>
|
||||||
|
#include <sprout/utility/swap.hpp>
|
||||||
|
#include <sprout/utility/forward.hpp>
|
||||||
|
#include <sprout/utility/move.hpp>
|
||||||
|
#include <sprout/utility/limited.hpp>
|
||||||
|
#include <sprout/type_traits/arithmetic_promote.hpp>
|
||||||
|
|
||||||
|
namespace sprout {
|
||||||
|
//
|
||||||
|
// insert_iterator
|
||||||
|
//
|
||||||
|
template<typename Container>
|
||||||
|
class insert_iterator
|
||||||
|
: public std::iterator<std::output_iterator_tag, void, void, void, void>
|
||||||
|
, public sprout::container_holder<Container>
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
typedef sprout::container_holder<Container> base_type;
|
||||||
|
public:
|
||||||
|
typedef typename base_type::container_type container_type;
|
||||||
|
typedef typename base_type::internal_type internal_type;
|
||||||
|
typedef typename base_type::iterator iterator;
|
||||||
|
typedef typename base_type::const_iterator const_iterator;
|
||||||
|
typedef typename base_type::reference reference;
|
||||||
|
typedef typename base_type::const_reference const_reference;
|
||||||
|
typedef typename base_type::size_type size_type;
|
||||||
|
typedef typename base_type::difference_type difference_type;
|
||||||
|
typedef typename base_type::pointer pointer;
|
||||||
|
typedef typename base_type::const_pointer const_pointer;
|
||||||
|
typedef typename base_type::param_type param_type;
|
||||||
|
typedef typename base_type::const_param_type const_param_type;
|
||||||
|
typedef typename base_type::holder_type holder_type;
|
||||||
|
protected:
|
||||||
|
using base_type::container;
|
||||||
|
iterator iter;
|
||||||
|
public:
|
||||||
|
SPROUT_CONSTEXPR insert_iterator() = default;
|
||||||
|
SPROUT_CONSTEXPR insert_iterator(param_type x, iterator pos)
|
||||||
|
: base_type(x), iter(pos)
|
||||||
|
{}
|
||||||
|
SPROUT_CONSTEXPR insert_iterator(insert_iterator const&) = default;
|
||||||
|
SPROUT_CONSTEXPR iterator position() const {
|
||||||
|
return iter;
|
||||||
|
}
|
||||||
|
insert_iterator& operator=(typename container_type::value_type const& value) {
|
||||||
|
container->insert(iter, value);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
insert_iterator& operator=(typename container_type::value_type&& value) {
|
||||||
|
container->insert(iter, sprout::move(value));
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
SPROUT_CONSTEXPR insert_iterator const& operator*() const {
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
insert_iterator& operator*() {
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
insert_iterator& operator++() {
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
insert_iterator operator++(int) {
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
void swap(insert_iterator& other)
|
||||||
|
SPROUT_NOEXCEPT_EXPR(
|
||||||
|
SPROUT_NOEXCEPT_EXPR(base_type::swap(other))
|
||||||
|
&& SPROUT_NOEXCEPT_EXPR(sprout::swap(other.iter, iter))
|
||||||
|
)
|
||||||
|
{
|
||||||
|
base_type::swap(other);
|
||||||
|
sprout::swap(other.iter, iter);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//
|
||||||
|
// swap
|
||||||
|
//
|
||||||
|
template<typename Container>
|
||||||
|
inline void
|
||||||
|
swap(sprout::insert_iterator<Container>& lhs, sprout::insert_iterator<Container>& rhs)
|
||||||
|
SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(lhs.swap(rhs)))
|
||||||
|
{
|
||||||
|
lhs.swap(rhs);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// inserter
|
||||||
|
//
|
||||||
|
template<typename Container>
|
||||||
|
inline SPROUT_CONSTEXPR sprout::insert_iterator<Container>
|
||||||
|
inserter(Container& x, typename sprout::insert_iterator<Container>::iterator pos) {
|
||||||
|
return sprout::insert_iterator<Container>(x, pos);
|
||||||
|
}
|
||||||
|
template<typename Container>
|
||||||
|
inline SPROUT_CONSTEXPR sprout::insert_iterator<Container const>
|
||||||
|
inserter(Container const& x, typename sprout::insert_iterator<Container const>::iterator pos) {
|
||||||
|
return sprout::insert_iterator<Container const>(x, pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// container_construct_traits
|
||||||
|
//
|
||||||
|
template<typename Container>
|
||||||
|
struct container_construct_traits<sprout::insert_iterator<Container> > {
|
||||||
|
public:
|
||||||
|
typedef typename sprout::container_construct_traits<Container>::copied_type copied_type;
|
||||||
|
private:
|
||||||
|
template<typename Iterator, typename Cont, typename InputIterator>
|
||||||
|
static SPROUT_CONSTEXPR copied_type
|
||||||
|
remake_impl(Iterator pos, Cont&& cont, typename sprout::container_traits<Container>::difference_type size, InputIterator first, InputIterator last) {
|
||||||
|
return sprout::make<copied_type>(
|
||||||
|
sprout::make_insert_range_iterator(
|
||||||
|
sprout::internal_begin(cont), pos,
|
||||||
|
first, first, last
|
||||||
|
),
|
||||||
|
sprout::make_insert_range_iterator(
|
||||||
|
sprout::internal_end(cont), pos,
|
||||||
|
last, first, last
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
public:
|
||||||
|
template<typename Cont>
|
||||||
|
static SPROUT_CONSTEXPR copied_type
|
||||||
|
deep_copy(Cont&& cont) {
|
||||||
|
return sprout::deep_copy(sprout::get_internal(sprout::forward<Cont>(cont)));
|
||||||
|
}
|
||||||
|
template<typename... Args>
|
||||||
|
static SPROUT_CONSTEXPR copied_type
|
||||||
|
make(Args&&... args) {
|
||||||
|
return sprout::make<copied_type>(sprout::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
template<typename Cont, typename... Args>
|
||||||
|
static SPROUT_CONSTEXPR copied_type
|
||||||
|
remake(Cont&& cont, typename sprout::container_traits<Container>::difference_type size, Args&&... args) {
|
||||||
|
return remake_impl(cont.position(), sprout::get_internal(sprout::forward<Cont>(cont)), size, sprout::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//
|
||||||
|
// container_fitness_traits
|
||||||
|
//
|
||||||
|
template<typename Container>
|
||||||
|
struct container_fitness_traits<sprout::insert_iterator<Container> > {
|
||||||
|
public:
|
||||||
|
template<typename Cont>
|
||||||
|
static SPROUT_CONSTEXPR typename sprout::container_traits<Container>::difference_type
|
||||||
|
fit_size(Cont&& cont, typename sprout::container_traits<Container>::difference_type size) {
|
||||||
|
return size + sprout::size(sprout::forward<Cont>(cont));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//
|
||||||
|
// sub_container_traits
|
||||||
|
//
|
||||||
|
template<typename Container>
|
||||||
|
struct sub_container_traits<sprout::insert_iterator<Container> >
|
||||||
|
: public sprout::sub_container_traits<sprout::container_holder<Container> >
|
||||||
|
{};
|
||||||
|
} // namespace sprout
|
||||||
|
|
||||||
|
#endif // #ifndef SPROUT_ITERATOR_INSERT_ITERATOR_HPP
|
|
@ -4,5 +4,6 @@
|
||||||
#include <sprout/config.hpp>
|
#include <sprout/config.hpp>
|
||||||
#include <sprout/iterator/back_insert_iterator.hpp>
|
#include <sprout/iterator/back_insert_iterator.hpp>
|
||||||
#include <sprout/iterator/front_insert_iterator.hpp>
|
#include <sprout/iterator/front_insert_iterator.hpp>
|
||||||
|
#include <sprout/iterator/insert_iterator.hpp>
|
||||||
|
|
||||||
#endif // #ifndef SPROUT_ITERATOR_INSERTER_HPP
|
#endif // #ifndef SPROUT_ITERATOR_INSERTER_HPP
|
||||||
|
|
|
@ -16,91 +16,121 @@ namespace sprout {
|
||||||
//
|
//
|
||||||
// is_iterator
|
// is_iterator
|
||||||
//
|
//
|
||||||
template<typename Iterator>
|
template<typename MaybeIterator>
|
||||||
struct is_iterator
|
struct is_iterator
|
||||||
: public sprout::detail::has_iterator_category<std::iterator_traits<Iterator> >
|
: public sprout::detail::has_iterator_category<std::iterator_traits<MaybeIterator> >
|
||||||
|
{};
|
||||||
|
|
||||||
|
//
|
||||||
|
// is_iterator_category_of
|
||||||
|
//
|
||||||
|
template<typename MaybeCategory, typename Category>
|
||||||
|
struct is_iterator_category_of
|
||||||
|
: public std::is_convertible<MaybeCategory, Category>
|
||||||
{};
|
{};
|
||||||
|
|
||||||
//
|
//
|
||||||
// is_input_iterator_category
|
// is_input_iterator_category
|
||||||
//
|
//
|
||||||
template<typename Category>
|
template<typename MaybeCategory>
|
||||||
struct is_input_iterator_category
|
struct is_input_iterator_category
|
||||||
: public std::is_convertible<Category, std::input_iterator_tag>
|
: public sprout::is_iterator_category_of<MaybeCategory, std::input_iterator_tag>
|
||||||
{};
|
{};
|
||||||
//
|
//
|
||||||
// is_output_iterator_category
|
// is_output_iterator_category
|
||||||
//
|
//
|
||||||
template<typename Category>
|
template<typename MaybeCategory>
|
||||||
struct is_output_iterator_category
|
struct is_output_iterator_category
|
||||||
: public std::is_convertible<Category, std::output_iterator_tag>
|
: public sprout::is_iterator_category_of<MaybeCategory, std::output_iterator_tag>
|
||||||
{};
|
{};
|
||||||
//
|
//
|
||||||
// is_forward_iterator_category
|
// is_forward_iterator_category
|
||||||
//
|
//
|
||||||
template<typename Category>
|
template<typename MaybeCategory>
|
||||||
struct is_forward_iterator_category
|
struct is_forward_iterator_category
|
||||||
: public std::is_convertible<Category, std::forward_iterator_tag>
|
: public sprout::is_iterator_category_of<MaybeCategory, std::forward_iterator_tag>
|
||||||
{};
|
{};
|
||||||
//
|
//
|
||||||
// is_bidirectional_iterator_category
|
// is_bidirectional_iterator_category
|
||||||
//
|
//
|
||||||
template<typename Category>
|
template<typename MaybeCategory>
|
||||||
struct is_bidirectional_iterator_category
|
struct is_bidirectional_iterator_category
|
||||||
: public std::is_convertible<Category, std::bidirectional_iterator_tag>
|
: public sprout::is_iterator_category_of<MaybeCategory, std::bidirectional_iterator_tag>
|
||||||
{};
|
{};
|
||||||
//
|
//
|
||||||
// is_random_access_iterator_category
|
// is_random_access_iterator_category
|
||||||
//
|
//
|
||||||
template<typename Category>
|
template<typename MaybeCategory>
|
||||||
struct is_random_access_iterator_category
|
struct is_random_access_iterator_category
|
||||||
: public std::is_convertible<Category, std::random_access_iterator_tag>
|
: public sprout::is_iterator_category_of<MaybeCategory, std::random_access_iterator_tag>
|
||||||
|
{};
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
|
template<typename MaybeIterator, typename Category, typename = void>
|
||||||
|
struct is_iterator_of_impl
|
||||||
|
: public std::false_type
|
||||||
|
{};
|
||||||
|
template<typename MaybeIterator, typename Category>
|
||||||
|
struct is_iterator_of_impl<
|
||||||
|
MaybeIterator, Category,
|
||||||
|
typename std::enable_if<sprout::is_iterator<MaybeIterator>::value>::type
|
||||||
|
>
|
||||||
|
: public sprout::is_iterator_category_of<typename std::iterator_traits<MaybeIterator>::iterator_category, Category>
|
||||||
|
{};
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
|
//
|
||||||
|
// is_iterator_of
|
||||||
|
//
|
||||||
|
template<typename MaybeIterator, typename Category>
|
||||||
|
struct is_iterator_of
|
||||||
|
: public sprout::detail::is_iterator_of_impl<MaybeIterator, Category>
|
||||||
{};
|
{};
|
||||||
|
|
||||||
//
|
//
|
||||||
// is_input_iterator
|
// is_input_iterator
|
||||||
//
|
//
|
||||||
template<typename Iterator>
|
template<typename MaybeIterator>
|
||||||
struct is_input_iterator
|
struct is_input_iterator
|
||||||
: public sprout::is_input_iterator_category<typename std::iterator_traits<Iterator>::iterator_category>
|
: public sprout::is_iterator_of<MaybeIterator, std::input_iterator_tag>
|
||||||
{};
|
{};
|
||||||
//
|
//
|
||||||
// is_output_iterator
|
// is_output_iterator
|
||||||
//
|
//
|
||||||
template<typename Iterator>
|
template<typename MaybeIterator>
|
||||||
struct is_output_iterator
|
struct is_output_iterator
|
||||||
: public sprout::is_output_iterator_category<typename std::iterator_traits<Iterator>::iterator_category>
|
: public sprout::is_iterator_of<MaybeIterator, std::output_iterator_tag>
|
||||||
{};
|
{};
|
||||||
//
|
//
|
||||||
// is_forward_iterator
|
// is_forward_iterator
|
||||||
//
|
//
|
||||||
template<typename Iterator>
|
template<typename MaybeIterator>
|
||||||
struct is_forward_iterator
|
struct is_forward_iterator
|
||||||
: public sprout::is_forward_iterator_category<typename std::iterator_traits<Iterator>::iterator_category>
|
: public sprout::is_iterator_of<MaybeIterator, std::forward_iterator_tag>
|
||||||
{};
|
{};
|
||||||
//
|
//
|
||||||
// is_bidirectional_iterator
|
// is_bidirectional_iterator
|
||||||
//
|
//
|
||||||
template<typename Iterator>
|
template<typename MaybeIterator>
|
||||||
struct is_bidirectional_iterator
|
struct is_bidirectional_iterator
|
||||||
: public sprout::is_bidirectional_iterator_category<typename std::iterator_traits<Iterator>::iterator_category>
|
: public sprout::is_iterator_of<MaybeIterator, std::bidirectional_iterator_tag>
|
||||||
{};
|
{};
|
||||||
//
|
//
|
||||||
// is_random_access_iterator
|
// is_random_access_iterator
|
||||||
//
|
//
|
||||||
template<typename Iterator>
|
template<typename MaybeIterator>
|
||||||
struct is_random_access_iterator
|
struct is_random_access_iterator
|
||||||
: public sprout::is_random_access_iterator_category<typename std::iterator_traits<Iterator>::iterator_category>
|
: public sprout::is_iterator_of<MaybeIterator, std::random_access_iterator_tag>
|
||||||
{};
|
{};
|
||||||
|
|
||||||
//
|
//
|
||||||
// is_constant_distance_iterator
|
// is_constant_distance_iterator
|
||||||
//
|
//
|
||||||
template<typename Iterator>
|
template<typename MaybeIterator>
|
||||||
struct is_constant_distance_iterator
|
struct is_constant_distance_iterator
|
||||||
: public std::integral_constant<
|
: public std::integral_constant<
|
||||||
bool,
|
bool,
|
||||||
sprout::is_random_access_iterator<Iterator>::value && !std::is_pointer<Iterator>::value
|
sprout::is_random_access_iterator<MaybeIterator>::value && !std::is_pointer<MaybeIterator>::value
|
||||||
>
|
>
|
||||||
{};
|
{};
|
||||||
} // namespace sprout
|
} // namespace sprout
|
||||||
|
|
Loading…
Reference in a new issue