From 1637cea0ade73163942ea9f25090146413ae4baf Mon Sep 17 00:00:00 2001 From: Alex Anderson Date: Sun, 16 Apr 2023 10:14:23 -0700 Subject: [PATCH] Add emplace_back_unchecked --- include/ankerl/svector.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/include/ankerl/svector.h b/include/ankerl/svector.h index 93ff91a..98483c2 100644 --- a/include/ankerl/svector.h +++ b/include/ankerl/svector.h @@ -692,6 +692,23 @@ class svector { [[nodiscard]] auto data() const -> T const* { return const_cast(this)->data(); // NOLINT(cppcoreguidelines-pro-type-const-cast) } + + // Danger: only use when you know size() < capacity() ahead of time + template + auto emplace_back_unchecked(Args&&... args) -> T& { + T* ptr; // NOLINT(cppcoreguidelines-init-variables) + size_t s; // NOLINT(cppcoreguidelines-init-variables) + if (is_dir) { + s = size(); + ptr = data() + s; + set_size(s + 1); + } else { + s = size(); + ptr = data() + s; + set_size(s + 1); + } + return *new (static_cast(ptr)) T(std::forward(args)...); + } template auto emplace_back(Args&&... args) -> T& {