diff --git a/libluna/include/luna/Vector.h b/libluna/include/luna/Vector.h index c008ddc4..c73d3989 100644 --- a/libluna/include/luna/Vector.h +++ b/libluna/include/luna/Vector.h @@ -15,12 +15,7 @@ template class Vector { } - Vector(const Vector& other) - { - reserve(other.capacity()); - memcpy(m_data, other.data(), other.size()); - m_size = other.size(); - } + Vector(const Vector& other) = delete; Vector(Vector&& other) { @@ -32,18 +27,7 @@ template class Vector other.m_data = nullptr; } - Vector& operator=(const Vector& other) - { - if (&other == this) return *this; - - if (m_data) clear(); - - reserve(other.capacity()); - for (usize i = 0; i < other.size(); i++) { new (&m_data[i]) T(other.m_data[i]); } - m_size = other.size(); - - return *this; - } + Vector& operator=(const Vector& other) = delete; Vector& operator=(Vector&& other) { @@ -217,6 +201,35 @@ template class Vector m_data = nullptr; } + Result> shallow_copy() + { + Vector other; + TRY(other.try_reserve(m_capacity)); + memcpy(other.m_data, m_data, m_size); + other.m_size = m_size; + return other; + } + + Result> deep_copy() + { + Vector other; + TRY(other.try_reserve(m_capacity)); + for (usize i = 0; i < m_size; i++) { TRY(other.try_append(m_data[i])); } + return other; + } + + Result> deep_copy(Result (*copy_function)(const T&)) + { + Vector other; + TRY(other.try_reserve(m_capacity)); + for (usize i = 0; i < m_size; i++) + { + auto copy = TRY(copy_function(m_data[i])); + TRY(other.try_append(move(copy))); + } + return other; + } + private: T* m_data { nullptr }; usize m_capacity { 0 };