libluna: Remove implicit vector copying and add an explicit way to do it
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Closes #28.
This commit is contained in:
parent
bc07cc94cb
commit
a1bf4dafbe
@ -15,12 +15,7 @@ template <typename T> class Vector
|
||||
{
|
||||
}
|
||||
|
||||
Vector(const Vector<T>& other)
|
||||
{
|
||||
reserve(other.capacity());
|
||||
memcpy(m_data, other.data(), other.size());
|
||||
m_size = other.size();
|
||||
}
|
||||
Vector(const Vector<T>& other) = delete;
|
||||
|
||||
Vector(Vector<T>&& other)
|
||||
{
|
||||
@ -32,18 +27,7 @@ template <typename T> class Vector
|
||||
other.m_data = nullptr;
|
||||
}
|
||||
|
||||
Vector<T>& operator=(const Vector<T>& 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<T>& operator=(const Vector<T>& other) = delete;
|
||||
|
||||
Vector<T>& operator=(Vector<T>&& other)
|
||||
{
|
||||
@ -217,6 +201,35 @@ template <typename T> class Vector
|
||||
m_data = nullptr;
|
||||
}
|
||||
|
||||
Result<Vector<T>> shallow_copy()
|
||||
{
|
||||
Vector<T> other;
|
||||
TRY(other.try_reserve(m_capacity));
|
||||
memcpy(other.m_data, m_data, m_size);
|
||||
other.m_size = m_size;
|
||||
return other;
|
||||
}
|
||||
|
||||
Result<Vector<T>> deep_copy()
|
||||
{
|
||||
Vector<T> 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<Vector<T>> deep_copy(Result<T> (*copy_function)(const T&))
|
||||
{
|
||||
Vector<T> 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 };
|
||||
|
Loading…
Reference in New Issue
Block a user