iterator_plus_distance.hpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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_PLUS_DISTANCE_HPP
  11. #define BOOST_COMPUTE_DETAIL_ITERATOR_PLUS_DISTANCE_HPP
  12. #include <iterator>
  13. namespace boost {
  14. namespace compute {
  15. namespace detail {
  16. template<class Iterator, class Distance, class Tag>
  17. inline Iterator iterator_plus_distance(Iterator i, Distance n, Tag)
  18. {
  19. while(n--){ i++; }
  20. return i;
  21. }
  22. template<class Iterator, class Distance>
  23. inline Iterator iterator_plus_distance(Iterator i,
  24. Distance n,
  25. std::random_access_iterator_tag)
  26. {
  27. typedef typename
  28. std::iterator_traits<Iterator>::difference_type difference_type;
  29. return i + static_cast<difference_type>(n);
  30. }
  31. // similar to std::advance() except returns the advanced iterator and
  32. // also works with iterators that don't define difference_type
  33. template<class Iterator, class Distance>
  34. inline Iterator iterator_plus_distance(Iterator i, Distance n)
  35. {
  36. typedef typename std::iterator_traits<Iterator>::iterator_category tag;
  37. return iterator_plus_distance(i, n, tag());
  38. }
  39. } // end detail namespace
  40. } // end compute namespace
  41. } // end boost namespace
  42. #endif // BOOST_COMPUTE_DETAIL_ITERATOR_PLUS_DISTANCE_HPP