[ox/std] Fix HashMap and Vector move operators
This commit is contained in:
parent
85b101884a
commit
0cf25f3029
26
deps/ox/src/ox/std/hashmap.hpp
vendored
26
deps/ox/src/ox/std/hashmap.hpp
vendored
@ -81,6 +81,8 @@ class HashMap {
|
|||||||
*/
|
*/
|
||||||
Pair *&access(Vector<Pair*> &pairs, K key);
|
Pair *&access(Vector<Pair*> &pairs, K key);
|
||||||
|
|
||||||
|
void clear();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename K, typename T>
|
template<typename K, typename T>
|
||||||
@ -96,15 +98,11 @@ template<typename K, typename T>
|
|||||||
HashMap<K, T>::HashMap(HashMap<K, T> &&other) {
|
HashMap<K, T>::HashMap(HashMap<K, T> &&other) {
|
||||||
m_keys = ox::move(other.m_keys);
|
m_keys = ox::move(other.m_keys);
|
||||||
m_pairs = ox::move(other.m_pairs);
|
m_pairs = ox::move(other.m_pairs);
|
||||||
other.m_keys = {};
|
|
||||||
other.m_pairs = {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename K, typename T>
|
template<typename K, typename T>
|
||||||
HashMap<K, T>::~HashMap() {
|
HashMap<K, T>::~HashMap() {
|
||||||
for (std::size_t i = 0; i < m_pairs.size(); i++) {
|
clear();
|
||||||
delete m_pairs[i];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename K, typename T>
|
template<typename K, typename T>
|
||||||
@ -123,19 +121,21 @@ bool HashMap<K, T>::operator==(const HashMap &other) const {
|
|||||||
|
|
||||||
template<typename K, typename T>
|
template<typename K, typename T>
|
||||||
HashMap<K, T> &HashMap<K, T>::operator=(const HashMap<K, T> &other) {
|
HashMap<K, T> &HashMap<K, T>::operator=(const HashMap<K, T> &other) {
|
||||||
this->~HashMap<K, T>();
|
if (this != &other) {
|
||||||
|
clear();
|
||||||
m_keys = other.m_keys;
|
m_keys = other.m_keys;
|
||||||
m_pairs = other.m_pairs;
|
m_pairs = other.m_pairs;
|
||||||
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename K, typename T>
|
template<typename K, typename T>
|
||||||
HashMap<K, T> &HashMap<K, T>::operator=(HashMap<K, T> &&other) {
|
HashMap<K, T> &HashMap<K, T>::operator=(HashMap<K, T> &&other) {
|
||||||
this->~HashMap<K, T>();
|
if (this != &other) {
|
||||||
|
clear();
|
||||||
m_keys = ox::move(other.m_keys);
|
m_keys = ox::move(other.m_keys);
|
||||||
m_pairs = ox::move(other.m_pairs);
|
m_pairs = ox::move(other.m_pairs);
|
||||||
other.m_keys = {};
|
}
|
||||||
other.m_pairs = {};
|
|
||||||
return *this;
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
36
deps/ox/src/ox/std/vector.hpp
vendored
36
deps/ox/src/ox/std/vector.hpp
vendored
@ -38,24 +38,18 @@ class Vector {
|
|||||||
|
|
||||||
Vector &operator=(const Vector &other);
|
Vector &operator=(const Vector &other);
|
||||||
|
|
||||||
Vector &operator=(Vector &&other);
|
Vector &operator=(Vector &&other) noexcept;
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
T &operator[](std::size_t i) noexcept;
|
T &operator[](std::size_t i) noexcept;
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
const T &operator[](std::size_t i) const noexcept;
|
const T &operator[](std::size_t i) const noexcept;
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
Result<T&> front() noexcept;
|
Result<T&> front() noexcept;
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
Result<const T&> front() const noexcept;
|
Result<const T&> front() const noexcept;
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
Result<T&> back() noexcept;
|
Result<T&> back() noexcept;
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
Result<const T&> back() const noexcept;
|
Result<const T&> back() const noexcept;
|
||||||
|
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
@ -142,17 +136,16 @@ Vector<T>::Vector(Vector<T> &&other) noexcept {
|
|||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Vector<T>::~Vector() {
|
Vector<T>::~Vector() {
|
||||||
if constexpr(ox::is_class<T>()) {
|
clear();
|
||||||
for (std::size_t i = 0; i < m_size; i++) {
|
|
||||||
m_items[i].~T();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
delete[] bit_cast<AllocAlias<T>*>(m_items);
|
delete[] bit_cast<AllocAlias<T>*>(m_items);
|
||||||
m_items = nullptr;
|
m_items = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
bool Vector<T>::operator==(const Vector<T> &other) const {
|
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++) {
|
for (std::size_t i = 0; i < m_size; i++) {
|
||||||
if (!(m_items[i] == other.m_items[i])) {
|
if (!(m_items[i] == other.m_items[i])) {
|
||||||
return false;
|
return false;
|
||||||
@ -163,25 +156,31 @@ bool Vector<T>::operator==(const Vector<T> &other) const {
|
|||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Vector<T> &Vector<T>::operator=(const Vector<T> &other) {
|
Vector<T> &Vector<T>::operator=(const Vector<T> &other) {
|
||||||
this->~Vector<T>();
|
if (this != &other) {
|
||||||
|
clear();
|
||||||
|
delete[] bit_cast<AllocAlias<T>*>(m_items);
|
||||||
m_size = other.m_size;
|
m_size = other.m_size;
|
||||||
m_cap = other.m_cap;
|
m_cap = other.m_cap;
|
||||||
m_items = bit_cast<T*>(new AllocAlias<T>[m_cap]);
|
m_items = bit_cast<T*>(new AllocAlias<T>[m_cap]);
|
||||||
for (std::size_t i = 0; i < m_size; i++) {
|
for (std::size_t i = 0; i < m_size; i++) {
|
||||||
m_items[i] = other.m_items[i];
|
m_items[i] = other.m_items[i];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Vector<T> &Vector<T>::operator=(Vector<T> &&other) {
|
Vector<T> &Vector<T>::operator=(Vector<T> &&other) noexcept {
|
||||||
this->~Vector<T>();
|
if (this != &other) {
|
||||||
|
clear();
|
||||||
|
delete[] bit_cast<AllocAlias<T>*>(m_items);
|
||||||
m_size = other.m_size;
|
m_size = other.m_size;
|
||||||
m_cap = other.m_cap;
|
m_cap = other.m_cap;
|
||||||
m_items = other.m_items;
|
m_items = other.m_items;
|
||||||
other.m_size = 0;
|
other.m_size = 0;
|
||||||
other.m_cap = 0;
|
other.m_cap = 0;
|
||||||
other.m_items = nullptr;
|
other.m_items = nullptr;
|
||||||
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -243,7 +242,12 @@ bool Vector<T>::empty() const noexcept {
|
|||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Vector<T>::clear() {
|
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>
|
template<typename T>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user