fill_n.hpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright Neil Groves 2009. 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_ALGORITHM_FILL_N_HPP_INCLUDED
  10. #define BOOST_RANGE_ALGORITHM_FILL_N_HPP_INCLUDED
  11. #include <boost/assert.hpp>
  12. #include <boost/concept_check.hpp>
  13. #include <boost/range/begin.hpp>
  14. #include <boost/range/end.hpp>
  15. #include <boost/range/concepts.hpp>
  16. #include <algorithm>
  17. namespace boost
  18. {
  19. namespace range
  20. {
  21. /// \brief template function fill_n
  22. ///
  23. /// range-based version of the fill_n std algorithm
  24. ///
  25. /// \pre ForwardRange is a model of the ForwardRangeConcept
  26. /// \pre n <= std::distance(boost::begin(rng), boost::end(rng))
  27. template< class ForwardRange, class Size, class Value >
  28. inline ForwardRange& fill_n(ForwardRange& rng, Size n, const Value& val)
  29. {
  30. BOOST_RANGE_CONCEPT_ASSERT(( ForwardRangeConcept<ForwardRange> ));
  31. BOOST_ASSERT( static_cast<Size>(std::distance(boost::begin(rng), boost::end(rng))) >= n );
  32. std::fill_n(boost::begin(rng), n, val);
  33. return rng;
  34. }
  35. /// \overload
  36. template< class ForwardRange, class Size, class Value >
  37. inline const ForwardRange& fill_n(const ForwardRange& rng, Size n, const Value& val)
  38. {
  39. BOOST_RANGE_CONCEPT_ASSERT(( ForwardRangeConcept<const ForwardRange> ));
  40. BOOST_ASSERT( static_cast<Size>(std::distance(boost::begin(rng), boost::end(rng))) >= n );
  41. std::fill_n(boost::begin(rng), n, val);
  42. return rng;
  43. }
  44. } // namespace range
  45. using range::fill_n;
  46. } // namespace boost
  47. #endif // include guard