xref: /llvm-project/libcxx/include/__algorithm/fill.h (revision 7ed7d4ccb8991e2b5b95334b508f8cec2faee737)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef _LIBCPP___ALGORITHM_FILL_H
10 #define _LIBCPP___ALGORITHM_FILL_H
11 
12 #include <__config>
13 #include <__iterator/iterator_traits.h>
14 #include <type_traits>
15 
16 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17 #pragma GCC system_header
18 #endif
19 
20 _LIBCPP_PUSH_MACROS
21 #include <__undef_macros>
22 
23 _LIBCPP_BEGIN_NAMESPACE_STD
24 
25 // fill_n
26 
27 template <class _OutputIterator, class _Size, class _Tp>
28 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
29 _OutputIterator
30 __fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
31 {
32     for (; __n > 0; ++__first, (void) --__n)
33         *__first = __value_;
34     return __first;
35 }
36 
37 template <class _OutputIterator, class _Size, class _Tp>
38 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
39 _OutputIterator
40 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
41 {
42    return _VSTD::__fill_n(__first, _VSTD::__convert_to_integral(__n), __value_);
43 }
44 
45 // fill
46 
47 template <class _ForwardIterator, class _Tp>
48 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
49 void
50 __fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag)
51 {
52     for (; __first != __last; ++__first)
53         *__first = __value_;
54 }
55 
56 template <class _RandomAccessIterator, class _Tp>
57 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
58 void
59 __fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag)
60 {
61     _VSTD::fill_n(__first, __last - __first, __value_);
62 }
63 
64 template <class _ForwardIterator, class _Tp>
65 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
66 void
67 fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
68 {
69     _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category());
70 }
71 
72 _LIBCPP_END_NAMESPACE_STD
73 
74 _LIBCPP_POP_MACROS
75 
76 #endif // _LIBCPP___ALGORITHM_FILL_H
77