| 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___CXX03___ALGORITHM_FILL_N_H |
| 10 |
#define _LIBCPP___CXX03___ALGORITHM_FILL_N_H |
| 11 |
|
| 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_header |
| 21 |
#endif |
| 22 |
|
| 23 |
_LIBCPP_PUSH_MACROS |
| 24 |
#include <__cxx03/__undef_macros> |
| 25 |
|
| 26 |
_LIBCPP_BEGIN_NAMESPACE_STD |
| 27 |
|
| 28 |
// fill_n isn't specialized for std::memset, because the compiler already optimizes the loop to a call to std::memset. |
| 29 |
|
| 30 |
template <class _OutputIterator, class _Size, class _Tp> |
| 31 |
inline _LIBCPP_HIDE_FROM_ABI _OutputIterator __fill_n(_OutputIterator __first, _Size __n, const _Tp& __value); |
| 32 |
|
| 33 |
template <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 word |
| 40 |
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 |
else |
| 47 |
*__first.__seg_ &= ~__m; |
| 48 |
__n -= __dn; |
| 49 |
++__first.__seg_; |
| 50 |
} |
| 51 |
// do middle whole words |
| 52 |
__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 word |
| 56 |
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 |
else |
| 62 |
*__first.__seg_ &= ~__m; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
template <class _Cp, class _Size> |
| 67 |
inline _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 |
else |
| 73 |
std::__fill_n_bool<false>(__first, __n); |
| 74 |
} |
| 75 |
return __first + __n; |
| 76 |
} |
| 77 |
|
| 78 |
template <class _OutputIterator, class _Size, class _Tp> |
| 79 |
inline _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 |
|
| 85 |
template <class _OutputIterator, class _Size, class _Tp> |
| 86 |
inline _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_STD |
| 91 |
|
| 92 |
_LIBCPP_POP_MACROS |
| 93 |
|
| 94 |
#endif // _LIBCPP___CXX03___ALGORITHM_FILL_N_H |