iterator_range_size.hpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. #ifndef BOOST_COMPUTE_DETAIL_ITERATOR_RANGE_SIZE_H
  11. #define BOOST_COMPUTE_DETAIL_ITERATOR_RANGE_SIZE_H
  12. #include <cstddef>
  13. #include <algorithm>
  14. #include <iterator>
  15. namespace boost {
  16. namespace compute {
  17. namespace detail {
  18. // This is a convenience function which returns the size of a range
  19. // bounded by two iterators. This function has two differences from
  20. // the std::distance() function: 1) the return type (size_t) is
  21. // unsigned, and 2) the return value is always positive.
  22. template<class Iterator>
  23. inline size_t iterator_range_size(Iterator first, Iterator last)
  24. {
  25. typedef typename
  26. std::iterator_traits<Iterator>::difference_type
  27. difference_type;
  28. difference_type difference = std::distance(first, last);
  29. return static_cast<size_t>(
  30. (std::max)(difference, static_cast<difference_type>(0))
  31. );
  32. }
  33. } // end detail namespace
  34. } // end compute namespace
  35. } // end boost namespace
  36. #endif // BOOST_COMPUTE_DETAIL_ITERATOR_RANGE_SIZE_H