|
| 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___ALGORITHM_FILL_FILL_N_COMMON_H |
| 10 | +#define _LIBCPP___ALGORITHM_FILL_FILL_N_COMMON_H |
| 11 | + |
| 12 | +#include <__algorithm/for_each_segment.h> |
| 13 | +#include <__algorithm/min.h> |
| 14 | +#include <__config> |
| 15 | +#include <__fwd/bit_reference.h> |
| 16 | +#include <__iterator/iterator_traits.h> |
| 17 | +#include <__iterator/next.h> |
| 18 | +#include <__iterator/segmented_iterator.h> |
| 19 | +#include <__memory/pointer_traits.h> |
| 20 | +#include <__type_traits/enable_if.h> |
| 21 | +#include <__utility/convert_to_integral.h> |
| 22 | + |
| 23 | +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 24 | +# pragma GCC system_header |
| 25 | +#endif |
| 26 | + |
| 27 | +_LIBCPP_PUSH_MACROS |
| 28 | +#include <__undef_macros> |
| 29 | + |
| 30 | +_LIBCPP_BEGIN_NAMESPACE_STD |
| 31 | + |
| 32 | +template <class _ForwardIterator, |
| 33 | + class _Sentinel, |
| 34 | + class _Tp, |
| 35 | + __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0> |
| 36 | +_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void |
| 37 | +__fill(_ForwardIterator __first, _Sentinel __last, const _Tp& __value); |
| 38 | + |
| 39 | +template <class _RandomAccessIterator, |
| 40 | + class _Tp, |
| 41 | + __enable_if_t<__has_random_access_iterator_category<_RandomAccessIterator>::value && |
| 42 | + !__is_segmented_iterator<_RandomAccessIterator>::value, |
| 43 | + int> = 0> |
| 44 | +_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void |
| 45 | +__fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value); |
| 46 | + |
| 47 | +template <class _SegmentedIterator, |
| 48 | + class _Tp, |
| 49 | + __enable_if_t<__is_segmented_iterator<_SegmentedIterator>::value, int> = 0> |
| 50 | +_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void |
| 51 | +__fill(_SegmentedIterator __first, _SegmentedIterator __last, const _Tp& __value); |
| 52 | + |
| 53 | +template <class _OutIter, class _Size, class _Tp, __enable_if_t<!__is_segmented_iterator<_OutIter>::value, int> = 0> |
| 54 | +inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutIter |
| 55 | +__fill_n(_OutIter __first, _Size __n, const _Tp& __value) { |
| 56 | + for (; __n > 0; ++__first, (void)--__n) |
| 57 | + *__first = __value; |
| 58 | + return __first; |
| 59 | +} |
| 60 | + |
| 61 | +template <bool _FillVal, class _Cp> |
| 62 | +_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void |
| 63 | +__fill_n_bool(__bit_iterator<_Cp, false> __first, typename __size_difference_type_traits<_Cp>::size_type __n) { |
| 64 | + using _It = __bit_iterator<_Cp, false>; |
| 65 | + using __storage_type = typename _It::__storage_type; |
| 66 | + |
| 67 | + const int __bits_per_word = _It::__bits_per_word; |
| 68 | + // do first partial word |
| 69 | + if (__first.__ctz_ != 0) { |
| 70 | + __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_); |
| 71 | + __storage_type __dn = std::min(__clz_f, __n); |
| 72 | + std::__fill_masked_range(std::__to_address(__first.__seg_), __clz_f - __dn, __first.__ctz_, _FillVal); |
| 73 | + __n -= __dn; |
| 74 | + ++__first.__seg_; |
| 75 | + } |
| 76 | + // do middle whole words |
| 77 | + __storage_type __nw = __n / __bits_per_word; |
| 78 | + std::__fill_n(std::__to_address(__first.__seg_), __nw, _FillVal ? static_cast<__storage_type>(-1) : 0); |
| 79 | + __n -= __nw * __bits_per_word; |
| 80 | + // do last partial word |
| 81 | + if (__n > 0) { |
| 82 | + __first.__seg_ += __nw; |
| 83 | + std::__fill_masked_range(std::__to_address(__first.__seg_), __bits_per_word - __n, 0u, _FillVal); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +template <class _Cp, class _Size> |
| 88 | +inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cp, false> |
| 89 | +__fill_n(__bit_iterator<_Cp, false> __first, _Size __n, const bool& __value) { |
| 90 | + if (__n > 0) { |
| 91 | + if (__value) |
| 92 | + std::__fill_n_bool<true>(__first, __n); |
| 93 | + else |
| 94 | + std::__fill_n_bool<false>(__first, __n); |
| 95 | + } |
| 96 | + return __first + __n; |
| 97 | +} |
| 98 | + |
| 99 | +template < class _OutIter, |
| 100 | + class _Size, |
| 101 | + class _Tp, |
| 102 | + __enable_if_t<__is_segmented_iterator<_OutIter>::value && __has_forward_iterator_category<_OutIter>::value, |
| 103 | + int> = 0> |
| 104 | +inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _OutIter |
| 105 | +__fill_n(_OutIter __first, _Size __n, const _Tp& __value) { |
| 106 | + _OutIter __last = std::next(__first, __n); |
| 107 | + std::__fill(__first, __last, __value); |
| 108 | + return __last; |
| 109 | +} |
| 110 | + |
| 111 | +template <class _ForwardIterator, |
| 112 | + class _Sentinel, |
| 113 | + class _Tp, |
| 114 | + __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> > |
| 115 | +inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void |
| 116 | +__fill(_ForwardIterator __first, _Sentinel __last, const _Tp& __value) { |
| 117 | + for (; __first != __last; ++__first) |
| 118 | + *__first = __value; |
| 119 | +} |
| 120 | + |
| 121 | +template <class _OutIter, class _Tp> |
| 122 | +struct _FillSegment { |
| 123 | + using _Traits _LIBCPP_NODEBUG = __segmented_iterator_traits<_OutIter>; |
| 124 | + |
| 125 | + const _Tp& __value_; |
| 126 | + |
| 127 | + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit _FillSegment(const _Tp& __value) : __value_(__value) {} |
| 128 | + |
| 129 | + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void |
| 130 | + operator()(typename _Traits::__local_iterator __lfirst, typename _Traits::__local_iterator __llast) { |
| 131 | + std::__fill(__lfirst, __llast, __value_); |
| 132 | + } |
| 133 | +}; |
| 134 | + |
| 135 | +template <class _RandomAccessIterator, |
| 136 | + class _Tp, |
| 137 | + __enable_if_t<__has_random_access_iterator_category<_RandomAccessIterator>::value && |
| 138 | + !__is_segmented_iterator<_RandomAccessIterator>::value, |
| 139 | + int> > |
| 140 | +inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void |
| 141 | +__fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value) { |
| 142 | + std::__fill_n(__first, __last - __first, __value); |
| 143 | +} |
| 144 | + |
| 145 | +template <class _SegmentedIterator, class _Tp, __enable_if_t<__is_segmented_iterator<_SegmentedIterator>::value, int> > |
| 146 | +inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void |
| 147 | +__fill(_SegmentedIterator __first, _SegmentedIterator __last, const _Tp& __value) { |
| 148 | + std::__for_each_segment(__first, __last, _FillSegment<_SegmentedIterator, _Tp>(__value)); |
| 149 | +} |
| 150 | + |
| 151 | +_LIBCPP_END_NAMESPACE_STD |
| 152 | + |
| 153 | +_LIBCPP_POP_MACROS |
| 154 | + |
| 155 | +#endif // _LIBCPP___ALGORITHM_FILL_FILL_N_COMMON_H |
0 commit comments