1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___ALGORITHM_FILL_N_H
10 #define _LIBCPP___ALGORITHM_FILL_N_H
12 #include <__cxx03/__algorithm/min.h>
13 #include <__cxx03/__config>
14 #include <__cxx03/__fwd/bit_reference.h>
15 #include <__cxx03/__iterator/iterator_traits.h>
16 #include <__cxx03/__memory/pointer_traits.h>
17 #include <__cxx03/__utility/convert_to_integral.h>
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 # pragma GCC system_header
24 #include <__cxx03/__undef_macros>
26 _LIBCPP_BEGIN_NAMESPACE_STD
28 // fill_n isn't specialized for std::memset, because the compiler already optimizes the loop to a call to std::memset.
30 template <class _OutputIterator
, class _Size
, class _Tp
>
31 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
32 __fill_n(_OutputIterator __first
, _Size __n
, const _Tp
& __value
);
34 template <bool _FillVal
, class _Cp
>
35 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
void
36 __fill_n_bool(__bit_iterator
<_Cp
, false> __first
, typename
_Cp::size_type __n
) {
37 using _It
= __bit_iterator
<_Cp
, false>;
38 using __storage_type
= typename
_It::__storage_type
;
40 const int __bits_per_word
= _It::__bits_per_word
;
41 // do first partial word
42 if (__first
.__ctz_
!= 0) {
43 __storage_type __clz_f
= static_cast<__storage_type
>(__bits_per_word
- __first
.__ctz_
);
44 __storage_type __dn
= std::min(__clz_f
, __n
);
45 __storage_type __m
= (~__storage_type(0) << __first
.__ctz_
) & (~__storage_type(0) >> (__clz_f
- __dn
));
47 *__first
.__seg_
|= __m
;
49 *__first
.__seg_
&= ~__m
;
53 // do middle whole words
54 __storage_type __nw
= __n
/ __bits_per_word
;
55 std::__fill_n(std::__to_address(__first
.__seg_
), __nw
, _FillVal
? static_cast<__storage_type
>(-1) : 0);
56 __n
-= __nw
* __bits_per_word
;
57 // do last partial word
59 __first
.__seg_
+= __nw
;
60 __storage_type __m
= ~__storage_type(0) >> (__bits_per_word
- __n
);
62 *__first
.__seg_
|= __m
;
64 *__first
.__seg_
&= ~__m
;
68 template <class _Cp
, class _Size
>
69 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator
<_Cp
, false>
70 __fill_n(__bit_iterator
<_Cp
, false> __first
, _Size __n
, const bool& __value
) {
73 std::__fill_n_bool
<true>(__first
, __n
);
75 std::__fill_n_bool
<false>(__first
, __n
);
80 template <class _OutputIterator
, class _Size
, class _Tp
>
81 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
82 __fill_n(_OutputIterator __first
, _Size __n
, const _Tp
& __value
) {
83 for (; __n
> 0; ++__first
, (void)--__n
)
88 template <class _OutputIterator
, class _Size
, class _Tp
>
89 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
90 fill_n(_OutputIterator __first
, _Size __n
, const _Tp
& __value
) {
91 return std::__fill_n(__first
, std::__convert_to_integral(__n
), __value
);
94 _LIBCPP_END_NAMESPACE_STD
98 #endif // _LIBCPP___ALGORITHM_FILL_N_H