1 // (C) Copyright David Abrahams and Jeremy Siek 2000-2001. Permission to copy,
2 // use, modify, sell and distribute this software is granted provided this
3 // copyright notice appears in all copies. This software is provided "as is"
4 // without express or implied warranty, and with no claim as to its suitability
7 // See http://www.boost.org/libs/utility/counting_iterator.htm for documentation.
11 // template <class Incrementable> class counting_iterator_traits;
12 // template <class Incrementable> class counting_iterator_policies;
14 // Iterator traits and policies for adapted iterators whose dereferenced
15 // value progresses through consecutive values of Incrementable when the
16 // iterator is derferenced.
18 // template <class Incrementable> struct counting_iterator_generator;
20 // A "type generator" whose nested type "type" is a counting iterator as
23 // template <class Incrementable>
24 // typename counting_iterator_generator<Incrementable>::type
25 // make_counting_iterator(Incrementable);
27 // A function which produces an adapted counting iterator over values of
31 // 14 Feb 2001 Removed unnecessary typedefs from counting_iterator_traits
33 // 11 Feb 2001 Use BOOST_STATIC_CONSTANT (Dave Abrahams)
34 // 11 Feb 2001 Clean up after John Maddocks's (finally effective!) Borland
35 // fixes (David Abrahams).
36 // 10 Feb 2001 Use new iterator_adaptor<> interface (David Abrahams)
37 // 10 Feb 2001 Rolled in supposed Borland fixes from John Maddock, but not
38 // seeing any improvement yet (David Abrahams)
39 // 09 Feb 2001 Factored out is_numeric computation. Borland still
40 // unhappy :( (David Abrahams)
41 // 08 Feb 2001 Beginning of a failed attempt to appease Borland
43 // 07 Feb 2001 rename counting_iterator() -> make_counting_iterator()
45 // 04 Feb 2001 Added counting_iterator_generator; updated comments
47 // 24 Jan 2001 initial revision, based on Jeremy Siek's
48 // boost/pending/integer_range.hpp (David Abrahams)
50 #ifndef BOOST_COUNTING_ITERATOR_HPP_DWA20000119
51 # define BOOST_COUNTING_ITERATOR_HPP_DWA20000119
53 # include <boost/config.hpp>
54 # include <boost/detail/iterator.hpp>
55 # include <boost/iterator_adaptors.hpp>
56 # include <boost/type_traits.hpp>
57 # include <boost/detail/numeric_traits.hpp>
58 # include <boost/static_assert.hpp>
59 # include <boost/limits.hpp>
65 // Template class counting_iterator_traits_select -- choose an
66 // iterator_category and difference_type for a counting_iterator at
67 // compile-time based on whether or not it wraps an integer or an iterator,
68 // using "poor man's partial specialization".
69 template <bool is_integer> struct counting_iterator_traits_select;
71 // Incrementable is an iterator type
73 struct counting_iterator_traits_select<false>
75 template <class Incrementable>
79 typedef boost::detail::iterator_traits<Incrementable> x;
81 typedef typename x::iterator_category iterator_category;
82 typedef typename x::difference_type difference_type;
86 // Incrementable is a numeric type
88 struct counting_iterator_traits_select<true>
90 template <class Incrementable>
94 boost::detail::numeric_traits<Incrementable>::difference_type
96 typedef std::random_access_iterator_tag iterator_category;
100 // Template class distance_policy_select -- choose a policy for computing the
101 // distance between counting_iterators at compile-time based on whether or not
102 // the iterator wraps an integer or an iterator, using "poor man's partial
105 template <bool is_integer> struct distance_policy_select;
107 // A policy for wrapped iterators
109 struct distance_policy_select<false>
111 template <class Distance, class Incrementable>
113 static Distance distance(Incrementable x, Incrementable y)
114 { return boost::detail::distance(x, y); }
118 // A policy for wrapped numbers
120 struct distance_policy_select<true>
122 template <class Distance, class Incrementable>
124 static Distance distance(Incrementable x, Incrementable y)
125 { return numeric_distance(x, y); }
129 // Try to detect numeric types at compile time in ways compatible with the
130 // limitations of the compiler and library.
133 // For a while, this wasn't true, but we rely on it below. This is a regression assert.
134 BOOST_STATIC_ASSERT(::boost::is_integral<char>::value);
135 # ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
136 # if defined(BOOST_HAS_LONG_LONG)
137 BOOST_STATIC_CONSTANT(bool,
139 std::numeric_limits<T>::is_specialized
140 | boost::is_same<T,long long>::value
141 | boost::is_same<T,unsigned long long>::value));
143 BOOST_STATIC_CONSTANT(bool, value = std::numeric_limits<T>::is_specialized);
146 # if !defined(__BORLANDC__)
147 BOOST_STATIC_CONSTANT(bool, value = (
148 boost::is_convertible<int,T>::value && boost::is_convertible<T,int>::value));
150 BOOST_STATIC_CONSTANT(bool, value = ::boost::is_arithmetic<T>::value);
155 // Compute the distance over arbitrary numeric and/or iterator types
156 template <class Distance, class Incrementable>
157 Distance any_distance(Incrementable start, Incrementable finish, Distance* = 0)
160 return distance_policy_select<(
161 is_numeric<Incrementable>::value)>::template
162 policy<Distance, Incrementable>::distance(start, finish);
165 } // namespace detail
167 template <class Incrementable>
168 struct counting_iterator_traits {
170 typedef ::boost::detail::counting_iterator_traits_select<(
171 ::boost::detail::is_numeric<Incrementable>::value
173 typedef typename binder::template traits<Incrementable> traits;
175 typedef typename traits::difference_type difference_type;
176 typedef typename traits::iterator_category iterator_category;
179 template <class Incrementable>
180 struct counting_iterator_policies : public default_iterator_policies
182 template <class IteratorAdaptor>
183 typename IteratorAdaptor::reference dereference(const IteratorAdaptor& i) const
186 template <class Iterator1, class Iterator2>
187 typename Iterator1::difference_type distance(
188 const Iterator1& x, const Iterator2& y) const
190 typedef typename Iterator1::difference_type difference_type;
191 return boost::detail::any_distance<difference_type>(
196 // A type generator for counting iterators
197 template <class Incrementable>
198 struct counting_iterator_generator
200 typedef typename boost::remove_const<
204 typedef counting_iterator_traits<value_type> traits;
206 typedef iterator_adaptor<
208 , counting_iterator_policies<value_type>
212 , typename traits::iterator_category
213 , typename traits::difference_type
217 // Manufacture a counting iterator for an arbitrary incrementable type
218 template <class Incrementable>
219 inline typename counting_iterator_generator<Incrementable>::type
220 make_counting_iterator(Incrementable x)
222 typedef typename counting_iterator_generator<Incrementable>::type result_t;
229 #endif // BOOST_COUNTING_ITERATOR_HPP_DWA20000119