1 | // Copyright (C) 2017 Michel Morin. |
---|---|
2 | // |
3 | // Distributed under the Boost Software License, Version 1.0. |
4 | // (See accompanying file LICENSE_1_0.txt or copy at |
5 | // http://www.boost.org/LICENSE_1_0.txt) |
6 | |
7 | #ifndef BOOST_ITERATOR_DISTANCE_HPP |
8 | #define BOOST_ITERATOR_DISTANCE_HPP |
9 | |
10 | #include <boost/config.hpp> |
11 | #include <boost/iterator/iterator_categories.hpp> |
12 | #include <boost/iterator/iterator_traits.hpp> |
13 | |
14 | namespace boost { |
15 | namespace iterators { |
16 | |
17 | namespace detail { |
18 | template <typename SinglePassIterator> |
19 | inline BOOST_CXX14_CONSTEXPR typename iterator_difference<SinglePassIterator>::type |
20 | distance_impl( |
21 | SinglePassIterator first |
22 | , SinglePassIterator last |
23 | , single_pass_traversal_tag |
24 | ) |
25 | { |
26 | typename iterator_difference<SinglePassIterator>::type n = 0; |
27 | while (first != last) { |
28 | ++first; |
29 | ++n; |
30 | } |
31 | return n; |
32 | } |
33 | |
34 | template <typename RandomAccessIterator> |
35 | inline BOOST_CXX14_CONSTEXPR typename iterator_difference<RandomAccessIterator>::type |
36 | distance_impl( |
37 | RandomAccessIterator first |
38 | , RandomAccessIterator last |
39 | , random_access_traversal_tag |
40 | ) |
41 | { |
42 | return last - first; |
43 | } |
44 | } |
45 | |
46 | namespace distance_adl_barrier { |
47 | template <typename SinglePassIterator> |
48 | inline BOOST_CXX14_CONSTEXPR typename iterator_difference<SinglePassIterator>::type |
49 | distance(SinglePassIterator first, SinglePassIterator last) |
50 | { |
51 | return detail::distance_impl( |
52 | first, last, typename iterator_traversal<SinglePassIterator>::type() |
53 | ); |
54 | } |
55 | } |
56 | |
57 | using namespace distance_adl_barrier; |
58 | |
59 | } // namespace iterators |
60 | |
61 | using namespace iterators::distance_adl_barrier; |
62 | |
63 | } // namespace boost |
64 | |
65 | #endif |
66 |