cpp-common: add vectorFastRemove
[babeltrace.git] / src / cpp-common / vector.hpp
diff --git a/src/cpp-common/vector.hpp b/src/cpp-common/vector.hpp
new file mode 100644 (file)
index 0000000..0f0f202
--- /dev/null
@@ -0,0 +1,30 @@
+#ifndef SRC_CPP_COMMON_VECTOR_HPP
+#define SRC_CPP_COMMON_VECTOR_HPP
+
+#include <vector>
+#include "common/assert.h"
+
+namespace bt2_common {
+
+/*
+ * Moves the last entry of `vec` to the index `idx`, then removes the last entry.
+ *
+ * Meant to be a direct replacement for g_ptr_array_remove_index_fast(), but for
+ * `std::vector`.
+ */
+template <typename T, typename AllocatorT>
+void vectorFastRemove(std::vector<T, AllocatorT>& vec,
+                      const typename std::vector<T, AllocatorT>::size_type idx)
+{
+    BT_ASSERT_DBG(idx < vec.size());
+
+    if (idx < vec.size() - 1) {
+        vec[idx] = std::move(vec.back());
+    }
+
+    vec.pop_back();
+}
+
+} /* namespace bt2_common */
+
+#endif /* SRC_CPP_COMMON_VECTOR_HPP */
This page took 0.022942 seconds and 4 git commands to generate.