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)
|
Vector(const Vector<T>& other) = delete;
|
||||||
{
|
|
||||||
reserve(other.capacity());
|
|
||||||
memcpy(m_data, other.data(), other.size());
|
|
||||||
m_size = other.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
Vector(Vector<T>&& other)
|
Vector(Vector<T>&& other)
|
||||||
{
|
{
|
||||||
@ -32,18 +27,7 @@ template <typename T> class Vector
|
|||||||
other.m_data = nullptr;
|
other.m_data = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector<T>& operator=(const Vector<T>& other)
|
Vector<T>& operator=(const Vector<T>& other) = delete;
|
||||||
{
|
|
||||||
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=(Vector<T>&& other)
|
Vector<T>& operator=(Vector<T>&& other)
|
||||||
{
|
{
|
||||||
@ -217,6 +201,35 @@ template <typename T> class Vector
|
|||||||
m_data = nullptr;
|
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:
|
private:
|
||||||
T* m_data { nullptr };
|
T* m_data { nullptr };
|
||||||
usize m_capacity { 0 };
|
usize m_capacity { 0 };
|
||||||
|
Loading…
Reference in New Issue
Block a user