[ox/std] Fix HashMap and Vector move operators

This commit is contained in:
Gary Talent 2021-04-22 01:17:57 -05:00
parent 85b101884a
commit 0cf25f3029
2 changed files with 52 additions and 40 deletions

View File

@ -81,6 +81,8 @@ class HashMap {
*/
Pair *&access(Vector<Pair*> &pairs, K key);
void clear();
};
template<typename K, typename T>
@ -96,15 +98,11 @@ template<typename K, typename T>
HashMap<K, T>::HashMap(HashMap<K, T> &&other) {
m_keys = ox::move(other.m_keys);
m_pairs = ox::move(other.m_pairs);
other.m_keys = {};
other.m_pairs = {};
}
template<typename K, typename T>
HashMap<K, T>::~HashMap() {
for (std::size_t i = 0; i < m_pairs.size(); i++) {
delete m_pairs[i];
}
clear();
}
template<typename K, typename T>
@ -123,19 +121,21 @@ bool HashMap<K, T>::operator==(const HashMap &other) const {
template<typename K, typename T>
HashMap<K, T> &HashMap<K, T>::operator=(const HashMap<K, T> &other) {
this->~HashMap<K, T>();
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<typename K, typename T>
HashMap<K, T> &HashMap<K, T>::operator=(HashMap<K, T> &&other) {
this->~HashMap<K, T>();
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<K, T>::Pair *&HashMap<K, T>::access(Vector<Pair*> &pairs, K k)
}
}
template<typename K, typename T>
void HashMap<K, T>::clear() {
for (std::size_t i = 0; i < m_pairs.size(); i++) {
delete m_pairs[i];
}
m_pairs.clear();
}
}

View File

@ -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<T&> front() noexcept;
[[nodiscard]]
Result<const T&> front() const noexcept;
[[nodiscard]]
Result<T&> back() noexcept;
[[nodiscard]]
Result<const T&> back() const noexcept;
[[nodiscard]]
@ -142,17 +136,16 @@ Vector<T>::Vector(Vector<T> &&other) noexcept {
template<typename T>
Vector<T>::~Vector() {
if constexpr(ox::is_class<T>()) {
for (std::size_t i = 0; i < m_size; i++) {
m_items[i].~T();
}
}
clear();
delete[] bit_cast<AllocAlias<T>*>(m_items);
m_items = nullptr;
}
template<typename T>
bool Vector<T>::operator==(const Vector<T> &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<T>::operator==(const Vector<T> &other) const {
template<typename T>
Vector<T> &Vector<T>::operator=(const Vector<T> &other) {
this->~Vector<T>();
m_size = other.m_size;
m_cap = other.m_cap;
m_items = bit_cast<T*>(new AllocAlias<T>[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<AllocAlias<T>*>(m_items);
m_size = other.m_size;
m_cap = other.m_cap;
m_items = bit_cast<T*>(new AllocAlias<T>[m_cap]);
for (std::size_t i = 0; i < m_size; i++) {
m_items[i] = other.m_items[i];
}
}
return *this;
}
template<typename T>
Vector<T> &Vector<T>::operator=(Vector<T> &&other) {
this->~Vector<T>();
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<T> &Vector<T>::operator=(Vector<T> &&other) noexcept {
if (this != &other) {
clear();
delete[] bit_cast<AllocAlias<T>*>(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<T>::empty() const noexcept {
template<typename T>
void Vector<T>::clear() {
resize(0);
if constexpr(ox::is_class<T>()) {
for (std::size_t i = 0; i < m_size; ++i) {
m_items[i].~T();
}
}
m_size = 0;
}
template<typename T>