44adc6e0cd9ac11eb18d1f14a69c4ac02f4c7499
[babeltrace.git] / src / cpp-common / vector.hpp
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 #include "common/assert.h"
12
13 namespace bt2_common {
14
15 /*
16 * Moves the last entry of `vec` to the index `idx`, then removes the last entry.
17 *
18 * Meant to be a direct replacement for g_ptr_array_remove_index_fast(), but for
19 * `std::vector`.
20 */
21 template <typename T, typename AllocatorT>
22 void vectorFastRemove(std::vector<T, AllocatorT>& vec,
23 const typename std::vector<T, AllocatorT>::size_type idx)
24 {
25 BT_ASSERT_DBG(idx < vec.size());
26
27 if (idx < vec.size() - 1) {
28 vec[idx] = std::move(vec.back());
29 }
30
31 vec.pop_back();
32 }
33
34 } /* namespace bt2_common */
35
36 #endif /* SRC_CPP_COMMON_VECTOR_HPP */
This page took 0.03075 seconds and 3 git commands to generate.