-
Notifications
You must be signed in to change notification settings - Fork 13.6k
DO NOT MERGE: Identify places that need reserve. #136543
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2113,12 +2113,63 @@ void erase(Container &C, ValueType V) { | |
C.erase(std::remove(C.begin(), C.end(), V), C.end()); | ||
} | ||
|
||
namespace detail { | ||
template <typename Range> | ||
using check_has_member_iterator_category_t = | ||
typename decltype(std::declval<Range &>().begin())::iterator_category; | ||
|
||
template <typename Range> | ||
static constexpr bool HasMemberIteratorCategory = | ||
is_detected<check_has_member_iterator_category_t, Range>::value; | ||
|
||
template <typename Container> | ||
using check_has_member_capacity_t = | ||
decltype(std::declval<Container &>().capacity()); | ||
|
||
template <typename Container> | ||
static constexpr bool HasMemberCapacity = | ||
is_detected<check_has_member_capacity_t, Container>::value; | ||
|
||
template <typename Container> | ||
using check_has_member_reserve_t = | ||
decltype(std::declval<Container &>().reserve(1U)); | ||
|
||
template <typename Container> | ||
static constexpr bool HasMemberReserve = | ||
is_detected<check_has_member_reserve_t, Container>::value; | ||
} // namespace detail | ||
|
||
/// Wrapper function to append range `R` to container `C`. | ||
/// | ||
/// C.insert(C.end(), R.begin(), R.end()); | ||
template <typename Container, typename Range> | ||
void append_range(Container &C, Range &&R) { | ||
size_t Before = 0; | ||
size_t After = 0; | ||
|
||
if constexpr (detail::HasMemberReserve<Container>) { | ||
using IteratorType = decltype(adl_begin(R)); | ||
if constexpr (std::is_pointer<IteratorType>::value) { | ||
C.reserve(C.size() + std::distance(adl_begin(R), adl_end(R))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The alternative would be to check that the type either has |
||
} else if constexpr (detail::HasMemberIteratorCategory<Range>) { | ||
if constexpr (std::is_convertible< | ||
typename IteratorType::iterator_category, | ||
std::random_access_iterator_tag>::value) { | ||
C.reserve(C.size() + std::distance(adl_begin(R), adl_end(R))); | ||
} | ||
} | ||
} | ||
|
||
if constexpr (detail::HasMemberCapacity<Container>) | ||
Before = C.capacity(); | ||
|
||
C.insert(C.end(), adl_begin(R), adl_end(R)); | ||
|
||
if constexpr (detail::HasMemberCapacity<Container>) | ||
After = C.capacity(); | ||
|
||
if (Before != After) | ||
llvm_unreachable("capacity changed"); | ||
Comment on lines
+2168
to
+2172
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a cool idea for verifying that it reserved as intended! |
||
} | ||
|
||
/// Appends all `Values` to container `C`. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
iterator_traits should already handle plain pointers, so I think we'd want to use that instead of looking for
iterator_category
?