Skip to content

Commit

Permalink
Tweaked: Faster erase_fast
Browse files Browse the repository at this point in the history
  • Loading branch information
richardbiely committed Jul 19, 2023
1 parent 07e3591 commit 3ad4997
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion include/gaia/utils/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -432,13 +432,18 @@ namespace gaia {
// Erasure
//----------------------------------------------------------------------

//! Replaces the item at \param idx in the array \param arr with the last item of the array if possible and removes
//! its last item.
//! Use when shifting of the entire erray is not wanted.
//! \warning If the item order is important and the size of the array changes after calling this function you need
//! to sort the array.
template <typename C>
void erase_fast(C& arr, size_t idx) {
if (idx >= arr.size())
return;

if (idx + 1 != arr.size())
utils::swap(arr[idx], arr[arr.size() - 1]);
arr[idx] = arr[arr.size() - 1];

arr.pop_back();
}
Expand Down

0 comments on commit 3ad4997

Please sign in to comment.