| 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_H |
| 10 | #define _LIBCPP___ALGORITHM_FILL_H |
| 11 | |
| 12 | #include <__algorithm/fill_n.h> |
| 13 | #include <__algorithm/for_each_segment.h> |
| 14 | #include <__config> |
| 15 | #include <__iterator/iterator_traits.h> |
| 16 | #include <__iterator/segmented_iterator.h> |
| 17 | #include <__type_traits/enable_if.h> |
| 18 | #include <__type_traits/is_same.h> |
| 19 | |
| 20 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 21 | # pragma GCC system_header |
| 22 | #endif |
| 23 | |
| 24 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 25 | |
| 26 | // fill isn't specialized for std::memset, because the compiler already optimizes the loop to a call to std::memset. |
| 27 | |
| 28 | template <class _ForwardIterator, class _Sentinel, class _Tp> |
| 29 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator |
| 30 | __fill(_ForwardIterator __first, _Sentinel __last, const _Tp& __value) { |
| 31 | #ifndef _LIBCPP_CXX03_LANG |
| 32 | if constexpr (is_same<_ForwardIterator, _Sentinel>::value && __is_segmented_iterator_v<_ForwardIterator>) { |
| 33 | using __local_iterator_t = typename __segmented_iterator_traits<_ForwardIterator>::__local_iterator; |
| 34 | std::__for_each_segment(__first, __last, [&](__local_iterator_t __lfirst, __local_iterator_t __llast) { |
| 35 | std::__fill(__lfirst, __llast, __value); |
| 36 | }); |
| 37 | return __last; |
| 38 | } |
| 39 | #endif |
| 40 | for (; __first != __last; ++__first) |
| 41 | *__first = __value; |
| 42 | return __first; |
| 43 | } |
| 44 | |
| 45 | template <class _RandomAccessIterator, |
| 46 | class _Tp, |
| 47 | __enable_if_t<__has_random_access_iterator_category<_RandomAccessIterator>::value && |
| 48 | !__is_segmented_iterator_v<_RandomAccessIterator>, |
| 49 | int> = 0> |
| 50 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _RandomAccessIterator |
| 51 | __fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value) { |
| 52 | return std::__fill_n(__first, __last - __first, __value); |
| 53 | } |
| 54 | |
| 55 | template <class _ForwardIterator, class _Tp> |
| 56 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void |
| 57 | fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) { |
| 58 | std::__fill(__first, __last, __value); |
| 59 | } |
| 60 | |
| 61 | _LIBCPP_END_NAMESPACE_STD |
| 62 | |
| 63 | #endif // _LIBCPP___ALGORITHM_FILL_H |
| 64 | |