================
@@ -136,4 +137,185 @@ class kmp_str_ref final {
const char *end() const { return sv.data() + length(); }
};
+/// kmp_vector is a vector class for managing small vectors.
+/// inline_threshold: Number of elements in the inline array. If exceeded, the
+/// vector will grow dynamically.
+template <typename T, size_t inline_threshold = 8> class kmp_vector final {
+ static_assert(std::is_copy_constructible_v<T>,
+ "T must be copy constructible");
+ static_assert(std::is_destructible_v<T>, "T must be destructible");
+
+ T inline_data[inline_threshold];
+ T *data = inline_data;
+ size_t count = 0;
+ size_t capacity = inline_threshold;
+
+ void copy_data(T *dst, const T *src, size_t num_elements) {
+ if constexpr (std::is_trivially_copyable_v<T>) {
+ memcpy(dst, src, num_elements * sizeof(T));
+ } else {
+ for (size_t i = 0; i < num_elements; i++)
+ new (&dst[i]) T(src[i]); // copy-construct to memory
+ }
+ }
+
+ void grow() {
----------------
ro-i wrote:
There already is a reserve method.
Note that grow is just an internal / private helper, that's why it's not taking
an argument at the moment
https://github.com/llvm/llvm-project/pull/176163
_______________________________________________
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits