Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions libcxx/docs/ReleaseNotes/21.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ Improvements and New Features
- The ``std::stable_sort`` algorithm uses radix sort for floating-point types now, which can improve the performance
up to 10x, depending on type of sorted elements and the initial state of the sorted array.

- The segmented iterator optimization for ``std::for_each`` has been backported to C++11. Previously it was only available
in C++23 and later.

Deprecations and Removals
-------------------------

Expand Down
44 changes: 20 additions & 24 deletions libcxx/include/__algorithm/for_each.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,40 @@
#include <__algorithm/for_each_segment.h>
#include <__config>
#include <__iterator/segmented_iterator.h>
#include <__ranges/movable_box.h>
#include <__utility/in_place.h>
#include <__utility/move.h>
#include <__type_traits/enable_if.h>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif

_LIBCPP_PUSH_MACROS
#include <__undef_macros>

_LIBCPP_BEGIN_NAMESPACE_STD

template <class _InputIterator, class _Function>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Function
for_each(_InputIterator __first, _InputIterator __last, _Function __f) {
template <class _InputIterator, class _Sent, class _Func>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __for_each(_InputIterator __first, _Sent __last, _Func& __f) {
for (; __first != __last; ++__first)
__f(*__first);
return __f;
}

// __movable_box is available in C++20, but is actually a copyable-box, so optimization is only correct in C++23
#if _LIBCPP_STD_VER >= 23
template <class _SegmentedIterator, class _Function>
requires __is_segmented_iterator<_SegmentedIterator>::value
_LIBCPP_HIDE_FROM_ABI constexpr _Function
for_each(_SegmentedIterator __first, _SegmentedIterator __last, _Function __func) {
ranges::__movable_box<_Function> __wrapped_func(in_place, std::move(__func));
std::__for_each_segment(__first, __last, [&](auto __lfirst, auto __llast) {
__wrapped_func =
ranges::__movable_box<_Function>(in_place, std::for_each(__lfirst, __llast, std::move(*__wrapped_func)));
#ifndef _LIBCPP_CXX03_LANG
template <class _SegmentedIterator,
class _Function,
__enable_if_t<__is_segmented_iterator<_SegmentedIterator>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
__for_each(_SegmentedIterator __first, _SegmentedIterator __last, _Function& __func) {
using __local_iterator_t = typename __segmented_iterator_traits<_SegmentedIterator>::__local_iterator;
std::__for_each_segment(__first, __last, [&](__local_iterator_t __lfirst, __local_iterator_t __llast) {
std::__for_each(__lfirst, __llast, __func);
});
return std::move(*__wrapped_func);
}
#endif // _LIBCPP_STD_VER >= 23
#endif // !_LIBCPP_CXX03_LANG

_LIBCPP_END_NAMESPACE_STD
template <class _InputIterator, class _Function>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Function
for_each(_InputIterator __first, _InputIterator __last, _Function __f) {
std::__for_each(__first, __last, __f);
return __f;
}

_LIBCPP_POP_MACROS
_LIBCPP_END_NAMESPACE_STD

#endif // _LIBCPP___ALGORITHM_FOR_EACH_H
1 change: 1 addition & 0 deletions libcxx/include/algorithm
Original file line number Diff line number Diff line change
Expand Up @@ -2061,6 +2061,7 @@ template <class BidirectionalIterator, class Compare>
# include <cstring>
# include <iterator>
# include <memory>
# include <optional>
# include <stdexcept>
# include <type_traits>
# include <utility>
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/array
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ _LIBCPP_POP_MACROS
# include <cstdlib>
# include <iterator>
# include <new>
# include <optional>
# include <type_traits>
# include <utility>
# endif
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/bitset
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,7 @@ _LIBCPP_POP_MACROS
# if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
# include <concepts>
# include <cstdlib>
# include <optional>
# include <type_traits>
# endif
#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/codecvt
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ _LIBCPP_END_NAMESPACE_STD
# include <limits>
# include <mutex>
# include <new>
# include <optional>
# include <stdexcept>
# include <type_traits>
# include <typeinfo>
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/condition_variable
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ _LIBCPP_POP_MACROS
# include <initializer_list>
# include <iosfwd>
# include <new>
# include <optional>
# include <stdexcept>
# include <system_error>
# include <type_traits>
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/ios
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,7 @@ _LIBCPP_POP_MACROS
# include <limits>
# include <mutex>
# include <new>
# include <optional>
# include <stdexcept>
# include <system_error>
# include <type_traits>
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/locale
Original file line number Diff line number Diff line change
Expand Up @@ -3692,6 +3692,7 @@ _LIBCPP_POP_MACROS
# include <cstdarg>
# include <iterator>
# include <mutex>
# include <optional>
# include <stdexcept>
# include <type_traits>
# include <typeinfo>
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/streambuf
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ _LIBCPP_POP_MACROS

# if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
# include <cstdint>
# include <optional>
# endif
#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)

Expand Down
1 change: 1 addition & 0 deletions libcxx/include/string
Original file line number Diff line number Diff line change
Expand Up @@ -4027,6 +4027,7 @@ _LIBCPP_POP_MACROS
# include <cstdlib>
# include <iterator>
# include <new>
# include <optional>
# include <type_traits>
# include <typeinfo>
# include <utility>
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/string_view
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,7 @@ _LIBCPP_POP_MACROS
# include <concepts>
# include <cstdlib>
# include <iterator>
# include <optional>
# include <type_traits>
# endif
#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/system_error
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ template <> struct hash<std::error_condition>;
# include <cstdint>
# include <cstring>
# include <limits>
# include <optional>
# include <type_traits>
# endif
#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/vector
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ template<class T, class charT> requires is-vector-bool-reference<T> // Since C++
# if _LIBCPP_HAS_LOCALIZATION
# include <locale>
# endif
# include <optional>
# include <string>
# include <string_view>
# include <tuple>
Expand Down
Loading
Loading