Files
boost_iterator/include/boost/iterator/zip_iterator.hpp

333 lines
9.1 KiB
C++
Raw Normal View History

// Copyright David Abrahams and Thomas Becker 2000-2006.
// Copyright Kohei Takahashi 2012-2014.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
2006-09-12 22:34:33 +00:00
// http://www.boost.org/LICENSE_1_0.txt)
2003-09-09 03:22:50 +00:00
#ifndef BOOST_ZIP_ITERATOR_TMB_07_13_2003_HPP_
# define BOOST_ZIP_ITERATOR_TMB_07_13_2003_HPP_
2003-09-09 03:22:50 +00:00
#include <stddef.h>
#include <boost/iterator.hpp>
#include <boost/iterator/iterator_traits.hpp>
#include <boost/iterator/iterator_facade.hpp>
#include <boost/iterator/iterator_adaptor.hpp> // for enable_if_convertible
#include <boost/iterator/iterator_categories.hpp>
2003-09-30 12:22:56 +00:00
#include <boost/detail/iterator.hpp>
2003-09-09 03:22:50 +00:00
#include <boost/iterator/detail/minimum_category.hpp>
#include <boost/fusion/adapted/boost_tuple.hpp> // for backward compatibility
2003-09-09 03:22:50 +00:00
#include <boost/type_traits/remove_reference.hpp>
#include <boost/type_traits/remove_cv.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/fold.hpp>
#include <boost/mpl/transform.hpp>
2003-09-09 03:22:50 +00:00
#include <boost/mpl/placeholders.hpp>
#include <boost/mpl/aux_/lambda_support.hpp>
#include <boost/fusion/algorithm/iteration/for_each.hpp>
#include <boost/fusion/algorithm/transformation/transform.hpp>
#include <boost/fusion/sequence/convert.hpp>
#include <boost/fusion/sequence/intrinsic/at_c.hpp>
#include <boost/fusion/sequence/comparison/equal_to.hpp>
#include <boost/fusion/support/tag_of_fwd.hpp>
2003-09-09 03:22:50 +00:00
namespace boost {
// Zip iterator forward declaration for zip_iterator_base
template<typename IteratorTuple>
class zip_iterator;
namespace detail
{
// Functors to be used with tuple algorithms
//
template<typename DiffType>
class advance_iterator
{
public:
advance_iterator(DiffType step) : m_step(step) {}
2003-09-09 03:22:50 +00:00
template<typename Iterator>
void operator()(Iterator& it) const
{ it += m_step; }
private:
DiffType m_step;
};
//
struct increment_iterator
{
template<typename Iterator>
void operator()(Iterator& it) const
2003-09-09 03:22:50 +00:00
{ ++it; }
};
//
struct decrement_iterator
{
template<typename Iterator>
void operator()(Iterator& it) const
2003-09-09 03:22:50 +00:00
{ --it; }
};
//
struct dereference_iterator
{
template<typename>
struct result;
template<typename This, typename Iterator>
struct result<This(Iterator)>
{
2003-09-09 03:22:50 +00:00
typedef typename
remove_reference<typename remove_cv<Iterator>::type>::type
iterator;
typedef typename
iterator_traits<iterator>::reference
2003-09-09 03:22:50 +00:00
type;
};
template<typename Iterator>
typename result<dereference_iterator(Iterator)>::type
operator()(Iterator const& it) const
2003-09-09 03:22:50 +00:00
{ return *it; }
};
template<typename Iterator>
struct iterator_reference
{
typedef typename iterator_traits<Iterator>::reference type;
};
#ifdef BOOST_MPL_CFG_NO_FULL_LAMBDA_SUPPORT
2003-09-09 03:22:50 +00:00
// Hack because BOOST_MPL_AUX_LAMBDA_SUPPORT doesn't seem to work
// out well. Instantiating the nested apply template also
// requires instantiating iterator_traits on the
// placeholder. Instead we just specialize it as a metafunction
// class.
template<>
struct iterator_reference<mpl::_1>
{
template <class T>
struct apply : iterator_reference<T> {};
};
#endif
2003-09-09 03:22:50 +00:00
// Metafunction to obtain the type of the tuple whose element types
// are the reference types of an iterator tuple.
//
template<typename IteratorTuple>
struct tuple_of_references
: mpl::transform<
IteratorTuple,
2003-09-09 03:22:50 +00:00
iterator_reference<mpl::_1>
>
{
};
// Metafunction to obtain the minimal traversal tag in a tuple
// of iterators.
//
template<typename IteratorTuple>
struct minimum_traversal_category_in_iterator_tuple
{
typedef typename mpl::transform<
2003-09-09 03:22:50 +00:00
IteratorTuple
, pure_traversal_tag<iterator_traversal<> >
2003-09-09 03:22:50 +00:00
>::type tuple_of_traversal_tags;
typedef typename mpl::fold<
2003-09-09 03:22:50 +00:00
tuple_of_traversal_tags
, random_access_traversal_tag
, minimum_category<>
2003-09-09 03:22:50 +00:00
>::type type;
};
2003-09-09 03:22:50 +00:00
///////////////////////////////////////////////////////////////////
//
// Class zip_iterator_base
//
// Builds and exposes the iterator facade type from which the zip
2003-09-09 03:22:50 +00:00
// iterator will be derived.
//
template<typename IteratorTuple>
struct zip_iterator_base
{
private:
// Reference type is the type of the tuple obtained from the
// iterators' reference types.
typedef typename
2003-09-09 03:22:50 +00:00
detail::tuple_of_references<IteratorTuple>::type reference;
2003-09-09 03:22:50 +00:00
// Value type is the same as reference type.
typedef reference value_type;
2003-09-09 03:22:50 +00:00
// Difference type is the first iterator's difference type
typedef typename iterator_traits<
typename mpl::at_c<IteratorTuple, 0>::type
2003-09-09 03:22:50 +00:00
>::difference_type difference_type;
// Traversal catetgory is the minimum traversal category in the
2003-09-09 03:22:50 +00:00
// iterator tuple.
typedef typename
2003-09-09 03:22:50 +00:00
detail::minimum_traversal_category_in_iterator_tuple<
IteratorTuple
>::type traversal_category;
public:
2003-09-09 03:22:50 +00:00
// The iterator facade type from which the zip iterator will
// be derived.
typedef iterator_facade<
zip_iterator<IteratorTuple>,
value_type,
2003-09-09 03:22:50 +00:00
traversal_category,
reference,
difference_type
> type;
};
template <>
struct zip_iterator_base<int>
{
typedef int type;
};
}
2003-09-09 03:22:50 +00:00
/////////////////////////////////////////////////////////////////////
//
// zip_iterator class definition
//
template<typename IteratorTuple>
class zip_iterator :
2003-09-09 03:22:50 +00:00
public detail::zip_iterator_base<IteratorTuple>::type
{
2003-09-09 03:22:50 +00:00
// Typedef super_t as our base class.
typedef typename
2003-09-09 03:22:50 +00:00
detail::zip_iterator_base<IteratorTuple>::type super_t;
// iterator_core_access is the iterator's best friend.
friend class iterator_core_access;
public:
2003-09-09 03:22:50 +00:00
// Construction
// ============
2003-09-09 03:22:50 +00:00
// Default constructor
zip_iterator() { }
// Constructor from iterator tuple
zip_iterator(IteratorTuple iterator_tuple)
: m_iterator_tuple(iterator_tuple)
2003-09-09 03:22:50 +00:00
{ }
// Copy constructor
template<typename OtherIteratorTuple>
zip_iterator(
const zip_iterator<OtherIteratorTuple>& other,
typename enable_if_convertible<
OtherIteratorTuple,
IteratorTuple
>::type* = 0
) : m_iterator_tuple(other.get_iterator_tuple())
{}
// Get method for the iterator tuple.
const IteratorTuple& get_iterator_tuple() const
{ return m_iterator_tuple; }
private:
2003-09-09 03:22:50 +00:00
// Implementation of Iterator Operations
// =====================================
2003-09-09 03:22:50 +00:00
// Dereferencing returns a tuple built from the dereferenced
// iterators in the iterator tuple.
typename super_t::reference dereference() const
{
typedef typename super_t::reference reference;
typedef typename fusion::traits::tag_of<reference>::type tag;
return fusion::convert<tag>(fusion::transform(
get_iterator_tuple(),
detail::dereference_iterator()));
2003-09-09 03:22:50 +00:00
}
// Two zip iterators are equal if all iterators in the iterator
// tuple are equal. NOTE: It should be possible to implement this
// as
//
// return get_iterator_tuple() == other.get_iterator_tuple();
//
// but equality of tuples currently (7/2003) does not compile
// under several compilers. No point in bringing in a bunch
// of #ifdefs here.
//
template<typename OtherIteratorTuple>
2003-09-09 03:22:50 +00:00
bool equal(const zip_iterator<OtherIteratorTuple>& other) const
{
return fusion::equal_to(
get_iterator_tuple(),
other.get_iterator_tuple());
2003-09-09 03:22:50 +00:00
}
// Advancing a zip iterator means to advance all iterators in the
// iterator tuple.
void advance(typename super_t::difference_type n)
{
fusion::for_each(
2003-09-09 03:22:50 +00:00
m_iterator_tuple,
detail::advance_iterator<BOOST_DEDUCED_TYPENAME super_t::difference_type>(n));
2003-09-09 03:22:50 +00:00
}
// Incrementing a zip iterator means to increment all iterators in
// the iterator tuple.
void increment()
{
fusion::for_each(
m_iterator_tuple,
detail::increment_iterator());
2003-09-09 03:22:50 +00:00
}
2003-09-09 03:22:50 +00:00
// Decrementing a zip iterator means to decrement all iterators in
// the iterator tuple.
void decrement()
{
fusion::for_each(
m_iterator_tuple,
detail::decrement_iterator());
2003-09-09 03:22:50 +00:00
}
2003-09-09 03:22:50 +00:00
// Distance is calculated using the first iterator in the tuple.
template<typename OtherIteratorTuple>
typename super_t::difference_type distance_to(
const zip_iterator<OtherIteratorTuple>& other
) const
{
return fusion::at_c<0>(other.get_iterator_tuple()) -
fusion::at_c<0>(this->get_iterator_tuple());
2003-09-09 03:22:50 +00:00
}
2003-09-09 03:22:50 +00:00
// Data Members
// ============
2003-09-09 03:22:50 +00:00
// The iterator tuple.
IteratorTuple m_iterator_tuple;
2003-09-09 03:22:50 +00:00
};
// Make function for zip iterator
//
template<typename IteratorTuple>
zip_iterator<IteratorTuple>
2003-09-09 03:22:50 +00:00
make_zip_iterator(IteratorTuple t)
{ return zip_iterator<IteratorTuple>(t); }
}
#endif