Path: blob/main/contrib/llvm-project/libcxx/include/__algorithm/fill_n.h
35233 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef _LIBCPP___ALGORITHM_FILL_N_H9#define _LIBCPP___ALGORITHM_FILL_N_H1011#include <__algorithm/min.h>12#include <__config>13#include <__fwd/bit_reference.h>14#include <__iterator/iterator_traits.h>15#include <__memory/pointer_traits.h>16#include <__utility/convert_to_integral.h>1718#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)19# pragma GCC system_header20#endif2122_LIBCPP_PUSH_MACROS23#include <__undef_macros>2425_LIBCPP_BEGIN_NAMESPACE_STD2627// fill_n isn't specialized for std::memset, because the compiler already optimizes the loop to a call to std::memset.2829template <class _OutputIterator, class _Size, class _Tp>30inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator31__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value);3233template <bool _FillVal, class _Cp>34_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void35__fill_n_bool(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n) {36using _It = __bit_iterator<_Cp, false>;37using __storage_type = typename _It::__storage_type;3839const int __bits_per_word = _It::__bits_per_word;40// do first partial word41if (__first.__ctz_ != 0) {42__storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);43__storage_type __dn = std::min(__clz_f, __n);44__storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));45if (_FillVal)46*__first.__seg_ |= __m;47else48*__first.__seg_ &= ~__m;49__n -= __dn;50++__first.__seg_;51}52// do middle whole words53__storage_type __nw = __n / __bits_per_word;54std::__fill_n(std::__to_address(__first.__seg_), __nw, _FillVal ? static_cast<__storage_type>(-1) : 0);55__n -= __nw * __bits_per_word;56// do last partial word57if (__n > 0) {58__first.__seg_ += __nw;59__storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);60if (_FillVal)61*__first.__seg_ |= __m;62else63*__first.__seg_ &= ~__m;64}65}6667template <class _Cp, class _Size>68inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cp, false>69__fill_n(__bit_iterator<_Cp, false> __first, _Size __n, const bool& __value) {70if (__n > 0) {71if (__value)72std::__fill_n_bool<true>(__first, __n);73else74std::__fill_n_bool<false>(__first, __n);75}76return __first + __n;77}7879template <class _OutputIterator, class _Size, class _Tp>80inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator81__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value) {82for (; __n > 0; ++__first, (void)--__n)83*__first = __value;84return __first;85}8687template <class _OutputIterator, class _Size, class _Tp>88inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator89fill_n(_OutputIterator __first, _Size __n, const _Tp& __value) {90return std::__fill_n(__first, std::__convert_to_integral(__n), __value);91}9293_LIBCPP_END_NAMESPACE_STD9495_LIBCPP_POP_MACROS9697#endif // _LIBCPP___ALGORITHM_FILL_N_H9899100