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