From cc6bad98d7e7c89e352ce6206af22f8e96c55150 Mon Sep 17 00:00:00 2001 From: jyxiong Date: Thu, 25 Sep 2025 23:02:33 +0800 Subject: [PATCH 1/2] feat(vector): impl operator= --- source/tinystl/container/vector.h | 85 ++++++++++++++++++++++--------- 1 file changed, 62 insertions(+), 23 deletions(-) diff --git a/source/tinystl/container/vector.h b/source/tinystl/container/vector.h index fb70d69..6073a47 100644 --- a/source/tinystl/container/vector.h +++ b/source/tinystl/container/vector.h @@ -138,9 +138,9 @@ class vector { private: Alloc m_alloc; - T *m_begin; - T *m_end; - T *m_cap; + T *m_begin = nullptr; + T *m_end = nullptr; + T *m_cap = nullptr; }; template @@ -175,8 +175,6 @@ vector::vector(std::size_t n, const Alloc &alloc) : m_alloc(alloc) { if (n > 0) { this->allocate(n); this->construct(n); - } else { - m_begin = m_end = m_cap = nullptr; } } @@ -186,7 +184,9 @@ vector::vector(std::size_t n, const T &val, const Alloc &alloc) if (n > 0) { this->allocate(n); this->construct(n, val); - } else { + } + // FIXME: necessary? + else { m_begin = m_end = m_cap = nullptr; } } @@ -202,8 +202,6 @@ vector::vector( if (n > 0) { this->allocate(n); this->construct(first, last, n); - } else { - m_begin = m_end = m_cap = nullptr; } } @@ -225,8 +223,6 @@ vector::vector(std::initializer_list init, const Alloc &alloc) if (n > 0) { this->allocate(n); this->construct(init.begin(), init.end(), n); - } else { - m_begin = m_end = m_cap = nullptr; } } @@ -240,8 +236,6 @@ vector::vector(const vector &other) if (n > 0) { this->allocate(n); this->construct(other.begin(), other.end(), n); - } else { - m_end = m_cap = m_begin = nullptr; } } @@ -252,8 +246,6 @@ vector::vector(const vector &other, const Alloc &alloc) if (n > 0) { this->allocate(n); this->construct(other.begin(), other.end(), n); - } else { - m_begin = m_end = m_cap = nullptr; } } @@ -282,24 +274,69 @@ vector::vector(vector &&other, const Alloc &alloc) : m_alloc(alloc) { std::make_move_iterator(other.begin()), std::make_move_iterator(other.end()), n ); - } else { - m_begin = m_end = m_cap = nullptr; } } } template -vector::~vector() {} +vector::~vector() { + if (m_begin != nullptr) { + this->clear(); + this->deallocate(); + } +} template -vector &vector::operator=(const vector &other) {} +vector &vector::operator=(const vector &other) { + if (this != std::addressof(other)) { + if constexpr (alloc_traits::propagate_on_container_copy_assignment::value) { + if (m_alloc != other.m_alloc) { + this->clear(); + this->deallocate(); + } + + m_alloc = other.m_alloc; + } + + this->assign(other.begin(), other.end()); + } + + return *this; +} template -vector &vector::operator=(vector &&other) {} +vector &vector::operator=(vector &&other) { + if constexpr (alloc_traits::propagate_on_container_move_assignment::value) { + this->deallocate(); + m_alloc = std::move(other.m_alloc); + this->m_begin = other.m_begin; + this->m_end = other.m_end; + this->m_cap = other.m_cap; + other.m_begin = other.m_end = other.m_cap = nullptr; + } else { + if (m_alloc != other.m_alloc) { + assign( + std::make_move_iterator(other.begin()), + std::make_move_iterator(other.end()) + ); + } else { + this->deallocate(); + this->m_begin = other.m_begin; + this->m_end = other.m_end; + this->m_cap = other.m_cap; + other.m_begin = other.m_end = other.m_cap = nullptr; + } + } + + return *this; +} template vector & -vector::operator=(std::initializer_list init) {} +vector::operator=(std::initializer_list init) { + assign(init.begin(), init.end()); + return *this; +} template void vector::assign(std::size_t n, const T &val) { @@ -344,8 +381,8 @@ void vector::assign(ForwardIter first, ForwardIter last) { template template -requires std::constructible_from> && - (!std::forward_iterator) + requires std::constructible_from> && + (!std::forward_iterator) void vector::assign(InputIter first, InputIter last) { pointer cur = m_begin; for (; cur != m_end && first != last; ++cur, ++first) { @@ -428,7 +465,9 @@ void vector::construct(std::size_t n, const T &val) { template template -void vector::construct(InputIter first, Sentinel last, std::size_t n) { +void vector::construct( + InputIter first, Sentinel last, std::size_t n +) { // FIXME: check last and n m_end = m_begin; for (std::size_t i = 0; i < n; ++i) { From ea602858da9cd3f91fd25cec8277304357430d77 Mon Sep 17 00:00:00 2001 From: jyxiong Date: Thu, 25 Sep 2025 23:04:01 +0800 Subject: [PATCH 2/2] fix --- source/tinystl/container/vector.h | 4 ---- 1 file changed, 4 deletions(-) diff --git a/source/tinystl/container/vector.h b/source/tinystl/container/vector.h index 6073a47..aea4f46 100644 --- a/source/tinystl/container/vector.h +++ b/source/tinystl/container/vector.h @@ -185,10 +185,6 @@ vector::vector(std::size_t n, const T &val, const Alloc &alloc) this->allocate(n); this->construct(n, val); } - // FIXME: necessary? - else { - m_begin = m_end = m_cap = nullptr; - } } template