Pārlūkot izejas kodu

Simplify List and Vector operator=

Daniele Bartolini 12 gadi atpakaļ
vecāks
revīzija
1eb05cc0b4
2 mainītis faili ar 7 papildinājumiem un 28 dzēšanām
  1. 3 14
      engine/core/containers/List.h
  2. 4 14
      engine/core/containers/Vector.h

+ 3 - 14
engine/core/containers/List.h

@@ -316,20 +316,9 @@ inline void List<T>::clear()
 template <typename T>
 inline const List<T>& List<T>::operator=(const List<T>& other)
 {
-	if (m_array)
-	{
-		m_allocator->deallocate(m_array);
-	}
-
-	m_size = other.m_size;
-	m_capacity = other.m_capacity;
-
-	if (m_capacity)
-	{
-		m_array = (T*)m_allocator->allocate(m_capacity * sizeof(T), CE_ALIGNOF(T));
-
-		memcpy(m_array, other.m_array, m_size * sizeof(T));
-	}
+	const uint32_t size = other.m_size;
+	resize(size);
+	memcpy(m_array, other.m_array, sizeof(T) * size);
 
 	return *this;
 }

+ 4 - 14
engine/core/containers/Vector.h

@@ -337,22 +337,12 @@ inline void Vector<T>::clear()
 template <typename T>
 inline const Vector<T>& Vector<T>::operator=(const Vector<T>& other)
 {
-	if (m_array)
-	{
-		m_allocator->deallocate(m_array);
-	}
-
-	m_size = other.m_size;
-	m_capacity = other.m_capacity;
+	const uint32_t size = other.m_size;
+	resize(size);
 
-	if (m_capacity)
+	for (uint32_t i = 0; i < size; i++)
 	{
-		m_array = (T*)m_allocator->allocate(m_capacity * sizeof(T));
-
-		for (uint32_t i = 0; i < m_size; i++)
-		{
-			m_array[i] = other.m_array[i];
-		}
+		m_array[i] = other.m_array[i];
 	}
 
 	return *this;