1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
3 //
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
7 //
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
10 
11 #ifndef BOOST_COMPUTE_DETAIL_ITERATOR_RANGE_SIZE_H
12 #define BOOST_COMPUTE_DETAIL_ITERATOR_RANGE_SIZE_H
13 
14 #include <cstddef>
15 #include <algorithm>
16 #include <iterator>
17 
18 namespace boost {
19 namespace compute {
20 namespace detail {
21 
22 // This is a convenience function which returns the size of a range
23 // bounded by two iterators. This function has two differences from
24 // the std::distance() function: 1) the return type (size_t) is
25 // unsigned, and 2) the return value is always positive.
26 template<class Iterator>
iterator_range_size(Iterator first,Iterator last)27 inline size_t iterator_range_size(Iterator first, Iterator last)
28 {
29     typedef typename
30         std::iterator_traits<Iterator>::difference_type
31         difference_type;
32 
33     difference_type difference = std::distance(first, last);
34 
35     return static_cast<size_t>(
36         (std::max)(difference, static_cast<difference_type>(0))
37     );
38 }
39 
40 } // end detail namespace
41 } // end compute namespace
42 } // end boost namespace
43 
44 #endif // BOOST_COMPUTE_DETAIL_ITERATOR_RANGE_SIZE_H
45