diff options
| author | Lukas Oesch <lukasoesch20@gmail.com> | 2026-04-08 18:51:30 +0200 |
|---|---|---|
| committer | Lukas Oesch <lukasoesch20@gmail.com> | 2026-04-11 07:58:22 +0200 |
| commit | 869ff69ebae160006e31eb0f24ed927bb65f3c63 (patch) | |
| tree | 53b6db826cd8093067de3e8deda44d641f40df66 /libs/kstd | |
| parent | 648fc5c801fbebaed5fe0825e88b359476f57a84 (diff) | |
| download | teachos-869ff69ebae160006e31eb0f24ed927bb65f3c63.tar.xz teachos-869ff69ebae160006e31eb0f24ed927bb65f3c63.zip | |
implement vector resize
Diffstat (limited to 'libs/kstd')
| -rw-r--r-- | libs/kstd/include/kstd/vector | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/libs/kstd/include/kstd/vector b/libs/kstd/include/kstd/vector index 9e41cb6..e51cbac 100644 --- a/libs/kstd/include/kstd/vector +++ b/libs/kstd/include/kstd/vector @@ -559,6 +559,45 @@ namespace kstd m_size = old_size; } + //! Resize this vector to contain @p new_size elements. + constexpr auto resize(size_type new_size) -> void + { + resize(new_size, value_type{}); + } + + //! Resize this vector to contain @p new_size elements, filling new elements with @p value. + constexpr auto resize(size_type new_size, const_reference value) -> void + { + if (new_size < size()) + { + destroy_n(begin() + new_size, size() - new_size); + m_size = new_size; + return; + } + + if (new_size == size()) + { + return; + } + + if (new_size > max_size()) + { + kstd::os::panic("[kstd:vector] Tried to resize more space than theoretically possible."); + } + + if (new_size > capacity()) + { + reserve(new_size); + } + + for (auto i = size(); i < new_size; ++i) + { + std::allocator_traits<allocator_type>::construct(m_allocator, m_data + i, value); + } + + m_size = new_size; + } + //! Get the number of element this vector has currently space for, including elements currently in this vector. [[nodiscard]] constexpr auto capacity() const noexcept -> size_type { |
