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

363 lines
10 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 <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>
#include <boost/iterator/minimum_category.hpp>
#include <utility> // for std::pair
#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>
Mostly remove pre-CXX11 workarounds. С++03 support was deprecated in 1.85 and now can be removed. This PR clears many of workarounds, which are no longer needed now. * Remove unused workaround macros (many of). * Remove BOOST_STATIC_ASSERT usages. * Minimize Boost::type_traits dependency (in favour of STL's type_traits). Closes https://github.com/boostorg/iterator/pull/82. Squashed commit of the following: commit 741a627b736ba81fe0054e5bf373141b04a8a597 Author: Georgy Guminov <gogagum@gmail.com> Date: Sat Jan 25 12:13:05 2025 +0300 Replace testers with standard metafunctions. commit bf4cce611454713f1c8e5f46a2c3e599c548656d Author: Georgy Guminov <gogagum@gmail.com> Date: Sat Jan 25 11:51:32 2025 +0300 Refactor is_lvalue_iterator.hpp. commit 8d080c6c58726269cf55aedd64aa239f7d098fc7 Author: Georgy Guminov <gogagum@gmail.com> Date: Sat Jan 25 10:27:32 2025 +0300 Remove more workarounds. commit 5a4ba24d361ac0676d2cce95fdff22824ecdc287 Author: Georgy Guminov <gogagum@gmail.com> Date: Sun Jan 19 16:38:30 2025 +0300 Fixes. commit fdfafce2b9a71b1d85cbc697983652788f1c4bb7 Author: Georgy Guminov <gogagum@gmail.com> Date: Sat Oct 26 15:06:43 2024 +0300 Remove BOOST_NO_STRICT_ITERATOR_INTEROPERABILITY Correct static_assert messages. Fix messages & replace is_standard_layout with is_copy_constructible. commit c69ac1408af2fe5a105b288644367bbb6e0bc30d Author: Georgy Guminov <gogagum@gmail.com> Date: Sat Oct 26 14:48:51 2024 +0300 Correct static_assert messages. commit b5df827151a6d752168a59fa8c20c9ffd3766c0b Author: Georqy Guminov <gogagum@gmail.com> Date: Sun Jun 23 16:12:29 2024 +0300 Fixes. Remove some Boost.MPL usages. Remove unused includes. commit 01fd35e9f87d43f4bb46ed525a0e8eec46cba44a Author: Georgiy Guminov <gogagum@gmail.com> Date: Wed Jun 12 17:14:21 2024 +0300 abstract conjunction. commit c02def8acf68e0829082761d26955320974e5078 Author: Georgiy Guminov <gogagum@gmail.com> Date: Wed Jun 12 16:35:43 2024 +0300 return addressof & conjunction. commit 3b3d1625752cbe8c9ec62662e594a1af1fd9a458 Author: Georgiy Guminov <gogagum@gmail.com> Date: Wed Jun 12 16:30:44 2024 +0300 Make macro more readable. commit 4ab19e045fc535dc80228062d11bb6584ccd17ff Author: Georgiy Guminov <gogagum@gmail.com> Date: Wed Jun 12 15:56:49 2024 +0300 Add static_assert messages. commit 82b5c44cd34435d63af5cdbc7fcc1b07b39394de Author: Georgiy Guminov <gogagum@gmail.com> Date: Wed Jun 12 14:12:10 2024 +0300 Return is iterator CXX17 test. commit 2d58d65462e837430a0990c2a414d5c397e9fa31 Author: Georgiy Guminov <gogagum@gmail.com> Date: Tue Jun 11 14:04:17 2024 +0300 Omitted. commit a0d04d9491de818df990188436e04afc3ddba3ad Author: Georgiy Guminov <gogagum@gmail.com> Date: Tue Jun 11 14:00:35 2024 +0300 Replace move with static_cast commit 4a49b8a1a2d44d2da728dd064fe810ca86d8d1fd Author: Georgiy Guminov <gogagum@gmail.com> Date: Mon Jun 10 21:38:53 2024 +0300 Return BOOST_NOEXCEPT commit 054c013bba75c42710537a819de91d6abfe25658 Author: Georgiy Guminov <gogagum@gmail.com> Date: Sun Jun 9 15:20:41 2024 +0300 CXX11
2025-01-26 15:17:36 +03:00
#include <boost/fusion/adapted/boost_tuple.hpp> // for backward compatibility
#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 {
namespace iterators {
2003-09-09 03:22:50 +00:00
// 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
Mostly remove pre-CXX11 workarounds. С++03 support was deprecated in 1.85 and now can be removed. This PR clears many of workarounds, which are no longer needed now. * Remove unused workaround macros (many of). * Remove BOOST_STATIC_ASSERT usages. * Minimize Boost::type_traits dependency (in favour of STL's type_traits). Closes https://github.com/boostorg/iterator/pull/82. Squashed commit of the following: commit 741a627b736ba81fe0054e5bf373141b04a8a597 Author: Georgy Guminov <gogagum@gmail.com> Date: Sat Jan 25 12:13:05 2025 +0300 Replace testers with standard metafunctions. commit bf4cce611454713f1c8e5f46a2c3e599c548656d Author: Georgy Guminov <gogagum@gmail.com> Date: Sat Jan 25 11:51:32 2025 +0300 Refactor is_lvalue_iterator.hpp. commit 8d080c6c58726269cf55aedd64aa239f7d098fc7 Author: Georgy Guminov <gogagum@gmail.com> Date: Sat Jan 25 10:27:32 2025 +0300 Remove more workarounds. commit 5a4ba24d361ac0676d2cce95fdff22824ecdc287 Author: Georgy Guminov <gogagum@gmail.com> Date: Sun Jan 19 16:38:30 2025 +0300 Fixes. commit fdfafce2b9a71b1d85cbc697983652788f1c4bb7 Author: Georgy Guminov <gogagum@gmail.com> Date: Sat Oct 26 15:06:43 2024 +0300 Remove BOOST_NO_STRICT_ITERATOR_INTEROPERABILITY Correct static_assert messages. Fix messages & replace is_standard_layout with is_copy_constructible. commit c69ac1408af2fe5a105b288644367bbb6e0bc30d Author: Georgy Guminov <gogagum@gmail.com> Date: Sat Oct 26 14:48:51 2024 +0300 Correct static_assert messages. commit b5df827151a6d752168a59fa8c20c9ffd3766c0b Author: Georqy Guminov <gogagum@gmail.com> Date: Sun Jun 23 16:12:29 2024 +0300 Fixes. Remove some Boost.MPL usages. Remove unused includes. commit 01fd35e9f87d43f4bb46ed525a0e8eec46cba44a Author: Georgiy Guminov <gogagum@gmail.com> Date: Wed Jun 12 17:14:21 2024 +0300 abstract conjunction. commit c02def8acf68e0829082761d26955320974e5078 Author: Georgiy Guminov <gogagum@gmail.com> Date: Wed Jun 12 16:35:43 2024 +0300 return addressof & conjunction. commit 3b3d1625752cbe8c9ec62662e594a1af1fd9a458 Author: Georgiy Guminov <gogagum@gmail.com> Date: Wed Jun 12 16:30:44 2024 +0300 Make macro more readable. commit 4ab19e045fc535dc80228062d11bb6584ccd17ff Author: Georgiy Guminov <gogagum@gmail.com> Date: Wed Jun 12 15:56:49 2024 +0300 Add static_assert messages. commit 82b5c44cd34435d63af5cdbc7fcc1b07b39394de Author: Georgiy Guminov <gogagum@gmail.com> Date: Wed Jun 12 14:12:10 2024 +0300 Return is iterator CXX17 test. commit 2d58d65462e837430a0990c2a414d5c397e9fa31 Author: Georgiy Guminov <gogagum@gmail.com> Date: Tue Jun 11 14:04:17 2024 +0300 Omitted. commit a0d04d9491de818df990188436e04afc3ddba3ad Author: Georgiy Guminov <gogagum@gmail.com> Date: Tue Jun 11 14:00:35 2024 +0300 Replace move with static_cast commit 4a49b8a1a2d44d2da728dd064fe810ca86d8d1fd Author: Georgiy Guminov <gogagum@gmail.com> Date: Mon Jun 10 21:38:53 2024 +0300 Return BOOST_NOEXCEPT commit 054c013bba75c42710537a819de91d6abfe25658 Author: Georgiy Guminov <gogagum@gmail.com> Date: Sun Jun 9 15:20:41 2024 +0300 CXX11
2025-01-26 15:17:36 +03:00
std::remove_cv<typename std::remove_reference<Iterator>::type>::type
iterator;
typedef typename iterator_reference<iterator>::type type;
2003-09-09 03:22:50 +00:00
};
template<typename Iterator>
typename result<dereference_iterator(Iterator)>::type
operator()(Iterator const& it) const
2003-09-09 03:22:50 +00:00
{ return *it; }
};
// 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>
>
{
};
// Specialization for std::pair
template<typename Iterator1, typename Iterator2>
struct tuple_of_references<std::pair<Iterator1, Iterator2> >
{
typedef std::pair<
typename iterator_reference<Iterator1>::type
, typename iterator_reference<Iterator2>::type
> type;
};
2003-09-09 03:22:50 +00:00
// 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;
};
template<typename Iterator1, typename Iterator2>
struct minimum_traversal_category_in_iterator_tuple<std::pair<Iterator1, Iterator2> >
{
typedef typename pure_traversal_tag<
typename iterator_traversal<Iterator1>::type
>::type iterator1_traversal;
typedef typename pure_traversal_tag<
typename iterator_traversal<Iterator2>::type
>::type iterator2_traversal;
typedef typename minimum_category<
iterator1_traversal
, typename minimum_category<
iterator2_traversal
, random_access_traversal_tag
>::type
>::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_difference<
typename mpl::at_c<IteratorTuple, 0>::type
>::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;
};
template <typename reference>
struct converter
{
template <typename Seq>
static reference call(Seq seq)
{
typedef typename fusion::traits::tag_of<reference>::type tag;
return fusion::convert<tag>(seq);
}
};
template <typename Reference1, typename Reference2>
struct converter<std::pair<Reference1, Reference2> >
{
typedef std::pair<Reference1, Reference2> reference;
template <typename Seq>
static reference call(Seq seq)
{
return reference(
fusion::at_c<0>(seq)
, fusion::at_c<1>(seq));
}
};
2003-09-09 03:22:50 +00:00
}
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 detail::converter<reference> gen;
return gen::call(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>
inline zip_iterator<IteratorTuple>
2003-09-09 03:22:50 +00:00
make_zip_iterator(IteratorTuple t)
{ return zip_iterator<IteratorTuple>(t); }
} // namespace iterators
using iterators::zip_iterator;
using iterators::make_zip_iterator;
} // namespace boost
2003-09-09 03:22:50 +00:00
#endif