counting_range.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright Neil Groves 2010. Use, modification and
  2. // distribution is subject to the Boost Software License, Version
  3. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. //
  6. //
  7. // For more information, see http://www.boost.org/libs/range/
  8. //
  9. #ifndef BOOST_RANGE_COUNTING_RANGE_HPP_INCLUDED
  10. #define BOOST_RANGE_COUNTING_RANGE_HPP_INCLUDED
  11. #include <boost/config.hpp>
  12. #if BOOST_MSVC >= 1400
  13. #pragma warning(push)
  14. #pragma warning(disable : 4244)
  15. #endif
  16. #include <boost/range/iterator_range_core.hpp>
  17. #include <boost/range/value_type.hpp>
  18. #include <boost/iterator/counting_iterator.hpp>
  19. namespace boost
  20. {
  21. template<class Value>
  22. inline iterator_range<counting_iterator<Value> >
  23. counting_range(Value first, Value last)
  24. {
  25. typedef counting_iterator<Value> counting_iterator_t;
  26. typedef iterator_range<counting_iterator_t> result_t;
  27. return result_t(counting_iterator_t(first),
  28. counting_iterator_t(last));
  29. }
  30. template<class Range>
  31. inline iterator_range<counting_iterator<BOOST_DEDUCED_TYPENAME range_value<const Range>::type> >
  32. counting_range(const Range& rng)
  33. {
  34. typedef counting_iterator<BOOST_DEDUCED_TYPENAME range_value<const Range>::type> counting_iterator_t;
  35. typedef iterator_range<counting_iterator_t> result_t;
  36. return boost::empty(rng)
  37. ? result_t()
  38. : result_t(
  39. counting_iterator_t(*boost::begin(rng)),
  40. counting_iterator_t(*boost::prior(boost::end(rng))));
  41. }
  42. template<class Range>
  43. inline iterator_range<counting_iterator<BOOST_DEDUCED_TYPENAME range_value<Range>::type> >
  44. counting_range(Range& rng)
  45. {
  46. typedef counting_iterator<BOOST_DEDUCED_TYPENAME range_value<Range>::type> counting_iterator_t;
  47. typedef iterator_range<counting_iterator_t> result_t;
  48. return boost::empty(rng)
  49. ? result_t()
  50. : result_t(
  51. counting_iterator_t(*boost::begin(rng)),
  52. counting_iterator_t(*boost::prior(boost::end(rng))));
  53. }
  54. } // namespace boost
  55. #if BOOST_MSVC >= 1400
  56. #pragma warning(pop)
  57. #endif
  58. #endif // include guard