From 0cf25f30296cc732eea02c182f5f15b4e1558644 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 22 Apr 2021 01:17:57 -0500 Subject: [PATCH] [ox/std] Fix HashMap and Vector move operators --- deps/ox/src/ox/std/hashmap.hpp | 34 ++++++++++++-------- deps/ox/src/ox/std/vector.hpp | 58 ++++++++++++++++++---------------- 2 files changed, 52 insertions(+), 40 deletions(-) diff --git a/deps/ox/src/ox/std/hashmap.hpp b/deps/ox/src/ox/std/hashmap.hpp index 5c9a3bf3..efd1d3d7 100644 --- a/deps/ox/src/ox/std/hashmap.hpp +++ b/deps/ox/src/ox/std/hashmap.hpp @@ -81,6 +81,8 @@ class HashMap { */ Pair *&access(Vector &pairs, K key); + void clear(); + }; template @@ -96,15 +98,11 @@ template HashMap::HashMap(HashMap &&other) { m_keys = ox::move(other.m_keys); m_pairs = ox::move(other.m_pairs); - other.m_keys = {}; - other.m_pairs = {}; } template HashMap::~HashMap() { - for (std::size_t i = 0; i < m_pairs.size(); i++) { - delete m_pairs[i]; - } + clear(); } template @@ -123,19 +121,21 @@ bool HashMap::operator==(const HashMap &other) const { template HashMap &HashMap::operator=(const HashMap &other) { - this->~HashMap(); - m_keys = other.m_keys; - m_pairs = other.m_pairs; + if (this != &other) { + clear(); + m_keys = other.m_keys; + m_pairs = other.m_pairs; + } return *this; } template HashMap &HashMap::operator=(HashMap &&other) { - this->~HashMap(); - m_keys = ox::move(other.m_keys); - m_pairs = ox::move(other.m_pairs); - other.m_keys = {}; - other.m_pairs = {}; + if (this != &other) { + clear(); + m_keys = ox::move(other.m_keys); + m_pairs = ox::move(other.m_pairs); + } return *this; } @@ -235,4 +235,12 @@ typename HashMap::Pair *&HashMap::access(Vector &pairs, K k) } } +template +void HashMap::clear() { + for (std::size_t i = 0; i < m_pairs.size(); i++) { + delete m_pairs[i]; + } + m_pairs.clear(); +} + } diff --git a/deps/ox/src/ox/std/vector.hpp b/deps/ox/src/ox/std/vector.hpp index 7ed32213..799343cb 100644 --- a/deps/ox/src/ox/std/vector.hpp +++ b/deps/ox/src/ox/std/vector.hpp @@ -38,24 +38,18 @@ class Vector { Vector &operator=(const Vector &other); - Vector &operator=(Vector &&other); + Vector &operator=(Vector &&other) noexcept; - [[nodiscard]] T &operator[](std::size_t i) noexcept; - [[nodiscard]] const T &operator[](std::size_t i) const noexcept; - [[nodiscard]] Result front() noexcept; - [[nodiscard]] Result front() const noexcept; - [[nodiscard]] Result back() noexcept; - [[nodiscard]] Result back() const noexcept; [[nodiscard]] @@ -142,17 +136,16 @@ Vector::Vector(Vector &&other) noexcept { template Vector::~Vector() { - if constexpr(ox::is_class()) { - for (std::size_t i = 0; i < m_size; i++) { - m_items[i].~T(); - } - } + clear(); delete[] bit_cast*>(m_items); m_items = nullptr; } template bool Vector::operator==(const Vector &other) const { + if (m_size != other.m_size) { + return false; + } for (std::size_t i = 0; i < m_size; i++) { if (!(m_items[i] == other.m_items[i])) { return false; @@ -163,25 +156,31 @@ bool Vector::operator==(const Vector &other) const { template Vector &Vector::operator=(const Vector &other) { - this->~Vector(); - m_size = other.m_size; - m_cap = other.m_cap; - m_items = bit_cast(new AllocAlias[m_cap]); - for (std::size_t i = 0; i < m_size; i++) { - m_items[i] = other.m_items[i]; + if (this != &other) { + clear(); + delete[] bit_cast*>(m_items); + m_size = other.m_size; + m_cap = other.m_cap; + m_items = bit_cast(new AllocAlias[m_cap]); + for (std::size_t i = 0; i < m_size; i++) { + m_items[i] = other.m_items[i]; + } } return *this; } template -Vector &Vector::operator=(Vector &&other) { - this->~Vector(); - m_size = other.m_size; - m_cap = other.m_cap; - m_items = other.m_items; - other.m_size = 0; - other.m_cap = 0; - other.m_items = nullptr; +Vector &Vector::operator=(Vector &&other) noexcept { + if (this != &other) { + clear(); + delete[] bit_cast*>(m_items); + m_size = other.m_size; + m_cap = other.m_cap; + m_items = other.m_items; + other.m_size = 0; + other.m_cap = 0; + other.m_items = nullptr; + } return *this; } @@ -243,7 +242,12 @@ bool Vector::empty() const noexcept { template void Vector::clear() { - resize(0); + if constexpr(ox::is_class()) { + for (std::size_t i = 0; i < m_size; ++i) { + m_items[i].~T(); + } + } + m_size = 0; } template