-
-
Notifications
You must be signed in to change notification settings - Fork 24.2k
Add back() and pop_back() to LocalVector
#99955
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
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 |
|---|---|---|
|
|
@@ -57,6 +57,16 @@ class LocalVector { | |
| return data; | ||
| } | ||
|
|
||
| T &back() { | ||
| CRASH_BAD_UNSIGNED_INDEX(0, count); | ||
| return data[count - 1]; | ||
| } | ||
|
|
||
| const T &back() const { | ||
| CRASH_BAD_UNSIGNED_INDEX(0, count); | ||
| return data[count - 1]; | ||
| } | ||
|
|
||
| // Must take a copy instead of a reference (see GH-31736). | ||
| _FORCE_INLINE_ void push_back(T p_elem) { | ||
| if (unlikely(count == capacity)) { | ||
|
|
@@ -72,6 +82,14 @@ class LocalVector { | |
| } | ||
| } | ||
|
|
||
| void pop_back() { | ||
| ERR_FAIL_COND(count == 0); | ||
|
||
| count--; | ||
| if constexpr (!std::is_trivially_destructible_v<T> && !force_trivial) { | ||
| data[count].~T(); | ||
| } | ||
| } | ||
|
|
||
| void remove_at(U p_index) { | ||
| ERR_FAIL_UNSIGNED_INDEX(p_index, count); | ||
| count--; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
It's customary to return the value instead of destructing it, so that it can be used in structures like:
while (element = list.pop_pack()) { }But this is, unfortunately, not the case with equivalent functions of other list types. So I think it would be better to keep it as you have it, and address this shortcoming for all list types in a separate PR.
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.
I remember
std::vector::pop_backreturn void because of multi-thread concern so I follow this prcatice.