Files
boost_iterator/include/boost/iterator/counting_iterator.hpp
T

248 lines
6.9 KiB
C++
Raw Normal View History

// Copyright David Abrahams 2003.
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
2003-07-07 14:14:36 +00:00
#ifndef COUNTING_ITERATOR_DWA200348_HPP
# define COUNTING_ITERATOR_DWA200348_HPP
2024-06-09 15:20:41 +03:00
# include <type_traits>
# include <boost/config.hpp>
2024-06-09 15:20:41 +03:00
# include <boost/detail/workaround.hpp>
# ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
# include <limits>
# endif
# include <boost/type_traits/type_identity.hpp>
2003-07-07 14:14:36 +00:00
# include <boost/detail/numeric_traits.hpp>
# include <boost/iterator/iterator_adaptor.hpp>
2003-07-07 14:14:36 +00:00
namespace boost {
namespace iterators {
2003-07-07 14:14:36 +00:00
template <
class Incrementable
, class CategoryOrTraversal
, class Difference
>
class counting_iterator;
2003-07-07 14:14:36 +00:00
namespace detail
{
// Try to detect numeric types at compile time in ways compatible
// with the limitations of the compiler and library.
template <class T>
struct is_numeric_impl
{
2003-07-07 14:14:36 +00:00
# ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
2003-07-07 14:14:36 +00:00
BOOST_STATIC_CONSTANT(bool, value = std::numeric_limits<T>::is_specialized);
2003-07-07 14:14:36 +00:00
# else
# if !BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x551))
2003-07-07 14:14:36 +00:00
BOOST_STATIC_CONSTANT(
bool, value = (
2024-06-09 15:20:41 +03:00
std::is_convertible<int,T>::value
&& std::is_convertible<T,int>::value
2003-07-07 14:14:36 +00:00
));
# else
2024-06-09 15:20:41 +03:00
BOOST_STATIC_CONSTANT(bool, value = std::is_arithmetic<T>::value);
2003-07-07 14:14:36 +00:00
# endif
2003-07-07 14:14:36 +00:00
# endif
};
template <class T>
struct is_numeric
2024-06-09 15:20:41 +03:00
: std::integral_constant<bool, ::boost::iterators::detail::is_numeric_impl<T>::value>
2003-07-07 14:14:36 +00:00
{};
# if defined(BOOST_HAS_LONG_LONG)
template <>
struct is_numeric<boost::long_long_type>
: boost::true_type {};
template <>
struct is_numeric<boost::ulong_long_type>
: boost::true_type {};
# endif
# if defined(BOOST_HAS_INT128)
template <>
struct is_numeric<boost::int128_type>
: boost::true_type {};
template <>
struct is_numeric<boost::uint128_type>
: boost::true_type {};
# endif
// Some compilers fail to have a numeric_limits specialization
template <>
struct is_numeric<wchar_t>
: true_type {};
2003-07-07 14:14:36 +00:00
template <class T>
struct numeric_difference
{
typedef typename boost::detail::numeric_traits<T>::difference_type type;
};
# if defined(BOOST_HAS_INT128)
// std::numeric_limits, which is used by numeric_traits, is not specialized for __int128 in some standard libraries
template <>
struct numeric_difference<boost::int128_type>
{
typedef boost::int128_type type;
};
template <>
struct numeric_difference<boost::uint128_type>
{
typedef boost::int128_type type;
};
# endif
template <class Incrementable, class CategoryOrTraversal, class Difference>
2003-07-07 14:14:36 +00:00
struct counting_iterator_base
{
typedef typename detail::ia_dflt_help<
CategoryOrTraversal
2024-06-09 15:20:41 +03:00
, typename std::conditional<
is_numeric<Incrementable>::value
, boost::type_identity<random_access_traversal_tag>
, iterator_traversal<Incrementable>
>::type
>::type traversal;
typedef typename detail::ia_dflt_help<
Difference
2024-06-09 15:20:41 +03:00
, typename std::conditional<
is_numeric<Incrementable>::value
2003-07-07 14:14:36 +00:00
, numeric_difference<Incrementable>
, iterator_difference<Incrementable>
>::type
2003-07-07 14:14:36 +00:00
>::type difference;
2003-07-07 14:14:36 +00:00
typedef iterator_adaptor<
counting_iterator<Incrementable, CategoryOrTraversal, Difference> // self
, Incrementable // Base
, Incrementable // Value
# ifndef BOOST_ITERATOR_REF_CONSTNESS_KILLS_WRITABILITY
const // MSVC won't strip this. Instead we enable Thomas'
// criterion (see boost/iterator/detail/facade_iterator_category.hpp)
# endif
, traversal
, Incrementable const& // reference
2003-07-07 14:14:36 +00:00
, difference
> type;
};
// Template class distance_policy_select -- choose a policy for computing the
// distance between counting_iterators at compile-time based on whether or not
// the iterator wraps an integer or an iterator, using "poor man's partial
// specialization".
template <bool is_integer> struct distance_policy_select;
// A policy for wrapped iterators
template <class Difference, class Incrementable1, class Incrementable2>
struct iterator_distance
{
static Difference distance(Incrementable1 x, Incrementable2 y)
{
return y - x;
2003-07-07 14:14:36 +00:00
}
};
// A policy for wrapped numbers
template <class Difference, class Incrementable1, class Incrementable2>
struct number_distance
{
static Difference distance(Incrementable1 x, Incrementable2 y)
{
return boost::detail::numeric_distance(x, y);
2003-07-07 14:14:36 +00:00
}
};
}
template <
class Incrementable
, class CategoryOrTraversal = use_default
, class Difference = use_default
>
2003-07-07 14:14:36 +00:00
class counting_iterator
: public detail::counting_iterator_base<
Incrementable, CategoryOrTraversal, Difference
>::type
2003-07-07 14:14:36 +00:00
{
typedef typename detail::counting_iterator_base<
Incrementable, CategoryOrTraversal, Difference
>::type super_t;
2003-07-07 14:14:36 +00:00
friend class iterator_core_access;
public:
typedef typename super_t::difference_type difference_type;
BOOST_DEFAULTED_FUNCTION(counting_iterator(), {})
BOOST_DEFAULTED_FUNCTION(counting_iterator(counting_iterator const& rhs), : super_t(rhs.base()) {})
2003-07-07 14:14:36 +00:00
counting_iterator(Incrementable x)
: super_t(x)
{
}
# if 0
template<class OtherIncrementable>
counting_iterator(
counting_iterator<OtherIncrementable, CategoryOrTraversal, Difference> const& t
2003-07-07 14:14:36 +00:00
, typename enable_if_convertible<OtherIncrementable, Incrementable>::type* = 0
)
: super_t(t.base())
{}
# endif
2003-07-07 14:14:36 +00:00
BOOST_DEFAULTED_FUNCTION(counting_iterator& operator=(counting_iterator const& rhs), { *static_cast< super_t* >(this) = static_cast< super_t const& >(rhs); return *this; })
2003-07-07 14:14:36 +00:00
private:
2003-07-07 14:14:36 +00:00
typename super_t::reference dereference() const
{
return this->base_reference();
}
template <class OtherIncrementable>
difference_type
distance_to(counting_iterator<OtherIncrementable, CategoryOrTraversal, Difference> const& y) const
2003-07-07 14:14:36 +00:00
{
2024-06-09 15:20:41 +03:00
typedef typename std::conditional<
detail::is_numeric<Incrementable>::value
, detail::number_distance<difference_type, Incrementable, OtherIncrementable>
, detail::iterator_distance<difference_type, Incrementable, OtherIncrementable>
>::type d;
2003-07-07 14:14:36 +00:00
return d::distance(this->base(), y.base());
2003-07-07 14:14:36 +00:00
}
};
// Manufacture a counting iterator for an arbitrary incrementable type
template <class Incrementable>
inline counting_iterator<Incrementable>
make_counting_iterator(Incrementable x)
{
typedef counting_iterator<Incrementable> result_t;
return result_t(x);
2003-07-07 14:14:36 +00:00
}
} // namespace iterators
2003-07-07 14:14:36 +00:00
using iterators::counting_iterator;
using iterators::make_counting_iterator;
} // namespace boost
2003-07-07 14:14:36 +00:00
#endif // COUNTING_ITERATOR_DWA200348_HPP