overwrite.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Boost.Range library
  2. //
  3. // Copyright Neil Groves 2009. Use, modification and
  4. // distribution is subject to the Boost Software License, Version
  5. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // For more information, see http://www.boost.org/libs/range/
  9. //
  10. #ifndef BOOST_RANGE_ALGORITHM_EXT_OVERWRITE_HPP_INCLUDED
  11. #define BOOST_RANGE_ALGORITHM_EXT_OVERWRITE_HPP_INCLUDED
  12. #include <boost/range/config.hpp>
  13. #include <boost/range/concepts.hpp>
  14. #include <boost/range/difference_type.hpp>
  15. #include <boost/range/iterator.hpp>
  16. #include <boost/range/begin.hpp>
  17. #include <boost/range/end.hpp>
  18. #include <boost/assert.hpp>
  19. namespace boost
  20. {
  21. namespace range
  22. {
  23. template< class SinglePassRange1, class SinglePassRange2 >
  24. inline void overwrite( const SinglePassRange1& from, SinglePassRange2& to )
  25. {
  26. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange1> ));
  27. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<SinglePassRange2> ));
  28. BOOST_DEDUCED_TYPENAME range_iterator<const SinglePassRange1>::type
  29. i = boost::begin(from), e = boost::end(from);
  30. BOOST_DEDUCED_TYPENAME range_iterator<SinglePassRange2>::type
  31. out = boost::begin(to);
  32. #ifndef NDEBUG
  33. BOOST_DEDUCED_TYPENAME range_iterator<SinglePassRange2>::type
  34. last_out = boost::end(to);
  35. #endif
  36. for( ; i != e; ++out, ++i )
  37. {
  38. #ifndef NDEBUG
  39. BOOST_ASSERT( out != last_out
  40. && "out of bounds in boost::overwrite()" );
  41. #endif
  42. *out = *i;
  43. }
  44. }
  45. template< class SinglePassRange1, class SinglePassRange2 >
  46. inline void overwrite( const SinglePassRange1& from, const SinglePassRange2& to )
  47. {
  48. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange1> ));
  49. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange2> ));
  50. BOOST_DEDUCED_TYPENAME range_iterator<const SinglePassRange1>::type
  51. i = boost::begin(from), e = boost::end(from);
  52. BOOST_DEDUCED_TYPENAME range_iterator<const SinglePassRange2>::type
  53. out = boost::begin(to);
  54. #ifndef NDEBUG
  55. BOOST_DEDUCED_TYPENAME range_iterator<const SinglePassRange2>::type
  56. last_out = boost::end(to);
  57. #endif
  58. for( ; i != e; ++out, ++i )
  59. {
  60. #ifndef NDEBUG
  61. BOOST_ASSERT( out != last_out
  62. && "out of bounds in boost::overwrite()" );
  63. #endif
  64. *out = *i;
  65. }
  66. }
  67. } // namespace range
  68. using range::overwrite;
  69. } // namespace boost
  70. #endif // include guard