95 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef _LIBCPP___CXX03___ALGORITHM_FILL_N_H10#define _LIBCPP___CXX03___ALGORITHM_FILL_N_H11 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>18 19#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)20# pragma GCC system_header21#endif22 23_LIBCPP_PUSH_MACROS24#include <__cxx03/__undef_macros>25 26_LIBCPP_BEGIN_NAMESPACE_STD27 28// fill_n isn't specialized for std::memset, because the compiler already optimizes the loop to a call to std::memset.29 30template <class _OutputIterator, class _Size, class _Tp>31inline _LIBCPP_HIDE_FROM_ABI _OutputIterator __fill_n(_OutputIterator __first, _Size __n, const _Tp& __value);32 33template <bool _FillVal, class _Cp>34_LIBCPP_HIDE_FROM_ABI void __fill_n_bool(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n) {35 using _It = __bit_iterator<_Cp, false>;36 using __storage_type = typename _It::__storage_type;37 38 const int __bits_per_word = _It::__bits_per_word;39 // do first partial word40 if (__first.__ctz_ != 0) {41 __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);42 __storage_type __dn = std::min(__clz_f, __n);43 __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));44 if (_FillVal)45 *__first.__seg_ |= __m;46 else47 *__first.__seg_ &= ~__m;48 __n -= __dn;49 ++__first.__seg_;50 }51 // do middle whole words52 __storage_type __nw = __n / __bits_per_word;53 std::__fill_n(std::__to_address(__first.__seg_), __nw, _FillVal ? static_cast<__storage_type>(-1) : 0);54 __n -= __nw * __bits_per_word;55 // do last partial word56 if (__n > 0) {57 __first.__seg_ += __nw;58 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);59 if (_FillVal)60 *__first.__seg_ |= __m;61 else62 *__first.__seg_ &= ~__m;63 }64}65 66template <class _Cp, class _Size>67inline _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, false>68__fill_n(__bit_iterator<_Cp, false> __first, _Size __n, const bool& __value) {69 if (__n > 0) {70 if (__value)71 std::__fill_n_bool<true>(__first, __n);72 else73 std::__fill_n_bool<false>(__first, __n);74 }75 return __first + __n;76}77 78template <class _OutputIterator, class _Size, class _Tp>79inline _LIBCPP_HIDE_FROM_ABI _OutputIterator __fill_n(_OutputIterator __first, _Size __n, const _Tp& __value) {80 for (; __n > 0; ++__first, (void)--__n)81 *__first = __value;82 return __first;83}84 85template <class _OutputIterator, class _Size, class _Tp>86inline _LIBCPP_HIDE_FROM_ABI _OutputIterator fill_n(_OutputIterator __first, _Size __n, const _Tp& __value) {87 return std::__fill_n(__first, std::__convert_to_integral(__n), __value);88}89 90_LIBCPP_END_NAMESPACE_STD91 92_LIBCPP_POP_MACROS93 94#endif // _LIBCPP___CXX03___ALGORITHM_FILL_N_H95