| 1 | #ifndef SRC_CPP_COMMON_VECTOR_HPP |
| 2 | #define SRC_CPP_COMMON_VECTOR_HPP |
| 3 | |
| 4 | #include <vector> |
| 5 | #include "common/assert.h" |
| 6 | |
| 7 | namespace bt2_common { |
| 8 | |
| 9 | /* |
| 10 | * Moves the last entry of `vec` to the index `idx`, then removes the last entry. |
| 11 | * |
| 12 | * Meant to be a direct replacement for g_ptr_array_remove_index_fast(), but for |
| 13 | * `std::vector`. |
| 14 | */ |
| 15 | template <typename T, typename AllocatorT> |
| 16 | void vectorFastRemove(std::vector<T, AllocatorT>& vec, |
| 17 | const typename std::vector<T, AllocatorT>::size_type idx) |
| 18 | { |
| 19 | BT_ASSERT_DBG(idx < vec.size()); |
| 20 | |
| 21 | if (idx < vec.size() - 1) { |
| 22 | vec[idx] = std::move(vec.back()); |
| 23 | } |
| 24 | |
| 25 | vec.pop_back(); |
| 26 | } |
| 27 | |
| 28 | } /* namespace bt2_common */ |
| 29 | |
| 30 | #endif /* SRC_CPP_COMMON_VECTOR_HPP */ |