File indexing completed on 2026-05-03 08:13:07
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___ALGORITHM_FILL_N_H
0010 #define _LIBCPP___ALGORITHM_FILL_N_H
0011
0012 #include <__algorithm/min.h>
0013 #include <__config>
0014 #include <__fwd/bit_reference.h>
0015 #include <__memory/pointer_traits.h>
0016 #include <__utility/convert_to_integral.h>
0017
0018 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0019 # pragma GCC system_header
0020 #endif
0021
0022 _LIBCPP_PUSH_MACROS
0023 #include <__undef_macros>
0024
0025 _LIBCPP_BEGIN_NAMESPACE_STD
0026
0027
0028
0029 template <class _OutputIterator, class _Size, class _Tp>
0030 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
0031 __fill_n(_OutputIterator __first, _Size __n, const _Tp& __value);
0032
0033 template <bool _FillVal, class _Cp>
0034 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
0035 __fill_n_bool(__bit_iterator<_Cp, false> __first, typename __size_difference_type_traits<_Cp>::size_type __n) {
0036 using _It = __bit_iterator<_Cp, false>;
0037 using __storage_type = typename _It::__storage_type;
0038
0039 const int __bits_per_word = _It::__bits_per_word;
0040
0041 if (__first.__ctz_ != 0) {
0042 __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
0043 __storage_type __dn = std::min(__clz_f, __n);
0044 __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
0045 if (_FillVal)
0046 *__first.__seg_ |= __m;
0047 else
0048 *__first.__seg_ &= ~__m;
0049 __n -= __dn;
0050 ++__first.__seg_;
0051 }
0052
0053 __storage_type __nw = __n / __bits_per_word;
0054 std::__fill_n(std::__to_address(__first.__seg_), __nw, _FillVal ? static_cast<__storage_type>(-1) : 0);
0055 __n -= __nw * __bits_per_word;
0056
0057 if (__n > 0) {
0058 __first.__seg_ += __nw;
0059 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
0060 if (_FillVal)
0061 *__first.__seg_ |= __m;
0062 else
0063 *__first.__seg_ &= ~__m;
0064 }
0065 }
0066
0067 template <class _Cp, class _Size>
0068 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cp, false>
0069 __fill_n(__bit_iterator<_Cp, false> __first, _Size __n, const bool& __value) {
0070 if (__n > 0) {
0071 if (__value)
0072 std::__fill_n_bool<true>(__first, __n);
0073 else
0074 std::__fill_n_bool<false>(__first, __n);
0075 }
0076 return __first + __n;
0077 }
0078
0079 template <class _OutputIterator, class _Size, class _Tp>
0080 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
0081 __fill_n(_OutputIterator __first, _Size __n, const _Tp& __value) {
0082 for (; __n > 0; ++__first, (void)--__n)
0083 *__first = __value;
0084 return __first;
0085 }
0086
0087 template <class _OutputIterator, class _Size, class _Tp>
0088 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
0089 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value) {
0090 return std::__fill_n(__first, std::__convert_to_integral(__n), __value);
0091 }
0092
0093 _LIBCPP_END_NAMESPACE_STD
0094
0095 _LIBCPP_POP_MACROS
0096
0097 #endif